From: remm Date: Wed, 3 May 2006 09:11:13 +0000 (+0000) Subject: - Add resource injection for tags (listeners are being handled in the servlet container). X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=3cd051ea42c52ac452082c52617dcc7f7b44d13a;p=tomcat7.0 - Add resource injection for tags (listeners are being handled in the servlet container). git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@399216 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/jasper/compiler/Generator.java b/java/org/apache/jasper/compiler/Generator.java index ddf0af59e..45ef31345 100644 --- a/java/org/apache/jasper/compiler/Generator.java +++ b/java/org/apache/jasper/compiler/Generator.java @@ -504,7 +504,8 @@ class Generator { } out.printin("private javax.el.ExpressionFactory "); out.print(VAR_EXPRESSIONFACTORY); - out.println(";\n"); + out.println(";"); + out.println(); } /** @@ -2370,6 +2371,11 @@ class Generator { 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) { /* @@ -2411,6 +2417,11 @@ class Generator { 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()); } diff --git a/java/org/apache/jasper/runtime/AnnotationProcessor.java b/java/org/apache/jasper/runtime/AnnotationProcessor.java index cab6a1ad7..cd23a2c29 100644 --- a/java/org/apache/jasper/runtime/AnnotationProcessor.java +++ b/java/org/apache/jasper/runtime/AnnotationProcessor.java @@ -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); diff --git a/java/org/apache/jasper/runtime/TagHandlerPool.java b/java/org/apache/jasper/runtime/TagHandlerPool.java index 309194bfb..5574ccf78 100644 --- a/java/org/apache/jasper/runtime/TagHandlerPool.java +++ b/java/org/apache/jasper/runtime/TagHandlerPool.java @@ -19,6 +19,9 @@ package org.apache.jasper.runtime; 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 class TagHandlerPool { 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 @@ public class TagHandlerPool { // 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 @@ public class TagHandlerPool { } // 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 @@ public class TagHandlerPool { * 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) {