You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2006/05/03 11:11:16 UTC

svn commit: r399216 - in /tomcat/tc6.0.x/trunk/java/org/apache/jasper: compiler/Generator.java runtime/AnnotationProcessor.java runtime/TagHandlerPool.java

Author: remm
Date: Wed May  3 02:11:13 2006
New Revision: 399216

URL: http://svn.apache.org/viewcvs?rev=399216&view=rev
Log:
- Add resource injection for tags (listeners are being handled in the servlet container).

Modified:
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/Generator.java
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/TagHandlerPool.java

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/Generator.java
URL: http://svn.apache.org/viewcvs/tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/Generator.java?rev=399216&r1=399215&r2=399216&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/Generator.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/Generator.java Wed May  3 02:11:13 2006
@@ -504,7 +504,8 @@
         }
         out.printin("private javax.el.ExpressionFactory ");
         out.print(VAR_EXPRESSIONFACTORY);
-        out.println(";\n");
+        out.println(";");
+        out.println();
     }
 
     /**
@@ -2370,6 +2371,11 @@
 
             generateSetters(n, tagHandlerVar, handlerInfo, true);
 
+            // Resource injection
+            out.printin("org.apache.jasper.runtime.AnnotationProcessor.postConstruct(");
+            out.print(tagHandlerVar);
+            out.println(");");
+            
             // Set the body
             if (findJspBody(n) == null) {
                 /*
@@ -2410,6 +2416,11 @@
             // Declare and synchronize AT_END scripting variables
             declareScriptingVars(n, VariableInfo.AT_END);
             syncScriptingVars(n, VariableInfo.AT_END);
+
+            // Resource injection
+            out.printin("org.apache.jasper.runtime.AnnotationProcessor.preDestroy(");
+            out.print(tagHandlerVar);
+            out.println(");");
 
             n.setEndJavaLine(out.getJavaLine());
         }

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java
URL: http://svn.apache.org/viewcvs/tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java?rev=399216&r1=399215&r2=399216&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java Wed May  3 02:11:13 2006
@@ -24,6 +24,8 @@
 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 @@
  * @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 @@
     
     
     /**
-     * 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 @@
     /**
      * 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 @@
         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);

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/TagHandlerPool.java
URL: http://svn.apache.org/viewcvs/tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/TagHandlerPool.java?rev=399216&r1=399215&r2=399216&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/TagHandlerPool.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/TagHandlerPool.java Wed May  3 02:11:13 2006
@@ -19,6 +19,9 @@
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.tagext.Tag;
 import javax.servlet.ServletConfig;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.jasper.Constants;
 
 /**
@@ -33,6 +36,8 @@
     public static String OPTION_TAGPOOL="tagpoolClassName";
     public static String OPTION_MAXSIZE="tagpoolMaxSize";
 
+    private Log log = LogFactory.getLog(TagHandlerPool.class);
+    
     // index of next available tag handler
     private int current;
 
@@ -113,7 +118,9 @@
         // Out of sync block - there is no need for other threads to
         // wait for us to construct a tag for this thread.
         try {
-            return (Tag) handlerClass.newInstance();
+            Tag instance = (Tag) handlerClass.newInstance();
+            AnnotationProcessor.postConstruct(instance);
+            return instance;
         } catch (Exception e) {
             throw new JspException(e.getMessage(), e);
         }
@@ -135,6 +142,12 @@
         }
         // There is no need for other threads to wait for us to release
         handler.release();
+        try {
+            AnnotationProcessor.preDestroy(handler);
+        } catch (Exception e) {
+            log.warn("Error processing preDestroy on tag instance of " 
+                    + handler.getClass().getName(), e);
+        }
     }
 
     /**
@@ -142,9 +155,15 @@
      * handler pool.
      */
     public synchronized void release() {
-	for (int i=current; i>=0; i--) {
-	    handlers[i].release();
-	}
+        for (int i = current; i >= 0; i--) {
+            handlers[i].release();
+            try {
+                AnnotationProcessor.preDestroy(handlers[i]);
+            } catch (Exception e) {
+                log.warn("Error processing preDestroy on tag instance of " 
+                        + handlers[i].getClass().getName(), e);
+            }
+        }
     }
 
     protected static String getOption( ServletConfig config, String name, String defaultV) {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r399216 - in /tomcat/tc6.0.x/trunk/java/org/apache/jasper: compiler/Generator.java runtime/AnnotationProcessor.java runtime/TagHandlerPool.java

Posted by Remy Maucherat <re...@apache.org>.
Bill Barker wrote:
> Section 7.1.11 specifically says Annotation processing happens before the
> setters, not after.  

I didn't notice that. Thanks.

Rémy

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


RE: svn commit: r399216 - in /tomcat/tc6.0.x/trunk/java/org/apache/jasper: compiler/Generator.java runtime/AnnotationProcessor.java runtime/TagHandlerPool.java

Posted by Bill Barker <wb...@wilshire.com>.
 

> -----Original Message-----
> From: remm@apache.org [mailto:remm@apache.org] 
> Sent: Wednesday, May 03, 2006 2:11 AM
> To: tomcat-dev@jakarta.apache.org
> Subject: svn commit: r399216 - in 
> /tomcat/tc6.0.x/trunk/java/org/apache/jasper: 
> compiler/Generator.java runtime/AnnotationProcessor.java 
> runtime/TagHandlerPool.java
> 
> Author: remm
> Date: Wed May  3 02:11:13 2006
> New Revision: 399216
> 
> URL: http://svn.apache.org/viewcvs?rev=399216&view=rev
> Log:
> - Add resource injection for tags (listeners are being 
> handled in the servlet container).
> 

>      /**
> @@ -2370,6 +2371,11 @@
>  
>              generateSetters(n, tagHandlerVar, handlerInfo, true);
>  
> +            // Resource injection
> +            
> out.printin("org.apache.jasper.runtime.AnnotationProcessor.pos
> tConstruct(");
> +            out.print(tagHandlerVar);
> +            out.println(");");
> +            
>              // Set the body

Section 7.1.11 specifically says Annotation processing happens before the
setters, not after.  




This message is intended only for the use of the person(s) listed above as the intended recipient(s), and may contain information that is PRIVILEGED and CONFIDENTIAL.  If you are not an intended recipient, you may not read, copy, or distribute this message or any attachment. If you received this communication in error, please notify us immediately by e-mail and then delete all copies of this message and any attachments.

In addition you should be aware that ordinary (unencrypted) e-mail sent through the Internet is not secure. Do not send confidential or sensitive information, such as social security numbers, account numbers, personal identification numbers and passwords, to us via ordinary (unencrypted) e-mail.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org