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