You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2012/09/25 12:51:19 UTC

svn commit: r1389787 - in /openwebbeans/trunk/webbeans-tomcat7/src: it/servletinjection/ it/servletinjection/src/main/resources/ it/servletinjection/src/main/webapp/META-INF/ it/servletinjection/src/main/webapp/WEB-INF/ it/servletinjection/src/test/jav...

Author: rmannibucau
Date: Tue Sep 25 10:51:18 2012
New Revision: 1389787

URL: http://svn.apache.org/viewvc?rev=1389787&view=rev
Log:
OWB-705 making basic it for tomcat7 passing

Added:
    openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/META-INF/
    openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/META-INF/context.xml
    openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/WEB-INF/beans.xml
Removed:
    openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/resources/
    openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/test/tomcat/
Modified:
    openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/pom.xml
    openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/test/java/org/apache/webbeans/web/tomcat7/test/OwbTomcatPluginIT.java
    openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/ContextLifecycleListener.java
    openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java

Modified: openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/pom.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/pom.xml?rev=1389787&r1=1389786&r2=1389787&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/pom.xml (original)
+++ openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/pom.xml Tue Sep 25 10:51:18 2012
@@ -53,47 +53,22 @@
             <scope>test</scope>
         </dependency>
 
-
-        <dependency>
-            <groupId>org.apache.tomcat</groupId>
-            <artifactId>tomcat-catalina</artifactId>
-            <version>7.0.0</version>
-            <scope>provided</scope>
-        </dependency>
-
         <dependency>
-            <groupId>org.apache.openwebbeans</groupId>
-            <artifactId>openwebbeans-spi</artifactId>
-            <version>@project.version@</version>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-jcdi_1.0_spec</artifactId>
             <scope>provided</scope>
         </dependency>
 
         <dependency>
-            <groupId>org.apache.openwebbeans</groupId>
-            <artifactId>openwebbeans-impl</artifactId>
-            <version>@project.version@</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.openwebbeans</groupId>
-            <artifactId>openwebbeans-web</artifactId>
-            <version>@project.version@</version>
-        </dependency>
-
-        <dependency>
-            <groupId>org.apache.openwebbeans</groupId>
-            <artifactId>openwebbeans-tomcat7</artifactId>
-            <version>@project.version@</version>
-        </dependency>
-
-        <dependency>
             <groupId>org.apache.geronimo.specs</groupId>
-            <artifactId>geronimo-jcdi_1.0_spec</artifactId>
+            <artifactId>geronimo-atinject_1.0_spec</artifactId>
             <scope>provided</scope>
         </dependency>
 
         <dependency>
             <groupId>org.apache.geronimo.specs</groupId>
-            <artifactId>geronimo-atinject_1.0_spec</artifactId>
+            <artifactId>geronimo-servlet_3.0_spec</artifactId>
+            <version>1.0</version>
             <scope>provided</scope>
         </dependency>
 
@@ -101,6 +76,12 @@
 
     <build>
         <finalName>owbtomcat7it</finalName>
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <filtering>true</filtering>
+            </resource>
+        </resources>
         <pluginManagement>
             <plugins>
                 <plugin>
@@ -226,16 +207,6 @@
                             <version>${tomcat7.version}</version>
                         </dependency>
 
-                        <!--
-                         * since we register the tomcat listener we also need
-                         * all owb stuff in the tomcat root classpath
-                        -->
-                        <dependency>
-                            <groupId>org.apache.tomcat</groupId>
-                            <artifactId>tomcat-catalina</artifactId>
-                            <version>7.0.0</version>
-                        </dependency>
-
                         <dependency>
                             <groupId>org.apache.openwebbeans</groupId>
                             <artifactId>openwebbeans-spi</artifactId>
@@ -262,16 +233,20 @@
                         <dependency>
                             <groupId>org.apache.geronimo.specs</groupId>
                             <artifactId>geronimo-jcdi_1.0_spec</artifactId>
-                            <version>${geronimo_cdi.version}</version>
+                            <version>1.0</version>
                         </dependency>
 
                         <dependency>
                             <groupId>org.apache.geronimo.specs</groupId>
                             <artifactId>geronimo-atinject_1.0_spec</artifactId>
