EXTSCRIPT-107: ongoing works for the cdi integration

git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/scripting/trunk@1309857 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/listener/ReloadingListener.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/core/ReloadingListener.java
similarity index 97%
rename from extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/listener/ReloadingListener.java
rename to extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/core/ReloadingListener.java
index 6f74c65..4edbda2 100644
--- a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/listener/ReloadingListener.java
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/core/ReloadingListener.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-package org.apache.myfaces.extensions.scripting.cdi.listener;
+package org.apache.myfaces.extensions.scripting.cdi.core;
 
 import org.apache.myfaces.extensions.scripting.cdi.api.CdiContainerLoader;
 import org.apache.myfaces.extensions.scripting.core.api.eventhandling.WeavingEvent;
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/CDIServletContainerInitializer.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/CDIServletContainerInitializer.java
new file mode 100644
index 0000000..1badd95
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/CDIServletContainerInitializer.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.myfaces.extensions.scripting.cdi.startup;
+
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
+import java.lang.ref.WeakReference;
+import java.util.Set;
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ *
+ * Initializer which stores the servlet context
+ * for later non servlet based references.
+ *
+ * This is the first stage in the servlet lifecycle
+ * and starts before the cdi container.
+ *
+ * Stage 0 of our startup cycle
+ */
+
+public class CDIServletContainerInitializer implements ServletContainerInitializer
+{
+    private static WeakReference<ServletContext> _contextHolder = null;
+
+    public void onStartup(Set<Class<?>> c, ServletContext cx)
+    {
+        _contextHolder = new WeakReference(cx);
+    }
+
+    public static ServletContext getContext()
+    {
+        return _contextHolder.get();
+    }
+
+}
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupPlugin.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/ExtScriptStartupPlugin.java
similarity index 84%
rename from extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupPlugin.java
rename to extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/ExtScriptStartupPlugin.java
index 1c7f9ce..1e39ab0 100644
--- a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupPlugin.java
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/ExtScriptStartupPlugin.java
@@ -20,15 +20,23 @@
 package org.apache.myfaces.extensions.scripting.cdi.startup;
 
 import javax.servlet.ServletContextEvent;
+
+import org.apache.myfaces.extensions.scripting.cdi.core.ReloadingListener;
 import org.apache.myfaces.extensions.scripting.core.api.Plugin;
 import org.apache.myfaces.extensions.scripting.core.api.WeavingContext;
 
 /**
  * @author Werner Punz (latest modification by $Author$)
  * @version $Revision$ $Date$
+ *
+ * An Ext-Scripting startup plugin
+ * which is called at the various
+ * stages of the startup lifecycle
+ *
+ * Stage 2 of our startup cycle
  */
 
-public class StartupPlugin implements Plugin
+public class ExtScriptStartupPlugin implements Plugin
 {
     /**
      * This method is called before myfaces initializes
@@ -38,6 +46,7 @@
     public void preInit(ServletContextEvent evt)
     {
         //WeavingContext.getInstance().
+        System.out.println("Starting up the jsf subsystem extension");
     }
 
     /**
@@ -47,7 +56,7 @@
      */
     public void postInit(ServletContextEvent evt)
     {
-
+        WeavingContext.getInstance().addListener(new ReloadingListener());
     }
 
     /**
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupExtension.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupExtension.java
index 275c7a3..8e1a0df 100644
--- a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupExtension.java
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupExtension.java
@@ -19,10 +19,15 @@
 
 package org.apache.myfaces.extensions.scripting.cdi.startup;
 
+import org.apache.myfaces.extensions.scripting.core.engine.ThrowAwayClassloader;
+import org.apache.myfaces.extensions.scripting.jsf.startup.StartupServletContextPluginChainLoader;
+
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.AfterDeploymentValidation;
 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
 import javax.enterprise.inject.spi.Extension;
+import java.io.IOException;
 
 /**
  * @author Werner Punz (latest modification by $Author$)
@@ -31,15 +36,40 @@
  *          An extension for cdi which does the initial lifecycle
  *          trigger from CDI instead of JSF since CDI is enabled
  *          before JSF we have to do it that way
+ *
+ *          Stage 1 of our startup cycle
  */
 
 public class StartupExtension implements Extension
 {
+    static ThreadLocal<ClassLoader> _classLoaderHolder = new ThreadLocal<ClassLoader>();
+
     void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd)
     {
+        //We have to trigger our initial lifecycle with
+        //the compile runs but not with the daemon thread
+        //after that we can load the classes
+        //by temporarily plugging in our throw away classloader
+        try
+        {
+            StartupServletContextPluginChainLoader.startup(CDIServletContainerInitializer.getContext());
+            _classLoaderHolder.set(Thread.currentThread().getContextClassLoader());
+            Thread.currentThread().setContextClassLoader(new ThrowAwayClassloader());
+        }
+        catch (IOException e)
+        {
+            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+        }
     }
 
     void afterBeanDiscovery(@Observes AfterBeanDiscovery abd)
     {
+        //here we unplug our temporary classloader
     }
+
+
+    void afterDeploymentValidation(@Observes AfterDeploymentValidation abv)
+      {
+            Thread.currentThread().setContextClassLoader(_classLoaderHolder.get());
+      }
 }
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupServletContextListener.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupServletContextListener.java
deleted file mode 100644
index 0f99824..0000000
--- a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/StartupServletContextListener.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.myfaces.extensions.scripting.cdi.startup;
-
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
-import javax.servlet.annotation.WebListener;
-
-/**
- * @author Werner Punz (latest modification by $Author$)
- * @version $Revision$ $Date$
- */
-
-@WebListener
-public class StartupServletContextListener implements ServletContextListener
-{
-
-    public void contextInitialized(ServletContextEvent sce)
-    {
-    }
-
-    public void contextDestroyed(ServletContextEvent sce)
-    {
-    }
-}
diff --git a/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
index fc26579..a394685 100644
--- a/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
+++ b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -1 +1,20 @@
+#####################################################################################
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#####################################################################################
+
 org.apache.myfaces.extensions.scripting.cdi.startup.StartupExtension
