Package org.picocontainer.classname
Class DefaultClassLoadingPicoContainer
java.lang.Object
org.picocontainer.containers.AbstractDelegatingPicoContainer
org.picocontainer.containers.AbstractDelegatingMutablePicoContainer
org.picocontainer.classname.DefaultClassLoadingPicoContainer
- All Implemented Interfaces:
Serializable
,ClassLoadingPicoContainer
,ComponentMonitorStrategy
,Converting
,Disposable
,MutablePicoContainer
,PicoContainer
,Startable
public class DefaultClassLoadingPicoContainer
extends AbstractDelegatingMutablePicoContainer
implements ClassLoadingPicoContainer, ComponentMonitorStrategy
Default implementation of ClassLoadingPicoContainer.
- Author:
- Paul Hammant, Mauro Talevi, Michael Rimov
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDefaultClassLoadingPicoContainer
(ClassLoader classLoader) DefaultClassLoadingPicoContainer
(ClassLoader classLoader, ComponentFactory componentFactory, PicoContainer parent) DefaultClassLoadingPicoContainer
(ClassLoader classLoader, MutablePicoContainer delegate) DefaultClassLoadingPicoContainer
(ClassLoader classLoader, PicoContainer parent, ComponentMonitor componentMonitor) DefaultClassLoadingPicoContainer
(ComponentFactory componentFactory) DefaultClassLoadingPicoContainer
(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, ClassLoader cl, ComponentMonitor componentMonitor) -
Method Summary
Modifier and TypeMethodDescriptionaddAdapter
(ComponentAdapter<?> componentAdapter) Register a component via a ComponentAdapter.addChildContainer
(String name, PicoContainer child) Addes a child container with a given nameaddChildContainer
(PicoContainer child) Add a child container.addClassLoaderURL
(URL url) Adds a new URL that will be used in classloadingaddComponent
(Object implOrInstance) Register an arbitrary object.addComponent
(Object key, 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.as
(Properties... properties) You can set for the following operation only the characteristic of registration of a component on the fly.change
(Properties... properties) You can change the characteristic of registration of all subsequent components in this container.void
changeMonitor
(ComponentMonitor monitor) Propagates the monitor change down the delegate chain if a delegate that implements ComponentMonitorStrategy exists.protected DefaultClassLoadingPicoContainer
Returns the monitor currently usedfinal Object
getComponent
(Object componentKeyOrType) Retrieve a component instance registered with a specific key or type.getComponentAdapter
(Object componentKey) Find a component adapter associated with the specified key.Returns class loader that is the aggregate of the URLs added.protected final Map
<String, PicoContainer> final MutablePicoContainer
Make a child container, using both the same implementation of MutablePicoContainer as the parent and identical behaviors as well.makeChildContainer
(String name) Makes a child container with the same basic characteristics of this object (ComponentFactory, PicoContainer type, Behavior, etc)boolean
Remove a child container from this container.int
visit
(File pkgDir, String pkgName, Pattern pattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor) int
visit
(String pkgName, String codeSourceRoot, Pattern compiledPattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor) int
visit
(ClassName thisClassesPackage, String regex, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor) Methods inherited from class org.picocontainer.containers.AbstractDelegatingMutablePicoContainer
addConfig, dispose, getDelegate, getLifecycleState, getName, removeComponent, removeComponentByInstance, setLifecycleState, setName, start, stop
Methods inherited from class org.picocontainer.containers.AbstractDelegatingPicoContainer
accept, equals, getComponent, getComponent, getComponent, getComponentAdapter, getComponentAdapter, getComponentAdapters, getComponentAdapters, getComponentAdapters, getComponents, getComponents, getConverters, getParent, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.picocontainer.Disposable
dispose
Methods inherited from interface org.picocontainer.MutablePicoContainer
addConfig, getLifecycleState, getName, removeComponent, removeComponentByInstance, setLifecycleState, setName
Methods inherited from interface org.picocontainer.PicoContainer
accept, getComponent, getComponent, getComponent, getComponentAdapter, getComponentAdapter, getComponentAdapters, getComponentAdapters, getComponentAdapters, getComponents, getComponents, getParent
-
Field Details
-
namedChildContainers
-
-
Constructor Details
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(ClassLoader classLoader, ComponentFactory componentFactory, PicoContainer parent) -
DefaultClassLoadingPicoContainer
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(ClassLoader classLoader, PicoContainer parent, ComponentMonitor componentMonitor) -
DefaultClassLoadingPicoContainer
-
DefaultClassLoadingPicoContainer
-
DefaultClassLoadingPicoContainer
-
DefaultClassLoadingPicoContainer
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer() -
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, ClassLoader cl, ComponentMonitor componentMonitor)
-
-
Method Details
-
createChildContainer
-
changeMonitor
Propagates the monitor change down the delegate chain if a delegate that implements ComponentMonitorStrategy exists. Because of the ComponentMonitorStrategy API, not all delegates can have their API changed. If a delegate implementing ComponentMonitorStrategy cannot be found, an exception is thrown.- Specified by:
changeMonitor
in interfaceComponentMonitorStrategy
- Parameters:
monitor
- the monitor to swap.- Throws:
IllegalStateException
- if no delegate can be found that implements ComponentMonitorStrategy.
-
currentMonitor
Description copied from interface:ComponentMonitorStrategy
Returns the monitor currently used- Specified by:
currentMonitor
in interfaceComponentMonitorStrategy
- Returns:
- The ComponentMonitor currently used
-
getComponent
Description copied from interface:PicoContainer
Retrieve a component instance registered with a specific key or type. If a component cannot be found in this container, the parent container (if one exists) will be searched.- Specified by:
getComponent
in interfacePicoContainer
- Overrides:
getComponent
in classAbstractDelegatingPicoContainer
- Parameters:
componentKeyOrType
- the key or Type that the component was registered with.- Returns:
- an instantiated component, or
null
if no component has been registered for the specified key. - Throws:
PicoException
-
makeChildContainer
Description copied from interface:MutablePicoContainer
Make a child container, using both the same implementation of MutablePicoContainer as the parent and identical behaviors as well. It will have a reference to this as parent. This will list the resulting MPC as a child. Lifecycle events will be cascaded from parent to child as a consequence of this.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;
- Specified by:
makeChildContainer
in interfaceMutablePicoContainer
- Overrides:
makeChildContainer
in classAbstractDelegatingMutablePicoContainer
- Returns:
- the new child container.
-
makeChildContainer
Makes a child container with the same basic characteristics of this object (ComponentFactory, PicoContainer type, Behavior, etc)- Specified by:
makeChildContainer
in interfaceClassLoadingPicoContainer
- Parameters:
name
- the name of the child container- Returns:
- The child MutablePicoContainer
-
removeChildContainer
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 interfaceMutablePicoContainer
- Overrides:
removeChildContainer
in classAbstractDelegatingMutablePicoContainer
- Parameters:
child
- the child container- Returns:
true
if the child container has been removed.
-
getNamedContainers
-
addClassLoaderURL
Description copied from interface:ClassLoadingPicoContainer
Adds a new URL that will be used in classloading- Specified by:
addClassLoaderURL
in interfaceClassLoadingPicoContainer
- Parameters:
url
- url of the jar to find components in.- Returns:
- ClassPathElement to add permissions to (subject to security policy)
-
addComponent
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 callingaddComponent(componentImplementation, componentImplementation)
.- Specified by:
addComponent
in interfaceMutablePicoContainer
- Overrides:
addComponent
in classAbstractDelegatingMutablePicoContainer
- Parameters:
implOrInstance
- Component implementation or instance- Returns:
- the same instance of MutablePicoContainer
-
addComponent
public MutablePicoContainer addComponent(Object key, Object componentImplementationOrInstance, Parameter... parameters) 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
- Partial Autowiring: If you have two constructor args to match and you only wish to specify one of the constructors and
let PicoContainer wire the other one, you can use as parameters:
new ComponentParameter(), new ComponentParameter("someService")
The default constructor for the component parameter indicates auto-wiring should take place for that parameter. - Force No-Arg constructor usage: If you wish to force a component to be constructed with
the no-arg constructor, use a zero length Parameter array. Ex:
new Parameter[0]
- Specified by:
addComponent
in interfaceMutablePicoContainer
- Overrides:
addComponent
in classAbstractDelegatingMutablePicoContainer
- Parameters:
key
- 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.- Returns:
- the same instance of MutablePicoContainer
- See Also:
- Partial Autowiring: If you have two constructor args to match and you only wish to specify one of the constructors and
let PicoContainer wire the other one, you can use as parameters:
-
addAdapter
public MutablePicoContainer addAdapter(ComponentAdapter<?> componentAdapter) throws PicoCompositionException 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. The adapter will be wrapped in whatever behaviors that the the container has been set up with. If you want to bypass that behavior for the adapter you are adding, you should use Characteristics.NONE like so pico.as(Characteristics.NONE).addAdapter(...)- Specified by:
addAdapter
in interfaceMutablePicoContainer
- Overrides:
addAdapter
in classAbstractDelegatingMutablePicoContainer
- Parameters:
componentAdapter
- the adapter- Returns:
- the same instance of MutablePicoContainer
- Throws:
PicoCompositionException
- if registration fails.
-
getComponentClassLoader
Description copied from interface:ClassLoadingPicoContainer
Returns class loader that is the aggregate of the URLs added.- Specified by:
getComponentClassLoader
in interfaceClassLoadingPicoContainer
- Returns:
- A ClassLoader
-
addChildContainer
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 interfaceMutablePicoContainer
- Overrides:
addChildContainer
in classAbstractDelegatingMutablePicoContainer
- Parameters:
child
- the child container- Returns:
- the same instance of MutablePicoContainer
-
addChildContainer
Description copied from interface:ClassLoadingPicoContainer
Addes a child container with a given name- Specified by:
addChildContainer
in interfaceClassLoadingPicoContainer
- Parameters:
name
- the container namechild
- the child PicoContainer
-
getComponentAdapter
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 interfacePicoContainer
- Overrides:
getComponentAdapter
in classAbstractDelegatingPicoContainer
- 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.
-
change
Description copied from interface:MutablePicoContainer
You can change the characteristic of registration of all subsequent components in this container.- Specified by:
change
in interfaceMutablePicoContainer
- Overrides:
change
in classAbstractDelegatingMutablePicoContainer
- Parameters:
properties
-- Returns:
- the same Pico instance with changed properties
-
as
Description copied from interface:MutablePicoContainer
You can set for the following operation only the characteristic of registration of a component on the fly.- Specified by:
as
in interfaceMutablePicoContainer
- Overrides:
as
in classAbstractDelegatingMutablePicoContainer
- Parameters:
properties
-- Returns:
- the same Pico instance with temporary properties
-
visit
public int visit(ClassName thisClassesPackage, String regex, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor) -
visit
public int visit(String pkgName, String codeSourceRoot, Pattern compiledPattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor) -
visit
public int visit(File pkgDir, String pkgName, Pattern pattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor)
-