Always use the InstanceManager to create listeners, filters & servlets
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 21 Mar 2010 22:18:03 +0000 (22:18 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 21 Mar 2010 22:18:03 +0000 (22:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@925921 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/ApplicationContext.java

index 5d10989..2fbc249 100644 (file)
@@ -1207,27 +1207,36 @@ public class ApplicationContext
     @Override
     public void addListener(String className) {
         
-        Class<?> clazz;
-        
         try {
-             clazz = Class.forName(className);
-        } catch (ClassNotFoundException e) {
-            throw new IllegalArgumentException(sm.getString(
-                    "applicationContext.addListener.iae.cnfe", className), e);
-        }
-        
-        if (!EventListener.class.isAssignableFrom(clazz)) {
-            throw new IllegalArgumentException(sm.getString(
-                    "applicationContext.addListener.iae.wrongType", className));
-        }
+            Object obj = context.getInstanceManager().newInstance(className);
 
-        try {
-            @SuppressWarnings("unchecked") // tested above
-            EventListener listener = createListener((Class<EventListener>)clazz);
+            if (!(obj instanceof EventListener)) {
+                throw new IllegalArgumentException(sm.getString(
+                        "applicationContext.addListener.iae.wrongType",
+                        className));
+            }
+
+            EventListener listener = (EventListener) obj;
             addListener(listener);
-        } catch (ServletException e) {
+        } catch (IllegalAccessException e) {
+            throw new IllegalArgumentException(sm.getString(
+                    "applicationContext.addListener.iae.cnfe", className),
+                    e);
+        } catch (InvocationTargetException e) {
+            throw new IllegalArgumentException(sm.getString(
+                    "applicationContext.addListener.iae.cnfe", className),
+                    e);
+        } catch (NamingException e) {
+            throw new IllegalArgumentException(sm.getString(
+                    "applicationContext.addListener.iae.cnfe", className),
+                    e);
+        } catch (InstantiationException e) {
+            throw new IllegalArgumentException(sm.getString(
+                    "applicationContext.addListener.iae.cnfe", className),
+                    e);
+        } catch (ClassNotFoundException e) {
             throw new IllegalArgumentException(sm.getString(
-                    "applicationContext.addListener.iae.wrongType", className),
+                    "applicationContext.addListener.iae.cnfe", className),
                     e);
         }