Author: mazi
Date: 2010-04-13 10:57:17 -0400 (Tue, 13 Apr 2010)
New Revision: 6102
Added:
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldForwardingAnnotationProcessor.java
Removed:
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/WeldLifecycleListener.java
Modified:
servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/Listener.java
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context-servlet-injection.xml
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context.xml
Log:
WELDX-84: Move tomcat servlet injection support from weld-tomcat-support.jar into
weld-servlet.jar
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
14:38:02 UTC (rev 6101)
+++
servlet/trunk/int/src/main/java/org/jboss/weld/environment/servlet/Listener.java 2010-04-13
14:57:17 UTC (rev 6102)
@@ -33,7 +33,7 @@
import org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices;
import org.jboss.weld.environment.servlet.services.ServletServicesImpl;
import org.jboss.weld.environment.servlet.util.Reflections;
-import org.jboss.weld.environment.tomcat.WeldAnnotationProcessor;
+import org.jboss.weld.environment.tomcat.WeldForwardingAnnotationProcessor;
import org.jboss.weld.injection.spi.ResourceInjectionServices;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.servlet.api.ServletListener;
@@ -87,9 +87,9 @@
try
{
Reflections.classForName("org.apache.AnnotationProcessor");
-
sce.getServletContext().removeAttribute(WeldAnnotationProcessor.class.getName());
+ WeldForwardingAnnotationProcessor.restoreAnnotationProcessor(sce);
}
- catch (IllegalArgumentException e) {}
+ catch (IllegalArgumentException ignore) {}
try
{
Reflections.classForName(JETTY_REQUIRED_CLASS_NAME);
@@ -145,17 +145,14 @@
if (tomcat)
{
- // Try pushing a Tomcat AnnotationProcessor into the servlet context
try
{
- Class<?> clazz =
Reflections.classForName(WeldAnnotationProcessor.class.getName());
- Object annotationProcessor =
clazz.getConstructor(WeldManager.class).newInstance(manager);
- sce.getServletContext().setAttribute(WeldAnnotationProcessor.class.getName(),
annotationProcessor);
+ WeldForwardingAnnotationProcessor.replaceAnnotationProcessor(sce, manager);
log.info("Tomcat 6 detected, JSR-299 injection will be available in
Servlets, Filters etc.");
}
catch (Exception e)
{
- log.error("Unable to create Tomcat AnnotationProcessor. JSR-299
injection will not be available in Servlets, Filters etc.", e);
+ log.error("Unable to replace Tomcat AnnotationProcessor. JSR-299
injection will not be available in Servlets, Filters etc.", e);
}
}
Added:
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java
===================================================================
---
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java
(rev 0)
+++
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java 2010-04-13
14:57:17 UTC (rev 6102)
@@ -0,0 +1,29 @@
+package org.jboss.weld.environment.tomcat;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.naming.NamingException;
+
+import org.apache.AnnotationProcessor;
+
+public abstract class ForwardingAnnotationProcessor implements AnnotationProcessor
+{
+
+ protected abstract AnnotationProcessor delegate();
+
+ public void postConstruct(Object instance) throws IllegalAccessException,
InvocationTargetException
+ {
+ delegate().postConstruct(instance);
+ }
+
+ public void preDestroy(Object instance) throws IllegalAccessException,
InvocationTargetException
+ {
+ delegate().preDestroy(instance);
+ }
+
+ public void processAnnotations(Object instance) throws IllegalAccessException,
InvocationTargetException, NamingException
+ {
+ delegate().processAnnotations(instance);
+ }
+
+}
Added:
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldForwardingAnnotationProcessor.java
===================================================================
---
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldForwardingAnnotationProcessor.java
(rev 0)
+++
servlet/trunk/int/src/main/java/org/jboss/weld/environment/tomcat/WeldForwardingAnnotationProcessor.java 2010-04-13
14:57:17 UTC (rev 6102)
@@ -0,0 +1,110 @@
+package org.jboss.weld.environment.tomcat;
+
+import org.apache.AnnotationProcessor;
+import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.core.ApplicationContext;
+import org.apache.catalina.core.ApplicationContextFacade;
+import org.jboss.weld.manager.api.WeldManager;
+import org.jboss.weld.environment.servlet.util.Reflections;
+
+import javax.naming.NamingException;
+import javax.servlet.ServletContextEvent;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Field;
+
+/**
+ * @author <a href="mailto:matija.mazi@gmail.com">Matija Mazi</a>
+ *
+ * Forwards all calls in turn to two delegates: first to
+ * originalAnnotationProcessor, then to weldProcessor.
+ */
+public class WeldForwardingAnnotationProcessor extends ForwardingAnnotationProcessor
+{
+ private final AnnotationProcessor firstProcessor;
+ private final AnnotationProcessor secondProcessor;
+
+ public WeldForwardingAnnotationProcessor(AnnotationProcessor
originalAnnotationProcessor, AnnotationProcessor weldProcessor)
+ {
+ this.firstProcessor = originalAnnotationProcessor;
+ this.secondProcessor = weldProcessor;
+ }
+
+ @Override
+ protected AnnotationProcessor delegate()
+ {
+ return firstProcessor;
+ }
+
+ @Override
+ public void processAnnotations(Object instance) throws IllegalAccessException,
InvocationTargetException, NamingException
+ {
+ super.processAnnotations(instance);
+ secondProcessor.processAnnotations(instance);
+ }
+
+ @Override
+ public void postConstruct(Object instance) throws IllegalAccessException,
InvocationTargetException
+ {
+ super.postConstruct(instance);
+ secondProcessor.postConstruct(instance);
+ }
+
+ @Override
+ public void preDestroy(Object instance) throws IllegalAccessException,
InvocationTargetException
+ {
+ super.preDestroy(instance);
+ secondProcessor.preDestroy(instance);
+ }
+
+ public static void replaceAnnotationProcessor(ServletContextEvent sce, WeldManager
manager)
+ {
+ StandardContext stdContext = getStandardContext(sce);
+ stdContext.setAnnotationProcessor(createInstance(manager, stdContext));
+ }
+
+ private static WeldForwardingAnnotationProcessor createInstance(WeldManager manager,
StandardContext stdContext)
+ {
+ try
+ {
+ Class<?> clazz =
Reflections.classForName(WeldAnnotationProcessor.class.getName());
+ AnnotationProcessor weldProcessor = (AnnotationProcessor)
clazz.getConstructor(WeldManager.class).newInstance(manager);
+ return new
WeldForwardingAnnotationProcessor(stdContext.getAnnotationProcessor(), weldProcessor);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Cannot create
WeldForwardingAnnotationProcessor", e);
+ }
+ }
+
+ private static StandardContext getStandardContext(ServletContextEvent sce)
+ {
+ try
+ {
+ // Hack into Tomcat to replace the AnnotationProcessor using reflection to
access private fields
+ ApplicationContext appContext = (ApplicationContext)
getContextFieldValue((ApplicationContextFacade)sce.getServletContext(),
ApplicationContextFacade.class);
+ return (StandardContext) getContextFieldValue(appContext,
ApplicationContext.class);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Cannot get StandardContext from
ServletContext", e);
+ }
+ }
+
+ private static <E> Object getContextFieldValue(E obj, Class<E> clazz)
+ throws NoSuchFieldException, IllegalAccessException
+ {
+ Field f = clazz.getDeclaredField("context");
+ f.setAccessible(true);
+ return f.get(obj);
+ }
+
+ public static void restoreAnnotationProcessor(ServletContextEvent sce)
+ {
+ StandardContext stdContext = getStandardContext(sce);
+ AnnotationProcessor ap = stdContext.getAnnotationProcessor();
+ if (ap instanceof WeldForwardingAnnotationProcessor)
+ {
+
stdContext.setAnnotationProcessor(((WeldForwardingAnnotationProcessor)ap).firstProcessor);
+ }
+ }
+}
\ No newline at end of file
Deleted:
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java
===================================================================
---
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java 2010-04-13
14:38:02 UTC (rev 6101)
+++
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/ForwardingAnnotationProcessor.java 2010-04-13
14:57:17 UTC (rev 6102)
@@ -1,29 +0,0 @@
-package org.jboss.weld.environment.tomcat;
-
-import java.lang.reflect.InvocationTargetException;
-
-import javax.naming.NamingException;
-
-import org.apache.AnnotationProcessor;
-
-public abstract class ForwardingAnnotationProcessor implements AnnotationProcessor
-{
-
- protected abstract AnnotationProcessor delegate();
-
- public void postConstruct(Object instance) throws IllegalAccessException,
InvocationTargetException
- {
- delegate().postConstruct(instance);
- }
-
- public void preDestroy(Object instance) throws IllegalAccessException,
InvocationTargetException
- {
- delegate().preDestroy(instance);
- }
-
- public void processAnnotations(Object instance) throws IllegalAccessException,
InvocationTargetException, NamingException
- {
- delegate().processAnnotations(instance);
- }
-
-}
Deleted:
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/WeldLifecycleListener.java
===================================================================
---
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/WeldLifecycleListener.java 2010-04-13
14:38:02 UTC (rev 6101)
+++
servlet/trunk/support/src/main/java/org/jboss/weld/environment/tomcat/WeldLifecycleListener.java 2010-04-13
14:57:17 UTC (rev 6102)
@@ -1,89 +0,0 @@
-package org.jboss.weld.environment.tomcat;
-
-import java.lang.reflect.InvocationTargetException;
-
-import javax.naming.NamingException;
-import javax.servlet.ServletContext;
-
-import org.apache.AnnotationProcessor;
-import org.apache.catalina.LifecycleEvent;
-import org.apache.catalina.LifecycleListener;
-import org.apache.catalina.core.StandardContext;
-
-public class WeldLifecycleListener implements LifecycleListener
-{
-
- private static final AnnotationProcessor DUMMY_PROCESSOR = new AnnotationProcessor()
- {
-
- public void postConstruct(Object arg0) throws IllegalAccessException,
InvocationTargetException {}
-
- public void preDestroy(Object arg0) throws IllegalAccessException,
InvocationTargetException {}
-
- public void processAnnotations(Object arg0) throws IllegalAccessException,
InvocationTargetException, NamingException {}
-
- };
-
- public void lifecycleEvent(LifecycleEvent event)
- {
- if (event.getType().equals("after_start") && event.getLifecycle()
instanceof StandardContext)
- {
- StandardContext context = (StandardContext) event.getLifecycle();
- final ServletContext servletContext = context.getServletContext();
-
- // Initialize servlet injection
- final AnnotationProcessor originalAnnotationProcessor =
context.getAnnotationProcessor();
- context.setAnnotationProcessor(new ForwardingAnnotationProcessor()
- {
-
- private AnnotationProcessor processor;
-
- @Override
- protected AnnotationProcessor delegate()
- {
- return originalAnnotationProcessor;
- }
-
- @Override
- public void processAnnotations(Object instance) throws
IllegalAccessException, InvocationTargetException, NamingException
- {
- super.processAnnotations(instance);
- getProcessor().processAnnotations(instance);
- }
-
- @Override
- public void postConstruct(Object instance) throws IllegalAccessException,
InvocationTargetException
- {
- super.postConstruct(instance);
- getProcessor().postConstruct(instance);
- }
-
- @Override
- public void preDestroy(Object instance) throws IllegalAccessException,
InvocationTargetException
- {
- super.preDestroy(instance);
- getProcessor().preDestroy(instance);
- }
-
- private AnnotationProcessor getProcessor()
- {
- if (processor == null)
- {
- Object o =
servletContext.getAttribute("org.jboss.weld.environment.tomcat.WeldAnnotationProcessor");
- if (o instanceof AnnotationProcessor)
- {
- processor = (AnnotationProcessor) o;
- }
- else
- {
- return DUMMY_PROCESSOR;
- }
- }
- return processor;
- }
-
- });
- }
- }
-
-}
Modified:
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context-servlet-injection.xml
===================================================================
---
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context-servlet-injection.xml 2010-04-13
14:38:02 UTC (rev 6101)
+++
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context-servlet-injection.xml 2010-04-13
14:57:17 UTC (rev 6102)
@@ -2,8 +2,6 @@
<Resource name="BeanManager" auth="Container"
type="javax.inject.manager.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
-<!-- Uncomment to enable injection into Servlet -->
- <Listener
className="org.jboss.weld.environment.tomcat.WeldLifecycleListener" />
</Context>
Modified:
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context.xml
===================================================================
---
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context.xml 2010-04-13
14:38:02 UTC (rev 6101)
+++
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/context.xml 2010-04-13
14:57:17 UTC (rev 6102)
@@ -2,8 +2,6 @@
<Resource name="BeanManager" auth="Container"
type="javax.inject.manager.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
-<!-- Uncomment to enable injection into Servlet -->
-<Listener
className="org.jboss.weld.environment.tomcat.WeldLifecycleListener" />
+
-
</Context>