/**
- * The servlet context attribute under which we store the annotation
- * processor that is used by the JSP engine.
- */
- public static final String ANNOTATION_PROCESSOR_ATTR =
- "org.apache.catalina.annotation_processor";
-
-
- /**
* The request attribute under which we forward a Java exception
* (as an object of type Throwable) to an error page.
*/
}
}
- // Initialize annotation processor
- if (ok && !getIgnoreAnnotations()) {
- if (annotationProcessor == null) {
- if (isUseNaming() && namingContextListener != null) {
- annotationProcessor =
- new DefaultAnnotationProcessor(namingContextListener.getEnvContext());
- } else {
- annotationProcessor = new DefaultAnnotationProcessor(null);
- }
- }
- getServletContext().setAttribute
- (Globals.ANNOTATION_PROCESSOR_ATTR, annotationProcessor);
- }
-
// Standard container startup
if (log.isDebugEnabled())
log.debug("Processing standard container startup");
// Set annotation processing parameter for Jasper (unfortunately, since
// this can be configured in many places and not just in /WEB-INF/web.xml,
// there are not many solutions)
- if (ignoreAnnotations) {
- Wrapper jspServlet = (Wrapper) findChild(Constants.JSP_SERVLET_NAME);
- jspServlet.addInitParameter("org.apache.jasper.IGNORE_ANNOTATIONS", "true");
+ // Initialize annotation processor
+ if (ok && !getIgnoreAnnotations()) {
+ if (annotationProcessor == null) {
+ if (isUseNaming() && namingContextListener != null) {
+ annotationProcessor =
+ new DefaultAnnotationProcessor(namingContextListener.getEnvContext());
+ } else {
+ annotationProcessor = new DefaultAnnotationProcessor(null);
+ }
+ }
+ getServletContext().setAttribute
+ (AnnotationProcessor.class.getName(), annotationProcessor);
}
-
+
try {
// Create context attributes that will be required
*/
private boolean xpoweredBy;
- /**
- * Should annotations be ignored?
- */
- private boolean ignoreAnnotations = false;
-
public String getProperty(String name ) {
return settings.getProperty( name );
}
}
/**
- * Should annotations on tags be ignored?
- */
- public boolean getIgnoreAnnotations() {
- return ignoreAnnotations;
- }
-
- /**
* Is the generation of SMAP info for JSR45 debuggin suppressed?
*/
public boolean isSmapSuppressed() {
String validating=config.getInitParameter( "validating");
if( "false".equals( validating )) ParserUtils.validating=false;
- String annotations = config.getInitParameter("org.apache.jasper.IGNORE_ANNOTATIONS");
- if ("true".equals(annotations)) {
- ignoreAnnotations = true;
- }
-
String keepgen = config.getInitParameter("keepgenerated");
if (keepgen != null) {
if (keepgen.equalsIgnoreCase("true")) {
private boolean smapSuppressed = true;
private boolean smapDumped = false;
private boolean caching = true;
- private boolean ignoreAnnotations = false;
private Map cache = new HashMap();
private String compiler = null;
compilerSourceVM = vm;
}
- /**
- * Should annotations on tags be ignored?
- */
- public boolean getIgnoreAnnotations() {
- return ignoreAnnotations;
- }
-
- /**
- * Should annotations on tags be ignored?
- */
- public void setIgnoreAnnotations(boolean ignoreAnnotations) {
- this.ignoreAnnotations = ignoreAnnotations;
- }
-
public TldLocationsCache getTldLocationsCache() {
return tldLocationsCache;
}
public boolean getTrimSpaces();
/**
- * Should annotations on tags be ignored?
- */
- public boolean getIgnoreAnnotations();
-
- /**
* Class ID for use in the plugin tag when the browser is IE.
*/
public String getIeClassId();
/*
- * Copyright 1999,2004 The Apache Software Foundation.
+ * Copyright 1999,2004-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.
*
* Tomcat 6.x
* @author Jacob Hookom
+ * @author Remy Maucherat
*/
class Generator {
private static final Class[] OBJECT_CLASS = { Object.class };
private static final String VAR_EXPRESSIONFACTORY = "_el_expressionfactory";
+ private static final String VAR_ANNOTATIONPROCESSOR = "_jsp_annotationprocessor";
private ServletWriter out;
out.print("getServletConfig()");
}
out.println(".getServletContext()).getExpressionFactory();");
-
+
+ out.printin(VAR_ANNOTATIONPROCESSOR);
+ out.print(" = (org.apache.AnnotationProcessor) ");
+ if (ctxt.isTagFile()) {
+ out.print("config");
+ } else {
+ out.print("getServletConfig()");
+ }
+ out.println(".getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());");
+
out.popIndent();
out.printil("}");
out.println();
out.printin("private javax.el.ExpressionFactory ");
out.print(VAR_EXPRESSIONFACTORY);
out.println(";");
+ out.printin("private org.apache.AnnotationProcessor ");
+ out.print(VAR_ANNOTATIONPROCESSOR);
+ out.println(";");
out.println();
}
out.print("new ");
out.print(tagHandlerClassName);
out.println("();");
- if (!ctxt.getOptions().getIgnoreAnnotations()) {
- out.printin("org.apache.jasper.runtime.AnnotationProcessor.postConstruct(");
- out.print(tagHandlerVar);
- out.println(");");
- }
+ out.printin("org.apache.jasper.runtime.AnnotationHelper.postConstruct(");
+ out.print(VAR_ANNOTATIONPROCESSOR);
+ out.print(", ");
+ out.print(tagHandlerVar);
+ out.println(");");
}
// includes setting the context
} else {
out.printin(tagHandlerVar);
out.println(".release();");
- if (!ctxt.getOptions().getIgnoreAnnotations()) {
- out.printil("try {");
- out.pushIndent();
- out.printin("org.apache.jasper.runtime.AnnotationProcessor.preDestroy(");
- out.print(tagHandlerVar);
- out.println(");");
- out.popIndent();
- out.printil("} catch (Exception e) {");
- out.pushIndent();
- out.printin("log(\"Error processing preDestroy on tag instance of \" +");
- out.print(tagHandlerVar);
- out.println(".getClass().getName());");
- out.popIndent();
- out.printil("}");
- }
+ out.printil("try {");
+ out.pushIndent();
+ out.printin("org.apache.jasper.runtime.AnnotationHelper.preDestroy(");
+ out.print(VAR_ANNOTATIONPROCESSOR);
+ out.print(", ");
+ out.print(tagHandlerVar);
+ out.println(");");
+ out.popIndent();
+ out.printil("} catch (Exception e) {");
+ out.pushIndent();
+ out.printin("log(\"Error processing preDestroy on tag instance of \" +");
+ out.print(tagHandlerVar);
+ out.println(".getClass().getName());");
+ out.popIndent();
+ out.printil("}");
}
}
if (isTagFile || isFragment) {
} else {
out.printin(tagHandlerVar);
out.println(".release();");
- if (!ctxt.getOptions().getIgnoreAnnotations()) {
- out.printil("try {");
- out.pushIndent();
- out.printin("org.apache.jasper.runtime.AnnotationProcessor.preDestroy(");
- out.print(tagHandlerVar);
- out.println(");");
- out.popIndent();
- out.printil("} catch (Exception e) {");
- out.pushIndent();
- out.printin("log(\"Error processing preDestroy on tag instance of \" +");
- out.print(tagHandlerVar);
- out.println(".getClass().getName());");
- out.popIndent();
- out.printil("}");
- }
+ out.printil("try {");
+ out.pushIndent();
+ out.printin("org.apache.jasper.runtime.AnnotationHelper.preDestroy(");
+ out.print(VAR_ANNOTATIONPROCESSOR);
+ out.print(", ");
+ out.print(tagHandlerVar);
+ out.println(");");
+ out.popIndent();
+ out.printil("} catch (Exception e) {");
+ out.pushIndent();
+ out.printin("log(\"Error processing preDestroy on tag instance of \" +");
+ out.print(tagHandlerVar);
+ out.println(".getClass().getName());");
+ out.popIndent();
+ out.printil("}");
}
if (n.implementsTryCatchFinally()) {
out.println("();");
// Resource injection
- if (!ctxt.getOptions().getIgnoreAnnotations()) {
- out.printin("org.apache.jasper.runtime.AnnotationProcessor.postConstruct(");
- out.print(tagHandlerVar);
- out.println(");");
- }
+ out.printin("org.apache.jasper.runtime.AnnotationHelper.postConstruct(");
+ out.print(VAR_ANNOTATIONPROCESSOR);
+ out.print(", ");
+ out.print(tagHandlerVar);
+ out.println(");");
generateSetters(n, tagHandlerVar, handlerInfo, true);
syncScriptingVars(n, VariableInfo.AT_END);
// Resource injection
- if (!ctxt.getOptions().getIgnoreAnnotations()) {
- out.printin("org.apache.jasper.runtime.AnnotationProcessor.preDestroy(");
- out.print(tagHandlerVar);
- out.println(");");
- }
+ out.printin("org.apache.jasper.runtime.AnnotationHelper.preDestroy(");
+ out.print(VAR_ANNOTATIONPROCESSOR);
+ out.print(", ");
+ out.print(tagHandlerVar);
+ out.println(");");
n.setEndJavaLine(out.getJavaLine());
}
--- /dev/null
+/*
+ * 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.InvocationTargetException;
+
+import javax.naming.NamingException;
+
+import org.apache.AnnotationProcessor;
+
+
+/**
+ * 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 AnnotationHelper {
+
+
+ /**
+ * Call postConstruct method on the specified instance. Note: In Jasper, this
+ * calls naming resources injection as well.
+ */
+ public static void postConstruct(AnnotationProcessor processor, Object instance)
+ throws IllegalAccessException, InvocationTargetException, NamingException {
+ if (processor != null) {
+ processor.processAnnotations(instance);
+ processor.postConstruct(instance);
+ }
+ }
+
+
+ /**
+ * Call preDestroy method on the specified instance.
+ */
+ public static void preDestroy(AnnotationProcessor processor, Object instance)
+ throws IllegalAccessException, InvocationTargetException {
+ if (processor != null) {
+ processor.preDestroy(instance);
+ }
+ }
+
+
+}
+++ /dev/null
-/*
- * 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.PostConstruct;
-import javax.annotation.PreDestroy;
-import javax.annotation.Resource;
-import javax.ejb.EJB;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-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. Note: In Jasper, this
- * calls naming resources injection as well.
- */
- public static void postConstruct(Object instance)
- throws IllegalAccessException, InvocationTargetException, NamingException {
-
- // Initialize fields annotations
- Field[] fields = instance.getClass().getDeclaredFields();
- 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().getDeclaredMethods();
- 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)) {
- 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().getDeclaredMethods();
- Method preDestroy = null;
- for (int i = 0; i < methods.length; i++) {
- if (methods[i].isAnnotationPresent(PreDestroy.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 field.
- */
- 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);
- } 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(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;
-
- Context context = (Context) (new InitialContext()).lookup("java:comp/env");
- 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);
- }
-
-
-}
package org.apache.jasper.runtime;
+import javax.servlet.ServletConfig;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
-import javax.servlet.ServletConfig;
+import org.apache.AnnotationProcessor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jasper.Constants;
// index of next available tag handler
private int current;
- private boolean ignoreAnnotations = false;
+ protected AnnotationProcessor annotationProcessor = null;
public static TagHandlerPool getTagHandlerPool( ServletConfig config) {
TagHandlerPool result=null;
}
this.handlers = new Tag[maxSize];
this.current = -1;
- String annotations = getOption(config, "org.apache.jasper.IGNORE_ANNOTATIONS", null);
- if ("true".equals(annotations)) {
- ignoreAnnotations = true;
- }
+ this.annotationProcessor =
+ (AnnotationProcessor) config.getServletContext().getAttribute(AnnotationProcessor.class.getName());
}
/**
// wait for us to construct a tag for this thread.
try {
Tag instance = (Tag) handlerClass.newInstance();
- if (!ignoreAnnotations) {
- AnnotationProcessor.postConstruct(instance);
- }
+ AnnotationHelper.postConstruct(annotationProcessor, instance);
return instance;
} catch (Exception e) {
throw new JspException(e.getMessage(), e);
}
// There is no need for other threads to wait for us to release
handler.release();
- if (!ignoreAnnotations) {
+ if (annotationProcessor != null) {
try {
- AnnotationProcessor.preDestroy(handler);
+ AnnotationHelper.preDestroy(annotationProcessor, handler);
} catch (Exception e) {
log.warn("Error processing preDestroy on tag instance of "
+ handler.getClass().getName(), e);
public synchronized void release() {
for (int i = current; i >= 0; i--) {
handlers[i].release();
- if (!ignoreAnnotations) {
+ if (annotationProcessor != null) {
try {
- AnnotationProcessor.preDestroy(handlers[i]);
+ AnnotationHelper.preDestroy(annotationProcessor, handlers[i]);
} catch (Exception e) {
log.warn("Error processing preDestroy on tag instance of "
+ handlers[i].getClass().getName(), e);