org.picocontainer.alternatives
Class AbstractDelegatingMutablePicoContainer

java.lang.Object
  extended by org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer
All Implemented Interfaces:
java.io.Serializable, Disposable, MutablePicoContainer, PicoContainer, Startable
Direct Known Subclasses:
CachingPicoContainer, ImplementationHidingCachingPicoContainer, ImplementationHidingPicoContainer

public abstract class AbstractDelegatingMutablePicoContainer
extends java.lang.Object
implements MutablePicoContainer, java.io.Serializable

Version:
$Revision: 2230 $
Author:
Paul Hammant
See Also:
Serialized Form

Field Summary
private  MutablePicoContainer delegate
           
 
Constructor Summary
AbstractDelegatingMutablePicoContainer(MutablePicoContainer delegate)
           
 
Method Summary
 void accept(PicoVisitor visitor)
          Accepts a visitor that should visit the child containers, component adapters and component instances.
 boolean addChildContainer(PicoContainer child)
          Add a child container.
 void dispose()
          Dispose this component.
 boolean equals(java.lang.Object obj)
           
 ComponentAdapter getComponentAdapter(java.lang.Object componentKey)
          Find a component adapter associated with the specified key.
 ComponentAdapter getComponentAdapterOfType(java.lang.Class componentType)
          Find a component adapter associated with the specified type.
 java.util.Collection getComponentAdapters()
          Retrieve all the component adapters inside this container.
 java.util.List getComponentAdaptersOfType(java.lang.Class componentType)
          Retrieve all component adapters inside this container that are associated with the specified type.
 java.lang.Object getComponentInstance(java.lang.Object componentKey)
          Retrieve a component instance registered with a specific key.
 java.lang.Object getComponentInstanceOfType(java.lang.Class componentType)
          Find a component instance matching the specified type.
 java.util.List getComponentInstances()
          Retrieve all the registered component instances in the container, (not including those in the parent container).
 java.util.List getComponentInstancesOfType(java.lang.Class type)
          Returns a List of components of a certain componentType.
