Add mode comments to the classes
[proteocache.git] / server / compbio / listeners / ContextListener.java
1 package compbio.listeners;
2
3 import java.io.IOException;
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.ScheduledExecutorService;
6 import java.util.concurrent.ScheduledFuture;
7 import java.util.concurrent.TimeUnit;
8
9 import javax.servlet.ServletContextEvent;
10 import javax.servlet.ServletContextListener;
11 import javax.servlet.annotation.WebListener;
12
13 import org.apache.log4j.Logger;
14
15 import compbio.cassandra.CassandraNativeConnector;
16 import compbio.cassandra.CassandraNewTableWriter;
17 import compbio.cassandra.JpredParserHTTP;
18 import compbio.cassandra.JpredParserLocalFile;
19 import compbio.engine.ProteoCachePropertyHelperManager;
20 import compbio.util.PropertyHelper;
21 import compbio.util.Util;
22
23 /**
24  * Application Lifecycle Listener implementation class ContextListener
25  * 
26  */
27 @WebListener
28 public class ContextListener implements ServletContextListener {
29         //private ScheduledExecutorService webjob_scheduler;
30         //private ScheduledExecutorService localjob_scheduler;
31         //private ScheduledExecutorService update_scheduler;
32         private ScheduledExecutorService executor;
33         private ScheduledFuture<?> webjobs;
34         private ScheduledFuture<?> localjobs;
35         private ScheduledFuture<?> updates;
36         CassandraNativeConnector db = new CassandraNativeConnector();
37         private static PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
38         private static Logger log = Logger.getLogger(ContextListener.class);
39         public static boolean READ_WEB_JPRED = false;
40         public static boolean READ_LOCALFILE_JPRED = false;
41         public static boolean FILL_NEWTABLE = false;
42         public static boolean UPDATE_TABLE = false;
43
44         private static boolean initBooleanValue(String key) {
45                 assert key != null;
46                 String status = ph.getProperty(key);
47                 log.debug("Loading property: " + key + " with value: " + status);
48                 if (Util.isEmpty(status)) {
49                         return false;
50                 }
51                 return new Boolean(status.trim()).booleanValue();
52         }
53
54         /**
55          * @see ServletContextListener#contextInitialized(ServletContextEvent)
56          */
57         public void contextInitialized(ServletContextEvent arg0) {
58                 System.out.println("ProteoCache session start......");
59                 // connect to the db and create table if needed
60                 db.Connect();
61                 final CassandraNewTableWriter updater = new CassandraNewTableWriter();
62
63                 READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
64                 READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
65                 FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update");
66                 UPDATE_TABLE = initBooleanValue("cassandra.version.update");
67
68                 executor = Executors.newScheduledThreadPool(3);
69                 if (UPDATE_TABLE) {
70                         System.out.println("Initializating update version parameter");
71                         //update_scheduler = Executors.newSingleThreadScheduledExecutor();
72                         //update_scheduler.schedule(new Runnable() {
73                         executor.schedule(new Runnable() {
74                                 @Override
75                                 public void run() {
76                                         updater.FillNewParameter();
77                                 }
78                         }, 10, TimeUnit.SECONDS);
79                 }
80                 if (FILL_NEWTABLE) {
81                         System.out.println("Initializating new table update scheduler");
82                         //update_scheduler = Executors.newSingleThreadScheduledExecutor();
83                         //update_scheduler.schedule(new Runnable() {
84                         executor.schedule(new Runnable() {
85                                 @Override
86                                 public void run() {
87                                         updater.FillNewTable();
88                                 }
89                         }, 10, TimeUnit.SECONDS);
90                 }
91
92                 if (READ_WEB_JPRED) {
93                         // get data from real Jpred production server
94                         String theprefix = ph.getProperty("cassandra.jpred.web.prefix");
95                         if (null == theprefix) {
96                                 theprefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
97                         }
98                         
99                         final String datasrc = theprefix + "/usage-new/alljobs.dat";
100                         final String prefix = theprefix;
101                         final JpredParserHTTP parser = new JpredParserHTTP(prefix);
102
103                         int initialdelay = 300;
104                         int updaterate = 600;
105                         int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
106                         if (0 <= newinitialdelay) {
107                                 initialdelay = newinitialdelay;
108                         }
109                         int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
110                         if (0 < newupdaterate) {
111                                 updaterate = newupdaterate;
112                         }
113                         final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
114
115                         //webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
116                         System.out.println("Initializating web job scheduler");
117                         System.out.println("    initial delay = " + initialdelay + " seconds");
118                         System.out.println("    update rate = " + updaterate + " seconds");
119                         if (0 < updateperiod)
120                                 System.out.println("    update period = " + updateperiod + " days");
121                         else
122                                 System.out.println("    update period = 5 days");
123
124                         //webjob_scheduler.scheduleAtFixedRate(new Runnable() {
125                         webjobs = executor.scheduleAtFixedRate(new Runnable() {
126                                 @Override
127                                 public void run() {
128                                         try {
129                                                 if (0 < updateperiod) {
130                                                         parser.Parsing(datasrc, updateperiod);
131                                                 } else {
132                                                         parser.Parsing(datasrc, 5);
133                                                 }
134                                         } catch (IOException e) {
135                                                 // TODO Auto-generated catch block
136                                                 e.printStackTrace();
137                                         }
138                                 }
139                         }, initialdelay, updaterate, TimeUnit.SECONDS);
140                 }
141
142                 if (READ_LOCALFILE_JPRED) {
143                         // get irtifical data generated for the DB stress tests
144                         final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
145                         final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
146                         final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
147
148                         int initialdelay = 300;
149                         int updaterate = 600;
150                         int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
151                         if (0 <= newinitialdelay) {
152                                 initialdelay = newinitialdelay;
153                         }
154                         int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
155                         if (0 < newupdaterate) {
156                                 updaterate = newupdaterate;
157                         }
158                         final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
159
160                         //localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
161                         System.out.println("Initializating local job scheduler");
162                         System.out.println("    initial delay = " + initialdelay + " seconds");
163                         System.out.println("    update rate = " + updaterate + " seconds");
164                         if (0 < updateperiod)
165                                 System.out.println("    update period = " + updateperiod + " days");
166                         else
167                                 System.out.println("    update period = 5 days");
168                         //localjob_scheduler.scheduleAtFixedRate(new Runnable() {
169                         localjobs = executor.scheduleAtFixedRate(new Runnable() {
170                                 @Override
171                                 public void run() {
172                                         try {
173                                                 if (0 < updateperiod) {
174                                                         parser.Parsing(datasrc, updateperiod);
175                                                 } else {
176                                                         parser.Parsing(datasrc, 100);
177                                                 }
178                                         } catch (IOException e) {
179                                                 // TODO Auto-generated catch block
180                                                 e.printStackTrace();
181                                         }
182                                 }
183                         }, initialdelay, updaterate, TimeUnit.SECONDS);
184                 }
185
186         }
187
188         /**
189          * @see ServletContextListener#contextDestroyed(ServletContextEvent)
190          */
191         /*
192         public void contextDestroyed(ServletContextEvent arg0) {
193                 db.Closing();
194                 System.out.println("Shut down ProteoCache......");
195                 if (READ_WEB_JPRED) {
196                         webjob_scheduler.shutdownNow();
197                 }
198                 if (READ_LOCALFILE_JPRED) {
199                         localjob_scheduler.shutdownNow();
200                 }
201                 update_scheduler.shutdownNow();
202         }
203         */
204         public void contextDestroyed(ServletContextEvent arg0) {
205                 db.Closing();
206         try {
207                 System.out.println("Shut down ProteoCache......");
208                 if (READ_WEB_JPRED) {
209                         webjobs.cancel(true);
210                 }
211                 if (READ_LOCALFILE_JPRED) {
212                         localjobs.cancel(true);
213                 }
214                 executor.shutdown();
215                 executor.awaitTermination(3, TimeUnit.SECONDS);
216         } catch (InterruptedException e) {
217                 log.warn(e.getMessage(), e);
218         }
219         }
220 }