# Class ContainerBase

java.lang.Object
org.apache.catalina.util.LifecycleBase
org.apache.catalina.util.LifecycleMBeanBase
org.apache.catalina.core.ContainerBase

All Implemented Interfaces:
  `MBeanRegistration`, `Container`, `JmxEnabled`, `Lifecycle`

Direct Known Subclasses:
  `StandardContext`, `StandardEngine`, `StandardHost`, `StandardWrapper`

public abstract class ContainerBase extends LifecycleMBeanBase implements Container
Abstract implementation of the Container interface, providing common functionality required by nearly every implementation. Classes extending this base class must may implement a replacement for `invoke()`.

All subclasses of this abstract base class will include support for a Pipeline object that defines the processing to be performed for each request received by the `invoke()` method of this class, utilizing the "Chain of Responsibility" design pattern. A subclass should encapsulate its own processing functionality as a `Valve`, and configure this Valve into the pipeline by calling `setBasic()`.

This implementation fires property change events, per the JavaBeans design pattern, for changes in singleton properties. In addition, it fires the following `ContainerEvent` events to listeners who register themselves with `addContainerListener()`:

ContainerEvents fired by this implementation
Type | Data | Description
--- | --- | ---
`addChild` | `Container` | Child container added to this Container.
``pipeline`.addValve` | `Valve` | Valve added to this Container.
`removeChild` | `Container` | Child container removed from this Container.
``pipeline`.removeValve` | `Valve` | Valve removed from this Container.
`start` | `null` | Container was started.
`stop` | `null` | Container was stopped.

Subclasses that fire additional events should document them in the class comments of the implementation class.

Author:
  Craig R. McClanahan

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`protected class`
`ContainerBase.ContainerBackgroundProcessor`
Private runnable class to invoke the backgroundProcess method of this container and its children after a fixed delay.
`protected class`
`ContainerBase.ContainerBackgroundProcessorMonitor`
`protected class`
`ContainerBase.PrivilegedAddChild`
Perform addChild with the permissions of this class. addChild can be called with the XML parser on the stack, this allows the XML parser to have fewer privileges than Tomcat.

### Nested classes/interfaces inherited from interface org.apache.catalina.Lifecycle

