org.picocontainer.gems.containers
Class ReusablePicoContainer

java.lang.Object
  extended by org.picocontainer.DefaultPicoContainer
      extended by org.picocontainer.gems.containers.ReusablePicoContainer
All Implemented Interfaces:
Serializable, ComponentMonitorStrategy, Disposable, MutablePicoContainer, PicoContainer, Startable

public class ReusablePicoContainer
extends DefaultPicoContainer

Normal PicoContainers are meant to be created, started, stopped, disposed and garbage collected. The goal of this container is to reduce the number of registration calls (and therefore objects created) in areas where performance is key (for example, this might be used in NanoContainer request containers).

It accomplishes its goal in two ways:
(1) Once a container is disposed of, start() may be called again, allowing for recycling of the container. (This is default behavior with a picocontainer)

(2) All instance registrations will be unregistered when stop is called. (For example, HttpServletRequest would be removed), and all component adapter instance values are flushed.

Container Storage

It is still up to the builder of this container to decide where to store its reference. Since it is reusable, it needs to be stored someplace that doesn't easily expire. Probably the most common storage location would be ThreadLocal storage. HttpSession might be another valid storage location.

Author:
Michael Rimov
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.picocontainer.DefaultPicoContainer
componentFactory, componentMonitor, lifecycleStrategy, orderedComponentAdapters
 
Constructor Summary
ReusablePicoContainer()
           
ReusablePicoContainer(ComponentFactory componentFactory)
           
ReusablePicoContainer(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent)
           
ReusablePicoContainer(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, ComponentMonitor componentMonitor)
           
ReusablePicoContainer(ComponentFactory componentFactory, PicoContainer parent)
           
ReusablePicoContainer(ComponentMonitor monitor)
           
ReusablePicoContainer(ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy, PicoContainer parent)
           
ReusablePicoContainer(ComponentMonitor monitor, PicoContainer parent)
           
ReusablePicoContainer(LifecycleStrategy lifecycleStrategy, PicoContainer parent)
           
ReusablePicoContainer(PicoContainer parent)
           
 
Method Summary
 MutablePicoContainer addAdapter(ComponentAdapter<?> componentAdapter)
          Register a component via a ComponentAdapter.
 MutablePicoContainer addAdapter(ComponentAdapter<?> componentAdapter, Properties properties)
          
 MutablePicoContainer addComponent(Object implOrInstance)
          Register an arbitrary object.
 MutablePicoContainer addComponent(Object componentKey, Object componentImplementationOrInstance, Parameter... parameters)
          Register a component and creates specific instructions on which constructor to use, along with which components and/or constants to provide as constructor arguments.
 MutablePicoContainer makeChildContainer()
          Make a child container, using the same implementation of MutablePicoContainer as the parent.
<T> ComponentAdapter<T>
removeComponent(Object componentKey)
          Unregister a component by key.
<T> ComponentAdapter<T>
removeComponentByInstance(T componentInstance)
          Unregister a component by instance.
 void stop()
          Stop the components of this PicoContainer and all its logical child containers.
 
Methods inherited from class org.picocontainer.DefaultPicoContainer
accept, addAdapterInternal, addChildContainer, addConfig, as, change, changeMonitor, currentMonitor, dispose, getComponent, getComponent, getComponent, getComponent, getComponent, getComponentAdapter, getComponentAdapter, getComponentAdapter, getComponentAdapters, getComponentAdapters, getComponentAdapters, getComponentKeyToAdapterCache, getComponents, getComponents, getModifiableComponentAdapterList, getOrderedComponentAdapters, getParent, removeChildContainer, setLifecycleState, setName, start, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ReusablePicoContainer

public ReusablePicoContainer()

ReusablePicoContainer

public ReusablePicoContainer(ComponentFactory componentFactory,
                             LifecycleStrategy lifecycleStrategy,
                             PicoContainer parent,
                             ComponentMonitor componentMonitor)

ReusablePicoContainer

public ReusablePicoContainer(ComponentFactory componentFactory,
                             LifecycleStrategy lifecycleStrategy,
                             PicoContainer parent)

ReusablePicoContainer

public ReusablePicoContainer(ComponentFactory componentFactory,
                             PicoContainer parent)

ReusablePicoContainer

public ReusablePicoContainer(ComponentFactory componentFactory)

ReusablePicoContainer

public ReusablePicoContainer(ComponentMonitor monitor,
                             LifecycleStrategy lifecycleStrategy,
                             PicoContainer parent)

ReusablePicoContainer

public ReusablePicoContainer(ComponentMonitor monitor,
                             PicoContainer parent)