-                            <version>${geronimo_atinject.version}</version>
+                            <version>1.0</version>
                         </dependency>
 
-
+                        <dependency>
+                            <groupId>org.apache.geronimo.specs</groupId>
+                            <artifactId>geronimo-interceptor_1.1_spec</artifactId>
+                            <version>1.0</version>
+                        </dependency>
                     </dependencies>
                 </plugin>
 
@@ -305,7 +280,6 @@
                 <version>2.0-beta-1</version>
                 <configuration>
                     <path>/${project.build.finalName}</path>
-                    <serverXml>src/test/tomcat/server.xml</serverXml>
                     <addContextWarDependencies>true</addContextWarDependencies>
                     <port>${tomcat.port.it}</port>
                     <fork>true</fork> <!-- needed for testing ITs -->
@@ -319,7 +293,7 @@
                         <id>start-tomcat</id>
                         <phase>pre-integration-test</phase>
                         <goals>
-                            <goal>run</goal>
+                            <goal>run-war</goal>
                         </goals>
                     </execution>
                     <execution>

Added: openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/META-INF/context.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/META-INF/context.xml?rev=1389787&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/META-INF/context.xml (added)
+++ openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/META-INF/context.xml Tue Sep 25 10:51:18 2012
@@ -0,0 +1,19 @@
+<!--
+
+    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.
+-->
+<Context>
+  <Listener className="org.apache.webbeans.web.tomcat.ContextLifecycleListener" />
+</Context>

Added: openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/WEB-INF/beans.xml?rev=1389787&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/WEB-INF/beans.xml (added)
+++ openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/main/webapp/WEB-INF/beans.xml Tue Sep 25 10:51:18 2012
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<beans/>

Modified: openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/test/java/org/apache/webbeans/web/tomcat7/test/OwbTomcatPluginIT.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/test/java/org/apache/webbeans/web/tomcat7/test/OwbTomcatPluginIT.java?rev=1389787&r1=1389786&r2=1389787&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/test/java/org/apache/webbeans/web/tomcat7/test/OwbTomcatPluginIT.java (original)
+++ openwebbeans/trunk/webbeans-tomcat7/src/it/servletinjection/src/test/java/org/apache/webbeans/web/tomcat7/test/OwbTomcatPluginIT.java Tue Sep 25 10:51:18 2012
@@ -26,6 +26,10 @@ import org.apache.http.impl.client.Defau
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.lang.StringBuilder;
+
 /**
  * Simple requests to the tomcat installation
  */
@@ -38,8 +42,19 @@ public class OwbTomcatPluginIT
         HttpGet httpGet = new HttpGet("http://localhost:9081/owbtomcat7it/test.test");
 
         HttpResponse response = httpclient.execute(httpGet);
+
+        // Get the response
+        BufferedReader rd = new BufferedReader
+                (new InputStreamReader(response.getEntity().getContent()));
+
+        StringBuilder builder = new StringBuilder();
+        String line = "";
+        while ((line = rd.readLine()) != null) {
+            builder.append(line);
+        }
+
         Assert.assertNotNull(response);
-        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("Got " + builder.toString(), 200, response.getStatusLine().getStatusCode());
     }
 
 }

Modified: openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/ContextLifecycleListener.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/ContextLifecycleListener.java?rev=1389787&r1=1389786&r2=1389787&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/ContextLifecycleListener.java (original)
+++ openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/ContextLifecycleListener.java Tue Sep 25 10:51:18 2012
@@ -21,10 +21,10 @@ package org.apache.webbeans.web.tomcat;
 import org.apache.catalina.Container;
 import org.apache.catalina.ContainerEvent;
 import org.apache.catalina.ContainerListener;
+import org.apache.catalina.Lifecycle;
 import org.apache.catalina.LifecycleEvent;
 import org.apache.catalina.LifecycleListener;
 import org.apache.catalina.Service;
-import org.apache.catalina.Lifecycle;
 import org.apache.catalina.core.ContainerBase;
 import org.apache.catalina.core.StandardContext;
 import org.apache.catalina.core.StandardEngine;
