EXTSCRIPT-107: CDI Integration work started

git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/scripting/trunk@1309683 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/CdiContainer.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/CdiContainer.java
new file mode 100644
index 0000000..37e0cb3
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/CdiContainer.java
@@ -0,0 +1,63 @@
+/*
+ * 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.api;
+
+
+
+import javax.enterprise.inject.spi.BeanManager;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+
+/**
+ * <p>A CdiTestContainer provides access to an underlying JSR-299 (CDI)
+ * Container. It allows starting and stopping the container and to start
+ * and stop the built-in contexts of that container.</p>
+ *
+ * <p>The intention is to provide a portable control for CDI containers in
+ * Java SE environments. It is <b>not</b> intended for environments in which the
+ * CDI container is under full control of the server already, e.g. in
+ * EE-containers.</p>
+ */
+public interface CdiContainer
+{
+    /**
+     * Booting the CdiTestContainer will scan the whole classpath
+     * for Beans and extensions available.
+     * The container might throw a DeploymentException or similar on startup.
+     */
+    void boot();
+    
+    /**
+     * This will shutdown the underlying CDI container.
+     */
+    void shutdown();
+    
+
+    /**
+     * @return the {@link javax.enterprise.inject.spi.BeanManager} or <code>null</code> it not available
+     */
+    BeanManager getBeanManager();
+
+    /**
+     * @return ContextControl for the started Container. <code>null</code> if the container is not yet started
+     */
+    ContextControl getContextControl(ServletContext context, HttpSession session);
+
+}
+
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/CdiContainerLoader.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/CdiContainerLoader.java
new file mode 100644
index 0000000..83ece78
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/CdiContainerLoader.java
@@ -0,0 +1,81 @@
+/*
+ * 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.api;
+
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+/**
+ * <p>This class provides access to the ContainerControl.</p>
+ * <p>It uses the {@code java.util.ServiceLoader} mechanism  to 
+ * automatically pickup the container providers from the classpath.</p>
+ */
+public final class CdiContainerLoader
+{
+    private CdiContainerLoader()
+    {
+        // private ct to prevent instantiation
+    }
+
+    
+    public static CdiContainer getCdiContainer()
+    {
+        CdiContainer testContainer;
+
+        //doesn't support the implementation loader (there is no dependency to owb-impl
+        ServiceLoader<CdiContainer> cdiContainerLoader = ServiceLoader.load(CdiContainer.class);
+        Iterator<CdiContainer> cdiIt = cdiContainerLoader.iterator();
+        if (cdiIt.hasNext())
+        {
+            testContainer = cdiIt.next();
+        }
+        else
+        {
+            throw new IllegalStateException("Could not find an implementation of " + org.apache.deltaspike.cdise.api.CdiContainer.class.getName() +
+                " available in the classpath!");
+        }
+
+        if (cdiIt.hasNext())
+        {
+            String foundContainers = getContainerDetails();
+            throw new IllegalStateException("Too many implementations of " + org.apache.deltaspike.cdise.api.CdiContainer.class.getName() +
+                " found in the classpath! Details: " + foundContainers);
+        }
+
+        return testContainer;
+    }
+
+    private static String getContainerDetails()
+    {
+        StringBuilder result = new StringBuilder();
+
+        Class containerClass;
+        for (CdiContainer cdiContainer : ServiceLoader.load(CdiContainer.class))
+        {
+            containerClass = cdiContainer.getClass();
+            result.append(containerClass.getProtectionDomain().getCodeSource().getLocation().toExternalForm());
+            result.append(containerClass.getName());
+
+            result.append(System.getProperty("line.separator"));
+        }
+
+        return result.toString();
+    }
+}
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/ContextControl.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/ContextControl.java
new file mode 100644
index 0000000..0947c96
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/api/ContextControl.java
@@ -0,0 +1,64 @@
+/*
+ * 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.api;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import java.lang.annotation.Annotation;
+
+/**
+ * Control native CDI Container Contexts.
+ * Just inject this interface and you gain manual access over built-in Contexts.
+ * The respective integration code will provide a &064;Dependent scoped instance.
+ */
+public interface ContextControl
+{
+    /**
+     * This will start all container built-in Contexts
+     */
+    void startContexts();
+
+    /**
+     * Stop all container built-in Contexts and destroy all beans properly
+     */
+    void stopContexts();
+
+    /**
+     * Start the specified scope. This only works for scopes which are handled
+     * by the CDI container itself. Custom scoped of 3rd party
+     * Context implementations shall be started directly (they are portable anyway).
+     *
+     * @param scopeClass e.g. RequestScoped.class
+     */
+    void startContext(Class<? extends Annotation> scopeClass);
+
+    /**
+     * Stop the specified scope. This only works for scopes which are handled
+     * by the CDI container itself. Custom scoped of 3rd party
+     * Context implementations shall be stopped directly (they are portable anyway).
+     *
+     * @param scopeClass e.g. RequestScoped.class
+     */
+    void stopContext(Class<? extends Annotation> scopeClass);
+
+
+    void setSession(HttpSession session);
+
+    void setServletContext(ServletContext servletContext);
+}
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/listener/ReloadingListener.java
new file mode 100644
index 0000000..6f74c65
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/listener/ReloadingListener.java
@@ -0,0 +1,82 @@
+/*
+ * 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.listener;
+
+import org.apache.myfaces.extensions.scripting.cdi.api.CdiContainerLoader;
+import org.apache.myfaces.extensions.scripting.core.api.eventhandling.WeavingEvent;
+import org.apache.myfaces.extensions.scripting.core.api.eventhandling.WeavingEventListener;
+import org.apache.myfaces.extensions.scripting.core.api.eventhandling.events.RefreshBeginEvent;
+import org.apache.myfaces.extensions.scripting.core.api.eventhandling.events.TaintedEvent;
+import org.apache.myfaces.extensions.scripting.core.engine.ThrowAwayClassloader;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ *          <p/>
+ *          Reloading listener which triggers a container reload
+ *          the reload can happen at the end of the extscript lifecycle
+ *          when all beans have been tainted
+ */
+public class ReloadingListener implements WeavingEventListener
+{
+    boolean _tainted = false;
+    ServletContext context;
+
+    @Override
+    public void onEvent(WeavingEvent evt)
+    {
+        if (evt instanceof TaintedEvent)
+        {
+            _tainted = true;
+        } else if (evt instanceof RefreshBeginEvent)
+        {
+            RefreshBeginEvent refreshEvent = (RefreshBeginEvent) evt;
+            if (_tainted)
+            {
+                _tainted = false;
+                HttpServletRequest req = (HttpServletRequest) refreshEvent.getRequest();
+                //TODO plug reloadable classloader in here temporarily
+                //as context classloader, then restart the container
+                //then restore the old classloader
+                ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+                ClassLoader tempClassLoader = new ThrowAwayClassloader(oldClassLoader);
+                Thread.currentThread().setContextClassLoader(tempClassLoader);
+                try
+                {
+                    CdiContainerLoader.getCdiContainer().getContextControl(req.getServletContext(),
+                            req.getSession()).stopContexts();
+                    CdiContainerLoader.getCdiContainer().shutdown();
+
+                    CdiContainerLoader.getCdiContainer().boot();
+
+                    CdiContainerLoader.getCdiContainer().getContextControl(req.getServletContext(),
+                            req.getSession()).startContexts();
+                }
+                finally
+                {
+                    Thread.currentThread().setContextClassLoader(oldClassLoader);
+                }
+            }
+        }
+    }
+}
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/owb/OpenWebBeansContainerControl.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/owb/OpenWebBeansContainerControl.java
new file mode 100644
index 0000000..3e66918
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/owb/OpenWebBeansContainerControl.java
@@ -0,0 +1,90 @@
+/*
+ * 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.owb;
+
+import org.apache.myfaces.extensions.scripting.cdi.api.CdiContainer;
+import org.apache.myfaces.extensions.scripting.cdi.api.ContextControl;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.spi.ContainerLifecycle;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import java.util.Set;
+
+/**
+ * OpenWebBeans specific implementation of {@link org.apache.deltaspike.cdise.api.CdiContainer}.
+ */
+public class OpenWebBeansContainerControl implements CdiContainer
+{
+
+    private ContainerLifecycle lifecycle;
+
+    private ContextControl ctxCtrl = null;
+    private Bean<ContextControl> ctxCtrlBean = null;
+    private CreationalContext<ContextControl> ctxCtrlCreationalContext = null;
+
+    @Override
+    public  BeanManager getBeanManager()
+    {
+        return lifecycle.getBeanManager();
+    }
+
+    @Override
+    public synchronized void boot()
+    {
+
+        lifecycle = WebBeansContext.getInstance().getService(ContainerLifecycle.class);
+        lifecycle.startApplication(null);
+    }
+
+    @Override
+    public synchronized void shutdown()
+    {
+        if (ctxCtrl != null)
+        {
+            ctxCtrlBean.destroy(ctxCtrl, ctxCtrlCreationalContext);
+
+        }
+
+        if (lifecycle != null) 
+        {
+            lifecycle.stopApplication(null);
+        }
+    }
+
+    public synchronized ContextControl getContextControl(ServletContext context, HttpSession session)
+    {
+        if (ctxCtrl == null)
+        {
+            Set<Bean<?>> beans = getBeanManager().getBeans(ContextControl.class);
+            ctxCtrlBean = (Bean<ContextControl>) getBeanManager().resolve(beans);
+            ctxCtrlCreationalContext = getBeanManager().createCreationalContext(ctxCtrlBean);
+            ctxCtrl = (ContextControl)
+                    getBeanManager().getReference(ctxCtrlBean, ContextControl.class, ctxCtrlCreationalContext);
+            ctxCtrl.setServletContext(context);
+            ctxCtrl.setSession(session);
+        }
+        return ctxCtrl;
+    }
+
+
+}
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/owb/OpenWebBeansContextControl.java b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/owb/OpenWebBeansContextControl.java
new file mode 100644
index 0000000..1ec6cfe
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/owb/OpenWebBeansContextControl.java
@@ -0,0 +1,245 @@
+/*
+ * 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.owb;
+
+import org.apache.myfaces.extensions.scripting.cdi.api.ContextControl;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.context.ContextFactory;
+import org.apache.webbeans.context.type.ContextTypes;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.ConversationScoped;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.context.spi.Context;
+import javax.inject.Named;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import java.lang.annotation.Annotation;
+import java.util.logging.Logger;
+
+/**
+ * OWB specific impl of the {@link ContextControl}
+ */
+@Named
+@Dependent
+public class OpenWebBeansContextControl implements ContextControl
+{
+
+    private static final Logger LOG = Logger.getLogger(org.apache.deltaspike.cdise.owb.OpenWebBeansContextControl.class.getName());
+
+    private HttpSession session = null;
+    private ServletContext servletContext  = null;
+
+    public OpenWebBeansContextControl()
+    {
+    }
+
+    public OpenWebBeansContextControl(HttpSession session, ServletContext servletContext)
+    {
+        this.session = session;
+        this.servletContext = servletContext;
+    }
+
+    @Override
+    public void startContexts()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        contextFactory.initSingletonContext(servletContext);
+        contextFactory.initApplicationContext(servletContext);
+        contextFactory.initSessionContext(session);
+        contextFactory.initRequestContext(null);
+        contextFactory.initConversationContext(null);
+    }
+
+
+    public void stopContexts()
+    {
+        stopSessionScope();
+        stopConversationScope();
+        stopRequestScope();
+        stopApplicationScope();
+        stopSingletonScope();
+    }
+
+
+
+    @Override
+    public void startContext(Class<? extends Annotation> scopeClass)
+    {
+        if (scopeClass.isAssignableFrom(ApplicationScoped.class))
+        {
+            startApplicationScope();
+        }
+        else if (scopeClass.isAssignableFrom(SessionScoped.class))
+        {
+            startSessionScope();
+        }
+        else if (scopeClass.isAssignableFrom(RequestScoped.class))
+        {
+            startRequestScope();
+        }
+        else if (scopeClass.isAssignableFrom(ConversationScoped.class))
+        {
+            startConversationScope();
+        }
+    }
+
+
+    public void stopContext(Class<? extends Annotation> scopeClass)
+    {
+        if (scopeClass.isAssignableFrom(ApplicationScoped.class))
+        {
+            stopApplicationScope();
+        }
+        else if (scopeClass.isAssignableFrom(SessionScoped.class))
+        {
+            stopSessionScope();
+        }
+        else if (scopeClass.isAssignableFrom(RequestScoped.class))
+        {
+            stopRequestScope();
+        }
+        else if (scopeClass.isAssignableFrom(ConversationScoped.class))
+        {
+            stopConversationScope();
+        }
+    }
+
+    /*
+    * start scopes
+    */
+
+    private void startApplicationScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        contextFactory.initApplicationContext(servletContext);
+    }
+
+    private void startSessionScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        contextFactory.initSessionContext(session);
+    }
+
+    private void startRequestScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        contextFactory.initRequestContext(null);
+    }
+
+    private void startConversationScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        contextFactory.initConversationContext(null);
+    }
+
+    /*
+     * stop scopes
+     */
+
+    private void stopSingletonScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        Context context = contextFactory.getStandardContext(ContextTypes.SINGLETON);
+        if (context != null)
+        {
+            contextFactory.destroySingletonContext(servletContext);
+        }
+    }
+
+    private void stopApplicationScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        Context context = contextFactory.getStandardContext(ContextTypes.APPLICATION);
+        if (context != null)
+        {
+            contextFactory.destroyApplicationContext(servletContext);
+        }
+    }
+
+    private void stopSessionScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        Context context = contextFactory.getStandardContext(ContextTypes.SESSION);
+        if (context != null)
+        {
+            contextFactory.destroySessionContext(session);
+        }
+    }
+
+    private void stopRequestScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        Context context = contextFactory.getStandardContext(ContextTypes.REQUEST);
+        if (context != null)
+        {
+            contextFactory.destroyRequestContext(null);
+        }
+    }
+
+    private void stopConversationScope()
+    {
+        ContextFactory contextFactory = getContextFactory();
+
+        Context context = contextFactory.getStandardContext(ContextTypes.CONVERSATION);
+        if (context != null)
+        {
+            contextFactory.destroyConversationContext();
+        }
+    }
+
+    private ContextFactory getContextFactory()
+    {
+        WebBeansContext webBeansContext = WebBeansContext.getInstance();
+        return webBeansContext.getContextFactory();
+    }
+
+    //--------------------Setter and Getter------------------------------------
+
+    public HttpSession getSession()
+    {
+        return session;
+    }
+
+    public void setSession(HttpSession session)
+    {
+        this.session = session;
+    }
+
+    public ServletContext getServletContext()
+    {
+        return servletContext;
+    }
+
+    public void setServletContext(ServletContext servletContext)
+    {
+        this.servletContext = servletContext;
+    }
+}
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/StartupPlugin.java
index c856d04..1c7f9ce 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/StartupPlugin.java
@@ -21,6 +21,7 @@
 
 import javax.servlet.ServletContextEvent;
 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$)