ReusablePicoContainer

public ReusablePicoContainer(ComponentMonitor monitor)

ReusablePicoContainer

public ReusablePicoContainer(LifecycleStrategy lifecycleStrategy,
                             PicoContainer parent)

ReusablePicoContainer

public ReusablePicoContainer(PicoContainer parent)
Method Detail

addComponent

public MutablePicoContainer addComponent(Object componentKey,
                                         Object componentImplementationOrInstance,
                                         Parameter... parameters)
                                  throws PicoCompositionException
Description copied from class: DefaultPicoContainer
Register a component and creates specific instructions on which constructor to use, along with which components and/or constants to provide as constructor arguments. These "directives" are provided through an array of Parameter objects. Parameter[0] correspondes to the first constructor argument, Parameter[N] corresponds to the N+1th constructor argument.

Tips for Parameter usage


addComponent

public MutablePicoContainer addComponent(Object implOrInstance)
                                  throws PicoCompositionException
Description copied from class: DefaultPicoContainer
Register an arbitrary object. The class of the object will be used as a key. Calling this method is equivalent to calling addComponent(componentImplementation, componentImplementation). The returned ComponentAdapter will be an InstanceAdapter.

Specified by:
addComponent in interface MutablePicoContainer
Overrides:
addComponent in class DefaultPicoContainer
Parameters:
implOrInstance - Component implementation or instance
Returns:
the same instance of MutablePicoContainer
Throws:
PicoCompositionException - if registration fails.

stop

public void stop()
Description copied from class: DefaultPicoContainer
Stop the components of this PicoContainer and all its logical child containers. The stopping of the child containers is only attempted for those that have been started, possibly not successfully. The lifecycle operation is delegated to the component adapter, if it is an instance of lifecycle manager. The actual lifecycle strategy supported depends on the concrete implementation of the adapter.

Specified by:
stop in interface Startable
Overrides:
stop in class DefaultPicoContainer
See Also:
Behavior, LifecycleStrategy, DefaultPicoContainer.makeChildContainer(), DefaultPicoContainer.addChildContainer(PicoContainer), DefaultPicoContainer.removeChildContainer(PicoContainer)

addAdapter

public MutablePicoContainer addAdapter(ComponentAdapter<?> componentAdapter,
                                       Properties properties)
Description copied from class: DefaultPicoContainer

Overrides:
addAdapter in class DefaultPicoContainer

addAdapter

public MutablePicoContainer addAdapter(ComponentAdapter<?> componentAdapter)
Description copied from class: DefaultPicoContainer
Register a component via a ComponentAdapter. Use this if you need fine grained control over what ComponentAdapter to use for a specific component. The adapter will be wrapped in whatever behaviors that the the container has been set up with. If you want to bypass that behavior for the adapter you are adding, you should use Characteristics.NONE like so pico.as(Characteristics.NONE).addAdapter(...) This method can be used to override the ComponentAdapter created by the ComponentFactory passed to the constructor of this container.

Specified by:
addAdapter in interface MutablePicoContainer
Overrides:
addAdapter in class DefaultPicoContainer
Parameters:
componentAdapter - the adapter
Returns:
the same instance of MutablePicoContainer

removeComponent

public <T> ComponentAdapter<T> removeComponent(Object componentKey)
Description copied from class: DefaultPicoContainer
Unregister a component by key.

Specified by:
removeComponent in interface MutablePicoContainer
Overrides:
removeComponent in class DefaultPicoContainer
Parameters:
componentKey - key of the component to unregister.
Returns:
the ComponentAdapter that was associated with this component.

removeComponentByInstance

public <T> ComponentAdapter<T> removeComponentByInstance(T componentInstance)
Description copied from class: DefaultPicoContainer
Unregister a component by instance.

Specified by:
removeComponentByInstance in interface MutablePicoContainer
Overrides:
removeComponentByInstance in class DefaultPicoContainer
Parameters:
componentInstance - the component instance to unregister.
Returns:
the same instance of MutablePicoContainer

makeChildContainer

public MutablePicoContainer makeChildContainer()
Description copied from interface: MutablePicoContainer
Make a child container, using the same implementation of MutablePicoContainer as the parent. It will have a reference to this as parent. This will list the resulting MPC as a child. Lifecycle events will be cascaded from parent to child as a consequence of this.

Specified by:
makeChildContainer in interface MutablePicoContainer
Overrides:
makeChildContainer in class DefaultPicoContainer
Returns:
the new child container.


Copyright © 2003-2010 Codehaus. All Rights Reserved.