@@ -32,8 +32,12 @@ import org.apache.catalina.core.Standard
 import org.apache.catalina.core.StandardServer;
 import org.apache.naming.ContextAccessController;
 import org.apache.tomcat.InstanceManager;
+import org.apache.webbeans.exception.WebBeansException;
 import org.apache.webbeans.servlet.WebBeansConfigurationListener;
 
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextAttributeEvent;
+import javax.servlet.ServletContextAttributeListener;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.lang.reflect.AccessibleObject;
@@ -46,8 +50,6 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
 
-import javax.servlet.ServletContext;
-
 /**
  * Context lifecycle listener. Adapted from
  * OpenEJB Tomcat and updated.
@@ -55,7 +57,7 @@ import javax.servlet.ServletContext;
  * @version $Rev$ $Date$
  *
  */
-public class ContextLifecycleListener implements PropertyChangeListener, LifecycleListener, ContainerListener
+public class ContextLifecycleListener implements PropertyChangeListener, LifecycleListener, ContainerListener, ServletContextAttributeListener
 {
 
     private StandardServer standardServer;
@@ -79,7 +81,6 @@ public class ContextLifecycleListener im
             else if (event.getSource() instanceof StandardContext)
             {
                 StandardContext context = (StandardContext) event.getSource();
-                
                 if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT))
                 {
                     ServletContext scontext = context.getServletContext();
@@ -88,7 +89,7 @@ public class ContextLifecycleListener im
                     {
                         //Registering ELResolver with JSP container
                         System.setProperty("org.apache.webbeans.application.jsp", "true");
-                        
+
                         String[] oldListeners = context.findApplicationListeners();
                         LinkedList<String> listeners = new LinkedList<String>();
 
@@ -107,10 +108,11 @@ public class ContextLifecycleListener im
                         }                        
                         
                         context.addApplicationListener(TomcatSecurityListener.class.getName());
+                        context.addApplicationEventListener(this);
                         //context.addInstanceListener(TomcatInstanceListener.class.getName());             
                     }
                 }
