package edu.mayo.ve; /**
 * Created with IntelliJ IDEA.
 * User: m102417
 * Date: 9/25/13
 * Time: 4:40 PM
 * To change this template use File | Settings | File Templates.
 */

import edu.mayo.concurrency.workerQueue.WorkerPool;
import edu.mayo.util.Tokens;
import edu.mayo.ve.VCFParser.LoadWorker;
import edu.mayo.ve.VCFParser.VCFParser;
import edu.mayo.ve.resources.WorkerPoolManager;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class VCFLoaderPool implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    private static WorkerPool wp = null;
    private static int maxCache = 50000;
    // Public constructor is required by servlet spec
    public VCFLoaderPool() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        //initialize the worker pool if this is the first load
        if(wp == null){
            LoadWorker logic = new LoadWorker(new VCFParser(), maxCache);//do we want to let them pass this value?
            wp = new WorkerPool(logic, 1);
            WorkerPoolManager.registerWorkerPool(Tokens.VCF_WORKERS, wp);
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
        try{
            wp.shutdown(1);
        }catch (Exception e){

        }
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
      /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
      /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }

    public static WorkerPool getWp() {
        return wp;
    }

    public static void setWp(WorkerPool wp) {
        VCFLoaderPool.wp = wp;
    }

    public static void setReportingTrueAndResetPool(int typeAheadCacheSize){
        LoadWorker logic = new LoadWorker(new VCFParser(), typeAheadCacheSize, true);
        logic.setLogStackTrace(true);
        wp = new WorkerPool(logic, 1);
        WorkerPoolManager.registerWorkerPool(Tokens.VCF_WORKERS, wp);
        return;
    }

    public static void reset(int typeAheadCacheSize){
        LoadWorker logic = new LoadWorker(new VCFParser(), typeAheadCacheSize);//do we want to let them pass this value?
        wp = new WorkerPool(logic, 1);
        WorkerPoolManager.registerWorkerPool(Tokens.VCF_WORKERS, wp);
        return;
    }
}

@@ -379,16 +379,90 @@
 Manager;
+%0Aimport edu.mayo.ve.util.SystemProperties;%0Aimport org.apache.log4j.Logger;
 %0A%0Aimport
@@ -739,16 +739,44 @@
 ngEvent;
+%0Aimport java.io.IOException;
 %0A%0Apublic
@@ -892,16 +892,94 @@
 ener %7B%0A%0A
+    private static final Logger log = Logger.getLogger(VCFLoaderPool.class);%0A%0A
     priv
@@ -1134,32 +1134,345 @@
 Pool() %7B%0A    %7D%0A%0A
+%0A    public static final String NUM_WORKERS = %22num_workers%22;%0A    private static int numberworkers = 1;%0A    public static int initNumberWorkers() throws IOException %7B%0A        SystemProperties sysprop = new SystemProperties();%0A        String v = sysprop.get(NUM_WORKERS);%0A        return Integer.parseInt(v);%0A    %7D%0A%0A
     // ---------
@@ -1967,16 +1967,344 @@
  null)%7B%0A
+            try %7B%0A                numberworkers = initNumberWorkers();%0A                log.debug(%22Number of Threads in the Workder Pool: %22 + numberworkers);%0A            %7Dcatch (IOException e)%7B%0A                throw new RuntimeException(e.getMessage(),e);    //todo: probably a better way to handle this exception!%0A            %7D%0A
         
@@ -2441,33 +2441,45 @@
 rkerPool(logic, 
-1
+numberworkers
 );%0A            W
@@ -4264,33 +4264,45 @@
 rkerPool(logic, 
-1
+numberworkers
 );%0A        Worke
@@ -4591,17 +4591,29 @@
 (logic, 
-1
+numberworkers
 );%0A     
@@ -4693,16 +4693,18 @@
 return;%0A
-
     %7D%0A
+%0A%0A
 %7D%0A

update to change the number of workers in the thread pool, this should allow multiple VCF files to be loaded at the same time