[weld-commits] Weld SVN: r6101 - in servlet/trunk: build and 6 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Tue Apr 13 10:38:03 EDT 2010


Author: mazi
Date: 2010-04-13 10:38:02 -0400 (Tue, 13 Apr 2010)
New Revision: 6101

Added:
   servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/
   servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/JettyWeldInjector.java
   servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/WeldServletHandler.java
   servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/inject/
   servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/inject/AbstractInjector.java
Modified:
   servlet/trunk/build/pom.xml
   servlet/trunk/int/pom.xml
   servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/Listener.java
   servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldAnnotationProcessor.java
   servlet/trunk/pom.xml
Log:
WELDX-23: Servlet injection support on Jetty

Modified: servlet/trunk/build/pom.xml
===================================================================
--- servlet/trunk/build/pom.xml	2010-04-13 13:12:16 UTC (rev 6100)
+++ servlet/trunk/build/pom.xml	2010-04-13 14:38:02 UTC (rev 6101)
@@ -80,6 +80,7 @@
                      <exclude>org.apache.tomcat:annotations-api</exclude>
                      <exclude>javax.faces:jsf-api</exclude>
                      <exclude>javax.el:el-api</exclude>
+                     <exclude>org.mortbay.jetty:jetty</exclude>
                   </excludes>
                </artifactSet>
             </configuration>

Modified: servlet/trunk/int/pom.xml
===================================================================
--- servlet/trunk/int/pom.xml	2010-04-13 13:12:16 UTC (rev 6100)
+++ servlet/trunk/int/pom.xml	2010-04-13 14:38:02 UTC (rev 6101)
@@ -61,6 +61,12 @@
       </dependency>
 
       <dependency>
+         <groupId>org.mortbay.jetty</groupId>
+         <artifactId>jetty</artifactId>
+         <scope>provided</scope>
+      </dependency>
+
+      <dependency>
          <groupId>javax.faces</groupId>
          <artifactId>jsf-api</artifactId>
       </dependency>

Added: servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/JettyWeldInjector.java
===================================================================
--- servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/JettyWeldInjector.java	                        (rev 0)
+++ servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/JettyWeldInjector.java	2010-04-13 14:38:02 UTC (rev 6101)
@@ -0,0 +1,22 @@
+package org.jboss.weld.environment.jetty;
+
+import org.jboss.weld.environment.servlet.inject.AbstractInjector;
+import org.jboss.weld.manager.api.WeldManager;
+
+/**
+ *  
+ *
+ * @author <a href="mailto:matija.mazi at gmail.com">Matija Mazi</a>
+ */
+public class JettyWeldInjector extends AbstractInjector
+{
+   public JettyWeldInjector(WeldManager manager)
+   {
+      super(manager);
+   }
+
+   public void inject(Object targetInstance)
+   {
+      super.inject(targetInstance);
+   }
+}

Added: servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/WeldServletHandler.java
===================================================================
--- servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/WeldServletHandler.java	                        (rev 0)
+++ servlet/trunk/int/src/main/java/org/jboss/weld/environment/jetty/WeldServletHandler.java	2010-04-13 14:38:02 UTC (rev 6101)
@@ -0,0 +1,63 @@
+package org.jboss.weld.environment.jetty;
+
+import org.mortbay.jetty.servlet.ServletHandler;
+import org.mortbay.jetty.webapp.WebAppContext;
+import org.mortbay.log.Log;
+
+import javax.servlet.Servlet;
+import javax.servlet.Filter;
+import javax.servlet.ServletContext;
+
+/**
+ * @author <a href="mailto:matija.mazi at gmail.com">Matija Mazi</a>
+*/
+public class WeldServletHandler extends ServletHandler
+{
+   private ServletContext sco;
+   private JettyWeldInjector injector;
+
+   public WeldServletHandler(ServletHandler existingHandler, ServletContext servletContext)
+   {
+      sco = servletContext;
+      setFilters(existingHandler.getFilters());
+      setFilterMappings(existingHandler.getFilterMappings());
+      setServlets(existingHandler.getServlets());
+      setServletMappings(existingHandler.getServletMappings());
+   }
+
+   @Override
+   public Servlet customizeServlet(Servlet servlet) throws Exception
+   {
+      inject(servlet);
+      return servlet;
+   }
+
+   @Override
+   public Filter customizeFilter(Filter filter) throws Exception
+   {
+      inject(filter);
+      return filter;
+   }
+
+   protected void inject(Object injectable) {
+      if (injector == null)
+      {
+         injector = (JettyWeldInjector) sco.getAttribute(org.jboss.weld.environment.servlet.Listener.INJECTOR_ATTRIBUTE_NAME);
+      }
+      if (injector == null)
+      {
+         Log.warn("Can't find Injector in the servlet context so injection is not available for " + injectable);
+      }
+      else
+      {
+         injector.inject(injectable);
+      }
+   }
+
+   public static void process(WebAppContext wac)
+   {
+      WeldServletHandler wHanlder = new WeldServletHandler(wac.getServletHandler(), wac.getServletContext());
+      wac.setServletHandler(wHanlder);
+      wac.getSecurityHandler().setHandler(wHanlder);
+   }
+}