-            }                        
+            }
         }
         catch(Exception e)
         {
@@ -131,13 +133,11 @@ public class ContextLifecycleListener im
     public void containerEvent(ContainerEvent event)
     {
         StandardContext context;
-        
         try
         {
             if(event.getSource() instanceof StandardContext)
             {               
                 context = (StandardContext)event.getSource();
-                
                 if(event.getType().equals("beforeContextInitialized"))
                 {
                     ClassLoader loader = context.getLoader().getClassLoader();
@@ -161,14 +161,9 @@ public class ContextLifecycleListener im
                 {
                     ClassLoader loader = context.getLoader().getClassLoader();
                     Object listener = event.getData();
-                    
                     if(listener.getClass().getName().equals(WebBeansConfigurationListener.class.getName()))
-                    {   
-                        InstanceManager processor = context.getInstanceManager();
-                        InstanceManager custom = new TomcatInstanceManager(context.getLoader().getClassLoader(),processor);
-                        context.setInstanceManager(custom);
-                        
-                        context.getServletContext().setAttribute(InstanceManager.class.getName(), custom);
+                    {
+                        setInstanceManager(context);
                         
                         ContextAccessController.setReadOnly(context.getNamingContextListener().getName());
 
@@ -194,15 +189,28 @@ public class ContextLifecycleListener im
                         ContextAccessController.setWritable(context.getNamingContextListener().getName(),context);   
                     }
                 }
-            }        
-            
+            }
         }
         catch(Exception e)
         {
             throw new RuntimeException(e);
         }
     }
-    
+
+    private void setInstanceManager(final StandardContext context)
+    {
+        if (context.getInstanceManager() instanceof TomcatInstanceManager)
+        {
+            return;
+        }
+
+        InstanceManager processor = context.getInstanceManager();
+        InstanceManager custom = new TomcatInstanceManager(context.getLoader().getClassLoader(), processor);
+        context.setInstanceManager(custom);
+
+        context.getServletContext().setAttribute(InstanceManager.class.getName(), custom);
+    }
+
     public void start()
     {
         // hook the hosts so we get notified before contexts are started
@@ -348,6 +356,52 @@ public class ContextLifecycleListener im
 
     }
 
+    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent)
+    {
+        if (InstanceManager.class.getName().equals(servletContextAttributeEvent.getName()))
+        { // used as a hook to know we can override eagerly the InstanceManager
+            try
+            {
+                final StandardContext context = (StandardContext) getContext(
+                                            getContext(servletContextAttributeEvent.getServletContext()));
+                setInstanceManager(context);
+            }
+            catch (NoSuchFieldException e)
+            {
+                throw new WebBeansException(e.getMessage(), e);
+            }
+            catch (IllegalAccessException e)
+            {
+                throw new WebBeansException(e.getMessage(), e);
+            }
+        }
+    }
+
+    private static Object getContext(final Object o) throws NoSuchFieldException, IllegalAccessException
+    {
+        final Field getContext = o.getClass().getDeclaredField("context");
+        final boolean acc = getContext.isAccessible();
+        getContext.setAccessible(true);
+        try
+        {
+            return getContext.get(o);
+        }
+        finally
+        {
+            getContext.setAccessible(acc);
+        }
+    }
+
+    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent)
+    {
+        // no-op
+    }
+
+    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent)
+    {
+        // no-op
+    }
+
     public static class MoniterableHashMap extends HashMap<Object,Object>
     {
         private static final long serialVersionUID = 1L;

Modified: openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java?rev=1389787&r1=1389786&r2=1389787&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java (original)
+++ openwebbeans/trunk/webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java Tue Sep 25 10:51:18 2012
@@ -18,25 +18,67 @@
  */
 package org.apache.webbeans.web.tomcat;
 
-import java.lang.reflect.Method;
+import org.apache.webbeans.component.InjectionTargetWrapper;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanManagerImpl;
+import org.apache.webbeans.inject.OWBInjector;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.BeanManager;
 
 public class TomcatUtil
 {
     public static Object inject(Object object, ClassLoader loader) throws Exception
     {
-        Class<?> injector = loader.loadClass("org.apache.webbeans.inject.OWBInjector");
-        Object injectorInstance = injector.newInstance();
-        Method method = injector.getDeclaredMethod("inject", new Class<?>[]{Object.class});
-        injectorInstance = method.invoke(injectorInstance, new Object[]{object});       
-        
-        return injectorInstance;
+        final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
+        Thread.currentThread().setContextClassLoader(loader);
+        CreationalContext<?> context = null;
+        try
+        {
+            final BeanManager beanManager = WebBeansContext.currentInstance().getBeanManagerImpl();
+            context = beanManager.createCreationalContext(null);
+            OWBInjector.inject(beanManager, object, context);
+        }
+        finally
+        {
+            Thread.currentThread().setContextClassLoader(oldLoader);
+        }
+        return new Instance(object, context);
     }
     
     public static void destroy(Object injectorInstance, ClassLoader loader) throws Exception
     {
-        Class<?> injector = loader.loadClass("org.apache.webbeans.inject.OWBInjector");
-        Method method = injector.getDeclaredMethod("destroy", new Class<?>[]{});
-        method.invoke(injectorInstance, new Object[]{});               
+        final Instance instance = (TomcatUtil.Instance) injectorInstance;
+        final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
+        Thread.currentThread().setContextClassLoader(loader);
+        try
+        {
+            final BeanManagerImpl beanManager = WebBeansContext.currentInstance().getBeanManagerImpl();
+            final InjectionTargetWrapper wrapper = beanManager.getInjectionTargetWrapper(instance.object.getClass());
+            if (wrapper != null)
+            {
+                wrapper.dispose(instance.object);
+            }
+            else if (instance.context != null)
+            {
+                instance.context.release();
+            }
+        }
+        finally
+        {
+            Thread.currentThread().setContextClassLoader(oldLoader);
+        }
     }
 
+    private static class Instance
+    {
+        private Object object;
+        private CreationalContext<?> context;
+
+        private Instance(Object object, CreationalContext<?> context)
+        {
+            this.object = object;
+            this.context = context;
+        }
+    }
 }