Lines 19-28
Link Here
|
19 |
import java.beans.Introspector; |
19 |
import java.beans.Introspector; |
20 |
import java.io.IOException; |
20 |
import java.io.IOException; |
21 |
import java.io.InputStream; |
21 |
import java.io.InputStream; |
|
|
22 |
import java.lang.annotation.Annotation; |
22 |
import java.lang.reflect.Field; |
23 |
import java.lang.reflect.Field; |
23 |
import java.lang.reflect.InvocationTargetException; |
24 |
import java.lang.reflect.InvocationTargetException; |
24 |
import java.lang.reflect.Method; |
25 |
import java.lang.reflect.Method; |
25 |
import java.lang.reflect.Modifier; |
|
|
26 |
import java.security.AccessController; |
26 |
import java.security.AccessController; |
27 |
import java.security.PrivilegedAction; |
27 |
import java.security.PrivilegedAction; |
28 |
import java.security.PrivilegedActionException; |
28 |
import java.security.PrivilegedActionException; |
Lines 80-85
Link Here
|
80 |
private final Properties restrictedServlets = new Properties(); |
80 |
private final Properties restrictedServlets = new Properties(); |
81 |
private final Map<Class<?>, AnnotationCacheEntry[]> annotationCache = |
81 |
private final Map<Class<?>, AnnotationCacheEntry[]> annotationCache = |
82 |
new WeakHashMap<Class<?>, AnnotationCacheEntry[]>(); |
82 |
new WeakHashMap<Class<?>, AnnotationCacheEntry[]>(); |
|
|
83 |
private final Map<String, String> postConstructMethods; |
84 |
private final Map<String, String> preDestroyMethods; |
83 |
|
85 |
|
84 |
public DefaultInstanceManager(Context context, Map<String, Map<String, String>> injectionMap, org.apache.catalina.Context catalinaContext, ClassLoader containerClassLoader) { |
86 |
public DefaultInstanceManager(Context context, Map<String, Map<String, String>> injectionMap, org.apache.catalina.Context catalinaContext, ClassLoader containerClassLoader) { |
85 |
classLoader = catalinaContext.getLoader().getClassLoader(); |
87 |
classLoader = catalinaContext.getLoader().getClassLoader(); |
Lines 126-131
Link Here
|
126 |
} |
128 |
} |
127 |
this.context = context; |
129 |
this.context = context; |
128 |
this.injectionMap = injectionMap; |
130 |
this.injectionMap = injectionMap; |
|
|
131 |
this.postConstructMethods = catalinaContext.findPostConstructMethods(); |
132 |
this.preDestroyMethods = catalinaContext.findPreDestroyMethods(); |
129 |
} |
133 |
} |
130 |
|
134 |
|
131 |
@Override |
135 |
@Override |
Lines 332-338
Link Here
|
332 |
// Initialize methods annotations |
336 |
// Initialize methods annotations |
333 |
Method[] methods = Introspection.getDeclaredMethods(clazz); |
337 |
Method[] methods = Introspection.getDeclaredMethods(clazz); |
334 |
Method postConstruct = null; |
338 |
Method postConstruct = null; |
|
|
339 |
String postConstructFromXml = this.postConstructMethods.get(clazz.getName()); |
335 |
Method preDestroy = null; |
340 |
Method preDestroy = null; |
|
|
341 |
String preDestroyFromXml = this.preDestroyMethods.get(clazz.getName()); |
336 |
for (Method method : methods) { |
342 |
for (Method method : methods) { |
337 |
if (context != null) { |
343 |
if (context != null) { |
338 |
// Resource injection only if JNDI is enabled |
344 |
// Resource injection only if JNDI is enabled |
Lines 389-429
Link Here
|
389 |
} |
395 |
} |
390 |
} |
396 |
} |
391 |
|
397 |
|
392 |
if (method.isAnnotationPresent(PostConstruct.class)) { |
398 |
postConstruct = findPostConstruct(postConstruct, postConstructFromXml, method); |
393 |
if ((postConstruct != null) || |
|
|
394 |
(method.getParameterTypes().length != 0) || |
395 |
(Modifier.isStatic(method.getModifiers())) || |
396 |
(method.getExceptionTypes().length > 0) || |
397 |
(!method.getReturnType().getName().equals("void"))) { |
398 |
throw new IllegalArgumentException( |
399 |
"Invalid PostConstruct annotation"); |
400 |
} |
401 |
postConstruct = method; |
402 |
} |
403 |
|
399 |
|
404 |
if (method.isAnnotationPresent(PreDestroy.class)) { |
400 |
preDestroy = findPreDestroy(preDestroy, preDestroyFromXml, method); |
405 |
if ((preDestroy != null || |
|
|
406 |
method.getParameterTypes().length != 0) || |
407 |
(Modifier.isStatic(method.getModifiers())) || |
408 |
(method.getExceptionTypes().length > 0) || |
409 |
(!method.getReturnType().getName().equals("void"))) { |
410 |
throw new IllegalArgumentException( |
411 |
"Invalid PreDestroy annotation"); |
412 |
} |
413 |
preDestroy = method; |
414 |
} |
415 |
} |
401 |
} |
|
|
402 |
|
416 |
if (postConstruct != null) { |
403 |
if (postConstruct != null) { |
417 |
annotations.add(new AnnotationCacheEntry( |
404 |
annotations.add(new AnnotationCacheEntry( |
418 |
postConstruct.getName(), |
405 |
postConstruct.getName(), |
419 |
postConstruct.getParameterTypes(), null, |
406 |
postConstruct.getParameterTypes(), null, |
420 |
AnnotationCacheEntryType.POST_CONSTRUCT)); |
407 |
AnnotationCacheEntryType.POST_CONSTRUCT)); |
|
|
408 |
} else if (postConstructFromXml != null) { |
409 |
throw new IllegalArgumentException("Post construct method " |
410 |
+ postConstructFromXml + " for class " + clazz.getName() |
411 |
+ " is declared in deployment descriptor but cannot be found."); |
421 |
} |
412 |
} |
422 |
if (preDestroy != null) { |
413 |
if (preDestroy != null) { |
423 |
annotations.add(new AnnotationCacheEntry( |
414 |
annotations.add(new AnnotationCacheEntry( |
424 |
preDestroy.getName(), |
415 |
preDestroy.getName(), |
425 |
preDestroy.getParameterTypes(), null, |
416 |
preDestroy.getParameterTypes(), null, |
426 |
AnnotationCacheEntryType.PRE_DESTROY)); |
417 |
AnnotationCacheEntryType.PRE_DESTROY)); |
|
|
418 |
} else if (preDestroyFromXml != null) { |
419 |
throw new IllegalArgumentException("Pre destroy method " |
420 |
+ preDestroyFromXml + " for class " + clazz.getName() |
421 |
+ " is declared in deployment descriptor but cannot be found."); |
427 |
} |
422 |
} |
428 |
if (annotations.isEmpty()) { |
423 |
if (annotations.isEmpty()) { |
429 |
// Use common object to save memory |
424 |
// Use common object to save memory |
Lines 745-748
Link Here
|
745 |
private static enum AnnotationCacheEntryType { |
740 |
private static enum AnnotationCacheEntryType { |
746 |
FIELD, SETTER, POST_CONSTRUCT, PRE_DESTROY |
741 |
FIELD, SETTER, POST_CONSTRUCT, PRE_DESTROY |
747 |
} |
742 |
} |
|
|
743 |
|
744 |
private Method findPostConstruct(Method currentPostConstruct, |
745 |
String postConstructFromXml, Method method) { |
746 |
return findLifecycleCallback(currentPostConstruct, |
747 |
postConstructFromXml, method, PostConstruct.class); |
748 |
} |
749 |
|
750 |
private Method findPreDestroy(Method currentPreDestroy, |
751 |
String preDestroyFromXml, Method method) { |
752 |
return findLifecycleCallback(currentPreDestroy, |
753 |
preDestroyFromXml, method, PreDestroy.class); |
754 |
} |
755 |
|
756 |
private Method findLifecycleCallback(Method currentMethod, String methodNameFromXml, |
757 |
Method method, Class<? extends Annotation> annotation) { |
758 |
Method result = currentMethod; |
759 |
if (methodNameFromXml != null) { |
760 |
if (method.getName().equals(methodNameFromXml)) { |
761 |
if (!Introspection.isValidLifecycleCallback(method)) { |
762 |
throw new IllegalArgumentException( |
763 |
"Invalid " + annotation.getName() + " annotation"); |
764 |
} |
765 |
result = method; |
766 |
} |
767 |
} else { |
768 |
if (method.isAnnotationPresent(annotation)) { |
769 |
if (currentMethod != null || |
770 |
!Introspection.isValidLifecycleCallback(method)) { |
771 |
throw new IllegalArgumentException( |
772 |
"Invalid " + annotation.getName() + " annotation"); |
773 |
} |
774 |
result = method; |
775 |
} |
776 |
} |
777 |
return result; |
778 |
} |
748 |
} |
779 |
} |