# Class DefaultInstanceManager

java.lang.Object
org.apache.catalina.core.DefaultInstanceManager

All Implemented Interfaces:
  `InstanceManager`

public class DefaultInstanceManager extends Object implements InstanceManager
Default implementation of `InstanceManager` that handles annotation-based dependency injection for EJB, JPA, and web service references.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final ClassLoader`
`classLoader`
The class loader for loading application classes.
`protected final ClassLoader`
`containerClassLoader`
The class loader for loading container classes.
`protected final boolean`
`ignoreAnnotations`
Whether to ignore annotations during injection.
`protected final boolean`
`privileged`
Whether to perform privileged operations.
`protected static final StringManager`
`sm`
The string manager for this package.

## Constructor Summary

Constructors
Constructor
Description
`DefaultInstanceManager(Context context, Map<String, Map<String,String>> injectionMap, Context catalinaContext, ClassLoader containerClassLoader)`
Create a new DefaultInstanceManager.

## Method Summary

Modifier and Type
Method
Description
`void`
`backgroundProcess()`
Called by the component using the InstanceManager periodically to perform any regular maintenance that might be required.
`void`
`destroyInstance(Object instance)`
Destroy the given instance, performing pre-destroy callbacks.
`protected int`
`getAnnotationCacheSize()`
Makes 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 void`
`lookupFieldResource(Context context, Object instance, Field field, String name, Class<?> clazz)`
Inject resources in specified field.
`protected static void`
`lookupMethodResource(Context context, Object instance, Method method, String name, Class<?> clazz)`
Inject resources in specified method.
`Object`
`newInstance(Class<?> clazz)`
Create a new instance of the given class.
`void`
`newInstance(Object o)`
Perform dependency injection on the given object.
`Object`
`newInstance(String className)`
Create a new instance of the class with the given name.
`Object`
`newInstance(String className, ClassLoader classLoader)`
Create a new instance of the class with the given name using the specified class loader.
`protected void`
`populateAnnotationsCache(Class<?> clazz, Map<String,String> injections)`
Make sure that the annotations cache has been populated for the provided class.
`protected void`
`postConstruct(Object instance, Class<?> clazz)`
Call postConstruct method on the specified instance recursively from the deepest superclass to actual class.
`protected void`
`preDestroy(Object instance, Class<?> clazz)`
Call preDestroy method on the specified instance recursively from the deepest superclass to actual class.
`protected void`
`processAnnotations(Object instance, Map<String,String> injections)`
Inject resources in specified instance.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### sm

protected static final StringManager sm
The string manager for this package.

### classLoader

protected final ClassLoader classLoader
The class loader for loading application classes.

### containerClassLoader

protected final 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 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 manager
  `injectionMap` - The injection map for JNDI resource lookups
  `catalinaContext` - The Catalina context for configuration
  `containerClassLoader` - The container class loader

## Method Details

### newInstance

public Object newInstance(Class<?> clazz) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, IllegalArgumentException, NoSuchMethodException, SecurityException
Description copied from interface: `InstanceManager`
Create a new instance of the given class.

Specified by:
  `newInstance` in interface `InstanceManager`
Parameters:
  `clazz` - The class to instantiate
Returns:
  the new instance
Throws:
  `IllegalAccessException` - if the class or its nullary constructor is not accessible
  `InvocationTargetException` - if the nullary constructor throws an exception
  `NamingException` - if a naming exception is encountered
  `InstantiationException` - if this Class represents an abstract class, an interface, an array class, a primitive type, or void
  `IllegalArgumentException` - if this method is invoked with illegal arguments
  `NoSuchMethodException` - if the nullary method cannot be found
  `SecurityException` - 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: `InstanceManager`
Create a new instance of the class with the given name.

Specified by:
  `newInstance` in interface `InstanceManager`
Parameters:
  `className` - The name of the class to instantiate
Returns:
  the new instance
