org.picocontainer.defaults
Class DefaultPicoContainer

java.lang.Object
  extended byorg.picocontainer.defaults.DefaultPicoContainer
All Implemented Interfaces:
Disposable, MutablePicoContainer, PicoContainer, java.io.Serializable, Startable

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

The Standard PicoContainer/MutablePicoContainer implementation. Constructing a container c with a parent p container will cause c to look up components in p if they cannot be found inside c itself.

Using Class objects as keys to the various registerXXX() methods makes a subtle semantic difference:

If there are more than one registered components of the same type and one of them are registered with a Class key of the corresponding type, this component will take precedence over other components during type resolution.

Another place where keys that are classes make a subtle difference is in ImplementationHidingComponentAdapter.

Version:
$Revision: 1.8 $
Author:
Paul Hammant, Aslak Hellesøy, Jon Tirsén, Thomas Heller
See Also:
Serialized Form

Constructor Summary
DefaultPicoContainer()
           
DefaultPicoContainer(ComponentAdapterFactory componentAdapterFactory)
           
DefaultPicoContainer(ComponentAdapterFactory componentAdapterFactory, PicoContainer parent)
           
DefaultPicoContainer(PicoContainer parent)
           
 
Method Summary
 void addOrderedComponentAdapter(ComponentAdapter componentAdapter)
          Callback method from the implementation to keep track of the instantiation order.
 void dispose()
           
 ComponentAdapter getComponentAdapter(java.lang.Object componentKey)
          Finds a ComponentAdapter matching the key, looking in parent if not found in self (unless parent is null).
 ComponentAdapter getComponentAdapterOfType(java.lang.Class componentType)
          Finds a ComponentAdapter matching the type, looking in parent if not found in self (unless parent is null).
 java.util.Collection getComponentAdapters()
          Returns all adapters (not including the adapters from the parent).
 java.lang.Object getComponentInstance(java.lang.Object componentKey)
          Gets a component instance registered with a specific key.
 java.lang.Object getComponentInstanceOfType(java.lang.Class componentType)
          Finds a component instance matching the type, looking in parent if not found in self (unless parent is null).
 java.util.List getComponentInstances()
          Gets all the registered component instances in the container, (not including those in the parent container).
 PicoContainer getParent()
          Get the parent container of this container.
 void registerComponent(ComponentAdapter componentAdapter)
          Registers a component via a ComponentAdapter. Use this if you need fine grained control over what ComponentAdapter to use for a specific component. This method can be used to override the ComponentAdapter created by the ComponentAdapterFactory passed to the constructor of this container.
 ComponentAdapter registerComponentImplementation(java.lang.Class componentImplementation)
          Registers a component using the componentImplementation as key. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.
 ComponentAdapter registerComponentImplementation(java.lang.Object componentKey, java.lang.Class componentImplementation)
          Registers a component. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.
 ComponentAdapter registerComponentImplementation(java.lang.Object componentKey, java.lang.Class componentImplementation, java.util.List parameters)
           
 ComponentAdapter registerComponentImplementation(java.lang.Object componentKey, java.lang.Class componentImplementation, Parameter[] parameters)
          Registers a component. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.
 ComponentAdapter registerComponentInstance(java.lang.Object component)
          Registers an arbitrary object, using its class as a key. The returned ComponentAdapter will be an InstanceComponentAdapter.
 ComponentAdapter registerComponentInstance(java.lang.Object componentKey, java.lang.Object componentInstance)
          Registers an arbitrary object as a compoent 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. The returned ComponentAdapter will be an InstanceComponentAdapter.
 void setParent(PicoContainer parent)
          Sets the Parent container.
 void start()
           
 void stop()
           
 ComponentAdapter unregisterComponent(java.lang.Object componentKey)
          Unregisters a component by key.
 ComponentAdapter unregisterComponentByInstance(java.lang.Object componentInstance)
          Unregisters a component by instance.
 void verify()
          Verifies that the dependencies for all the registered components can be satisfied None of the components are instantiated during the verification process.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultPicoContainer

public DefaultPicoContainer(ComponentAdapterFactory componentAdapterFactory,
                            PicoContainer parent)

DefaultPicoContainer

public DefaultPicoContainer(PicoContainer parent)

DefaultPicoContainer

public DefaultPicoContainer(ComponentAdapterFactory componentAdapterFactory)

DefaultPicoContainer

public DefaultPicoContainer()
Method Detail

getComponentAdapters

public java.util.Collection getComponentAdapters()
Description copied from interface: PicoContainer
Returns all adapters (not including the adapters from the parent).

Specified by:
getComponentAdapters in interface PicoContainer
Returns:
Collection of ComponentAdapter.

getComponentAdapter

public final ComponentAdapter getComponentAdapter(java.lang.Object componentKey)
                                           throws AmbiguousComponentResolutionException
Description copied from interface: PicoContainer
Finds a ComponentAdapter matching the key, looking in parent if not found in self (unless parent is null).

Specified by:
getComponentAdapter in interface PicoContainer
Parameters:
componentKey - key of the component.
Returns:
the adapter matching the key.
Throws:
AmbiguousComponentResolutionException

getComponentAdapterOfType

