Class DefaultInstanceManager
java.lang.Object
org.apache.catalina.core.DefaultInstanceManager
- All Implemented Interfaces:
InstanceManager
Default implementation of
InstanceManager that handles annotation-based
dependency injection for EJB, JPA, and web service references.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ClassLoaderThe class loader for loading application classes.protected final ClassLoaderThe class loader for loading container classes.protected final booleanWhether to ignore annotations during injection.protected final booleanWhether metadata is complete (no annotation scanning required).protected final booleanWhether to perform privileged operations.protected static final StringManagerThe string manager for this package. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultInstanceManager(Context context, Map<String, Map<String, String>> injectionMap, Context catalinaContext, ClassLoader containerClassLoader) Create a new DefaultInstanceManager. -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled by the component using the InstanceManager periodically to perform any regular maintenance that might be required.voiddestroyInstance(Object instance) Destroy the given instance, performing pre-destroy callbacks.protected intMakes cache size available to unit tests.protected Class<?> loadClass(String className, ClassLoader classLoader) Load a class using the appropriate class loader.protected Class<?> loadClassMaybePrivileged(String className, ClassLoader classLoader) Load a class, performing a privileged check if necessary.protected static voidInject resources in specified field.protected static voidInject resources in specified method.newInstance(Class<?> clazz) Create a new instance of the given class.voidPerform dependency injection on the given object.newInstance(String className) Create a new instance of the class with the given name.newInstance(String className, ClassLoader classLoader) Create a new instance of the class with the given name using the specified class loader.protected voidpopulateAnnotationsCache(Class<?> clazz, Map<String, String> injections) Make sure that the annotations cache has been populated for the provided class.protected voidpostConstruct(Object instance, Class<?> clazz) Call postConstruct method on the specified instance recursively from the deepest superclass to actual class.protected voidpreDestroy(Object instance, Class<?> clazz) Call preDestroy method on the specified instance recursively from the deepest superclass to actual class.protected voidprocessAnnotations(Object instance, Map<String, String> injections) Inject resources in specified instance.
-
Field Details
-
sm
The string manager for this package. -
classLoader
The class loader for loading application classes. -
containerClassLoader
The class loader for loading container classes. -
privileged
protected final boolean privilegedWhether to perform privileged operations. -
ignoreAnnotations
protected final boolean ignoreAnnotationsWhether to ignore annotations during injection. -
metadataComplete
protected final boolean metadataCompleteWhether metadata is complete (no annotation scanning required).
-
-
Constructor Details
-
DefaultInstanceManager
public DefaultInstanceManager(Context context, Map<String, Map<String, String>> injectionMap, Context catalinaContext, 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 Details
-
newInstance
public Object newInstance(Class<?> clazz) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, IllegalArgumentException, NoSuchMethodException, SecurityException Description 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:
IllegalAccessException- if the class or its nullary constructor is not accessibleInvocationTargetException- if the nullary constructor throws an exceptionNamingException- if a naming exception is encounteredInstantiationException- if this Class represents an abstract class, an interface, an array class, a primitive type, or voidIllegalArgumentException- if this method is invoked with illegal argumentsNoSuchMethodException- if the nullary method cannot be foundSecurityException- if a security manager, s, is present
-
newInstance
public Object newInstance(String className) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException, IllegalArgumentException, NoSuchMethodException, SecurityException Description 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:
IllegalAccessException- if the class or its nullary constructor is not accessibleInvocationTargetException- if the nullary constructor throws an exceptionNamingException- if a naming exception is encounteredInstantiationException- if this Class represents an abstract class, an interface, an array class, a primitive type, or voidClassNotFoundException- if the class cannot be foundIllegalArgumentException- if this method is invoked with illegal argumentsNoSuchMethodException- if the nullary method cannot be foundSecurityException- if a security manager, s, is present
-
newInstance
public Object newInstance(String className, ClassLoader classLoader) throws IllegalAccessException, NamingException, InvocationTargetException, InstantiationException, ClassNotFoundException, IllegalArgumentException, NoSuchMethodException, SecurityException Description 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:
IllegalAccessException- if the class or its nullary constructor is not accessibleNamingException- if a naming exception is encounteredInvocationTargetException- if the nullary constructor throws an exceptionInstantiationException- if this Class represents an abstract class, an interface, an array class, a primitive type, or voidClassNotFoundException- if the class cannot be foundIllegalArgumentException- if this method is invoked with illegal argumentsNoSuchMethodException- if the nullary method cannot be foundSecurityException- if a security manager, s, is present
-
newInstance
public void newInstance(Object o) throws IllegalAccessException, InvocationTargetException, NamingException Description copied from interface:InstanceManagerPerform dependency injection on the given object.- Specified by:
newInstancein interfaceInstanceManager- Parameters:
o- The object to inject dependencies into- Throws:
IllegalAccessException- if the class or its nullary constructor is not accessibleInvocationTargetException- if the nullary constructor throws an exceptionNamingException- if a naming exception is encountered
-
destroyInstance
public void destroyInstance(Object instance) throws IllegalAccessException, InvocationTargetException Description copied from interface:InstanceManagerDestroy the given instance, performing pre-destroy callbacks.- Specified by:
destroyInstancein interfaceInstanceManager- Parameters:
instance- The object to destroy- Throws:
IllegalAccessException- if the class or its nullary constructor is not accessibleInvocationTargetException- if the nullary constructor throws an exception
-
postConstruct
protected void postConstruct(Object instance, Class<?> clazz) throws IllegalAccessException, InvocationTargetException Call 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:
IllegalAccessException- if postConstruct method is inaccessible.InvocationTargetException- if call fails
-
preDestroy
protected void preDestroy(Object instance, Class<?> clazz) throws IllegalAccessException, InvocationTargetException Call 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:
IllegalAccessException- if preDestroy method is inaccessible.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(Class<?> clazz, Map<String, String> injections) throws IllegalAccessException, InvocationTargetException, 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:
IllegalAccessException- if injection target is inaccessibleNamingException- if value cannot be looked up in jndiInvocationTargetException- if injection fails
-
processAnnotations
protected void processAnnotations(Object instance, Map<String, String> injections) throws IllegalAccessException, InvocationTargetException, NamingExceptionInject resources in specified instance.- Parameters:
instance- instance to inject intoinjections- map of injections for this class from xml deployment descriptor- Throws:
IllegalAccessException- if injection target is inaccessibleNamingException- if value cannot be looked up in jndiInvocationTargetException- if injection fails
-
getAnnotationCacheSize
protected int getAnnotationCacheSize()Makes cache size available to unit tests.- Returns:
- the cache size
-
loadClassMaybePrivileged
protected Class<?> loadClassMaybePrivileged(String className, ClassLoader classLoader) throws ClassNotFoundException Load 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:
ClassNotFoundException- if the class cannot be found
-
loadClass
protected Class<?> loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException Load 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:
ClassNotFoundException- if the class cannot be found
-
lookupFieldResource
protected static void lookupFieldResource(Context context, Object instance, Field field, String name, Class<?> clazz) throws NamingException, IllegalAccessException Inject 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:
IllegalAccessException- if field is inaccessibleNamingException- if value is not accessible in naming context
-
lookupMethodResource
protected static void lookupMethodResource(Context context, Object instance, Method method, String name, Class<?> clazz) throws NamingException, IllegalAccessException, InvocationTargetException Inject 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:
IllegalAccessException- if method is inaccessibleNamingException- if value is not accessible in naming contextInvocationTargetException- if setter call fails
-