\ No newline at end of file
diff --git a/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
new file mode 100644
index 0000000..0ddf67b
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
@@ -0,0 +1,20 @@
+#####################################################################################
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#####################################################################################
+
+org.apache.myfaces.extensions.scripting.cdi.startup.CDIServletContainerInitializer
\ No newline at end of file
diff --git a/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/org.apache.myfaces.extensions.scripting.core.api.Plugin b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/org.apache.myfaces.extensions.scripting.core.api.Plugin
index 0a1d364..e93db49 100644
--- a/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/org.apache.myfaces.extensions.scripting.core.api.Plugin
+++ b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/org.apache.myfaces.extensions.scripting.core.api.Plugin
@@ -17,4 +17,4 @@
 # under the License.
 #####################################################################################
 
-org.apache.myfaces.extensions.scripting.cdi.startup.StartupPlugin
\ No newline at end of file
+org.apache.myfaces.extensions.scripting.cdi.startup.ExtScriptStartupPlugin
\ No newline at end of file
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/monitor/ResourceMonitor.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/monitor/ResourceMonitor.java
index e4a1af0..42c6b76 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/monitor/ResourceMonitor.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/monitor/ResourceMonitor.java
@@ -96,6 +96,7 @@
     {
         //we do it in this complicated manner because of find bugs
         //practically this cannot really happen except for shutdown were it is not important anymore
+        if(_externalContext == null) return null;
         ServletContext context = _externalContext.get();
         if (context != null)
         {
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/StartupServletContextPluginChainLoader.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/StartupServletContextPluginChainLoader.java
index c8b698f..f619d68 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/StartupServletContextPluginChainLoader.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/StartupServletContextPluginChainLoader.java
@@ -45,7 +45,7 @@
 
 public class StartupServletContextPluginChainLoader implements StartupListener
 {
-    final Logger _log = Logger.getLogger(this.getClass().getName());
+    static final Logger _log = Logger.getLogger(StartupServletContextPluginChainLoader.class.getName());
     Plugin[] _plugins = new Plugin[]{};
 
     public StartupServletContextPluginChainLoader()
@@ -65,26 +65,7 @@
         ServletContext servletContext = servletContextEvent.getServletContext();
         try
         {
-            WeavingContext context = WeavingContext.getInstance();
-            _log.info("[EXT-SCRIPTING] Instantiating StartupServletContextPluginChainLoader");
-            context.initEngines();
-            _log.info("[EXT-SCRIPTING] Loading configuration");
-            context.getConfiguration().init(servletContext);
-            _log.info("[EXT-SCRIPTING] Loading Scripting end");
-            _log.info("[EXT-SCRIPTING] initializing startup daemon");
-            ResourceMonitor.init(servletContext);
-            _log.info("[EXT-SCRIPTING] initializing startup daemon end");
-            _log.info("[EXT-SCRIPTING] Initial Scan and compile");
-            //the initial scan should happen synchronsously
-            ResourceMonitor.getInstance().performMonitoringTask();
-            _log.info("[EXT-SCRIPTING] Starting Change Monitor");
-            ResourceMonitor.getInstance().start();
-            _log.info("[EXT-SCRIPTING] Startup done");
-            _log.info("[EXT-SCRIPTING] init the chain loader for class loading");
-            //TODO make this more generic depending on the implementation
-            MyFacesSPI.getInstance().registerClassloadingExtension(servletContext);
-            _log.info("[EXT-SCRIPTING] registering the JSF Implementation");
-            WeavingContext.getInstance().setImplementation(MyFacesSPI.getInstance());
+            startup(servletContext);
 
             for (Plugin plugin : _plugins)
             {
@@ -98,6 +79,32 @@
 
     }
 
+    public static void startup(ServletContext servletContext) throws IOException
+    {
+        if(ResourceMonitor.getInstance() != null && ResourceMonitor.getInstance().isAlive()) return;
+
+        WeavingContext context = WeavingContext.getInstance();
+        _log.info("[EXT-SCRIPTING] Instantiating StartupServletContextPluginChainLoader");
+        context.initEngines();
+        _log.info("[EXT-SCRIPTING] Loading configuration");
+        context.getConfiguration().init(servletContext);
+        _log.info("[EXT-SCRIPTING] Loading Scripting end");
+        _log.info("[EXT-SCRIPTING] initializing startup daemon");
+        ResourceMonitor.init(servletContext);
+        _log.info("[EXT-SCRIPTING] initializing startup daemon end");
+        _log.info("[EXT-SCRIPTING] Initial Scan and compile");
+        //the initial scan should happen synchronsously
+        ResourceMonitor.getInstance().performMonitoringTask();
+        _log.info("[EXT-SCRIPTING] Starting Change Monitor");
+        ResourceMonitor.getInstance().start();
+        _log.info("[EXT-SCRIPTING] Startup done");
+        _log.info("[EXT-SCRIPTING] init the chain loader for class loading");
+        //TODO make this more generic depending on the implementation
+        MyFacesSPI.getInstance().registerClassloadingExtension(servletContext);
+        _log.info("[EXT-SCRIPTING] registering the JSF Implementation");
+        WeavingContext.getInstance().setImplementation(MyFacesSPI.getInstance());
+    }
+
     public void postInit(ServletContextEvent evt)
     {
         //tell the system that the startup phase is done