partly working security: registration form, authorization, simple authentification
[proteocache.git] / server / compbio / listeners / ContextListener.java
index 44cf66b..c3109a7 100644 (file)
@@ -9,7 +9,15 @@ import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 import javax.servlet.annotation.WebListener;
 
+import org.apache.log4j.Logger;
+
 import compbio.cassandra.CassandraNativeConnector;
+import compbio.cassandra.CassandraNewTableWriter;
+import compbio.cassandra.JpredParserHTTP;
+import compbio.cassandra.JpredParserLocalFile;
+import compbio.engine.ProteoCachePropertyHelperManager;
+import compbio.util.PropertyHelper;
+import compbio.util.Util;
 
 /**
  * Application Lifecycle Listener implementation class ContextListener
@@ -18,27 +26,135 @@ import compbio.cassandra.CassandraNativeConnector;
 @WebListener
 public class ContextListener implements ServletContextListener {
        private ScheduledExecutorService webjob_scheduler;
+       private ScheduledExecutorService localjob_scheduler;
+       private ScheduledExecutorService update_scheduler;
        CassandraNativeConnector db = new CassandraNativeConnector();
+       private static PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
+       private static Logger log = Logger.getLogger(ContextListener.class);
+       public static boolean READ_WEB_JPRED = false;
+       public static boolean READ_LOCALFILE_JPRED = false;
+       public static boolean FILL_NEWTABLE = false;
+
+       private static boolean initBooleanValue(String key) {
+               assert key != null;
+               String status = ph.getProperty(key);
+               log.debug("Loading property: " + key + " with value: " + status);
+               if (Util.isEmpty(status)) {
+                       return false;
+               }
+               return new Boolean(status.trim()).booleanValue();
+       }
 
        /**
         * @see ServletContextListener#contextInitialized(ServletContextEvent)
         */
        public void contextInitialized(ServletContextEvent arg0) {
                System.out.println("ProteoCache session start......");
+               // connect to the db and create table if needed
                db.Connect();
+               final CassandraNewTableWriter updater = new CassandraNewTableWriter();
+
+               READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
+               READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
+               FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update");
+
+               if (FILL_NEWTABLE) {
+                       System.out.println("Initializating new table update scheduler");
+                       update_scheduler = Executors.newSingleThreadScheduledExecutor();
+                       update_scheduler.schedule(new Runnable() {
+                               @Override
+                               public void run() {
+                                       updater.FillNewTable();
+                               }
+                       }, 10, TimeUnit.SECONDS);
+               }
+
+               if (READ_WEB_JPRED) {
+                       // get data from real Jpred production server
+                       final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
+                       final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
+                       final JpredParserHTTP parser = new JpredParserHTTP(prefix);
+
+                       int initialdelay = 300;
+                       int updaterate = 600;
+                       int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
+                       if (0 <= newinitialdelay) {
+                               initialdelay = newinitialdelay;
+                       }
+                       int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
+                       if (0 < newupdaterate) {
+                               updaterate = newupdaterate;
+                       }
+                       final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
 
-               webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
-               webjob_scheduler.scheduleAtFixedRate(new Runnable() {
-                       @Override
-                       public void run() {
-                               try {
-                                       db.Parsing();
-                               } catch (IOException e) {
-                                       // TODO Auto-generated catch block
-                                       e.printStackTrace();
+                       webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
+                       System.out.println("Initializating web job scheduler");
+                       System.out.println("    initial delay = " + initialdelay + " seconds");
+                       System.out.println("    update rate = " + updaterate + " seconds");
+                       if (0 < updateperiod)
+                               System.out.println("    update period = " + updateperiod + " days");
+                       else
+                               System.out.println("    update period = 5 days");
+
+                       webjob_scheduler.scheduleAtFixedRate(new Runnable() {
+                               @Override
+                               public void run() {
+                                       try {
+                                               if (0 < updateperiod) {
+                                                       parser.Parsing(datasrc, updateperiod);
+                                               } else {
+                                                       parser.Parsing(datasrc, 5);
+                                               }
+                                       } catch (IOException e) {
+                                               // TODO Auto-generated catch block
+                                               e.printStackTrace();
+                                       }
                                }
+                       }, initialdelay, updaterate, TimeUnit.SECONDS);
+               }
+
+               if (READ_LOCALFILE_JPRED) {
+                       // get irtifical data generated for the DB stress tests
+                       final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
+                       final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
+                       final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
+
+                       int initialdelay = 300;
+                       int updaterate = 600;
+                       int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
+                       if (0 <= newinitialdelay) {
+                               initialdelay = newinitialdelay;
+                       }
+                       int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
+                       if (0 < newupdaterate) {
+                               updaterate = newupdaterate;
                        }
-               }, 0, 60, TimeUnit.SECONDS);
+                       final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
+
+                       localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
+                       System.out.println("Initializating local job scheduler");
+                       System.out.println("    initial delay = " + initialdelay + " seconds");
+                       System.out.println("    update rate = " + updaterate + " seconds");
+                       if (0 < updateperiod)
+                               System.out.println("    update period = " + updateperiod + " days");
+                       else
+                               System.out.println("    update period = 5 days");
+                       localjob_scheduler.scheduleAtFixedRate(new Runnable() {
+                               @Override
+                               public void run() {
+                                       try {
+                                               if (0 < updateperiod) {
+                                                       parser.Parsing(datasrc, updateperiod);
+                                               } else {
+                                                       parser.Parsing(datasrc, 100);
+                                               }
+                                       } catch (IOException e) {
+                                               // TODO Auto-generated catch block
+                                               e.printStackTrace();
+                                       }
+                               }
+                       }, initialdelay, updaterate, TimeUnit.SECONDS);
+               }
 
        }
 
@@ -48,7 +164,13 @@ public class ContextListener implements ServletContextListener {
        public void contextDestroyed(ServletContextEvent arg0) {
                db.Closing();
                System.out.println("Shut down ProteoCache......");
-               webjob_scheduler.shutdownNow();
+               if (READ_WEB_JPRED) {
+                       webjob_scheduler.shutdownNow();
+               }
+               if (READ_LOCALFILE_JPRED) {
+                       localjob_scheduler.shutdownNow();
+               }
+               update_scheduler.shutdownNow();
        }
 
 }