Package org.picocontainer.classname
Class DefaultClassLoadingPicoContainer
- java.lang.Object
-
- org.picocontainer.containers.AbstractDelegatingPicoContainer
-
- org.picocontainer.containers.AbstractDelegatingMutablePicoContainer
-
- org.picocontainer.classname.DefaultClassLoadingPicoContainer
-
- All Implemented Interfaces:
java.io.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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDefaultClassLoadingPicoContainer.ClassVisitor
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,PicoContainer>namedChildContainers
-
Constructor Summary
Constructors Constructor Description DefaultClassLoadingPicoContainer()DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader)DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader, ComponentFactory componentFactory, PicoContainer parent)DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader, MutablePicoContainer delegate)DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader, PicoContainer parent, ComponentMonitor componentMonitor)DefaultClassLoadingPicoContainer(ComponentFactory componentFactory)DefaultClassLoadingPicoContainer(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, java.lang.ClassLoader cl, ComponentMonitor componentMonitor)DefaultClassLoadingPicoContainer(MutablePicoContainer delegate)DefaultClassLoadingPicoContainer(PicoContainer parent)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MutablePicoContaineraddAdapter(ComponentAdapter<?> componentAdapter)Register a component via a ComponentAdapter.ClassLoadingPicoContaineraddChildContainer(java.lang.String name, PicoContainer child)Addes a child container with a given nameMutablePicoContaineraddChildContainer(PicoContainer child)Add a child container.ClassPathElementaddClassLoaderURL(java.net.URL url)Adds a new URL that will be used in classloadingMutablePicoContaineraddComponent(java.lang.Object implOrInstance)Register an arbitrary object.MutablePicoContaineraddComponent(java.lang.Object key, java.lang.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.MutablePicoContaineras(java.util.Properties... properties)You can set for the following operation only the characteristic of registration of a component on the fly.MutablePicoContainerchange(java.util.Properties... properties)You can change the characteristic of registration of all subsequent components in this container.voidchangeMonitor(ComponentMonitor monitor)Propagates the monitor change down the delegate chain if a delegate that implements ComponentMonitorStrategy exists.protected DefaultClassLoadingPicoContainercreateChildContainer()ComponentMonitorcurrentMonitor()Returns the monitor currently usedjava.lang.ObjectgetComponent(java.lang.Object componentKeyOrType)Retrieve a component instance registered with a specific key or type.ComponentAdapter<?>getComponentAdapter(java.lang.Object componentKey)Find a component adapter associated with the specified key.java.lang.ClassLoadergetComponentClassLoader()Returns class loader that is the aggregate of the URLs added.protected java.util.Map<java.lang.String,PicoContainer>getNamedContainers()MutablePicoContainermakeChildContainer()Make a child container, using both the same implementation of MutablePicoContainer as the parent and identical behaviors as well.ClassLoadingPicoContainermakeChildContainer(java.lang.String name)Makes a child container with the same basic characteristics of this object (ComponentFactory, PicoContainer type, Behavior, etc)booleanremoveChildContainer(PicoContainer child)Remove a child container from this container.intvisit(java.io.File pkgDir, java.lang.String pkgName, java.util.regex.Pattern pattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor)intvisit(java.lang.String pkgName, java.lang.String codeSourceRoot, java.util.regex.Pattern compiledPattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor)intvisit(ClassName thisClassesPackage, java.lang.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 Detail
-
namedChildContainers
protected final java.util.Map<java.lang.String,PicoContainer> namedChildContainers
-
-
Constructor Detail
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader, ComponentFactory componentFactory, PicoContainer parent)
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader, MutablePicoContainer delegate)
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader, PicoContainer parent, ComponentMonitor componentMonitor)
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(ComponentFactory componentFactory)
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(PicoContainer parent)
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(MutablePicoContainer delegate)
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(java.lang.ClassLoader classLoader)
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer()
-
DefaultClassLoadingPicoContainer
public DefaultClassLoadingPicoContainer(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, java.lang.ClassLoader cl, ComponentMonitor componentMonitor)
-
-
Method Detail
-
createChildContainer
protected DefaultClassLoadingPicoContainer createChildContainer()
-
changeMonitor
public void changeMonitor(ComponentMonitor monitor)
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:
changeMonitorin interfaceComponentMonitorStrategy- Parameters:
monitor- the monitor to swap.- Throws:
java.lang.IllegalStateException- if no delegate can be found that implements ComponentMonitorStrategy.
-
currentMonitor
public ComponentMonitor currentMonitor()
Description copied from interface:ComponentMonitorStrategyReturns the monitor currently used- Specified by:
currentMonitorin interfaceComponentMonitorStrategy- Returns:
- The ComponentMonitor currently used
-
getComponent
public final java.lang.Object getComponent(java.lang.Object componentKeyOrType) throws PicoExceptionDescription copied from interface:PicoContainerRetrieve 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:
getComponentin interfacePicoContainer- Overrides:
getComponentin classAbstractDelegatingPicoContainer- Parameters:
componentKeyOrType- the key or Type that the component was registered with.- Returns:
- an instantiated component, or
nullif no component has been registered for the specified key. - Throws:
PicoException
-
makeChildContainer
public final MutablePicoContainer makeChildContainer()
Description copied from interface:MutablePicoContainerMake 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:
makeChildContainerin interfaceMutablePicoContainer- Overrides:
makeChildContainerin classAbstractDelegatingMutablePicoContainer- Returns:
- the new child container.
-
makeChildContainer
public ClassLoadingPicoContainer makeChildContainer(java.lang.String name)
Makes a child container with the same basic characteristics of this object (ComponentFactory, PicoContainer type, Behavior, etc)- Specified by:
makeChildContainerin interfaceClassLoadingPicoContainer- Parameters:
name- the name of the child container- Returns:
- The child MutablePicoContainer
-
removeChildContainer
public boolean removeChildContainer(PicoContainer child)
Description copied from interface:MutablePicoContainerRemove 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:
removeChildContainerin interfaceMutablePicoContainer- Overrides:
removeChildContainerin classAbstractDelegatingMutablePicoContainer- Parameters:
child- the child container- Returns:
trueif the child container has been removed.
-
getNamedContainers
protected final java.util.Map<java.lang.String,PicoContainer> getNamedContainers()
-
addClassLoaderURL
public ClassPathElement addClassLoaderURL(java.net.URL url)
Description copied from interface:ClassLoadingPicoContainerAdds a new URL that will be used in classloading- Specified by:
addClassLoaderURLin interfaceClassLoadingPicoContainer- Parameters:
url- url of the jar to find components in.- Returns:
- ClassPathElement to add permissions to (subject to security policy)
-
addComponent
public MutablePicoContainer addComponent(java.lang.Object implOrInstance)
Description copied from interface:MutablePicoContainerRegister 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:
addComponentin interfaceMutablePicoContainer- Overrides:
addComponentin classAbstractDelegatingMutablePicoContainer- Parameters:
implOrInstance- Component implementation or instance- Returns:
- the same instance of MutablePicoContainer
-
addComponent
public MutablePicoContainer addComponent(java.lang.Object key, java.lang.Object componentImplementationOrInstance, Parameter... parameters)
Description copied from interface:MutablePicoContainerRegister 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:
addComponentin interfaceMutablePicoContainer- Overrides:
addComponentin 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:
Parameter,ConstantParameter,ComponentParameter
- 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:MutablePicoContainerRegister 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:
addAdapterin interfaceMutablePicoContainer- Overrides:
addAdapterin classAbstractDelegatingMutablePicoContainer- Parameters:
componentAdapter- the adapter- Returns:
- the same instance of MutablePicoContainer
- Throws:
PicoCompositionException- if registration fails.
-
getComponentClassLoader
public java.lang.ClassLoader getComponentClassLoader()
Description copied from interface:ClassLoadingPicoContainerReturns class loader that is the aggregate of the URLs added.- Specified by:
getComponentClassLoaderin interfaceClassLoadingPicoContainer- Returns:
- A ClassLoader
-
addChildContainer
public MutablePicoContainer addChildContainer(PicoContainer child)
Description copied from interface:MutablePicoContainerAdd 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:
addChildContainerin interfaceMutablePicoContainer- Overrides:
addChildContainerin classAbstractDelegatingMutablePicoContainer- Parameters:
child- the child container- Returns:
- the same instance of MutablePicoContainer
-
addChildContainer
public ClassLoadingPicoContainer addChildContainer(java.lang.String name, PicoContainer child)
Description copied from interface:ClassLoadingPicoContainerAddes a child container with a given name- Specified by:
addChildContainerin interfaceClassLoadingPicoContainer- Parameters:
name- the container namechild- the child PicoContainer
-
getComponentAdapter
public ComponentAdapter<?> getComponentAdapter(java.lang.Object componentKey)
Description copied from interface:PicoContainerFind 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:
getComponentAdapterin interfacePicoContainer- Overrides:
getComponentAdapterin classAbstractDelegatingPicoContainer- Parameters:
componentKey- the key that the component was registered with.- Returns:
- the component adapter associated with this key, or
nullif no component has been registered for the specified key.
-
change
public MutablePicoContainer change(java.util.Properties... properties)
Description copied from interface:MutablePicoContainerYou can change the characteristic of registration of all subsequent components in this container.- Specified by:
changein interfaceMutablePicoContainer- Overrides:
changein classAbstractDelegatingMutablePicoContainer- Returns:
- the same Pico instance with changed properties
-
as
public MutablePicoContainer as(java.util.Properties... properties)
Description copied from interface:MutablePicoContainerYou can set for the following operation only the characteristic of registration of a component on the fly.- Specified by:
asin interfaceMutablePicoContainer- Overrides:
asin classAbstractDelegatingMutablePicoContainer- Returns:
- the same Pico instance with temporary properties
-
visit
public int visit(ClassName thisClassesPackage, java.lang.String regex, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor)
-
visit
public int visit(java.lang.String pkgName, java.lang.String codeSourceRoot, java.util.regex.Pattern compiledPattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor)
-
visit
public int visit(java.io.File pkgDir, java.lang.String pkgName, java.util.regex.Pattern pattern, boolean recursive, DefaultClassLoadingPicoContainer.ClassVisitor classNameVisitor)
-
-