Modify Eclipse configuration for log4j
[jabaws.git] / webservices / compbio / ws / server / MainManager.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.ws.server;\r
19 \r
20 import java.util.concurrent.Executors;\r
21 import java.util.concurrent.ScheduledExecutorService;\r
22 import java.util.concurrent.ScheduledFuture;\r
23 import java.util.concurrent.TimeUnit;\r
24 \r
25 import javax.servlet.ServletContextEvent;\r
26 import javax.servlet.ServletContextListener;\r
27 \r
28 import org.apache.log4j.Logger;\r
29 \r
30 import compbio.stat.collector.DirCleaner;\r
31 import compbio.stat.collector.StatDB;\r
32 import compbio.engine.conf.PropertyHelperManager;\r
33 import compbio.engine.local.ExecutableWrapper;\r
34 import compbio.engine.local.LocalExecutorService;\r
35 import compbio.util.PropertyHelper;\r
36 import compbio.util.Util;\r
37 \r
38 /**\r
39  * Two tasks:\r
40  * 1. Switch off engines if JABAWS web application is un-deployed, or web server is shutdown\r
41  * 2. delete old job directories\r
42  * \r
43  * @author Peter Troshin\r
44  * @author Alexander Sherstnev\r
45  * @version 2.0\r
46  */\r
47 public class MainManager implements ServletContextListener {\r
48 \r
49         private final Logger log = Logger.getLogger(MainManager.class);\r
50         static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();\r
51         \r
52         private ScheduledFuture<?> localcl;\r
53         private ScheduledFuture<?> clustercl;\r
54         private ScheduledExecutorService executor;\r
55         \r
56         @Override\r
57         public void contextDestroyed(ServletContextEvent ignored) {\r
58                 // stop cleaning job directories\r
59 //              try {\r
60                         if (null != localcl) {\r
61                                 localcl.cancel(true);\r
62                         }\r
63                         if (null != clustercl) {\r
64                                 clustercl.cancel(true);\r
65                         }\r
66                         //executor.shutdown();\r
67                         //executor.awaitTermination(3, TimeUnit.SECONDS);\r
68 //              } catch (InterruptedException e) {\r
69 //                      log.warn(e.getMessage(), e);\r
70 //              }\r
71                 // Shutdown local engine\r
72                 log.info("JABAWS context is destroyed. Shutting down engines...");\r
73                 LocalExecutorService.shutDown();\r
74                 log.info("Local engine is shutdown OK");\r
75                 ExecutableWrapper.shutdownService();\r
76                 log.info("Individual executables stream engine is shutdown OK");\r
77         }\r
78 \r
79         @Override\r
80         public void contextInitialized(ServletContextEvent arg0) {\r
81                 log.info("Initializing directory cleaners");\r
82                 executor = Executors.newScheduledThreadPool(2);\r
83 \r
84                 // configure cluster cleaner\r
85                 String clusterWorkDir = getClusterJobDir();\r
86                 int clusterDirLifespan = PropertyHelperManager.getIntProperty(ph.getProperty("cluster.jobdir.maxlifespan"));\r
87                 int clusterCleaningRate = PropertyHelperManager.getIntProperty(ph.getProperty("cluster.jobdir.cleaning.frequency"));\r
88                 boolean cleanClasterDir = PropertyHelperManager.getBooleanProperty(ph.getProperty("cluster.stat.collector.enable"));\r
89                 \r
90                 if (0 < clusterDirLifespan && cleanClasterDir) {\r
91                         DirCleaner clusterDirCleaner = new DirCleaner(  clusterWorkDir, clusterDirLifespan);\r
92                         clustercl = executor.scheduleAtFixedRate(clusterDirCleaner, 1, clusterCleaningRate, TimeUnit.MINUTES);\r
93                         log.info("Cleaning local job directory every " + clusterCleaningRate + " minutes");\r
94                 } else {\r
95                         log.info("Cluster job directory cleaner is disabled. ");\r
96                 }\r
97 \r
98                 // configure local cleaner\r
99                 String localWorkDir = compbio.engine.client.Util.convertToAbsolute(getLocalJobDir());\r
100                 int localDirLiveSpan = PropertyHelperManager.getIntProperty(ph.getProperty("local.jobdir.maxlifespan"));\r
101                 int localCleaningRate = PropertyHelperManager.getIntProperty(ph.getProperty("local.jobdir.cleaning.frequency"));\r
102                 boolean cleanLocalDir = PropertyHelperManager.getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
103 \r
104                 if (0 < localDirLiveSpan && cleanLocalDir) {\r
105                         DirCleaner localDirCleaner = new DirCleaner(    localWorkDir, localDirLiveSpan);\r
106                         localcl = executor.scheduleAtFixedRate(localDirCleaner, 1, localCleaningRate, TimeUnit.MINUTES);\r
107                         log.info("Cleaning local job directory every " + localCleaningRate + " minutes");\r
108                 } else {\r
109                         log.info("Local job directory cleaner is disabled. ");\r
110                 }\r
111         }\r
112 \r
113         static String getClusterJobDir() {\r
114                 String ln = ph.getProperty("cluster.tmp.directory");\r
115                 if (null != ln ) {\r
116                   ln = ln.trim();\r
117                 }\r
118                 return ln;\r
119         }\r
120 \r
121         static String getLocalJobDir() {\r
122                 String ln = ph.getProperty("local.tmp.directory");\r
123                 if (null != ln ) {\r
124                         ln = ln.trim();\r
125                 }\r
126                 return ln;\r
127         }\r
128 }\r