@@ -36,7 +37,7 @@
      */
     public void preInit(ServletContextEvent evt)
     {
-        System.out.println("-----------------PREINIT--------------------");
+        //WeavingContext.getInstance().
     }
 
     /**
@@ -46,7 +47,7 @@
      */
     public void postInit(ServletContextEvent evt)
     {
-        System.out.println("-----------------POSTINIT--------------------");
+
     }
 
     /**
@@ -56,7 +57,6 @@
      */
     public void preDestroy(ServletContextEvent evt)
     {
-        System.out.println("-----------------PREDESTROY--------------------");
     }
 
     /**
@@ -66,6 +66,5 @@
      */
     public void postDestroy(ServletContextEvent evt)
     {
-        System.out.println("-----------------POSTDESTROY--------------------");
     }
 }
diff --git a/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/readme.txt b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/readme.txt
new file mode 100644
index 0000000..088f574
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/java/org/apache/myfaces/extensions/scripting/cdi/startup/readme.txt
@@ -0,0 +1,5 @@
+What is missing is a cdi plugin which triggers the initial lifecycle once
+at cdi startup time to make a first compile then pushes the
+ThrowAwayClassloader in once for the bean loading
+and once the beans are loaded deregisters everything so that the jsf lifecycle
+can start.
diff --git a/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/org.apache.myfaces.extensions.scripting.cdi.api.CdiContainer b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/org.apache.myfaces.extensions.scripting.cdi.api.CdiContainer
new file mode 100644
index 0000000..ea989fb
--- /dev/null
+++ b/extscript-core-root/extscript-cdi/src/main/resources/META-INF/services/org.apache.myfaces.extensions.scripting.cdi.api.CdiContainer
@@ -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.owb.OpenWebBeansContainerControl
\ 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 e69de29..0a1d364 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
@@ -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.StartupPlugin
\ No newline at end of file
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/api/eventhandling/events/RefreshBeginEvent.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/api/eventhandling/events/RefreshBeginEvent.java
new file mode 100644
index 0000000..a2120ae
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/api/eventhandling/events/RefreshBeginEvent.java
@@ -0,0 +1,49 @@
+/*
+ * 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.core.api.eventhandling.events;
+
+import org.apache.myfaces.extensions.scripting.core.api.eventhandling.WeavingEvent;
+
+import javax.servlet.ServletRequest;
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+
+public class RefreshBeginEvent implements WeavingEvent
+{
+    Object request;
+
+    public RefreshBeginEvent(Object request)
+    {
+        this.request = request;
+    }
+
+    public Object getRequest()
+    {
+        return request;
+    }
+
+    public void setRequest(Object request)
+    {
+        this.request = request;
+    }
+}
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java
index 66232eb..b727390 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java
@@ -20,11 +20,13 @@
 package org.apache.myfaces.extensions.scripting.jsf.startup;
 
 import org.apache.myfaces.extensions.scripting.core.api.WeavingContext;
+import org.apache.myfaces.extensions.scripting.core.api.eventhandling.events.RefreshBeginEvent;
 
 import javax.faces.context.FacesContext;
 import javax.faces.event.PhaseEvent;
 import javax.faces.event.PhaseId;
 import javax.faces.event.PhaseListener;
+import javax.servlet.ServletRequest;
 import java.util.Map;
 
 /**
@@ -50,6 +52,10 @@
         Map<Object, Object> params = context.getAttributes();
         if(params.containsKey("ANN_PROCESSED")) return;
         else params.put("ANN_PROCESSED", Boolean.TRUE);
+        WeavingContext.getInstance().sendWeavingEvent(new RefreshBeginEvent(FacesContext
+                .getCurrentInstance()
+                .getExternalContext().getRequest()));
+
         WeavingContext.getInstance().getImplementationSPI().refreshManagedBeans();
         WeavingContext.getInstance().annotationScan();
     }
diff --git a/extscript-examples/cdi-example/pom.xml b/extscript-examples/cdi-example/pom.xml
index 4055175..34a32aa 100644
--- a/extscript-examples/cdi-example/pom.xml
+++ b/extscript-examples/cdi-example/pom.xml
@@ -10,7 +10,7 @@
     <groupId>org.apache.myfaces.extensions.scripting</groupId>
     <artifactId>myfaces21-extscript-cdi</artifactId>
     <packaging>war</packaging>
-    <name>A simple project for MyFaces 2.1 and Ext-Scripting 1.0</name>
+    <name>A simple project for MyFaces 2.1 and Ext-Scripting 1.0 and CDI</name>
     <version>1.0.4-SNAPSHOT</version>
     <url>http://www.apache.org</url>