- Add annotation processor to Jasper too (tags need injection too).
authorremm <remm@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 19 Apr 2006 14:51:10 +0000 (14:51 +0000)
committerremm <remm@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 19 Apr 2006 14:51:10 +0000 (14:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@395258 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/util/AnnotationProcessor.java
java/org/apache/jasper/runtime/AnnotationProcessor.java [new file with mode: 0644]

index a7426c9..499cb79 100644 (file)
@@ -35,8 +35,9 @@ import org.apache.tomcat.util.IntrospectionUtils;
 /**\r
  * Verify the annotation and Process it.\r
  *\r
- * @author    Fabien Carrion\r
- * @version   $Revision: 303236 $, $Date: 2006-03-09 16:46:52 -0600 (Thu, 09 Mar 2006) $\r
+ * @author Fabien Carrion\r
+ * @author Remy Maucherat\r
+ * @version $Revision: 303236 $, $Date: 2006-03-09 16:46:52 -0600 (Thu, 09 Mar 2006) $\r
  */\r
 public class AnnotationProcessor {\r
     \r
@@ -172,8 +173,11 @@ public class AnnotationProcessor {
     }\r
     \r
     \r
+    /**\r
+     * Inject resources in specified field.\r
+     */\r
     protected static void lookupFieldResource(javax.naming.Context context, \r
-            Object instance, Field f, String name)\r
+            Object instance, Field field, String name)\r
         throws NamingException, IllegalAccessException {\r
     \r
         Object lookedupResource = null;\r
@@ -183,16 +187,19 @@ public class AnnotationProcessor {
                 (name.length() > 0)) {\r
             lookedupResource = context.lookup(name);\r
         } else {\r
-            lookedupResource = context.lookup(instance.getClass().getName() + "/" + f.getName());\r
+            lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName());\r
         }\r
         \r
-        accessibility = f.isAccessible();\r
-        f.setAccessible(true);\r
-        f.set(instance, lookedupResource);\r
-        f.setAccessible(accessibility);\r
+        accessibility = field.isAccessible();\r
+        field.setAccessible(true);\r
+        field.set(instance, lookedupResource);\r
+        field.setAccessible(accessibility);\r
     }\r
 \r
 \r
+    /**\r
+     * Inject resources in specified method.\r
+     */\r
     protected static void lookupMethodResource(javax.naming.Context context, \r
             Object instance, Method method, String name)\r
         throws NamingException, IllegalAccessException, InvocationTargetException {\r
diff --git a/java/org/apache/jasper/runtime/AnnotationProcessor.java b/java/org/apache/jasper/runtime/AnnotationProcessor.java
new file mode 100644 (file)
index 0000000..cab6a1a
--- /dev/null
@@ -0,0 +1,229 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jasper.runtime;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import javax.annotation.EJB;
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import javax.naming.NamingException;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+import javax.xml.ws.WebServiceRef;
+
+
+/**
+ * Verify the annotation and Process it.
+ *
+ * @author Fabien Carrion
+ * @author Remy Maucherat
+ * @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.
+     */
+    public static void postConstruct(Object instance)
+        throws IllegalAccessException, InvocationTargetException {
+        
+        Method[] methods = instance.getClass().getMethods();
+        Method postConstruct = null;
+        for (int i = 0; i < methods.length; i++) {
+            if (methods[i].isAnnotationPresent(PostConstruct.class)) {
+                if ((postConstruct != null) 
+                        || (methods[i].getParameterTypes().length != 0)
+                        || (Modifier.isStatic(methods[i].getModifiers())) 
+                        || (methods[i].getExceptionTypes().length > 0)
+                        || (!methods[i].getReturnType().getName().equals("void"))) {
+                    throw new IllegalArgumentException("Invalid PostConstruct annotation");
+                }
+                postConstruct = methods[i];
+            }
+        }
+
+        // At the end the postconstruct annotated 
+        // method is invoked
+        if (postConstruct != null) {
+            boolean accessibility = postConstruct.isAccessible();
+            postConstruct.setAccessible(true);
+            postConstruct.invoke(instance);
+            postConstruct.setAccessible(accessibility);
+        }
+        
+    }
+    
+    
+    /**
+     * Call preDestroy method on the specified instance.
+     */
+    public static void preDestroy(Object instance)
+        throws IllegalAccessException, InvocationTargetException {
+        
+        Method[] methods = instance.getClass().getMethods();
+        Method preDestroy = null;
+        for (int i = 0; i < methods.length; i++) {
+            if (methods[i].isAnnotationPresent(PostConstruct.class)) {
+                if ((preDestroy != null) 
+                        || (methods[i].getParameterTypes().length != 0)
+                        || (Modifier.isStatic(methods[i].getModifiers())) 
+                        || (methods[i].getExceptionTypes().length > 0)
+                        || (!methods[i].getReturnType().getName().equals("void"))) {
+                    throw new IllegalArgumentException("Invalid PreDestroy annotation");
+                }
+                preDestroy = methods[i];
+            }
+        }
+
+        // At the end the postconstruct annotated 
+        // method is invoked
+        if (preDestroy != null) {
+            boolean accessibility = preDestroy.isAccessible();
+            preDestroy.setAccessible(true);
+            preDestroy.invoke(instance);
+            preDestroy.setAccessible(accessibility);
+        }
+        
+    }
+    
+    
+    /**
+     * 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)
+        throws NamingException, IllegalAccessException {
+    
+        Object lookedupResource = null;
+        boolean accessibility = false;
+        
+        if ((name != null) &&
+                (name.length() > 0)) {
+            lookedupResource = context.lookup(name);
+        } else {
+            lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName());
+        }
+        
+        accessibility = field.isAccessible();
+        field.setAccessible(true);
+        field.set(instance, lookedupResource);
+        field.setAccessible(accessibility);
+    }
+
+
+    /**
+     * Inject resources in specified method.
+     */
+    protected static void lookupMethodResource(javax.naming.Context context, 
+            Object instance, Method method, String name)
+        throws NamingException, IllegalAccessException, InvocationTargetException {
+        
+        if (!method.getName().startsWith("set") 
+                || method.getParameterTypes().length != 1
+                || !method.getReturnType().getName().equals("void")) {
+            throw new IllegalArgumentException("Invalid method resource injection annotation");
+        }
+        
+        Object lookedupResource = null;
+        boolean accessibility = false;
+        
+        if ((name != null) &&
+                (name.length() > 0)) {
+            lookedupResource = context.lookup(name);
+        } else {
+            lookedupResource = 
+                context.lookup(instance.getClass().getName() + "/" + method.getName().substring(3));
+        }
+        
+        accessibility = method.isAccessible();
+        method.setAccessible(true);
+        method.invoke(instance, lookedupResource);
+        method.setAccessible(accessibility);
+    }
+    
+
+}