org.picocontainer.defaults
Class DefaultPicoContainer

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

public class DefaultPicoContainer
extends Object
implements MutablePicoContainer, Serializable

Version:
$Revision: 1.8 $
Author:
Aslak Hellesøy
See Also:
Serialized Form

Constructor Summary
DefaultPicoContainer()
           
DefaultPicoContainer(ComponentAdapterFactory componentAdapterFactory)
           
 
Method Summary
 void addChild(MutablePicoContainer child)
          Adds a Child container.
 void addOrderedComponentAdapter(ComponentAdapter componentAdapter)
          Adds a component instance to the container.
 void addParent(MutablePicoContainer parent)
          Adds a Parent container.
 ComponentAdapter findComponentAdapter(Object componentKey)
          Finds a ComponentAdapter matching the key.
 Object findComponentInstance(Class componentType)
           
 ComponentAdapter findImplementingComponentAdapter(Class componentType)
           
 Collection getChildContainers()
          Get the child containers of this container.
 List getComponentAdapters()
           
 Object getComponentInstance(Object componentKey)
          Gets a component instance.
 List getComponentInstances()
          Gets all the registered component instances in the container.
 Collection getComponentKeys()
          Get all the component keys.
 Object getComponentMulticaster()
          Shorthand for getComponentMulticaster(boolean, boolean)
 Object getComponentMulticaster(boolean callInInstantiationOrder, boolean callUnmanagedComponents)
          Returns an object (in fact, a dynamic proxy) that implements the union of all the interfaces of the currently registered components.
 List getParentContainers()
          Get the parent containers of this container.
 boolean hasComponent(Object componentKey)
          Checks for the presence of a particular component.
 void registerComponent(ComponentAdapter componentAdapter)
           
 Object registerComponentImplementation(Class componentImplementation)
          Registers a component using the componentImplementation as key.
 Object registerComponentImplementation(Object componentKey, Class componentImplementation)
          Registers a component.
 Object registerComponentImplementation(Object componentKey, Class componentImplementation, Parameter[] parameters)
          Registers a component.
 Object registerComponentInstance(Object component)
          Registers an arbitrary object, using itself as a key.
 Object registerComponentInstance(Object componentKey, Object componentInstance)
          Registers an arbitrary object as a compoent in the container.
 Object unregisterComponent(Object componentKey)
          Unregisters a component.
 
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)

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 List getComponentAdapters()

registerComponent

public void registerComponent(ComponentAdapter componentAdapter)
                       throws DuplicateComponentKeyRegistrationException
Throws:
DuplicateComponentKeyRegistrationException

unregisterComponent

public Object 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 unregistered component.

findComponentAdapter

public final ComponentAdapter findComponentAdapter(Object componentKey)
                                            throws AmbiguousComponentResolutionException
Description copied from interface: PicoContainer
Finds a ComponentAdapter matching the key. This method is an "expert" method, and should normally not be called by clients of this API. (It is called by the implementation).

Specified by:
findComponentAdapter in interface PicoContainer
Parameters:
componentKey - key of the component.
Throws:
AmbiguousComponentResolutionException

registerComponentInstance

public Object registerComponentInstance(Object component)
                                 throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Registers an arbitrary object, using itself as a key.

Specified by:
registerComponentInstance in interface MutablePicoContainer
Parameters:
component -
Returns:
the key used for registration.
Throws:
PicoRegistrationException

registerComponentInstance

public Object registerComponentInstance(Object componentKey,
                                        Object componentInstance)
                                 throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
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.

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 key used for registration.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public Object registerComponentImplementation(Class componentImplementation)
                                       throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Registers a component using the componentImplementation as key.

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

getComponentMulticaster

public Object getComponentMulticaster(boolean callInInstantiationOrder,
                                      boolean callUnmanagedComponents)
                               throws PicoInitializationException,
                                      PicoIntrospectionException,
                                      AssignabilityRegistrationException,
                                      NotConcreteRegistrationException
Description copied from interface: PicoContainer
Returns an object (in fact, a dynamic proxy) that implements the union of all the interfaces of the currently registered components.

