- Add resource injection for tags (listeners are being handled in the servlet container).
authorremm <remm@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 3 May 2006 09:11:13 +0000 (09:11 +0000)
committerremm <remm@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 3 May 2006 09:11:13 +0000 (09:11 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@399216 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/jasper/compiler/Generator.java
java/org/apache/jasper/runtime/AnnotationProcessor.java
java/org/apache/jasper/runtime/TagHandlerPool.java

index ddf0af5..45ef313 100644 (file)
@@ -504,7 +504,8 @@ class Generator {
         }\r
         out.printin("private javax.el.ExpressionFactory ");\r
         out.print(VAR_EXPRESSIONFACTORY);\r
-        out.println(";\n");\r
+        out.println(";");\r
+        out.println();\r
     }\r
 \r
     /**\r
@@ -2370,6 +2371,11 @@ class Generator {
 \r
             generateSetters(n, tagHandlerVar, handlerInfo, true);\r
 \r
+            // Resource injection\r
+            out.printin("org.apache.jasper.runtime.AnnotationProcessor.postConstruct(");\r
+            out.print(tagHandlerVar);\r
+            out.println(");");\r
+            \r
             // Set the body\r
             if (findJspBody(n) == null) {\r
                 /*\r
@@ -2411,6 +2417,11 @@ class Generator {
             declareScriptingVars(n, VariableInfo.AT_END);\r
             syncScriptingVars(n, VariableInfo.AT_END);\r
 \r
+            // Resource injection\r
+            out.printin("org.apache.jasper.runtime.AnnotationProcessor.preDestroy(");\r
+            out.print(tagHandlerVar);\r
+            out.println(");");\r
+\r
             n.setEndJavaLine(out.getJavaLine());\r
         }\r
 \r
index cab6a1a..cd23a2c 100644 (file)
@@ -24,6 +24,8 @@ import java.lang.reflect.Modifier;
 import javax.annotation.EJB;
 import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
+import javax.naming.Context;
+import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceUnit;
@@ -38,15 +40,71 @@ import javax.xml.ws.WebServiceRef;
  * @version $Revision: 303236 $, $Date: 2006-03-09 16:46:52 -0600 (Thu, 09 Mar 2006) $
  */
 public class AnnotationProcessor {
-    
 
+    
     /**
-     * Call postConstruct method on the specified instance.
+     * Call postConstruct method on the specified instance. Note: In Jasper, this
+     * calls naming resources injection as well.
      */
     public static void postConstruct(Object instance)
-        throws IllegalAccessException, InvocationTargetException {
+        throws IllegalAccessException, InvocationTargetException, NamingException {
         
+        // Initialize fields annotations
+        Field[] fields = instance.getClass().getFields();
+        for (int i = 0; i < fields.length; i++) {
+            if (fields[i].isAnnotationPresent(Resource.class)) {
+                Resource annotation = (Resource) fields[i].getAnnotation(Resource.class);
+                lookupFieldResource(instance, fields[i], annotation.name());
+            }
+            if (fields[i].isAnnotationPresent(EJB.class)) {
+                EJB annotation = (EJB) fields[i].getAnnotation(EJB.class);
+                lookupFieldResource(instance, fields[i], annotation.name());
+            }
+            if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
+                WebServiceRef annotation = 
+                    (WebServiceRef) fields[i].getAnnotation(WebServiceRef.class);
+                lookupFieldResource(instance, fields[i], annotation.name());
+            }
+            if (fields[i].isAnnotationPresent(PersistenceContext.class)) {
+                PersistenceContext annotation = 
+                    (PersistenceContext) fields[i].getAnnotation(PersistenceContext.class);
+                lookupFieldResource(instance, fields[i], annotation.name());
+            }
+            if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
+                PersistenceUnit annotation = 
+                    (PersistenceUnit) fields[i].getAnnotation(PersistenceUnit.class);
+                lookupFieldResource(instance, fields[i], annotation.name());
+            }
+        }
+        
+        // Initialize methods annotations
         Method[] methods = instance.getClass().getMethods();
+        for (int i = 0; i < methods.length; i++) {
+            if (methods[i].isAnnotationPresent(Resource.class)) {
+                Resource annotation = (Resource) methods[i].getAnnotation(Resource.class);
+                lookupMethodResource(instance, methods[i], annotation.name());
+            }
+            if (methods[i].isAnnotationPresent(EJB.class)) {
+                EJB annotation = (EJB) methods[i].getAnnotation(EJB.class);
+                lookupMethodResource(instance, methods[i], annotation.name());
+            }
+            if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
+                WebServiceRef annotation = 
+                    (WebServiceRef) methods[i].getAnnotation(WebServiceRef.class);
+                lookupMethodResource(instance, methods[i], annotation.name());
+            }
+            if (methods[i].isAnnotationPresent(PersistenceContext.class)) {
+                PersistenceContext annotation = 
+                    (PersistenceContext) methods[i].getAnnotation(PersistenceContext.class);
+                lookupMethodResource(instance, methods[i], annotation.name());
+            }
+            if (methods[i].isAnnotationPresent(PersistenceUnit.class)) {
+                PersistenceUnit annotation = 
+                    (PersistenceUnit) methods[i].getAnnotation(PersistenceUnit.class);
+                lookupMethodResource(instance, methods[i], annotation.name());
+            }
+        }
+
         Method postConstruct = null;
         for (int i = 0; i < methods.length; i++) {
             if (methods[i].isAnnotationPresent(PostConstruct.class)) {
@@ -107,80 +165,15 @@ public class AnnotationProcessor {
     
     
     /**
-     * Inject resources in specified instance.
-     */
-    public static void injectNamingResources(javax.naming.Context context, Object instance)
-        throws IllegalAccessException, InvocationTargetException, NamingException {
-        
-        // Initialize fields annotations
-        Field[] fields = instance.getClass().getFields();
-        for (int i = 0; i < fields.length; i++) {
-            if (fields[i].isAnnotationPresent(Resource.class)) {
-                Resource annotation = (Resource) fields[i].getAnnotation(Resource.class);
-                lookupFieldResource(context, instance, fields[i], annotation.name());
-            }
-            if (fields[i].isAnnotationPresent(EJB.class)) {
-                EJB annotation = (EJB) fields[i].getAnnotation(EJB.class);
-                lookupFieldResource(context, instance, fields[i], annotation.name());
-            }
-            if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
-                WebServiceRef annotation = 
-                    (WebServiceRef) fields[i].getAnnotation(WebServiceRef.class);
-                lookupFieldResource(context, instance, fields[i], annotation.name());
-            }
-            if (fields[i].isAnnotationPresent(PersistenceContext.class)) {
-                PersistenceContext annotation = 
-                    (PersistenceContext) fields[i].getAnnotation(PersistenceContext.class);
-                lookupFieldResource(context, instance, fields[i], annotation.name());
-            }
-            if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
-                PersistenceUnit annotation = 
-                    (PersistenceUnit) fields[i].getAnnotation(PersistenceUnit.class);
-                lookupFieldResource(context, instance, fields[i], annotation.name());
-            }
-        }
-        
-        // Initialize methods annotations
-        Method[] methods = instance.getClass().getMethods();
-        for (int i = 0; i < methods.length; i++) {
-            if (methods[i].isAnnotationPresent(Resource.class)) {
-                Resource annotation = (Resource) methods[i].getAnnotation(Resource.class);
-                lookupMethodResource(context, instance, methods[i], annotation.name());
-            }
-            if (methods[i].isAnnotationPresent(EJB.class)) {
-                EJB annotation = (EJB) methods[i].getAnnotation(EJB.class);
-                lookupMethodResource(context, instance, methods[i], annotation.name());
-            }
-            if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
-                WebServiceRef annotation = 
-                    (WebServiceRef) methods[i].getAnnotation(WebServiceRef.class);
-                lookupMethodResource(context, instance, methods[i], annotation.name());
-            }
-            if (methods[i].isAnnotationPresent(PersistenceContext.class)) {
-                PersistenceContext annotation = 
-                    (PersistenceContext) methods[i].getAnnotation(PersistenceContext.class);
-                lookupMethodResource(context, instance, methods[i], annotation.name());
-            }
-            if (methods[i].isAnnotationPresent(PersistenceUnit.class)) {
-                PersistenceUnit annotation = 
-                    (PersistenceUnit) methods[i].getAnnotation(PersistenceUnit.class);
-                lookupMethodResource(context, instance, methods[i], annotation.name());
-            }
-        }
-
-    }
-    
-    
-    /**
      * Inject resources in specified field.
      */