Throws:
  `IllegalAccessException` - if the class or its nullary constructor is not accessible
  `InvocationTargetException` - if the nullary constructor throws an exception
  `NamingException` - if a naming exception is encountered
  `InstantiationException` - if this Class represents an abstract class, an interface, an array class, a primitive type, or void
  `ClassNotFoundException` - if the class cannot be found
  `IllegalArgumentException` - if this method is invoked with illegal arguments
  `NoSuchMethodException` - if the nullary method cannot be found
  `SecurityException` - 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: `InstanceManager`
Create a new instance of the class with the given name using the specified class loader.

Specified by:
  `newInstance` in interface `InstanceManager`
Parameters:
  `className` - The fully qualified class name
  `classLoader` - The class loader to use for loading the class
Returns:
  the new instance
Throws:
  `IllegalAccessException` - if the class or its nullary constructor is not accessible
  `NamingException` - if a naming exception is encountered
  `InvocationTargetException` - if the nullary constructor throws an exception
  `InstantiationException` - if this Class represents an abstract class, an interface, an array class, a primitive type, or void
  `ClassNotFoundException` - if the class cannot be found
  `IllegalArgumentException` - if this method is invoked with illegal arguments
  `NoSuchMethodException` - if the nullary method cannot be found
  `SecurityException` - if a security manager, s, is present

### newInstance

public void newInstance(Object o) throws IllegalAccessException, InvocationTargetException, NamingException
Description copied from interface: `InstanceManager`
Perform dependency injection on the given object.

Specified by:
  `newInstance` in interface `InstanceManager`
Parameters:
  `o` - The object to inject dependencies into
Throws:
  `IllegalAccessException` - if the class or its nullary constructor is not accessible
  `InvocationTargetException` - if the nullary constructor throws an exception
  `NamingException` - if a naming exception is encountered

### destroyInstance

public void destroyInstance(Object instance) throws IllegalAccessException, InvocationTargetException
Description copied from interface: `InstanceManager`
Destroy the given instance, performing pre-destroy callbacks.

Specified by:
  `destroyInstance` in interface `InstanceManager`
Parameters:
  `instance` - The object to destroy
Throws:
  `IllegalAccessException` - if the class or its nullary constructor is not accessible
  `InvocationTargetException` - 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 on
  `clazz` - (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 on
  `clazz` - (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: `InstanceManager`
Called 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:
  `backgroundProcess` in interface `InstanceManager`

### populateAnnotationsCache

protected void populateAnnotationsCache(Class<?> clazz, Map<String,String> injections) throws IllegalAccessException, InvocationTargetException, NamingException
Make sure that the annotations cache has been populated for the provided class.

Parameters:
  `clazz` - clazz to populate annotations for
  `injections` - map of injections for this class from xml deployment descriptor
Throws:
  `IllegalAccessException` - if injection target is inaccessible
  `NamingException` - if value cannot be looked up in jndi
  `InvocationTargetException` - if injection fails

### processAnnotations

protected void processAnnotations(Object instance, Map<String,String> injections) throws IllegalAccessException, InvocationTargetException, NamingException
Inject resources in specified instance.

Parameters:
  `instance` - instance to inject into
  `injections` - map of injections for this class from xml deployment descriptor
Throws:
  `IllegalAccessException` - if injection target is inaccessible
  `NamingException` - if value cannot be looked up in jndi
  `InvocationTargetException` - 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 load
  `classLoader` - 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 load
  `classLoader` - 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 from
  `instance` - object to inject into
  `field` - field target for injection
  `name` - jndi name value is bound under
  `clazz` - class annotation is defined in
Throws:
  `IllegalAccessException` - if field is inaccessible
  `NamingException` - 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 from
  `instance` - object to inject into
  `method` - field target for injection
  `name` - jndi name value is bound under
  `clazz` - class annotation is defined in
Throws:
  `IllegalAccessException` - if method is inaccessible
  `NamingException` - if value is not accessible in naming context
  `InvocationTargetException` - if setter call fails