http://issues.apache.org/jira/browse/EXTSCRIPT-154 remove duplicated calls in the startup process adding the missing links in the startup and shutdown, ready for testing now

git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/scripting/trunk@1297489 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/monitor/ResourceMonitor.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/monitor/ResourceMonitor.java
index 0497474..a708d76 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/monitor/ResourceMonitor.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/monitor/ResourceMonitor.java
@@ -73,6 +73,7 @@
     static WeakReference<ServletContext> _externalContext;
 
     public static synchronized void startup(ServletContext externalContext) {
+
         if (_externalContext == null) return;
         _externalContext = new WeakReference<ServletContext>(externalContext);
         if(getInstance() != null) return;
@@ -113,26 +114,32 @@
      * which performs the entire scanning process
      */
     public void run() {
-        WeavingContext context = WeavingContext.getInstance();
+
         while(_running) {
-            sleep();
+            if(!_running) break;
             //we run the full scan on the classes to bring our data structures up to the task
-            context.initialFullScan();
-            //we compile wherever needed, taints are now in place due to our scan already being performed
-            if(context.compile()) {
-                //we now have to perform a full dependency scan to bring our dependency map to the latest state
-                context.scanDependencies();
-                context.markTaintedDependends();
-                //we next retaint all classes according to our dependency graph
-            }
-
-
+            performMonitoringTask();
+            sleep();
         }
+
         if (_log.isLoggable(Level.INFO)) {
             _log.info("[EXT-SCRIPTING] Dynamic reloading watch daemon is shutting down");
         }
     }
 
+    public void performMonitoringTask()
+    {
+        WeavingContext context = WeavingContext.getInstance();
+        context.initialFullScan();
+        //we compile wherever needed, taints are now in place due to our scan already being performed
+        if(context.compile()) {
+            //we now have to perform a full dependency scan to bring our dependency map to the latest state
+            context.scanDependencies();
+            //we next retaint all classes according to our dependency graph
+            context.markTaintedDependends();
+        }
+    }
+
     private void sleep()
     {
         try {
@@ -150,30 +157,7 @@
      * as marks the engine as having to do a full recompile
      */
     private final void checkForChanges() {
-    /*    ScriptingWeaver weaver = WeavingContext.getWeaver();
-        if (weaver == null) return;
-        weaver.scanForAddedClasses();
-
-        for (Map.Entry<String, ClassResource> it : this._classMap.entrySet()) {
-
-            File proxyFile = it.getValue().getFile();
-            if (isModified(it, proxyFile)) {
-
-                _systemRecompileMap.put(it.getValue().getScriptingEngine(), Boolean.TRUE);
-                ClassResource meta = it.getValue();
-                meta.getRefreshAttribute().requestRefresh();
-                printInfo(it, proxyFile);
-
-                dependencyTainted(meta.getAClass().getName());
-
-                //we add our log entry for further reference
-                WeavingContext.getRefreshContext().addTaintLogEntry(meta);
-                WeavingContext.getExtensionEventRegistry().sendEvent(new ClassTaintedEvent(meta));
-            }
-            //}
-        }
-        //we clean up the taint log
-        WeavingContext.getRefreshContext().gcTaintLog();*/
+   
     }
 
     /**
@@ -185,51 +169,9 @@
      * @param className the origin classname which needs to be walked recursively
      */
     private void dependencyTainted(String className) {
-        /*Set<String> referrers = _dependencyMap.getReferringClasses(className);
-        if (referrers == null) return;
-        for (String referrer : referrers) {
-            ClassResource metaData = _classMap.get(referrer);
-            if (metaData == null) continue;
-            if (metaData.getRefreshAttribute().requiresRefresh()) continue;
-            printInfo(metaData);
-
-            metaData.getRefreshAttribute().requestRefresh();
-            
-            dependencyTainted(metaData.getAClass().getName());
-            WeavingContext.getRefreshContext().addTaintLogEntry(metaData);
-            WeavingContext.getExtensionEventRegistry().sendEvent(new ClassTaintedEvent(metaData));
-        } */
+    
     }
 
-    /*private final boolean isModified(Map.Entry<String, ClassResource> it, File proxyFile) {
-        return proxyFile.lastModified() > it.getValue().getRefreshAttribute().getRequestedRefreshDate() ;
-    }
-
-    private void printInfo(ClassResource it) {
-        if (_log.isLoggable(Level.INFO)) {
-            _log.log(Level.INFO, "[EXT-SCRIPTING] Tainting Dependency: {0}", it.getFile().getAbsolutePath());
-        }
-    }
-
-    private void printInfo(Map.Entry<String, ClassResource> it, File proxyFile) {
-        if (_log.isLoggable(Level.INFO)) {
-            _log.log(Level.INFO, "[EXT-SCRIPTING] comparing {0} Dates: {1} {2} ", new String[]{it.getKey(), Long.toString(proxyFile.lastModified()), Long.toString(it.getValue().getRefreshAttribute().getExecutedRefreshDate())});
-            _log.log(Level.INFO, "[EXT-SCRIPTING] Tainting: {0}", it.getValue().getFile().getAbsolutePath());
-        }
-    }
-
-    public boolean isRunning() {
-        return _running;
-    }
-
-
-    public Map<Integer, Boolean> getSystemRecompileMap() {
-        return _systemRecompileMap;
-    }
-
-    public void setSystemRecompileMap(Map<Integer, Boolean> systemRecompileMap) {
-        this._systemRecompileMap = systemRecompileMap;
-    } */
 
 
     public void setRunning(boolean running) {
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/startup/StartupServletContextPluginChainLoader.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/startup/StartupServletContextPluginChainLoader.java
index 55b95c2..5abacc7 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/startup/StartupServletContextPluginChainLoader.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/startup/StartupServletContextPluginChainLoader.java
@@ -25,6 +25,7 @@
 import rewrite.org.apache.myfaces.extensions.scripting.common.util.ReflectUtil;
 import rewrite.org.apache.myfaces.extensions.scripting.context.WeavingContext;
 import rewrite.org.apache.myfaces.extensions.scripting.engine.FactoryEngines;
+import rewrite.org.apache.myfaces.extensions.scripting.monitor.ResourceMonitor;
 
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
@@ -57,16 +58,13 @@
             context.getConfiguration().init(servletContext);
             _log.info("[EXT-SCRIPTING] Loading Scripting end");
 
-            _log.info("[EXT-SCRIPTING] Initial Scan");
-            context.initialFullScan();
-            _log.info("[EXT-SCRIPTING] Initial Compile");
-            //we can rely on the compiler picking the files
-            context.compile();
-            _log.info("[EXT-SCRIPTING] init the chain loader");
-            initChainLoader(servletContext);
+            _log.info("[EXT-SCRIPTING] Initial Scan and compile");
+            ResourceMonitor.getInstance().performMonitoringTask();
             _log.info("[EXT-SCRIPTING] Starting Change Monitor");
-            //TODO code the initial watchdog daemon start here
+            ResourceMonitor.getInstance().start();
             _log.info("[EXT-SCRIPTING] Startup done");
+            _log.info("[EXT-SCRIPTING] init the chain loader for class loading");
+            initChainLoader(servletContext);
 
         }
         catch (IOException e)
@@ -111,11 +109,6 @@
     {
         //context is destroyed we have to shut down our daemon as well, by giving it
         //a hint to shutdown
-
-        //TODO this is probably not needed because we run in a daemon thread anyway
-        //so the servlet should not have a problem to shut it down externally
-        /*  RefreshContext rContext = (RefreshContext) evt.getServletContext().getAttribute("RefreshContext");
-        rContext.getDaemon().setRunning(false);
-        */
+        ResourceMonitor.getInstance().setRunning(false);
     }
 }