Casting this object to any of those interfaces and then calling a method on it will result in that call being multicast to all the components implementing that given interface.

This is a simple yet extremely powerful way to handle lifecycle of components. Component writers can invent their own lifecycle interfaces, and then use the multicaster to invoke the method in one go.

Specified by:
getComponentMulticaster in interface PicoContainer
Parameters:
callInInstantiationOrder - whether or not to call the method in the order of instantiation, which depends on the components' inter-dependencies.
callUnmanagedComponents - whether or not to multicast to components that are not managed by this container.
Returns:
a multicaster object.
Throws:
PicoInitializationException
PicoIntrospectionException
AssignabilityRegistrationException
NotConcreteRegistrationException

getComponentMulticaster

public Object getComponentMulticaster()
                               throws PicoInitializationException,
                                      PicoIntrospectionException,
                                      AssignabilityRegistrationException,
                                      NotConcreteRegistrationException
Description copied from interface: PicoContainer
Shorthand for PicoContainer.getComponentMulticaster(boolean, boolean)
(true, false)
, which is the most common usage scenario.

Specified by:
getComponentMulticaster in interface PicoContainer
Returns:
a multicaster object.
Throws:
PicoInitializationException
PicoIntrospectionException
AssignabilityRegistrationException
NotConcreteRegistrationException

registerComponentImplementation

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

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.
Throws:
PicoRegistrationException - if registration fails.

registerComponentImplementation

public Object registerComponentImplementation(Object componentKey,
                                              Class componentImplementation,
                                              Parameter[] parameters)
                                       throws PicoRegistrationException
Description copied from interface: MutablePicoContainer
Registers a component.

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 key used for registration.
Throws:
PicoRegistrationException - if registration fails.

addOrderedComponentAdapter

public void addOrderedComponentAdapter(ComponentAdapter componentAdapter)
Description copied from interface: MutablePicoContainer
Adds a component instance to the container. Do not call this method explicitly. It is used by the internals. Use MutablePicoContainer.registerComponentInstance(java.lang.Object) instead if you wish to register externally instantiated objects.

Specified by:
addOrderedComponentAdapter in interface MutablePicoContainer
Parameters:
componentAdapter - key of the component.

getComponentInstances

public List getComponentInstances()
                           throws PicoException
Description copied from interface: PicoContainer
Gets all the registered component instances in the container. The components are returned in their order of instantiation, which depends on the dependency order between components.

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.

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.

findComponentInstance

public Object findComponentInstance(Class componentType)
                             throws PicoException
Throws:
PicoException

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.

findImplementingComponentAdapter

public ComponentAdapter findImplementingComponentAdapter(Class componentType)
                                                  throws PicoException
Throws:
PicoException

getChildContainers

public Collection getChildContainers()
Description copied from interface: PicoContainer
Get the child containers of this container. Any given container instance should not use the child containers to resolve components, but rahter their parents. This method is available merely to be able to traverse trees of containers, and is not used by the container itself.

Specified by:
getChildContainers in interface PicoContainer
Returns:
a Collection of PicoContainer.
See Also:
PicoContainer.getParentContainers()

getParentContainers

public List getParentContainers()
Description copied from interface: PicoContainer
Get the parent containers of this container. In a purely hierarchical (tree structure) container, there will be 0..1 parents. However, it is possible to have several parents. A container will look in its parents if a component can be found in self.

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

addChild

public void addChild(MutablePicoContainer child)
Description copied from interface: MutablePicoContainer
Adds a Child container. Will also add this instance as a parent to child, so calling MutablePicoContainer.addParent(org.picocontainer.MutablePicoContainer) is not necessary.

Specified by:
addChild in interface MutablePicoContainer
Parameters:
child - child container.

addParent

public void addParent(MutablePicoContainer parent)
Description copied from interface: MutablePicoContainer
Adds a Parent container. Will also add this instance as a child to parent, so calling MutablePicoContainer.addChild(org.picocontainer.MutablePicoContainer) is not necessary.

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


Copyright © 2003 Codehaus. All Rights Reserved.