`Lifecycle.SingleUse`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected AccessLog`
`accessLog`
The access log to use for requests normally handled by this container that have been handled earlier in the processing chain.
`protected int`
`backgroundProcessorDelay`
The processor delay for this component.
`protected ScheduledFuture<?>`
`backgroundProcessorFuture`
The future allowing control of the background processor.
`protected final HashMap<String, Container>`
`children`
The child Containers belonging to this Container, keyed by name.
`protected Cluster`
`cluster`
The cluster with which this Container is associated.
`protected final List<ContainerListener>`
`listeners`
The container event listeners for this Container.
`protected Log`
`logger`
The Logger implementation with which this Container is associated.
`protected String`
`logName`
Associated logger name.
`protected ScheduledFuture<?>`
`monitorFuture`
`protected String`
`name`
The human-readable name of this Container.
`protected Container`
`parent`
The parent Container to which this Container is a child.
`protected ClassLoader`
`parentClassLoader`
The parent class loader to be configured when we install a Loader.
`protected final Pipeline`
`pipeline`
The Pipeline object with which this Container is associated.
`protected static final StringManager`
`sm`
The string manager for this package.
`protected boolean`
`startChildren`
Will children be started automatically when they are added.
`protected ExecutorService`
`startStopExecutor`
`protected final PropertyChangeSupport`
`support`
The property change support for this component.

### Fields inherited from interface org.apache.catalina.Container

`ADD_CHILD_EVENT, ADD_VALVE_EVENT, REMOVE_CHILD_EVENT, REMOVE_VALVE_EVENT`

### Fields inherited from interface org.apache.catalina.Lifecycle

`AFTER_DESTROY_EVENT, AFTER_INIT_EVENT, AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_DESTROY_EVENT, BEFORE_INIT_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, CONFIGURE_START_EVENT, CONFIGURE_STOP_EVENT, PERIODIC_EVENT, START_EVENT, STOP_EVENT`

## Constructor Summary

Constructors
Constructor
Description
`ContainerBase()`

## Method Summary

Modifier and Type
Method
Description
`void`
`addChild(Container child)`
Add a new child Container to those associated with this Container, if supported.
`void`
`addContainerListener(ContainerListener listener)`
Add a container event listener to this component.
`void`
`addPropertyChangeListener(PropertyChangeListener listener)`
Add a property change listener to this component.
`void`
`addValve(Valve valve)`
Convenience method, intended for use by the digester to simplify the process of adding Valves to containers.
`void`
`backgroundProcess()`
Execute a periodic task, such as reloading, etc.
`protected void`
`destroyInternal()`
Subclasses implement this method to perform any instance destruction required.
`Container`
`findChild(String name)`
Obtain a child Container by name.
`Container[]`
`findChildren()`
Obtain the child Containers associated with this Container.
`ContainerListener[]`
`findContainerListeners()`
Obtain the container listeners associated with this Container.
`void`
`fireContainerEvent(String type, Object data)`
Notify all container event listeners that a particular event has occurred for this Container.
`AccessLog`
`getAccessLog()`
Obtain the AccessLog to use to log a request/response that is destined for this container.
`int`
`getBackgroundProcessorDelay()`
Get the delay between the invocation of the backgroundProcess method on this container and its children.
`File`
`getCatalinaBase()`
Obtain the location of CATALINA_BASE.
`File`
`getCatalinaHome()`
Obtain the location of CATALINA_HOME.
`ObjectName[]`
`getChildren()`
`Cluster`
`getCluster()`
Get the Cluster for this container.
`protected Cluster`
`getClusterInternal()`
`protected String`
`getDomainInternal()`
Method implemented by subclasses to identify the domain in which MBeans should be registered.
`Log`
`getLogger()`
Obtain the log to which events for this container should be logged.
`String`
`getLogName()`
Return the logger name that the container will use.
`String`
`getMBeanKeyProperties()`
Calculate the key properties string to be added to an object's `ObjectName` to indicate that it is associated with this container.
`String`
`getName()`
Return a name string (suitable for use by humans) that describes this Container.
`Container`
`getParent()`
Get the parent container.
`ClassLoader`
`getParentClassLoader()`
Get the parent class loader.
`Pipeline`
`getPipeline()`
Return the Pipeline object that manages the Valves associated with this Container.
`Realm`
`getRealm()`
Obtain the Realm with which this Container is associated.
`protected Realm`
`getRealmInternal()`
`boolean`
`getStartChildren()`
Return if children of this container will be started automatically when they are added to this container.
`int`
`getStartStopThreads()`
Obtain the number of threads available for starting and stopping any children associated with this container.
`void`
`logAccess(Request request, Response response, long time, boolean useDefault)`
Log a request/response that was destined for this container but has been handled earlier in the processing chain so that the request/response still appears in the correct access logs.
`void`
`removeChild(Container child)`
Remove an existing child Container from association with this parent Container.
`void`
`removeContainerListener(ContainerListener listener)`
Remove a container event listener from this component.
`void`
`removePropertyChangeListener(PropertyChangeListener listener)`
Remove a property change listener from this component.
`void`
`setBackgroundProcessorDelay(int delay)`
Set the delay between the invocation of the execute method on this container and its children.
`void`
`setCluster(Cluster cluster)`
Set the Cluster with which this Container is associated.
`void`
`setName(String name)`
Set a name string (suitable for use by humans) that describes this Container.
`void`
`setParent(Container container)`
Set the parent Container to which this Container is being added as a child.
`void`
`setParentClassLoader(ClassLoader parent)`
Set the parent class loader for this component.
`void`
`setRealm(Realm realm)`
Set the Realm with which this Container is associated.
`void`
`setStartChildren(boolean startChildren)`
Set if children of this container will be started automatically when they are added to this container.
`void`
`setStartStopThreads(int startStopThreads)`
Sets the number of threads available for starting and stopping any children associated with this container.
`protected void`
`startInternal()`
Start this component and implement the requirements of `LifecycleBase.startInternal()`.
`protected void`
`stopInternal()`
Stop this component and implement the requirements of `LifecycleBase.stopInternal()`.
`protected void`
`threadStart()`
Start the background thread that will periodically check for session timeouts.
`protected void`
`threadStop()`
Stop the background thread that is periodically checking for session timeouts.
`final String`
`toString()`

### Methods inherited from class org.apache.catalina.util.LifecycleMBeanBase

`getDomain, getObjectName, getObjectNameKeyProperties, initInternal, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister, unregister`

### Methods inherited from class org.apache.catalina.util.LifecycleBase

`addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stop`

### Methods inherited from class java.lang.Object

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

### Methods inherited from interface org.apache.catalina.Container

`getDomain, getObjectName`

### Methods inherited from interface org.apache.catalina.Lifecycle

`addLifecycleListener, destroy, findLifecycleListeners, getState, getStateName, init, removeLifecycleListener, start, stop`

## Field Details

### children

protected final HashMap<String, Container> children
The child Containers belonging to this Container, keyed by name.

### backgroundProcessorDelay

protected int backgroundProcessorDelay
The processor delay for this component.

### backgroundProcessorFuture

protected ScheduledFuture<?> backgroundProcessorFuture
The future allowing control of the background processor.

### monitorFuture

protected ScheduledFuture<?> monitorFuture

### listeners

protected final List<ContainerListener> listeners
The container event listeners for this Container. Implemented as a CopyOnWriteArrayList since listeners may invoke methods to add/remove themselves or other listeners and with a ReadWriteLock that would trigger a deadlock.

### logger

protected Log logger
The Logger implementation with which this Container is associated.

### logName

protected String logName
Associated logger name.

### cluster

protected Cluster cluster
The cluster with which this Container is associated.

### name

protected String name
The human-readable name of this Container.

### parent

protected Container parent
The parent Container to which this Container is a child.

### parentClassLoader

protected ClassLoader parentClassLoader
The parent class loader to be configured when we install a Loader.

### pipeline

protected final Pipeline pipeline
The Pipeline object with which this Container is associated.

### sm

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

### startChildren

protected boolean startChildren
Will children be started automatically when they are added.

### support

protected final PropertyChangeSupport support
The property change support for this component.

### accessLog

protected volatile AccessLog accessLog
The access log to use for requests normally handled by this container that have been handled earlier in the processing chain.

### startStopExecutor

protected ExecutorService startStopExecutor

## Constructor Details

### ContainerBase

public ContainerBase()

## Method Details

### getStartStopThreads

public int getStartStopThreads()
Description copied from interface: `Container`
Obtain the number of threads available for starting and stopping any children associated with this container. This allows start/stop calls to children to be processed in parallel.

Specified by:
  `getStartStopThreads` in interface `Container`
Returns:
  The currently configured number of threads used to start/stop children associated with this container

### setStartStopThreads

public void setStartStopThreads(int startStopThreads)
Description copied from interface: `Container`
Sets the number of threads available for starting and stopping any children associated with this container. This allows start/stop calls to children to be processed in parallel.

Specified by:
  `setStartStopThreads` in interface `Container`
Parameters:
  `startStopThreads` - The new number of threads to be used

### getBackgroundProcessorDelay

public int getBackgroundProcessorDelay()
Description copied from interface: `Container`
Get the delay between the invocation of the backgroundProcess method on this container and its children. Child containers will not be invoked if their delay value is positive (which would mean they are using their own thread). Setting this to a positive value will cause a thread to be spawned. After waiting the specified amount of time, the thread will invoke the `Container.backgroundProcess()` method on this container and all children with non-positive delay values.

Specified by:
  `getBackgroundProcessorDelay` in interface `Container`
Returns:
  The delay between the invocation of the backgroundProcess method on this container and its children. A non-positive value indicates that background processing will be managed by the parent.

### setBackgroundProcessorDelay

public void setBackgroundProcessorDelay(int delay)
Description copied from interface: `Container`
Set the delay between the invocation of the execute method on this container and its children.

Specified by:
  `setBackgroundProcessorDelay` in interface `Container`
Parameters:
  `delay` - The delay in seconds between the invocation of backgroundProcess methods

### getLogger

public Log getLogger()
Description copied from interface: `Container`
Obtain the log to which events for this container should be logged.

Specified by:
  `getLogger` in interface `Container`
Returns:
  The Logger with which this Container is associated. If there is no associated Logger, return the Logger associated with the parent Container (if any); otherwise return `null`.

### getLogName

public String getLogName()
Description copied from interface: `Container`
Return the logger name that the container will use.

Specified by:
  `getLogName` in interface `Container`
Returns:
  the abbreviated name of this container for logging messages

### getCluster

public Cluster getCluster()
Description copied from interface: `Container`
Get the Cluster for this container.

Specified by:
  `getCluster` in interface `Container`
Returns:
  The Cluster with which this Container is associated. If there is no associated Cluster, return the Cluster associated with our parent Container (if any); otherwise return `null`.

### getClusterInternal

protected Cluster getClusterInternal()

### setCluster

public void setCluster(Cluster cluster)
Description copied from interface: `Container`
Set the Cluster with which this Container is associated.

Specified by:
  `setCluster` in interface `Container`
Parameters:
  `cluster` - the Cluster with which this Container is associated.

### getName

public String getName()
Description copied from interface: `Container`
Return a name string (suitable for use by humans) that describes this Container. Within the set of child containers belonging to a particular parent, Container names must be unique.

Specified by:
  `getName` in interface `Container`
Returns:
  The human readable name of this container.

### setName

public void setName(String name)
Description copied from interface: `Container`
Set a name string (suitable for use by humans) that describes this Container. Within the set of child containers belonging to a particular parent, Container names must be unique.

Specified by:
  `setName` in interface `Container`
Parameters:
  `name` - New name of this container

### getStartChildren

public boolean getStartChildren()
Return if children of this container will be started automatically when they are added to this container.

Returns:
  `true` if the children will be started

### setStartChildren

public void setStartChildren(boolean startChildren)
Set if children of this container will be started automatically when they are added to this container.

Parameters:
  `startChildren` - New value of the startChildren flag

### getParent

public Container getParent()
Description copied from interface: `Container`
Get the parent container.

Specified by:
  `getParent` in interface `Container`
Returns:
  Return the Container for which this Container is a child, if there is one. If there is no defined parent, return `null`.

### setParent

public void setParent(Container container)
Description copied from interface: `Container`
Set the parent Container to which this Container is being added as a child. This Container may refuse to become attached to the specified Container by throwing an exception.

Specified by:
  `setParent` in interface `Container`
Parameters:
  `container` - Container to which this Container is being added as a child

### getParentClassLoader

public ClassLoader getParentClassLoader()
Description copied from interface: `Container`
Get the parent class loader.

Specified by:
  `getParentClassLoader` in interface `Container`
Returns:
  the parent class loader for this component. If not set, return `Container.getParent()`.`Container.getParentClassLoader()`. If no parent has been set, return the system class loader.

### setParentClassLoader

public void setParentClassLoader(ClassLoader parent)
Description copied from interface: `Container`
Set the parent class loader for this component. For `Context`s this call is meaningful only before a Loader has been configured, and the specified value (if non-null) should be passed as an argument to the class loader constructor.

Specified by:
  `setParentClassLoader` in interface `Container`
Parameters:
  `parent` - The new parent class loader

### getPipeline

public Pipeline getPipeline()
Description copied from interface: `Container`
Return the Pipeline object that manages the Valves associated with this Container.

Specified by:
  `getPipeline` in interface `Container`
Returns:
  The Pipeline

### getRealm

public Realm getRealm()
Description copied from interface: `Container`
Obtain the Realm with which this Container is associated.

Specified by:
  `getRealm` in interface `Container`
Returns:
  The associated Realm; if there is no associated Realm, the Realm associated with the parent Container (if any); otherwise return `null`.

### getRealmInternal

protected Realm getRealmInternal()

### setRealm

public void setRealm(Realm realm)
Description copied from interface: `Container`
Set the Realm with which this Container is associated.

Specified by:
  `setRealm` in interface `Container`
Parameters:
  `realm` - The newly associated Realm

### addChild

public void addChild(Container child)
Description copied from interface: `Container`
Add a new child Container to those associated with this Container, if supported. Prior to adding this Container to the set of children, the child's `setParent()` method must be called, with this Container as an argument. This method may throw an `IllegalArgumentException` if this Container chooses not to be attached to the specified Container, in which case it is not added

Specified by:
  `addChild` in interface `Container`
Parameters:
  `child` - New child Container to be added

### addContainerListener

public void addContainerListener(ContainerListener listener)
Description copied from interface: `Container`
Add a container event listener to this component.

Specified by:
  `addContainerListener` in interface `Container`
Parameters:
  `listener` - The listener to add

### addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Description copied from interface: `Container`
Add a property change listener to this component.

Specified by:
  `addPropertyChangeListener` in interface `Container`
Parameters:
  `listener` - The listener to add

### findChild

public Container findChild(String name)
Description copied from interface: `Container`
Obtain a child Container by name.

Specified by:
  `findChild` in interface `Container`
Parameters:
  `name` - Name of the child Container to be retrieved
Returns:
  The child Container with the given name or `null` if no such child exists.

### findChildren

public Container[] findChildren()
Description copied from interface: `Container`
Obtain the child Containers associated with this Container.

Specified by:
  `findChildren` in interface `Container`
Returns:
  An array containing all children of this container. If this Container has no children, a zero-length array is returned.

### findContainerListeners

public ContainerListener[] findContainerListeners()
Description copied from interface: `Container`
Obtain the container listeners associated with this Container.

Specified by:
  `findContainerListeners` in interface `Container`
Returns:
  An array containing the container listeners associated with this Container. If this Container has no registered container listeners, a zero-length array is returned.

### removeChild

public void removeChild(Container child)
Description copied from interface: `Container`
Remove an existing child Container from association with this parent Container.

Specified by:
  `removeChild` in interface `Container`
Parameters:
  `child` - Existing child Container to be removed

### removeContainerListener

public void removeContainerListener(ContainerListener listener)
Description copied from interface: `Container`
Remove a container event listener from this component.

Specified by:
  `removeContainerListener` in interface `Container`
Parameters:
  `listener` - The listener to remove

### removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Description copied from interface: `Container`
Remove a property change listener from this component.

Specified by:
  `removePropertyChangeListener` in interface `Container`
Parameters:
  `listener` - The listener to remove

### startInternal

protected void startInternal() throws LifecycleException
Start this component and implement the requirements of `LifecycleBase.startInternal()`.

Specified by:
  `startInternal` in class `LifecycleBase`
Throws:
  `LifecycleException` - if this component detects a fatal error that prevents this component from being used

### stopInternal

protected void stopInternal() throws LifecycleException
Stop this component and implement the requirements of `LifecycleBase.stopInternal()`.

Specified by:
  `stopInternal` in class `LifecycleBase`
Throws:
  `LifecycleException` - if this component detects a fatal error that prevents this component from being used

### destroyInternal

protected void destroyInternal() throws LifecycleException
Description copied from class: `LifecycleBase`
Subclasses implement this method to perform any instance destruction required.

Overrides:
  `destroyInternal` in class `LifecycleMBeanBase`
Throws:
  `LifecycleException` - If the destruction fails

### logAccess

public void logAccess(Request request, Response response, long time, boolean useDefault)
Description copied from interface: `Container`
Log a request/response that was destined for this container but has been handled earlier in the processing chain so that the request/response still appears in the correct access logs.

Specified by:
  `logAccess` in interface `Container`
Parameters:
  `request` - Request (associated with the response) to log
  `response` - Response (associated with the request) to log
  `time` - Time taken to process the request/response in milliseconds (use 0 if not known)
  `useDefault` - Flag that indicates that the request/response should be logged in the engine's default access log

### getAccessLog

public AccessLog getAccessLog()
Description copied from interface: `Container`
Obtain the AccessLog to use to log a request/response that is destined for this container. This is typically used when the request/response was handled (and rejected) earlier in the processing chain so that the request/response still appears in the correct access logs.

Specified by:
  `getAccessLog` in interface `Container`
Returns:
  The AccessLog to use for a request/response destined for this container

### addValve

public void addValve(Valve valve)
Convenience method, intended for use by the digester to simplify the process of adding Valves to containers. See `Pipeline.addValve(Valve)` for full details. Components other than the digester should use `getPipeline()`.`addValve(Valve)` in case a future implementation provides an alternative method for the digester to use.

Parameters:
  `valve` - Valve to be added
Throws:
  `IllegalArgumentException` - if this Container refused to accept the specified Valve
  `IllegalArgumentException` - if the specified Valve refuses to be associated with this Container
  `IllegalStateException` - if the specified Valve is already associated with a different Container

### backgroundProcess

public void backgroundProcess()
Description copied from interface: `Container`
Execute a periodic task, such as reloading, etc. This method will be invoked inside the classloading context of this container. Unexpected throwables will be caught and logged.

Specified by:
  `backgroundProcess` in interface `Container`

### getCatalinaBase

public File getCatalinaBase()
Description copied from interface: `Container`
Obtain the location of CATALINA_BASE.

Specified by:
  `getCatalinaBase` in interface `Container`
Returns:
  The location of CATALINA_BASE.

### getCatalinaHome

public File getCatalinaHome()
Description copied from interface: `Container`
Obtain the location of CATALINA_HOME.

Specified by:
  `getCatalinaHome` in interface `Container`
Returns:
  The location of CATALINA_HOME.

### fireContainerEvent

public void fireContainerEvent(String type, Object data)
Description copied from interface: `Container`
Notify all container event listeners that a particular event has occurred for this Container. The default implementation performs this notification synchronously using the calling thread.

Specified by:
  `fireContainerEvent` in interface `Container`
Parameters:
  `type` - Event type
  `data` - Event data

### getDomainInternal

protected String getDomainInternal()
Description copied from class: `LifecycleMBeanBase`
Method implemented by subclasses to identify the domain in which MBeans should be registered.

Specified by:
  `getDomainInternal` in class `LifecycleMBeanBase`
Returns:
  The name of the domain to use to register MBeans.

### getMBeanKeyProperties

public String getMBeanKeyProperties()
Description copied from interface: `Container`
Calculate the key properties string to be added to an object's `ObjectName` to indicate that it is associated with this container.

Specified by:
  `getMBeanKeyProperties` in interface `Container`
Returns:
  A string suitable for appending to the ObjectName

### getChildren

public ObjectName[] getChildren()

### threadStart

protected void threadStart()
Start the background thread that will periodically check for session timeouts.

### threadStop

protected void threadStop()
Stop the background thread that is periodically checking for session timeouts.

### toString

public final String toString()

Overrides:
  `toString` in class `Object`