Package org.apache.catalina.core
Class DefaultInstanceManager
- java.lang.Object
-
- org.apache.catalina.core.DefaultInstanceManager
-
- All Implemented Interfaces:
InstanceManager
public class DefaultInstanceManager extends java.lang.Object implements InstanceManager
Default implementation ofInstanceManagerthat handles annotation-based dependency injection for EJB, JPA, and web service references.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.ClassLoaderclassLoaderThe class loader for loading application classes.protected java.lang.ClassLoadercontainerClassLoaderThe class loader for loading container classes.protected booleanignoreAnnotationsWhether to ignore annotations during injection.protected booleanprivilegedWhether to perform privileged operations.protected static StringManagersmThe string manager for this package.
-
Constructor Summary
Constructors Constructor Description DefaultInstanceManager(javax.naming.Context context, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> injectionMap, Context catalinaContext, java.lang.ClassLoader containerClassLoader)Create a new DefaultInstanceManager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbackgroundProcess()Called by the component using the InstanceManager periodically to perform any regular maintenance that might be required.voiddestroyInstance(java.lang.Object instance)Destroy the given instance, performing pre-destroy callbacks.protected intgetAnnotationCacheSize()Makes cache size available to unit tests.protected java.lang.Class<?>loadClass(java.lang.String className, java.lang.ClassLoader classLoader)Load a class using the appropriate class loader.protected java.lang.Class<?>loadClassMaybePrivileged(java.lang.String className, java.lang.ClassLoader classLoader)Load a class, performing a privileged check if necessary.protected static voidlookupFieldResource(javax.naming.Context context, java.lang.Object instance, java.lang.reflect.Field field, java.lang.String name, java.lang.Class<?> clazz)Inject resources in specified field.protected static voidlookupMethodResource(javax.naming.Context context, java.lang.Object instance, java.lang.reflect.Method method, java.lang.String name, java.lang.Class<?> clazz)Inject resources in specified method.java.lang.ObjectnewInstance(java.lang.Class<?> clazz)Create a new instance of the given class.voidnewInstance(java.lang.Object o)Perform dependency injection on the given object.java.lang.ObjectnewInstance(java.lang.String className)Create a new instance of the class with the given name.java.lang.ObjectnewInstance(java.lang.String className, java.lang.ClassLoader classLoader)Create a new instance of the class with the given name using the specified class loader.protected voidpopulateAnnotationsCache(java.lang.Class<?> clazz, java.util.Map<java.lang.String,java.lang.String> injections)Make sure that the annotations cache has been populated for the provided class.protected voidpostConstruct(java.lang.Object instance, java.lang.Class<?> clazz)Call postConstruct method on the specified instance recursively from the deepest superclass to actual class.protected voidpreDestroy(java.lang.Object instance, java.lang.Class<?> clazz)Call preDestroy method on the specified instance recursively from the deepest superclass to actual class.protected voidprocessAnnotations(java.lang.Object instance, java.util.Map<java.lang.String,java.lang.String> injections)Inject resources in specified instance.
-
-
-
Field Detail
-
sm
protected static final StringManager sm
The string manager for this package.
-
classLoader
protected final java.lang.ClassLoader classLoader
The class loader for loading application classes.
-
containerClassLoader
protected final java.lang.ClassLoader containerClassLoader
The class loader for loading container classes.
-
privileged
protected final boolean privileged
Whether to perform privileged operations.
-
ignoreAnnotations
protected final boolean ignoreAnnotations
Whether to ignore annotations during injection.
-
-
Constructor Detail
-
DefaultInstanceManager
public DefaultInstanceManager(javax.naming.Context context, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> injectionMap, Context catalinaContext, java.lang.ClassLoader containerClassLoader)Create a new DefaultInstanceManager.- Parameters:
context- The context associated with this instance managerinjectionMap- The injection map for JNDI resource lookupscatalinaContext- The Catalina context for configurationcontainerClassLoader- The container class loader
-
-
Method Detail
-
newInstance
public java.lang.Object newInstance(java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, javax.naming.NamingException, java.lang.InstantiationException, java.lang.IllegalArgumentException, java.lang.NoSuchMethodException, java.lang.SecurityExceptionDescription copied from interface:InstanceManagerCreate a new instance of the given class.- Specified by:
newInstancein interfaceInstanceManager- Parameters:
clazz- The class to instantiate- Returns:
- the new instance
- Throws:
java.lang.IllegalAccessException- if the class or its nullary constructor is not accessiblejava.lang.reflect.InvocationTargetException- if the nullary constructor throws an exceptionjavax.naming.NamingException- if a naming exception is encounteredjava.lang.InstantiationException- if this Class represents an abstract class, an interface, an array class, a primitive type, or voidjava.lang.IllegalArgumentException- if this method is invoked with illegal argumentsjava.lang.NoSuchMethodException- if the nullary method cannot be foundjava.lang.SecurityException- if a security manager, s, is present
-
newInstance
public java.lang.Object newInstance(java.lang.String className) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, javax.naming.NamingException, java.lang.InstantiationException, java.lang.ClassNotFoundException, java.lang.IllegalArgumentException, java.lang.NoSuchMethodException, java.lang.SecurityExceptionDescription copied from interface:InstanceManagerCreate a new instance of the class with the given name.- Specified by:
newInstancein interfaceInstanceManager- Parameters:
className- The name of the class to instantiate- Returns:
- the new instance
- Throws:
java.lang.IllegalAccessException- if the class or its nullary constructor is not accessiblejava.lang.reflect.InvocationTargetException- if the nullary constructor throws an exceptionjavax.naming.NamingException- if a naming exception is encounteredjava.lang.InstantiationException- if this Class represents an abstract class, an interface, an array class, a primitive type, or voidjava.lang.ClassNotFoundException- if the class cannot be foundjava.lang.IllegalArgumentException- if this method is invoked with illegal argumentsjava.lang.NoSuchMethodException- if the nullary method cannot be foundjava.lang.SecurityException- if a security manager, s, is present
-
newInstance
public java.lang.Object newInstance(java.lang.String className, java.lang.ClassLoader classLoader) throws java.lang.IllegalAccessException, javax.naming.NamingException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.ClassNotFoundException, java.lang.IllegalArgumentException, java.lang.NoSuchMethodException, java.lang.SecurityExceptionDescription copied from interface:InstanceManagerCreate a new instance of the class with the given name using the specified class loader.- Specified by:
newInstancein interfaceInstanceManager- Parameters:
className- The fully qualified class nameclassLoader- The class loader to use for loading the class- Returns:
- the new instance
- Throws:
java.lang.IllegalAccessException- if the class or its nullary constructor is not accessiblejavax.naming.NamingException- if a naming exception is encounteredjava.lang.reflect.InvocationTargetException- if the nullary constructor throws an exceptionjava.lang.InstantiationException- if this Class represents an abstract class, an interface, an array class, a primitive type, or voidjava.lang.ClassNotFoundException- if the class cannot be foundjava.lang.IllegalArgumentException- if this method is invoked with illegal argumentsjava.lang.NoSuchMethodException- if the nullary method cannot be foundjava.lang.SecurityException- if a security manager, s, is present
-
newInstance
public void newInstance(java.lang.Object o) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, javax.naming.NamingExceptionDescription copied from interface:InstanceManagerPerform dependency injection on the given object.- Specified by:
newInstancein interfaceInstanceManager- Parameters:
o- The object to inject dependencies into- Throws:
java.lang.IllegalAccessException- if the class or its nullary constructor is not accessiblejava.lang.reflect.InvocationTargetException- if the nullary constructor throws an exceptionjavax.naming.NamingException- if a naming exception is encountered
-
destroyInstance
public void destroyInstance(java.lang.Object instance) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionDescription copied from interface:InstanceManagerDestroy the given instance, performing pre-destroy callbacks.- Specified by:
destroyInstancein interfaceInstanceManager- Parameters:
instance- The object to destroy- Throws:
java.lang.IllegalAccessException- if the class or its nullary constructor is not accessiblejava.lang.reflect.InvocationTargetException- if the nullary constructor throws an exception
-
postConstruct
protected void postConstruct(java.lang.Object instance, java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionCall postConstruct method on the specified instance recursively from the deepest superclass to actual class.- Parameters:
instance- object to call postconstruct methods onclazz- (super) class to examine for postConstruct annotation.- Throws:
java.lang.IllegalAccessException- if postConstruct method is inaccessible.java.lang.reflect.InvocationTargetException- if call fails
-
preDestroy
protected void preDestroy(java.lang.Object instance, java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionCall preDestroy method on the specified instance recursively from the deepest superclass to actual class.- Parameters:
instance- object to call preDestroy methods onclazz- (super) class to examine for preDestroy annotation.- Throws:
java.lang.IllegalAccessException- if preDestroy method is inaccessible.java.lang.reflect.InvocationTargetException- if call fails
-
backgroundProcess
public void backgroundProcess()
Description copied from interface:InstanceManagerCalled by the component using the InstanceManager periodically to perform any regular maintenance that might be required. By default, this method is a NO-OP.- Specified by:
backgroundProcessin interfaceInstanceManager
-
populateAnnotationsCache
protected void populateAnnotationsCache(java.lang.Class<?> clazz, java.util.Map<java.lang.String,java.lang.String> injections) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, javax.naming.NamingExceptionMake sure that the annotations cache has been populated for the provided class.- Parameters:
clazz- clazz to populate annotations forinjections- map of injections for this class from xml deployment descriptor- Throws:
java.lang.IllegalAccessException- if injection target is inaccessiblejavax.naming.NamingException- if value cannot be looked up in jndijava.lang.reflect.InvocationTargetException- if injection fails
-
processAnnotations
protected void processAnnotations(java.lang.Object instance, java.util.Map<java.lang.String,java.lang.String> injections) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, javax.naming.NamingExceptionInject resources in specified instance.- Parameters:
instance- instance to inject intoinjections- map of injections for this class from xml deployment descriptor- Throws:
java.lang.IllegalAccessException- if injection target is inaccessiblejavax.naming.NamingException- if value cannot be looked up in jndijava.lang.reflect.InvocationTargetException- if injection fails
-
getAnnotationCacheSize
protected int getAnnotationCacheSize()
Makes cache size available to unit tests.- Returns:
- the cache size
-
loadClassMaybePrivileged
protected java.lang.Class<?> loadClassMaybePrivileged(java.lang.String className, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundExceptionLoad a class, performing a privileged check if necessary.- Parameters:
className- The name of the class to loadclassLoader- The class loader to use- Returns:
- the loaded class
- Throws:
java.lang.ClassNotFoundException- if the class cannot be found
-
loadClass
protected java.lang.Class<?> loadClass(java.lang.String className, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundExceptionLoad a class using the appropriate class loader.- Parameters:
className- The name of the class to loadclassLoader- The class loader to use- Returns:
- the loaded class
- Throws:
java.lang.ClassNotFoundException- if the class cannot be found
-
lookupFieldResource
protected static void lookupFieldResource(javax.naming.Context context, java.lang.Object instance, java.lang.reflect.Field field, java.lang.String name, java.lang.Class<?> clazz) throws javax.naming.NamingException, java.lang.IllegalAccessExceptionInject resources in specified field.- Parameters:
context- jndi context to extract value frominstance- object to inject intofield- field target for injectionname- jndi name value is bound underclazz- class annotation is defined in- Throws:
java.lang.IllegalAccessException- if field is inaccessiblejavax.naming.NamingException- if value is not accessible in naming context
-
lookupMethodResource
protected static void lookupMethodResource(javax.naming.Context context, java.lang.Object instance, java.lang.reflect.Method method, java.lang.String name, java.lang.Class<?> clazz) throws javax.naming.NamingException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInject resources in specified method.- Parameters:
context- jndi context to extract value frominstance- object to inject intomethod- field target for injectionname- jndi name value is bound underclazz- class annotation is defined in- Throws:
java.lang.IllegalAccessException- if method is inaccessiblejavax.naming.NamingException- if value is not accessible in naming contextjava.lang.reflect.InvocationTargetException- if setter call fails
-
-