public class DefaultPicoContainer extends Object implements MutablePicoContainer, Converting, ComponentMonitorStrategy, Serializable
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 addComponent
will take precedence over other components during type resolution.
Another place where keys that are classes make a subtle difference is in
HiddenImplementation
.
This implementation of MutablePicoContainer
also supports
ComponentMonitorStrategy
.
Modifier and Type | Class and Description |
---|---|
static class |
DefaultPicoContainer.KnowsContainerAdapter<T> |
static class |
DefaultPicoContainer.LateInstance |
Modifier and Type | Field and Description |
---|---|
protected ComponentFactory |
componentFactory
Component factory instance.
|
protected ComponentMonitor |
componentMonitor
Component monitor instance.
|
protected LifecycleStrategy |
lifecycleStrategy
Lifecycle strategy instance.
|
protected List<ComponentAdapter<?>> |
orderedComponentAdapters |
Constructor and Description |
---|
DefaultPicoContainer()
Creates a new container with a
AdaptingBehavior and no parent container. |
DefaultPicoContainer(ComponentFactory componentFactory)
Creates a new container with a custom ComponentFactory and no parent container.
|
DefaultPicoContainer(ComponentFactory componentFactory,
LifecycleStrategy lifecycleStrategy,
PicoContainer parent)
Creates a new container with a custom ComponentFactory, LifecycleStrategy for instance registration,
and a parent container.
|
DefaultPicoContainer(ComponentFactory componentFactory,
LifecycleStrategy lifecycleStrategy,
PicoContainer parent,
ComponentMonitor componentMonitor) |
DefaultPicoContainer(ComponentFactory componentFactory,
PicoContainer parent)
Creates a new container with a custom ComponentFactory and a parent container.
|
DefaultPicoContainer(ComponentMonitor monitor)
Creates a new container with the AdaptingInjection using a
custom ComponentMonitor
|
DefaultPicoContainer(ComponentMonitor monitor,
LifecycleStrategy lifecycleStrategy,
PicoContainer parent)
Creates a new container with the AdaptingInjection using a
custom ComponentMonitor and lifecycle strategy
|
DefaultPicoContainer(ComponentMonitor monitor,
PicoContainer parent)
Creates a new container with the AdaptingInjection using a
custom ComponentMonitor
|
DefaultPicoContainer(LifecycleStrategy lifecycleStrategy,
PicoContainer parent)
Creates a new container with the AdaptingInjection using a
custom lifecycle strategy
|
DefaultPicoContainer(PicoContainer parent)
Creates a new container with a (caching)
AdaptingInjection
and a parent container. |
Modifier and Type | Method and Description |
---|---|
void |
accept(PicoVisitor visitor)
Accepts a visitor that should visit the child containers, component adapters and component instances.
|
MutablePicoContainer |
addAdapter(ComponentAdapter<?> componentAdapter)
Register a component via a ComponentAdapter.
|
MutablePicoContainer |
addAdapter(ComponentAdapter<?> componentAdapter,
Properties properties) |
protected MutablePicoContainer |
addAdapterInternal(ComponentAdapter<?> componentAdapter) |
MutablePicoContainer |
addChildContainer(PicoContainer child)
Add a child container.
|
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 |
addConfig(String name,
Object val)
Register a config item.
|
MutablePicoContainer |
as(Properties... properties)
You can set for the following operation only the characteristic of registration of a component on the fly.
|
MutablePicoContainer |
change(Properties... properties)
You can change the characteristic of registration of all subsequent components in this container.
|
void |
changeMonitor(ComponentMonitor monitor)
Changes monitor in the ComponentFactory, the component adapters
and the child containers, if these support a ComponentMonitorStrategy.
|
ComponentMonitor |
currentMonitor()
Returns the first current monitor found in the ComponentFactory, the component adapters
and the child containers, if these support a ComponentMonitorStrategy.
|
protected Object |
decorateComponent(Object component,
ComponentAdapter<?> componentAdapter)
This is invoked when getComponent(..) is called.
|
void |
dispose()
Dispose the components of this PicoContainer and all its logical child containers.
|
<T> T |
getComponent(Class<T> componentType)
Retrieve a component keyed by the component type.
|
<T> T |
getComponent(Class<T> componentType,
Class<? extends Annotation> binding)
Retrieve a component keyed by the component type and binding type.
|
Object |
getComponent(Object componentKeyOrType)
Retrieve a component instance registered with a specific key or type.
|
Object |
getComponent(Object componentKeyOrType,
Class<? extends Annotation> annotation) |
Object |
getComponent(Object componentKeyOrType,
Type into) |
<T> ComponentAdapter<T> |
getComponentAdapter(Class<T> componentType,
Class<? extends Annotation> binding)
Find a component adapter associated with the specified type and binding type.
|
<T> ComponentAdapter<T> |
getComponentAdapter(Class<T> componentType,
NameBinding componentNameBinding)
Find a component adapter associated with the specified type and binding name.
|
ComponentAdapter<?> |
getComponentAdapter(Object componentKey)
Find a component adapter associated with the specified key.
|
Collection<ComponentAdapter<?>> |
getComponentAdapters()
Retrieve all the component adapters inside this container.
|
<T> List<ComponentAdapter<T>> |
getComponentAdapters(Class<T> componentType)
Retrieve all component adapters inside this container that are associated with the specified type.
|
<T> List<ComponentAdapter<T>> |
getComponentAdapters(Class<T> componentType,
Class<? extends Annotation> binding)
Retrieve all component adapters inside this container that are associated with the specified type and binding type.
|
protected Map<Object,ComponentAdapter<?>> |
getComponentKeyToAdapterCache() |
List<Object> |
getComponents()
Retrieve all the registered component instances in the container, (not including those in the parent container).
|
<T> List<T> |
getComponents(Class<T> componentType)
Returns a List of components of a certain componentType.
|
Converters |
getConverters()
If this container has a set of converters, then return it.
|
LifecycleState |
getLifecycleState()
Allow querying of the current lifecycle state of a MutablePicoContainer.
|
protected Set<ComponentAdapter<?>> |
getModifiableComponentAdapterList() |
String |
getName()
Retrieve the name set (if any).
|
protected List<ComponentAdapter<?>> |
getOrderedComponentAdapters() |
PicoContainer |
getParent()
Retrieve the parent container of this container.
|
protected void |
instantiateComponentAsIsStartable(ComponentAdapter<?> adapter) |
MutablePicoContainer |
makeChildContainer()
Make a child container, using both the same implementation of MutablePicoContainer as the parent
and identical behaviors as well.
|
protected void |
potentiallyStartAdapter(ComponentAdapter<?> adapter) |
boolean |
removeChildContainer(PicoContainer child)
Remove a child container from this container.
|
<T> ComponentAdapter<T> |
removeComponent(Object componentKey)
Unregister a component by key.
|
<T> ComponentAdapter<T> |
removeComponentByInstance(T componentInstance)
Unregister a component by instance.
|
void |
setLifecycleState(LifecycleState lifecycleState)
To assist ThreadLocal usage, LifecycleState can be set.
|
void |
setName(String name)
Name the container instance, to assist debugging or other indexing.
|
void |
start()
Start the components of this PicoContainer and all its logical child containers.
|
void |
stop()
Stop the components of this PicoContainer and all its logical child containers.
|
String |
toString() |
protected final ComponentFactory componentFactory
protected final LifecycleStrategy lifecycleStrategy
protected ComponentMonitor componentMonitor
protected final List<ComponentAdapter<?>> orderedComponentAdapters
public DefaultPicoContainer(ComponentFactory componentFactory, PicoContainer parent)
Cached
instances, such as for example
Caching
. Caching can delegate to
other ComponentAdapterFactories.
componentFactory
- the factory to use for creation of ComponentAdapters.parent
- the parent container (used for component dependency lookups).public DefaultPicoContainer(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent)
Cached
instances, such as for example
Caching
. Caching can delegate to
other ComponentAdapterFactories.
componentFactory
- the factory to use for creation of ComponentAdapters.lifecycleStrategy
- the lifecycle strategy chosen for registered
instance (not implementations!)parent
- the parent container (used for component dependency lookups).public DefaultPicoContainer(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, ComponentMonitor componentMonitor)
public DefaultPicoContainer(ComponentMonitor monitor, PicoContainer parent)
monitor
- the ComponentMonitor to useparent
- the parent container (used for component dependency lookups).public DefaultPicoContainer(ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy, PicoContainer parent)
monitor
- the ComponentMonitor to uselifecycleStrategy
- the lifecycle strategy to use.parent
- the parent container (used for component dependency lookups).public DefaultPicoContainer(LifecycleStrategy lifecycleStrategy, PicoContainer parent)
lifecycleStrategy
- the lifecycle strategy to use.parent
- the parent container (used for component dependency lookups).public DefaultPicoContainer(ComponentFactory componentFactory)
componentFactory
- the ComponentFactory to use.public DefaultPicoContainer(ComponentMonitor monitor)
monitor
- the ComponentMonitor to usepublic DefaultPicoContainer(PicoContainer parent)
AdaptingInjection
and a parent container.parent
- the parent container (used for component dependency lookups).public DefaultPicoContainer()
AdaptingBehavior
and no parent container.public Collection<ComponentAdapter<?>> getComponentAdapters()
getComponentAdapters
in interface PicoContainer
ComponentAdapter
s inside this container. The collection will not
be modifiable.a variant of this method which returns the component adapters inside this
container that are associated with the specified type.
public final ComponentAdapter<?> getComponentAdapter(Object componentKey)
getComponentAdapter
in interface PicoContainer
componentKey
- the key that the component was registered with.null
if no component has been
registered for the specified key.public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType, NameBinding componentNameBinding)
getComponentAdapter
in interface PicoContainer
componentType
- the type of the component.componentNameBinding
- the name binding to usenull
if no component has been
registered for the specified key.public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType, Class<? extends Annotation> binding)
getComponentAdapter
in interface PicoContainer
componentType
- the type of the component.binding
- the typed binding to usenull
if no component has been
registered for the specified key.public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType)
getComponentAdapters
in interface PicoContainer
componentType
- the type of the components.ComponentAdapter
s inside this container that are associated with
the specified type. Changes to this collection will not be reflected in the container itself.public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType, Class<? extends Annotation> binding)
getComponentAdapters
in interface PicoContainer
componentType
- the type of the components.binding
- the typed binding to useComponentAdapter
s inside this container that are associated with
the specified type. Changes to this collection will not be reflected in the container itself.protected MutablePicoContainer addAdapterInternal(ComponentAdapter<?> componentAdapter)
public MutablePicoContainer addAdapter(ComponentAdapter<?> componentAdapter)
ComponentFactory
passed to the constructor of this container.addAdapter
in interface MutablePicoContainer
componentAdapter
- the adapterpublic MutablePicoContainer addAdapter(ComponentAdapter<?> componentAdapter, Properties properties)
public <T> ComponentAdapter<T> removeComponent(Object componentKey)
removeComponent
in interface MutablePicoContainer
componentKey
- key of the component to unregister.public MutablePicoContainer addComponent(Object implOrInstance)
addComponent(componentImplementation, componentImplementation)
.
The returned ComponentAdapter will be an InstanceAdapter
.addComponent
in interface MutablePicoContainer
implOrInstance
- Component implementation or instancepublic MutablePicoContainer addConfig(String name, Object val)
MutablePicoContainer
addConfig
in interface MutablePicoContainer
name
- the name of the config itemval
- the value of the config itempublic MutablePicoContainer addComponent(Object componentKey, Object componentImplementationOrInstance, Parameter... parameters)
new ComponentParameter(), new ComponentParameter("someService")
The default constructor for the component parameter indicates auto-wiring should take place for
that parameter.
new Parameter[0]
ComponentFactory
passed to the container's constructor.addComponent
in interface MutablePicoContainer
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.componentImplementationOrInstance
- the component's implementation class. This must be a concrete class (ie, a
class that can be instantiated). Or an intance of the compoent.parameters
- the parameters that gives the container hints about what arguments to pass
to the constructor when it is instantiated. Container implementations may ignore
one or more of these hints.Parameter
,
ConstantParameter
,
ComponentParameter
public List<Object> getComponents() throws PicoException
PicoContainer
getComponents
in interface PicoContainer
PicoException
- if the instantiation of the component failspublic <T> List<T> getComponents(Class<T> componentType)
PicoContainer
getComponents
in interface PicoContainer
componentType
- the searched type.public Object getComponent(Object componentKeyOrType)
PicoContainer
getComponent
in interface PicoContainer
componentKeyOrType
- the key or Type that the component was registered with.null
if no component has been registered for the specified
key.public Object getComponent(Object componentKeyOrType, Type into)
getComponent
in interface PicoContainer
public Object getComponent(Object componentKeyOrType, Class<? extends Annotation> annotation)
protected Object decorateComponent(Object component, ComponentAdapter<?> componentAdapter)
component
- the component that will be returned for getComponent(..)componentAdapter
- the component adapter that made that componentpublic <T> T getComponent(Class<T> componentType)
PicoContainer
getComponent
in interface PicoContainer
componentType
- the type of the componentpublic <T> T getComponent(Class<T> componentType, Class<? extends Annotation> binding)
PicoContainer
getComponent
in interface PicoContainer
componentType
- the type of the componentbinding
- the binding type of the componentpublic PicoContainer getParent()
getParent
in interface PicoContainer
PicoContainer
instance, or null
if this container does not have a parent.public <T> ComponentAdapter<T> removeComponentByInstance(T componentInstance)
removeComponentByInstance
in interface MutablePicoContainer
componentInstance
- the component instance to unregister.public void start()
lifecycle manager
.
The actual lifecycle strategy
supported
depends on the concrete implementation of the adapter.start
in interface Startable
Behavior
,
LifecycleStrategy
,
makeChildContainer()
,
addChildContainer(PicoContainer)
,
removeChildContainer(PicoContainer)
public void stop()
lifecycle manager
.
The actual lifecycle strategy
supported
depends on the concrete implementation of the adapter.stop
in interface Startable
Behavior
,
LifecycleStrategy
,
makeChildContainer()
,
addChildContainer(PicoContainer)
,
removeChildContainer(PicoContainer)
public void dispose()
lifecycle manager
.
The actual lifecycle strategy
supported
depends on the concrete implementation of the adapter.dispose
in interface Disposable
Behavior
,
LifecycleStrategy
,
makeChildContainer()
,
addChildContainer(PicoContainer)
,
removeChildContainer(PicoContainer)
public void setLifecycleState(LifecycleState lifecycleState)
MutablePicoContainer
setLifecycleState
in interface MutablePicoContainer
lifecycleState
- the lifecyle state to use.public MutablePicoContainer makeChildContainer()
MutablePicoContainer
Note that for long-lived parent containers, you need to unregister child containers made with this call before disposing or you will leak memory. (Experience speaking here! )
Incorrect Example:
MutablePicoContainer parent = new PicoBuilder().withCaching().withLifecycle().build(); MutablePicoContainer child = parent.makeChildContainer(); child = null; //Child still retains in memory because parent still holds reference.
Correct Example:
MutablePicoContainer parent = new PicoBuilder().withCaching().withLifecycle().build(); MutablePicoContainer child = parent.makeChildContainer(); parent.removeChildContainer(child); //Remove the bi-directional references. child = null;
makeChildContainer
in interface MutablePicoContainer
public MutablePicoContainer addChildContainer(PicoContainer child)
MutablePicoContainer
addChildContainer
in interface MutablePicoContainer
child
- the child containerpublic boolean removeChildContainer(PicoContainer child)
MutablePicoContainer
removeChildContainer
in interface MutablePicoContainer
child
- the child containertrue
if the child container has been removed.public MutablePicoContainer change(Properties... properties)
MutablePicoContainer
change
in interface MutablePicoContainer
public MutablePicoContainer as(Properties... properties)
MutablePicoContainer
as
in interface MutablePicoContainer
public void accept(PicoVisitor visitor)
PicoContainer
accept
in interface PicoContainer
visitor
- the visitorpublic void changeMonitor(ComponentMonitor monitor)
changeMonitor
in interface ComponentMonitorStrategy
monitor
- the new ComponentMonitor to usepublic ComponentMonitor currentMonitor()
currentMonitor
in interface ComponentMonitorStrategy
PicoCompositionException
- if no component monitor is found in container or its childrenprotected void potentiallyStartAdapter(ComponentAdapter<?> adapter)
protected void instantiateComponentAsIsStartable(ComponentAdapter<?> adapter)
protected List<ComponentAdapter<?>> getOrderedComponentAdapters()
protected Map<Object,ComponentAdapter<?>> getComponentKeyToAdapterCache()
protected Set<ComponentAdapter<?>> getModifiableComponentAdapterList()
public void setName(String name)
MutablePicoContainer
setName
in interface MutablePicoContainer
name
- the name to call it.public Converters getConverters()
getConverters
in interface Converting
public LifecycleState getLifecycleState()
getLifecycleState
in interface MutablePicoContainer
MutablePicoContainer.getLifecycleState()
public String getName()
getName
in interface MutablePicoContainer
setName
.MutablePicoContainer.getName()
Copyright © 2003–2015 Codehaus. All rights reserved.