protected  MutablePicoContainer getDelegate()
           
 PicoContainer getParent()
          Retrieve the parent container of this container.
 ComponentAdapter registerComponent(ComponentAdapter componentAdapter)
          Register a component via a ComponentAdapter.
 ComponentAdapter registerComponentImplementation(java.lang.Class componentImplementation)
          Register a component using the componentImplementation as key.
 ComponentAdapter registerComponentImplementation(java.lang.Object componentKey, java.lang.Class componentImplementation)
          Register a component.
 ComponentAdapter registerComponentImplementation(java.lang.Object componentKey, java.lang.Class componentImplementation, 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.
 ComponentAdapter registerComponentInstance(java.lang.Object componentInstance)
          Register an arbitrary object.
 ComponentAdapter registerComponentInstance(java.lang.Object componentKey, java.lang.Object componentInstance)
          Register an arbitrary object as a component in the container.
 boolean removeChildContainer(PicoContainer child)
          Remove a child container from this container.
 void start()
          Start this component.
 void stop()
          Stop this component.
 ComponentAdapter unregisterComponent(java.lang.Object componentKey)
          Unregister a component by key.
 ComponentAdapter unregisterComponentByInstance(java.lang.Object componentInstance)
          Unregister a component by instance.
 void verify()
          Deprecated. since 1.1 - Use new VerifyingVisitor().traverse(this)
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.picocontainer.MutablePicoContainer
makeChildContainer
 

Field Detail

delegate

private MutablePicoContainer delegate
Constructor Detail

AbstractDelegatingMutablePicoContainer

public AbstractDelegatingMutablePicoContainer(MutablePicoContainer delegate)
Method Detail

getDelegate

protected MutablePicoContainer getDelegate()

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(java.lang.Object componentKey,
                                                        java.lang.Class componentImplementation)
                                                 throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Register a component.

Specified by:
registerComponentImplementation in interface MutablePicoContainer
Parameters:
componentKey - a key that identifies the component. Must be unique within the container. The type of the key object has no semantic significance unless explicitly specified in the documentation of the implementing container.
componentImplementation - the component's implementation class. This must be a concrete class (ie, a class that can be instantiated).
Returns:
the ComponentAdapter that has been associated with this component. In the majority of cases, this return value can be safely ignored, as one of the getXXX() methods of the PicoContainer interface can be used to retrieve a reference to the component later on.
Throws:
PicoRegistrationException - if registration of the component fails.
See Also:
a variant of this method that allows more control over the parameters passed into the componentImplementation constructor when constructing an instance.

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(java.lang.Object componentKey,
                                                        java.lang.Class componentImplementation,
                                                        Parameter[] parameters)
                                                 throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
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


registerComponentImplementation

public ComponentAdapter registerComponentImplementation(java.lang.Class componentImplementation)
                                                 throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Register a component using the componentImplementation as key. Calling this method is equivalent to calling registerComponentImplementation(componentImplementation, componentImplementation).

Specified by:
registerComponentImplementation in interface MutablePicoContainer
Parameters:
componentImplementation - the concrete component class.
Returns:
the ComponentAdapter that has been associated with this component. In the majority of cases, this return value can be safely ignored, as one of the getXXX() methods of the PicoContainer interface can be used to retrieve a reference to the component later on.
Throws:
PicoRegistrationException - if registration fails.

registerComponentInstance

public ComponentAdapter registerComponentInstance(java.lang.Object componentInstance)
                                           throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Register an arbitrary object. The class of the object will be used as a key. Calling this method is equivalent to calling * registerComponentImplementation(componentImplementation, componentImplementation).

Specified by:
registerComponentInstance in interface MutablePicoContainer
Returns:
the ComponentAdapter that has been associated with this component. In the majority of cases, this return value can be safely ignored, as one of the getXXX() methods of the PicoContainer interface can be used to retrieve a reference to the component later on.
Throws:
PicoRegistrationException - if registration fails.

registerComponentInstance

public ComponentAdapter registerComponentInstance(java.lang.Object componentKey,
                                                  java.lang.Object componentInstance)
                                           throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Register an arbitrary object as a component in the container. This is handy when other components in the same container have dependencies on this kind of object, but where letting the container manage and instantiate it is impossible.

Beware that too much use of this method is an antipattern.

Specified by:
registerComponentInstance in interface MutablePicoContainer
Parameters:
componentKey - a key that identifies the component. Must be unique within the conainer. The type of the key object has no semantic significance unless explicitly specified in the implementing container.
componentInstance - an arbitrary object.
Returns:
the ComponentAdapter that has been associated with this component. In the majority of cases, this return value can be safely ignored, as one of the getXXX() methods of the PicoContainer interface can be used to retrieve a reference to the component later on.
Throws:
PicoRegistrationException - if registration fails.

registerComponent

public ComponentAdapter registerComponent(ComponentAdapter componentAdapter)
                                   throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Register a component via a ComponentAdapter. Use this if you need fine grained control over what ComponentAdapter to use for a specific component.

Specified by:
registerComponent in interface MutablePicoContainer
Parameters:
componentAdapter - the adapter
Returns:
the same adapter that was passed as an argument.
Throws:
PicoRegistrationException - if registration fails.

unregisterComponent

public ComponentAdapter unregisterComponent(java.lang.Object componentKey)
Description copied from interface: MutablePicoContainer
Unregister a component by key.

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

unregisterComponentByInstance

public ComponentAdapter unregisterComponentByInstance(java.lang.Object componentInstance)
Description copied from interface: MutablePicoContainer
Unregister a component by instance.

Specified by:
unregisterComponentByInstance in interface MutablePicoContainer
Parameters:
componentInstance - the component instance to unregister.
Returns:
the ComponentAdapter that was associated with this component.

getComponentInstance

public java.lang.Object getComponentInstance(java.lang.Object componentKey)
Description copied from interface: PicoContainer
Retrieve a component instance registered with a specific key. If a component cannot be found in this container, the parent container (if one exists) will be searched.

Specified by:
getComponentInstance in interface PicoContainer
Parameters:
componentKey - the key that the component was registered with.
Returns:
an instantiated component, or null if no component has been registered for the specified key.

getComponentInstanceOfType

public java.lang.Object getComponentInstanceOfType(java.lang.Class componentType)
Description copied from interface: PicoContainer
Find a component instance matching the specified type.

Specified by:
getComponentInstanceOfType in interface PicoContainer
Parameters:
componentType - the type of the component
Returns:
an instantiated component matching the class, or null if no component has been registered with a matching type

getComponentInstances

public java.util.List getComponentInstances()
Description copied from interface: PicoContainer
Retrieve all the registered component instances in the container, (not including those in the parent container). The components are returned in their order of instantiation, which depends on the dependency order between them.

Specified by:
getComponentInstances in interface PicoContainer
Returns:
all the components.

getParent

public PicoContainer getParent()
Description copied from interface: PicoContainer
Retrieve the parent container of this container.

Specified by:
getParent in interface PicoContainer
Returns:
a PicoContainer instance, or null if this container does not have a parent.

getComponentAdapter

public ComponentAdapter getComponentAdapter(java.lang.Object componentKey)
Description copied from interface: PicoContainer
Find a component adapter associated with the specified key. If a component adapter cannot be found in this container, the parent container (if one exists) will be searched.

Specified by:
getComponentAdapter in interface PicoContainer
Parameters:
componentKey - the key that the component was registered with.
Returns:
the component adapter associated with this key, or null if no component has been registered for the specified key.

getComponentAdapterOfType

public ComponentAdapter getComponentAdapterOfType(java.lang.Class componentType)
Description copied from interface: PicoContainer
Find a component adapter associated with the specified type. If a component adapter cannot be found in this container, the parent container (if one exists) will be searched.

Specified by:
getComponentAdapterOfType in interface PicoContainer
Parameters:
componentType - the type of the component.
Returns:
the component adapter associated with this class, or null if no component has been registered for the specified key.

getComponentAdapters

public java.util.Collection getComponentAdapters()
Description copied from interface: PicoContainer
Retrieve all the component adapters inside this container. The component adapters from the parent container are not returned.

Specified by:
getComponentAdapters in interface PicoContainer
Returns:
a collection containing all the ComponentAdapters inside this container. The collection will not be modifiable.
See Also:
a variant of this method which returns the component adapters inside this container that are associated with the specified type.

getComponentAdaptersOfType

public java.util.List getComponentAdaptersOfType(java.lang.Class componentType)
Description copied from interface: PicoContainer
Retrieve all component adapters inside this container that are associated with the specified type. The component adapters from the parent container are not returned.

Specified by:
getComponentAdaptersOfType in interface PicoContainer
Parameters:
componentType - the type of the components.
Returns:
a collection containing all the ComponentAdapters inside this container that are associated with the specified type. Changes to this collection will not be reflected in the container itself.

verify

public void verify()
            throws PicoVerificationException
Deprecated. since 1.1 - Use new VerifyingVisitor().traverse(this)

Description copied from interface: PicoContainer
Verify that the dependencies for all the registered components can be satisfied. No components are instantiated during the verification process.

Specified by:
verify in interface PicoContainer
Throws:
PicoVerificationException - if there are unsatisifiable dependencies.

start

public void start()
Description copied from interface: Startable
Start this component. Called initially at the begin of the lifecycle. It can be called again after a stop.

Specified by:
start in interface Startable

stop

public void stop()
Description copied from interface: Startable
Stop this component. Called near the end of the lifecycle. It can be called again after a further start. Implement Disposable if you need a single call at the definite end of the lifecycle.

Specified by:
stop in interface Startable

dispose

public void dispose()
Description copied from interface: Disposable
Dispose this component. The component should deallocate all resources. The contract for this method defines a single call at the end of this component's life.

Specified by:
dispose in interface Disposable

addChildContainer

public boolean addChildContainer(PicoContainer child)
Description copied from interface: MutablePicoContainer
Add a child container. This action will list the the 'child' as exactly that in the parents scope. It will not change the child's view of a parent. That is determined by the constructor arguments of the child itself. Lifecycle events will be cascaded from parent to child as a consequence of calling this method.

Specified by:
addChildContainer in interface MutablePicoContainer
Parameters:
child - the child container
Returns:
true if the child container was not already in.

removeChildContainer

public boolean removeChildContainer(PicoContainer child)
Description copied from interface: MutablePicoContainer
Remove a child container from this container. It will not change the child's view of a parent. Lifecycle event will no longer be cascaded from the parent to the child.

Specified by:
removeChildContainer in interface MutablePicoContainer
Parameters:
child - the child container
Returns:
true if the child container has been removed.

accept

public void accept(PicoVisitor visitor)
Description copied from interface: PicoContainer
Accepts a visitor that should visit the child containers, component adapters and component instances.

Specified by:
accept in interface PicoContainer
Parameters:
visitor - the visitor

getComponentInstancesOfType

public java.util.List getComponentInstancesOfType(java.lang.Class type)
                                           throws PicoException
Description copied from interface: PicoContainer
Returns a List of components of a certain componentType. The list is ordered by instantiation order, starting with the components instantiated first at the beginning.

Specified by:
getComponentInstancesOfType in interface PicoContainer
Parameters:
type - the searched type.
Returns:
a List of components.
Throws:
PicoException - if the instantiation of a component fails

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object