-    protected static void lookupFieldResource(javax.naming.Context context, 
-            Object instance, Field field, String name)
+    protected static void lookupFieldResource(Object instance, Field field, String name)
         throws NamingException, IllegalAccessException {
     
         Object lookedupResource = null;
         boolean accessibility = false;
         
+        Context context = (Context) (new InitialContext()).lookup("java:comp/env");
         if ((name != null) &&
                 (name.length() > 0)) {
             lookedupResource = context.lookup(name);
@@ -198,8 +191,7 @@ public class AnnotationProcessor {
     /**
      * Inject resources in specified method.
      */
-    protected static void lookupMethodResource(javax.naming.Context context, 
-            Object instance, Method method, String name)
+    protected static void lookupMethodResource(Object instance, Method method, String name)
         throws NamingException, IllegalAccessException, InvocationTargetException {
         
         if (!method.getName().startsWith("set") 
@@ -211,6 +203,7 @@ public class AnnotationProcessor {
         Object lookedupResource = null;
         boolean accessibility = false;
         
+        Context context = (Context) (new InitialContext()).lookup("java:comp/env");
         if ((name != null) &&
                 (name.length() > 0)) {
             lookedupResource = context.lookup(name);
index 309194b..5574ccf 100644 (file)
@@ -19,6 +19,9 @@ package org.apache.jasper.runtime;
 import javax.servlet.jsp.JspException;\r
 import javax.servlet.jsp.tagext.Tag;\r
 import javax.servlet.ServletConfig;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
 import org.apache.jasper.Constants;\r
 \r
 /**\r
@@ -33,6 +36,8 @@ public class TagHandlerPool {
     public static String OPTION_TAGPOOL="tagpoolClassName";\r
     public static String OPTION_MAXSIZE="tagpoolMaxSize";\r
 \r
+    private Log log = LogFactory.getLog(TagHandlerPool.class);\r
+    \r
     // index of next available tag handler\r
     private int current;\r
 \r
@@ -113,7 +118,9 @@ public class TagHandlerPool {
         // Out of sync block - there is no need for other threads to\r
         // wait for us to construct a tag for this thread.\r
         try {\r
-            return (Tag) handlerClass.newInstance();\r
+            Tag instance = (Tag) handlerClass.newInstance();\r
+            AnnotationProcessor.postConstruct(instance);\r
+            return instance;\r
         } catch (Exception e) {\r
             throw new JspException(e.getMessage(), e);\r
         }\r
@@ -135,6 +142,12 @@ public class TagHandlerPool {
         }\r
         // There is no need for other threads to wait for us to release\r
         handler.release();\r
+        try {\r
+            AnnotationProcessor.preDestroy(handler);\r
+        } catch (Exception e) {\r
+            log.warn("Error processing preDestroy on tag instance of " \r
+                    + handler.getClass().getName(), e);\r
+        }\r
     }\r
 \r
     /**\r
@@ -142,9 +155,15 @@ public class TagHandlerPool {
      * handler pool.\r
      */\r
     public synchronized void release() {\r
-       for (int i=current; i>=0; i--) {\r
-           handlers[i].release();\r
-       }\r
+        for (int i = current; i >= 0; i--) {\r
+            handlers[i].release();\r
+            try {\r
+                AnnotationProcessor.preDestroy(handlers[i]);\r
+            } catch (Exception e) {\r
+                log.warn("Error processing preDestroy on tag instance of " \r
+                        + handlers[i].getClass().getName(), e);\r
+            }\r
+        }\r
     }\r
 \r
     protected static String getOption( ServletConfig config, String name, String defaultV) {\r