org.picocontainer.defaults
Class DefaultPicoContainer

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

public class DefaultPicoContainer
extends Object
implements MutablePicoContainer, 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.

Version:
$Revision: 1.8 $
Author:
Paul Hammant, Aslak Hellesøy, Jon Tirsén
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(Object componentKey)
          Finds a ComponentAdapter matching the key, looking in parent if not found in self (unless parent is null).
 ComponentAdapter getComponentAdapterOfType(Class componentType)
          Finds a ComponentAdapter matching the type, looking in parent if not found in self (unless parent is null).
 Collection getComponentAdapters()
          Return all adapters (not including the adapters from the parent).
 Object getComponentInstance(Object componentKey)
          Gets a component instance registered with a specific key.
 Object getComponentInstanceOfType(Class componentType)
          Finds a component instance matching the type, looking in parent if not found in self (unless parent is null).
 List getComponentInstances()
          Gets all the registered component instances in the container, (not including those in the parent container).
 Collection getComponentKeys()
          Get all the component keys.
 PicoContainer getParent()
          Get the parent container of this container.
 boolean hasComponent(Object componentKey)
          Checks for the presence of a particular component.
 boolean isDisposed()
           
 boolean isStarted()
           
 boolean isStopped()
           
 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(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(Object componentKey, Class componentImplementation)
          Registers a component. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.
 ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, Parameter[] parameters)
          Registers a component. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.
 ComponentAdapter registerComponentInstance(Object component)
          Registers an arbitrary object, using itself as a key. The returned ComponentAdapter will be an InstanceComponentAdapter.
 ComponentAdapter registerComponentInstance(Object componentKey, 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. The returned ComponentAdapter will be an InstanceComponentAdapter.
 void setParent(PicoContainer parent)
          Sets the Parent container.
 void start()
           
 void stop()
           
 ComponentAdapter unregisterComponent(Object componentKey)
          Unregisters a component.
 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

getComponentKeys

public final Collection getComponentKeys()
Description copied from interface: PicoContainer
Get all the component keys.

Specified by:
getComponentKeys in interface PicoContainer
Returns:
all the component keys.

getComponentAdapters

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

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

getComponentAdapter

public final ComponentAdapter getComponentAdapter(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(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(Object componentKey)
Description copied from interface: MutablePicoContainer
Unregisters a component.

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

registerComponentInstance

public ComponentAdapter registerComponentInstance(Object component)
                                           throws PicoRegistrationException
Registers an arbitrary object, using itself 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(Object componentKey,
                                                  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. 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.
componentInstance - an arbitrary object.
Returns:
the associated ComponentAdapter.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(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(Object componentKey,
                                                        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.
componentImplementation - the concrete component class.
Returns:
the associated ComponentAdapter.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public ComponentAdapter registerComponentImplementation(Object componentKey,
                                                        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.
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.

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 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 - if one of the components could not be instantiated or dependencies could not be properly resolved.

getComponentInstance

public Object getComponentInstance(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 - if the component could not be instantiated or dependencies could not be properly resolved.

getComponentInstanceOfType

public Object getComponentInstanceOfType(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.

hasComponent

public boolean hasComponent(Object componentKey)
Description copied from interface: PicoContainer
Checks for the presence of a particular component.

Specified by:
hasComponent in interface PicoContainer
Parameters:
componentKey - key of the component to look for.
Returns:
true if there is a component for this key.

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.

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 Stoppable

dispose

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

isStarted

public boolean isStarted()

isStopped

public boolean isStopped()

isDisposed

public boolean isDisposed()


Copyright © 2003-2004 Codehaus. All Rights Reserved.