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