Class 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:
  • Field Details

  • Constructor Details

  • Method Details

    • 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:
      changeMonitor in interface ComponentMonitorStrategy
      Parameters:
      monitor - the monitor to swap.
      Throws:
      IllegalStateException - if no delegate can be found that implements ComponentMonitorStrategy.
    • currentMonitor

      public ComponentMonitor currentMonitor()
      Description copied from interface: ComponentMonitorStrategy
      Returns the monitor currently used
      Specified by:
      currentMonitor in interface ComponentMonitorStrategy
      Returns:
      The ComponentMonitor currently used
    • getComponent

      public final Object getComponent(Object componentKeyOrType) throws PicoException
      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 interface PicoContainer
      Overrides:
      getComponent in class AbstractDelegatingPicoContainer
      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

      public final MutablePicoContainer 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 interface MutablePicoContainer
      Overrides:
      makeChildContainer in class AbstractDelegatingMutablePicoContainer
      Returns:
      the new child container.
    • makeChildContainer

      public ClassLoadingPicoContainer makeChildContainer(String name)
      Makes a child container with the same basic characteristics of this object (ComponentFactory, PicoContainer type, Behavior, etc)
      Specified by:
      makeChildContainer in interface ClassLoadingPicoContainer
      Parameters:
      name - the name of the child container
      Returns:
      The child MutablePicoContainer
    • removeChildContainer

      public boolean removeChildContainer(PicoContainer child)
      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 interface MutablePicoContainer
      Overrides:
      removeChildContainer in class AbstractDelegatingMutablePicoContainer
      Parameters:
      child - the child container
      Returns:
      true if the child container has been removed.
    • getNamedContainers

      protected final Map<String,PicoContainer> getNamedContainers()
    • addClassLoaderURL

      public ClassPathElement addClassLoaderURL(URL url)
      Description copied from interface: ClassLoadingPicoContainer
      Adds a new URL that will be used in classloading
      Specified by:
      addClassLoaderURL in interface ClassLoadingPicoContainer
      Parameters:
      url - url of the jar to find components in.
      Returns:
      ClassPathElement to add permissions to (subject to security policy)
    • addComponent

      public MutablePicoContainer addComponent(Object implOrInstance)
      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 calling addComponent(componentImplementation, componentImplementation).
      Specified by:
      addComponent in interface MutablePicoContainer
      Overrides:
      addComponent in class AbstractDelegatingMutablePicoContainer
      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 interface MutablePicoContainer
        Overrides:
        addComponent in class AbstractDelegatingMutablePicoContainer
        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:
      • 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 interface MutablePicoContainer
        Overrides:
        addAdapter in class AbstractDelegatingMutablePicoContainer
        Parameters:
        componentAdapter - the adapter
        Returns:
        the same instance of MutablePicoContainer
        Throws:
        PicoCompositionException - if registration fails.
      • getComponentClassLoader

        public ClassLoader getComponentClassLoader()
        Description copied from interface: ClassLoadingPicoContainer
        Returns class loader that is the aggregate of the URLs added.
        Specified by:
        getComponentClassLoader in interface ClassLoadingPicoContainer
        Returns:
        A ClassLoader
      • addChildContainer

        public MutablePicoContainer addChildContainer(PicoContainer child)
        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 interface MutablePicoContainer
        Overrides:
        addChildContainer in class AbstractDelegatingMutablePicoContainer
        Parameters:
        child - the child container
        Returns:
        the same instance of MutablePicoContainer
      • addChildContainer

        public ClassLoadingPicoContainer addChildContainer(String name, PicoContainer child)
        Description copied from interface: ClassLoadingPicoContainer
        Addes a child container with a given name
        Specified by:
        addChildContainer in interface ClassLoadingPicoContainer
        Parameters:
        name - the container name
        child - the child PicoContainer
      • getComponentAdapter

        public ComponentAdapter<?> getComponentAdapter(Object componentKey)
        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 interface PicoContainer
        Overrides:
        getComponentAdapter in class AbstractDelegatingPicoContainer
        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

        public MutablePicoContainer change(Properties... properties)
        Description copied from interface: MutablePicoContainer
        You can change the characteristic of registration of all subsequent components in this container.
        Specified by:
        change in interface MutablePicoContainer
        Overrides:
        change in class AbstractDelegatingMutablePicoContainer
        Returns:
        the same Pico instance with changed properties
      • as

        public MutablePicoContainer as(Properties... properties)
        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 interface MutablePicoContainer
        Overrides:
        as in class AbstractDelegatingMutablePicoContainer
        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)