Author: remm Date: Wed Apr 19 07:51:10 2006 New Revision: 395258 URL: http://svn.apache.org/viewcvs?rev=395258&view=rev Log: - Add annotation processor to Jasper too (tags need injection too).
Added: tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java (with props) Modified: tomcat/tc6.0.x/trunk/java/org/apache/catalina/util/AnnotationProcessor.java Modified: tomcat/tc6.0.x/trunk/java/org/apache/catalina/util/AnnotationProcessor.java URL: http://svn.apache.org/viewcvs/tomcat/tc6.0.x/trunk/java/org/apache/catalina/util/AnnotationProcessor.java?rev=395258&r1=395257&r2=395258&view=diff ============================================================================== --- tomcat/tc6.0.x/trunk/java/org/apache/catalina/util/AnnotationProcessor.java (original) +++ tomcat/tc6.0.x/trunk/java/org/apache/catalina/util/AnnotationProcessor.java Wed Apr 19 07:51:10 2006 @@ -35,8 +35,9 @@ /** * Verify the annotation and Process it. * - * @author Fabien Carrion - * @version $Revision: 303236 $, $Date: 2006-03-09 16:46:52 -0600 (Thu, 09 Mar 2006) $ + * @author Fabien Carrion + * @author Remy Maucherat + * @version $Revision: 303236 $, $Date: 2006-03-09 16:46:52 -0600 (Thu, 09 Mar 2006) $ */ public class AnnotationProcessor { @@ -172,8 +173,11 @@ } + /** + * Inject resources in specified field. + */ protected static void lookupFieldResource(javax.naming.Context context, - Object instance, Field f, String name) + Object instance, Field field, String name) throws NamingException, IllegalAccessException { Object lookedupResource = null; @@ -183,16 +187,19 @@ (name.length() > 0)) { lookedupResource = context.lookup(name); } else { - lookedupResource = context.lookup(instance.getClass().getName() + "/" + f.getName()); + lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName()); } - accessibility = f.isAccessible(); - f.setAccessible(true); - f.set(instance, lookedupResource); - f.setAccessible(accessibility); + 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 { Added: 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=395258&view=auto ============================================================================== --- tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java (added) +++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java Wed Apr 19 07:51:10 2006 @@ -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); + } + + +} Propchange: tomcat/tc6.0.x/trunk/java/org/apache/jasper/runtime/AnnotationProcessor.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]