Modified: servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/Listener.java
===================================================================
--- servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/Listener.java	2010-04-13 13:12:16 UTC (rev 6100)
+++ servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/Listener.java	2010-04-13 14:38:02 UTC (rev 6101)
@@ -28,6 +28,7 @@
 import org.jboss.weld.bootstrap.api.Environments;
 import org.jboss.weld.context.api.BeanStore;
 import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore;
+import org.jboss.weld.environment.jetty.JettyWeldInjector;
 import org.jboss.weld.environment.servlet.deployment.ServletDeployment;
 import org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices;
 import org.jboss.weld.environment.servlet.services.ServletServicesImpl;
@@ -52,11 +53,13 @@
    private static final String WELD_LISTENER_CLASS_NAME = "org.jboss.weld.servlet.WeldListener";
    private static final String APPLICATION_BEAN_STORE_ATTRIBUTE_NAME = Listener.class.getName() + ".applicationBeanStore";
    private static final String EXPRESSION_FACTORY_NAME = "org.jboss.weld.el.ExpressionFactory";
-   
+   private static final String JETTY_REQUIRED_CLASS_NAME = "org.mortbay.jetty.servlet.ServletHandler";
+   public  static final String INJECTOR_ATTRIBUTE_NAME = "org.jboss.weld.environment.jetty.JettyWeldInjector";
+
    private final transient Bootstrap bootstrap;
    private final transient ServletListener weldListener;
    private WeldManager manager;
-   
+
    public Listener()
    {
       try
@@ -87,6 +90,12 @@
          sce.getServletContext().removeAttribute(WeldAnnotationProcessor.class.getName());
       }
       catch (IllegalArgumentException e) {}
+      try
+      {
+         Reflections.classForName(JETTY_REQUIRED_CLASS_NAME);
+         sce.getServletContext().removeAttribute(INJECTOR_ATTRIBUTE_NAME);
+      }
+      catch (IllegalArgumentException ignore) {}
       super.contextDestroyed(sce);
    }
 
@@ -123,7 +132,7 @@
       
       bootstrap.startContainer(Environments.SERVLET, deployment, applicationBeanStore).startInitialization();
       manager = bootstrap.getManager(deployment.getWebAppBeanDeploymentArchive());
-      
+
       boolean tomcat = true;
       try
       {
@@ -131,10 +140,9 @@
       }
       catch (IllegalArgumentException e)
       {
-         log.info("JSR-299 injection will not be available in Servlets, Filters etc. This facility is only available in Tomcat");
          tomcat = false;
       }
-      
+
       if (tomcat)
       {
          // Try pushing a Tomcat AnnotationProcessor into the servlet context
@@ -143,6 +151,7 @@
             Class<?> clazz = Reflections.classForName(WeldAnnotationProcessor.class.getName());
             Object annotationProcessor = clazz.getConstructor(WeldManager.class).newInstance(manager);
             sce.getServletContext().setAttribute(WeldAnnotationProcessor.class.getName(), annotationProcessor);
+            log.info("Tomcat 6 detected, JSR-299 injection will be available in Servlets, Filters etc.");
          }
          catch (Exception e)
          {
@@ -150,6 +159,36 @@
          }
       }
 
+      boolean jetty = true;
+      try
+      {
+         Reflections.classForName(JETTY_REQUIRED_CLASS_NAME);
+      }
+      catch (IllegalArgumentException e)
+      {
+         jetty = false;
+      }
+
+      if (jetty)
+      {
+         // Try pushing a Jetty Injector into the servlet context
+         try
+         {
+            Class<?> clazz = Reflections.classForName(JettyWeldInjector.class.getName());
+            Object injector = clazz.getConstructor(WeldManager.class).newInstance(manager);
+            sce.getServletContext().setAttribute(INJECTOR_ATTRIBUTE_NAME, injector);
+            log.info("Jetty detected, JSR-299 injection will be available in Servlets, Filters etc.");
+         }
+         catch (Exception e)
+         {
+            log.error("Unable to create JettyWeldInjector. JSR-299 injection will not be available in Servlets, Filters etc.", e);
+         }
+      }
+
+      if (!tomcat && !jetty) {
+         log.info("No supported servlet container detected, JSR-299 injection will NOT be available in Servlets, Filters etc.");
+      }
+
       // Push the manager into the servlet context so we can access in JSF
       
       if (JspFactory.getDefaultFactory() != null)
@@ -171,11 +210,11 @@
       bootstrap.deployBeans().validateBeans().endInitialization();
       super.contextInitialized(sce);
    }
-   
+
    @Override
    protected ServletListener delegate()
    {
       return weldListener;
    }
-   
+
 }

