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;
* @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)) {
/**
- * 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);
/**
* 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")
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);
import javax.servlet.jsp.JspException;\r
import javax.servlet.jsp.tagext.Tag;\r
import javax.servlet.ServletConfig;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
import org.apache.jasper.Constants;\r
\r
/**\r
public static String OPTION_TAGPOOL="tagpoolClassName";\r
public static String OPTION_MAXSIZE="tagpoolMaxSize";\r
\r
+ private Log log = LogFactory.getLog(TagHandlerPool.class);\r
+ \r
// index of next available tag handler\r
private int current;\r
\r
// Out of sync block - there is no need for other threads to\r
// wait for us to construct a tag for this thread.\r
try {\r
- return (Tag) handlerClass.newInstance();\r
+ Tag instance = (Tag) handlerClass.newInstance();\r
+ AnnotationProcessor.postConstruct(instance);\r
+ return instance;\r
} catch (Exception e) {\r
throw new JspException(e.getMessage(), e);\r
}\r
}\r
// There is no need for other threads to wait for us to release\r
handler.release();\r
+ try {\r
+ AnnotationProcessor.preDestroy(handler);\r
+ } catch (Exception e) {\r
+ log.warn("Error processing preDestroy on tag instance of " \r
+ + handler.getClass().getName(), e);\r
+ }\r
}\r
\r
/**\r
* handler pool.\r
*/\r
public synchronized void release() {\r
- for (int i=current; i>=0; i--) {\r
- handlers[i].release();\r
- }\r
+ for (int i = current; i >= 0; i--) {\r
+ handlers[i].release();\r
+ try {\r
+ AnnotationProcessor.preDestroy(handlers[i]);\r
+ } catch (Exception e) {\r
+ log.warn("Error processing preDestroy on tag instance of " \r
+ + handlers[i].getClass().getName(), e);\r
+ }\r
+ }\r
}\r
\r
protected static String getOption( ServletConfig config, String name, String defaultV) {\r