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