Author: markt
Date: Mon Sep 19 10:11:44 2011
New Revision: 1172556

URL: http://svn.apache.org/viewvc?rev=1172556&view=rev
Log:
Always process postConstruct / preDestroy annotations but only process resource 
annotations if JNDI is enabled.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java

Modified: tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java?rev=1172556&r1=1172555&r2=1172556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java Mon 
Sep 19 10:11:44 2011
@@ -242,62 +242,61 @@ public class DefaultInstanceManager impl
             Map<String, String> injections) throws IllegalAccessException,
             InvocationTargetException, NamingException {
 
-        if (context == null) {
-            // No resource injection
-            return;
-        }
-
         while (clazz != null) {
             List<AnnotationCacheEntry> annotations = 
annotationCache.get(clazz);
             if (annotations == null) {
                 annotations = new ArrayList<AnnotationCacheEntry>();
-                // Initialize fields annotations
-                Field[] fields = null;
-                if (Globals.IS_SECURITY_ENABLED) {
-                    final Class<?> clazz2 = clazz;
-                    fields = AccessController.doPrivileged(
-                            new PrivilegedAction<Field[]>(){
-                        @Override
-                        public Field[] run(){
-                            return clazz2.getDeclaredFields();
+                
+                if (context != null) {
+                    // Initialize fields annotations for resource injection if
+                    // JNDI is enabled
+                    Field[] fields = null;
+                    if (Globals.IS_SECURITY_ENABLED) {
+                        final Class<?> clazz2 = clazz;
+                        fields = AccessController.doPrivileged(
+                                new PrivilegedAction<Field[]>(){
+                            @Override
+                            public Field[] run(){
+                                return clazz2.getDeclaredFields();
+                            }
+                        });
+                    } else {
+                        fields = clazz.getDeclaredFields();
+                    }
+                    for (Field field : fields) {
+                        if (injections != null && 
injections.containsKey(field.getName())) {
+                            annotations.add(new AnnotationCacheEntry(field,
+                                    injections.get(field.getName()),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if (field.isAnnotationPresent(Resource.class)) {
+                            Resource annotation = 
field.getAnnotation(Resource.class);
+                            annotations.add(new AnnotationCacheEntry(field,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if (field.isAnnotationPresent(EJB.class)) {
+                            EJB annotation = field.getAnnotation(EJB.class);
+                            annotations.add(new AnnotationCacheEntry(field,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if 
(field.isAnnotationPresent(WebServiceRef.class)) {
+                            WebServiceRef annotation =
+                                    field.getAnnotation(WebServiceRef.class);
+                            annotations.add(new AnnotationCacheEntry(field,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if 
(field.isAnnotationPresent(PersistenceContext.class)) {
+                            PersistenceContext annotation =
+                                    
field.getAnnotation(PersistenceContext.class);
+                            annotations.add(new AnnotationCacheEntry(field,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if 
(field.isAnnotationPresent(PersistenceUnit.class)) {
+                            PersistenceUnit annotation =
+                                    field.getAnnotation(PersistenceUnit.class);
+                            annotations.add(new AnnotationCacheEntry(field,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
                         }
-                    });
-                } else {
-                    fields = clazz.getDeclaredFields();
-                }
-                for (Field field : fields) {
-                    if (injections != null && 
injections.containsKey(field.getName())) {
-                        annotations.add(new AnnotationCacheEntry(field,
-                                injections.get(field.getName()),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if (field.isAnnotationPresent(Resource.class)) {
-                        Resource annotation = 
field.getAnnotation(Resource.class);
-                        annotations.add(new AnnotationCacheEntry(field,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if (field.isAnnotationPresent(EJB.class)) {
-                        EJB annotation = field.getAnnotation(EJB.class);
-                        annotations.add(new AnnotationCacheEntry(field,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if (field.isAnnotationPresent(WebServiceRef.class)) 
{
-                        WebServiceRef annotation =
-                                field.getAnnotation(WebServiceRef.class);
-                        annotations.add(new AnnotationCacheEntry(field,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if 
(field.isAnnotationPresent(PersistenceContext.class)) {
-                        PersistenceContext annotation =
-                                field.getAnnotation(PersistenceContext.class);
-                        annotations.add(new AnnotationCacheEntry(field,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if 
(field.isAnnotationPresent(PersistenceUnit.class)) {
-                        PersistenceUnit annotation =
-                                field.getAnnotation(PersistenceUnit.class);
-                        annotations.add(new AnnotationCacheEntry(field,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
                     }
                 }
         
@@ -319,44 +318,47 @@ public class DefaultInstanceManager impl
                 Method preDestroy = null;
                 for (Method method : methods) {
                     String methodName = method.getName();
-                    if (injections != null && methodName.startsWith("set") && 
methodName.length() > 3) {
-                        String fieldName = 
Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
-                        if (injections.containsKey(fieldName)) {
+                    if (context != null) {
+                        // Resource injection only if JNDI is enabled
+                        if (injections != null && methodName.startsWith("set") 
&& methodName.length() > 3) {
+                            String fieldName = 
Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
+                            if (injections.containsKey(fieldName)) {
+                                annotations.add(new 
AnnotationCacheEntry(method,
+                                        injections.get(method.getName()),
+                                        AnnotationCacheEntryType.FIELD));
+                                break;
+                            }
+                        }
+                        if (method.isAnnotationPresent(Resource.class)) {
+                            Resource annotation = 
method.getAnnotation(Resource.class);
+                            annotations.add(new AnnotationCacheEntry(method,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if (method.isAnnotationPresent(EJB.class)) {
+                            EJB annotation = method.getAnnotation(EJB.class);
                             annotations.add(new AnnotationCacheEntry(method,
-                                    injections.get(method.getName()),
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if 
(method.isAnnotationPresent(WebServiceRef.class)) {
+                            WebServiceRef annotation =
+                                    method.getAnnotation(WebServiceRef.class);
+                            annotations.add(new AnnotationCacheEntry(method,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if 
(method.isAnnotationPresent(PersistenceContext.class)) {
+                            PersistenceContext annotation =
+                                    
method.getAnnotation(PersistenceContext.class);
+                            annotations.add(new AnnotationCacheEntry(method,
+                                    annotation.name(),
+                                    AnnotationCacheEntryType.FIELD));
+                        } else if 
(method.isAnnotationPresent(PersistenceUnit.class)) {
+                            PersistenceUnit annotation =
+                                    
method.getAnnotation(PersistenceUnit.class);
+                            annotations.add(new AnnotationCacheEntry(method,
+                                    annotation.name(),
                                     AnnotationCacheEntryType.FIELD));
-                            break;
                         }
                     }
-                    if (method.isAnnotationPresent(Resource.class)) {
-                        Resource annotation = 
method.getAnnotation(Resource.class);
-                        annotations.add(new AnnotationCacheEntry(method,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if (method.isAnnotationPresent(EJB.class)) {
-                        EJB annotation = method.getAnnotation(EJB.class);
-                        annotations.add(new AnnotationCacheEntry(method,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if 
(method.isAnnotationPresent(WebServiceRef.class)) {
-                        WebServiceRef annotation =
-                                method.getAnnotation(WebServiceRef.class);
-                        annotations.add(new AnnotationCacheEntry(method,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if 
(method.isAnnotationPresent(PersistenceContext.class)) {
-                        PersistenceContext annotation =
-                                method.getAnnotation(PersistenceContext.class);
-                        annotations.add(new AnnotationCacheEntry(method,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    } else if 
(method.isAnnotationPresent(PersistenceUnit.class)) {
-                        PersistenceUnit annotation =
-                                method.getAnnotation(PersistenceUnit.class);
-                        annotations.add(new AnnotationCacheEntry(method,
-                                annotation.name(),
-                                AnnotationCacheEntryType.FIELD));
-                    }
 
                     if (method.isAnnotationPresent(PostConstruct.class)) {
                         if ((postConstruct != null) ||



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to