public ComponentAdapter getComponentAdapterOfType(java.lang.Class componentType)
Description copied from interface: PicoContainer
Finds a ComponentAdapter matching the type, looking in parent if not found in self (unless parent is null).

Specified by:
getComponentAdapterOfType in interface PicoContainer
Parameters:
componentType - type of the component.
Returns:
the adapter matching the class.

registerComponent

public void registerComponent(ComponentAdapter componentAdapter)
                       throws DuplicateComponentKeyRegistrationException
Registers a component via a ComponentAdapter. Use this if you need fine grained control over what ComponentAdapter to use for a specific component. This method can be used to override the ComponentAdapter created by the ComponentAdapterFactory passed to the constructor of this container.

Specified by:
registerComponent in interface MutablePicoContainer
Parameters:
componentAdapter - the adapter
Throws:
DuplicateComponentKeyRegistrationException

unregisterComponent

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

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

registerComponentInstance

public ComponentAdapter registerComponentInstance(java.lang.Object component)
                                           throws PicoRegistrationException
Registers an arbitrary object, using its class as a key. The returned ComponentAdapter will be an InstanceComponentAdapter.

Specified by:
registerComponentInstance in interface MutablePicoContainer
Parameters:
component -
Returns:
the associated ComponentAdapter.
Throws:
PicoRegistrationException - if registration fails.

registerComponentInstance

public ComponentAdapter registerComponentInstance(java.lang.Object componentKey,
                                                  java.lang.Object componentInstance)
                                           throws PicoRegistrationException
Registers an arbitrary object as a compoent 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. The returned ComponentAdapter will be an InstanceComponentAdapter.

Specified by:
registerComponentInstance in interface MutablePicoContainer
Parameters:
componentKey - a key that identifies the compoent. 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 associated ComponentAdapter.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(java.lang.Class componentImplementation)
                                                 throws PicoRegistrationException
Registers a component using the componentImplementation as key. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.

Specified by:
registerComponentImplementation in interface MutablePicoContainer
Parameters:
componentImplementation - the concrete component class.
Returns:
the associated ComponentAdapter.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(java.lang.Object componentKey,
                                                        java.lang.Class componentImplementation)
                                                 throws PicoRegistrationException
Registers a component. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.

Specified by:
registerComponentImplementation in interface MutablePicoContainer
Parameters:
componentKey - a key that identifies the compoent. Must be unique within the conainer. The type of the key object has no semantic significance unless explicitly specified in the implementing container.
componentImplementation - the concrete component class.
Returns:
the associated ComponentAdapter.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(java.lang.Object componentKey,
                                                        java.lang.Class componentImplementation,
                                                        Parameter[] parameters)
                                                 throws PicoRegistrationException
Registers a component. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.

Specified by:
registerComponentImplementation in interface MutablePicoContainer
Parameters:
componentKey - a key that identifies the compoent. Must be unique within the conainer. The type of the key object has no semantic significance unless explicitly specified in the implementing container.
componentImplementation - the concrete component class.
parameters - an array of parameters that gives the container hints about what arguments to pass to the constructor when it is instantiated.
Returns:
the associated ComponentAdapter.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(java.lang.Object componentKey,
                                                        java.lang.Class componentImplementation,
                                                        java.util.List parameters)
                                                 throws PicoRegistrationException
Throws:
PicoRegistrationException

addOrderedComponentAdapter

public void addOrderedComponentAdapter(ComponentAdapter componentAdapter)
Description copied from interface: PicoContainer
Callback method from the implementation to keep track of the instantiation order. This method is not intended to be called explicitly by clients of the API!

Specified by:
addOrderedComponentAdapter in interface PicoContainer

getComponentInstances

public java.util.List getComponentInstances()
                                     throws PicoException
Description copied from interface: PicoContainer
Gets 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.
Throws:
PicoException

getComponentInstance

public java.lang.Object getComponentInstance(java.lang.Object componentKey)
                                      throws PicoException
Description copied from interface: PicoContainer
Gets a component instance registered with a specific key.

Specified by:
getComponentInstance in interface PicoContainer
Parameters:
componentKey - key the component was registered with.
Returns:
an instantiated component.
Throws:
PicoException

getComponentInstanceOfType

public java.lang.Object getComponentInstanceOfType(java.lang.Class componentType)
Description copied from interface: PicoContainer
Finds a component instance matching the type, looking in parent if not found in self (unless parent is null).

Specified by:
getComponentInstanceOfType in interface PicoContainer
Parameters:
componentType - type of the component.
Returns:
the adapter matching the class.

getParent

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

Specified by:
getParent in interface PicoContainer
Returns:
a Collection of PicoContainer.

setParent

public void setParent(PicoContainer parent)
Description copied from interface: MutablePicoContainer
Sets the Parent container.

Specified by:
setParent in interface MutablePicoContainer
Parameters:
parent - parent container.

unregisterComponentByInstance

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

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

verify

public void verify()
            throws PicoVerificationException
Description copied from interface: PicoContainer
Verifies that the dependencies for all the registered components can be satisfied None of the components are instantiated during the verification process.

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

start

public void start()
Specified by:
start in interface Startable

stop

public void stop()
Specified by:
stop in interface Startable

dispose

public void dispose()
Specified by:
dispose in interface Disposable


Copyright © 2003-2004 Codehaus. All Rights Reserved.