Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49937
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 3 Oct 2010 20:53:49 +0000 (20:53 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 3 Oct 2010 20:53:49 +0000 (20:53 +0000)
Use InstanceManager to create AsyncListeners so annotations are processed. Based on a patch by David Jencks.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1004047 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/AsyncContextImpl.java
webapps/docs/changelog.xml

index a5fad2f..065139c 100644 (file)
 package org.apache.catalina.core;
 
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import javax.naming.NamingException;
 import javax.servlet.AsyncContext;
 import javax.servlet.AsyncEvent;
 import javax.servlet.AsyncListener;
@@ -42,6 +46,7 @@ import org.apache.coyote.ActionCode;
 import org.apache.coyote.RequestInfo;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.InstanceManager;
 /**
  * 
  * @author fhanik
@@ -59,8 +64,8 @@ public class AsyncContextImpl implements AsyncContext {
     private Context context = null;
     private long timeout = -1;
     private AsyncEvent event = null;
-    
     private Request request;
+    private volatile InstanceManager instanceManager;
     
     public AsyncContextImpl(Request request) {
         if (log.isDebugEnabled()) {
@@ -203,18 +208,29 @@ public class AsyncContextImpl implements AsyncContext {
         listeners.add(wrapper);
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public <T extends AsyncListener> T createListener(Class<T> clazz)
             throws ServletException {
         T listener = null;
         try {
-             listener = clazz.newInstance();
+             listener = (T) getInstanceManager().newInstance(clazz.getName(),
+                     clazz.getClassLoader());
         } catch (InstantiationException e) {
             ServletException se = new ServletException(e);
             throw se;
         } catch (IllegalAccessException e) {
             ServletException se = new ServletException(e);
             throw se;
+        } catch (InvocationTargetException e) {
+            ServletException se = new ServletException(e);
+            throw se;
+        } catch (NamingException e) {
+            ServletException se = new ServletException(e);
+            throw se;
+        } catch (ClassNotFoundException e) {
+            ServletException se = new ServletException(e);
+            throw se;
         }
         return listener;
     }
@@ -374,6 +390,20 @@ public class AsyncContextImpl implements AsyncContext {
         }
     }
 
+    private InstanceManager getInstanceManager() {
+        if (instanceManager == null) {
+            if (context instanceof StandardContext) {
+                instanceManager = ((StandardContext)context).getInstanceManager();
+            } else {
+                instanceManager = new DefaultInstanceManager(null,
+                        new HashMap<String, Map<String, String>>(),
+                        context,
+                        getClass().getClassLoader()); 
+            }
+        }
+        return instanceManager;
+    }
+
     private static class DebugException extends Exception {
         private static final long serialVersionUID = 1L;
     }
index e52e06e..f3ca3fd 100644 (file)
     <changelog>
       <fix>
         <bug>49922</bug>: Don&apos;t add filter twice to filter chain if the
-        filter matches more than one URL pattern and/or Servlet name.
+        filter matches more than one URL pattern and/or Servlet name. (markt)
+      </fix>
+      <fix>
+        <bug>49937</bug>: Use an InstanceManager when creating an AsyncListener
+        through the AsyncContext to ensure annotations are processed. Based on a
+        patch by David Jencks. (markt)
       </fix>
     </changelog>
   </subsection>