Added: servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/inject/AbstractInjector.java
===================================================================
--- servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/inject/AbstractInjector.java	                        (rev 0)
+++ servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/inject/AbstractInjector.java	2010-04-13 14:38:02 UTC (rev 6101)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.weld.environment.servlet.inject;
+
+import org.jboss.weld.manager.api.WeldManager;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.InjectionTarget;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+/**
+ * Provides support for Weld injection into servlets, servlet filters etc.
+ *
+ * Used to wrap the Weld classes (WeldManager) so that they are not seen from tomcat-support.
+ * tomcat-support thus does not depend on Weld, which is deployed within the war and may be
+ * deployed several times (within several wars) in a single servlet container.
+ *
+ * @author Pete Muir
+ * @author <a href="mailto:matija.mazi at gmail.com">Matija Mazi</a>
+ */
+public abstract class AbstractInjector
+{
+   private final WeldManager manager;
+   private final Map<Class<?>, InjectionTarget<?>> cache = new WeakHashMap<Class<?>, InjectionTarget<?>>();
+
+   protected AbstractInjector(WeldManager manager)
+   {
+      if (manager == null)
+         throw new IllegalArgumentException("Null manager");
+      this.manager = manager;
+   }
+
+   protected void inject(Object instance)
+   {
+      // not data-race safe, however doesn't matter, as the injection target created for class A is interchangable for another injection target created for class A
+      // TODO Make this a concurrent cache when we switch to google collections
+      Class<?> clazz = instance.getClass();
+      if (!cache.containsKey(clazz))
+      {
+         cache.put(clazz, manager.createInjectionTarget(manager.createAnnotatedType(clazz)));
+      }
+      CreationalContext<Object> cc = manager.createCreationalContext(null);
+      InjectionTarget<Object> it = (InjectionTarget<Object>) cache.get(clazz);
+      it.inject(instance, cc);
+   }
+}
\ No newline at end of file

Modified: servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldAnnotationProcessor.java
===================================================================
--- servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldAnnotationProcessor.java	2010-04-13 13:12:16 UTC (rev 6100)
+++ servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldAnnotationProcessor.java	2010-04-13 14:38:02 UTC (rev 6101)
@@ -1,53 +1,29 @@
 package org.jboss.weld.environment.tomcat;
 
-import java.lang.reflect.InvocationTargetException;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.spi.InjectionTarget;
-import javax.naming.NamingException;
-
 import org.apache.AnnotationProcessor;
+import org.jboss.weld.environment.servlet.inject.AbstractInjector;
 import org.jboss.weld.manager.api.WeldManager;
 
-public class WeldAnnotationProcessor implements AnnotationProcessor
+import javax.naming.NamingException;
+import java.lang.reflect.InvocationTargetException;
+
+public class WeldAnnotationProcessor extends AbstractInjector implements AnnotationProcessor
 {
-   
-   private final Map<Class<?>, InjectionTarget<?>> injectionTargets;
-   
-   private final WeldManager manager;
-   
    public WeldAnnotationProcessor(WeldManager manager)
    {
-      this.manager = manager;
-      this.injectionTargets = new ConcurrentHashMap<Class<?>, InjectionTarget<?>>();
+      super(manager);
    }
 
    public void processAnnotations(Object instance) throws IllegalAccessException, InvocationTargetException, NamingException
    {
-      // not data-race safe, however doesn't matter, as the injection target created for class A is interchangable for another injection target created for class A
-      // TODO Make this a concurrent cache when we switch to google collections
-      Class<?> clazz = instance.getClass();
-      if (!injectionTargets.containsKey(clazz))
-      {
-         injectionTargets.put(clazz, manager.createInjectionTarget(manager.createAnnotatedType(clazz)));
-      }
-      CreationalContext<Object> cc = manager.createCreationalContext(null);
-      InjectionTarget<Object> it = (InjectionTarget<Object>) injectionTargets.get(clazz);
-      it.inject(instance, cc);
+      inject(instance);
    }
 
    public void postConstruct(Object arg0) throws IllegalAccessException, InvocationTargetException
    {
-      // TODO Auto-generated method stub
-      
    }
    
    public void preDestroy(Object arg0) throws IllegalAccessException, InvocationTargetException
    {
-      // TODO Auto-generated method stub
-      
    }
-   
 }

Modified: servlet/trunk/pom.xml
===================================================================
--- servlet/trunk/pom.xml	2010-04-13 13:12:16 UTC (rev 6100)
+++ servlet/trunk/pom.xml	2010-04-13 14:38:02 UTC (rev 6101)
@@ -60,6 +60,7 @@
       <!-- Use JSF 2 for all extensions support, despite core using JSF 1.2 -->
       <jsf.version>2.0.0-RC</jsf.version>
       <tomcat.version>6.0.20</tomcat.version>
+      <jetty.version>6.1.21</jetty.version>
       <uel.glassfish.version>2.1.2-b04</uel.glassfish.version>
       <slf4j.version>1.5.10</slf4j.version>
       <!-- Testing deps -->
@@ -148,6 +149,12 @@
          </dependency>
 
          <dependency>
+            <groupId>org.mortbay.jetty</groupId>
+            <artifactId>jetty</artifactId>
+            <version>${jetty.version}</version>
+         </dependency>
+
+         <dependency>
             <groupId>org.glassfish.web</groupId>
             <artifactId>el-impl</artifactId>
             <version>${uel.glassfish.version}</version>



More information about the weld-commits mailing list