org.picocontainer.defaults
Class InstantiatingComponentAdapter

java.lang.Object
  extended by org.picocontainer.defaults.MonitoringComponentAdapter
      extended by org.picocontainer.defaults.AbstractComponentAdapter
          extended by org.picocontainer.defaults.InstantiatingComponentAdapter
All Implemented Interfaces:
java.io.Serializable, ComponentAdapter, ComponentMonitorStrategy, LifecycleStrategy
Direct Known Subclasses:
ConstructorInjectionComponentAdapter, SetterInjectionComponentAdapter

public abstract class InstantiatingComponentAdapter
extends AbstractComponentAdapter
implements LifecycleStrategy

This ComponentAdapter will instantiate a new object for each call to ComponentAdapter.getComponentInstance(PicoContainer). That means that when used with a PicoContainer, getComponentInstance will return a new object each time.

Since:
1.0
Version:
$Revision: 2788 $
Author:
Aslak Hellesøy, Paul Hammant, Jörg Schaible, Mauro Talevi
See Also:
Serialized Form

Nested Class Summary
protected static class InstantiatingComponentAdapter.Guard
          The cycle guard for the verification.
 
Field Summary
protected  boolean allowNonPublicClasses
          Flag indicating instanciation of non-public classes.
protected  LifecycleStrategy lifecycleStrategy
          The strategy used to control the lifecycle
protected  Parameter[] parameters
          The parameters to use for initialization.
protected  InstantiatingComponentAdapter.Guard verifyingGuard
          The cycle guard for the verification.
 
Constructor Summary
protected InstantiatingComponentAdapter(java.lang.Object componentKey, java.lang.Class componentImplementation, Parameter[] parameters, boolean allowNonPublicClasses)
          Constructs a new ComponentAdapter for the given key and implementation.
protected InstantiatingComponentAdapter(java.lang.Object componentKey, java.lang.Class componentImplementation, Parameter[] parameters, boolean allowNonPublicClasses, ComponentMonitor monitor)
          Constructs a new ComponentAdapter for the given key and implementation.
protected InstantiatingComponentAdapter(java.lang.Object componentKey, java.lang.Class componentImplementation, Parameter[] parameters, boolean allowNonPublicClasses, ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy)
          Constructs a new ComponentAdapter for the given key and implementation.
 
Method Summary
 void accept(PicoVisitor visitor)
          Accepts a visitor for this ComponentAdapter.
private  void checkConcrete()
           
protected  Parameter[] createDefaultParameters(java.lang.Class[] parameters)
          Create default parameters for the given types.
 void dispose(java.lang.Object component)
          Invoke the "dispose" method on the component instance if this is disposable.
protected abstract  java.lang.reflect.Constructor getGreediestSatisfiableConstructor(PicoContainer container)
          Find and return the greediest satisfiable constructor.
 boolean hasLifecycle(java.lang.Class type)
          Test if a component instance has a lifecycle.
protected  java.lang.Object newInstance(java.lang.reflect.Constructor constructor, java.lang.Object[] parameters)
          Instantiate an object with given parameters and respect the accessible flag.
 void start(java.lang.Object component)
          Invoke the "start" method on the component instance if this is startable.
 void stop(java.lang.Object component)
          Invoke the "stop" method on the component instance if this is stoppable.
 void verify(PicoContainer container)
          Verify that all dependencies for this adapter can be satisifed.
 
Methods inherited from class org.picocontainer.defaults.AbstractComponentAdapter
checkTypeCompatibility, getComponentImplementation, getComponentKey, toString
 
Methods inherited from class org.picocontainer.defaults.MonitoringComponentAdapter
changeMonitor, currentMonitor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.picocontainer.ComponentAdapter
getComponentInstance
 

Field Detail

verifyingGuard

protected transient InstantiatingComponentAdapter.Guard verifyingGuard
The cycle guard for the verification.


parameters

protected transient Parameter[] parameters
The parameters to use for initialization.


allowNonPublicClasses

protected boolean allowNonPublicClasses
Flag indicating instanciation of non-public classes.


lifecycleStrategy

protected LifecycleStrategy lifecycleStrategy
The strategy used to control the lifecycle

Constructor Detail

InstantiatingComponentAdapter

protected InstantiatingComponentAdapter(java.lang.Object componentKey,
                                        java.lang.Class componentImplementation,
                                        Parameter[] parameters,
                                        boolean allowNonPublicClasses,
                                        ComponentMonitor monitor,
                                        LifecycleStrategy lifecycleStrategy)
Constructs a new ComponentAdapter for the given key and implementation.

Parameters:
componentKey - the search key for this implementation
componentImplementation - the concrete implementation
parameters - the parameters to use for the initialization
allowNonPublicClasses - flag to allow instantiation of non-public classes
monitor - the component monitor used by this ComponentAdapter
lifecycleStrategy - the lifecycle strategy used by this ComponentAdapter
Throws:
AssignabilityRegistrationException - if the key is a type and the implementation cannot be assigned to
NotConcreteRegistrationException - if the implementation is not a concrete class
java.lang.NullPointerException - if one of the parameters is null

InstantiatingComponentAdapter

protected InstantiatingComponentAdapter(java.lang.Object componentKey,
                                        java.lang.Class componentImplementation,
                                        Parameter[] parameters,
                                        boolean allowNonPublicClasses,
                                        ComponentMonitor monitor)
Constructs a new ComponentAdapter for the given key and implementation.

Parameters:
componentKey - the search key for this implementation
componentImplementation - the concrete implementation
parameters - the parameters to use for the initialization
allowNonPublicClasses - flag to allow instantiation of non-public classes
monitor - the component monitor used by this ComponentAdapter
Throws:
AssignabilityRegistrationException - if the key is a type and the implementation cannot be assigned to
NotConcreteRegistrationException - if the implementation is not a concrete class
java.lang.NullPointerException - if one of the parameters is null

InstantiatingComponentAdapter

protected InstantiatingComponentAdapter(java.lang.Object componentKey,
                                        java.lang.Class componentImplementation,
                                        Parameter[] parameters,
                                        boolean allowNonPublicClasses)
Constructs a new ComponentAdapter for the given key and implementation.

Parameters:
componentKey - the search key for this implementation
componentImplementation - the concrete implementation
parameters - the parameters to use for the initialization
allowNonPublicClasses - flag to allow instantiation of non-public classes.
Throws:
AssignabilityRegistrationException - if the key is a type and the implementation cannot be assigned to.
NotConcreteRegistrationException - if the implementation is not a concrete class.
java.lang.NullPointerException - if one of the parameters is null
Method Detail

checkConcrete

private void checkConcrete()
                    throws NotConcreteRegistrationException
Throws:
NotConcreteRegistrationException

createDefaultParameters

protected Parameter[] createDefaultParameters(java.lang.Class[] parameters)
Create default parameters for the given types.

Parameters:
parameters - the parameter types
Returns:
the array with the default parameters.

verify

public void verify(PicoContainer container)
            throws PicoIntrospectionException
Description copied from interface: ComponentAdapter
Verify that all dependencies for this adapter can be satisifed. Normally, the adapter should verify this by checking that the associated PicoContainer contains all the needed dependnecies.

Specified by:
verify in interface ComponentAdapter
Parameters:
container - the PicoContainer, that is used to resolve any possible dependencies of the instance.
Throws:
PicoIntrospectionException - if one or more dependencies cannot be resolved.

accept

public void accept(PicoVisitor visitor)
Description copied from interface: ComponentAdapter
Accepts a visitor for this ComponentAdapter. The method is normally called by visiting a PicoContainer, that cascades the visitor also down to all its ComponentAdapter instances.

Specified by:
accept in interface ComponentAdapter
Overrides:
accept in class AbstractComponentAdapter
Parameters:
visitor - the visitor.

start

public void start(java.lang.Object component)
Description copied from interface: LifecycleStrategy
Invoke the "start" method on the component instance if this is startable. It is up to the implementation of the strategy what "start" and "startable" means.

Specified by:
start in interface LifecycleStrategy
Parameters:
component - the instance of the component to start

stop

public void stop(java.lang.Object component)
Description copied from interface: LifecycleStrategy
Invoke the "stop" method on the component instance if this is stoppable. It is up to the implementation of the strategy what "stop" and "stoppable" means.

Specified by:
stop in interface LifecycleStrategy
Parameters:
component - the instance of the component to stop

dispose

public void dispose(java.lang.Object component)
Description copied from interface: LifecycleStrategy
Invoke the "dispose" method on the component instance if this is disposable. It is up to the implementation of the strategy what "dispose" and "disposable" means.

Specified by:
dispose in interface LifecycleStrategy
Parameters:
component - the instance of the component to dispose

hasLifecycle

public boolean hasLifecycle(java.lang.Class type)
Description copied from interface: LifecycleStrategy
Test if a component instance has a lifecycle.

Specified by:
hasLifecycle in interface LifecycleStrategy
Parameters:
type - the component's type
Returns:
true if the component has a lifecycle

newInstance

protected java.lang.Object newInstance(java.lang.reflect.Constructor constructor,
                                       java.lang.Object[] parameters)
                                throws java.lang.InstantiationException,
                                       java.lang.IllegalAccessException,
                                       java.lang.reflect.InvocationTargetException
Instantiate an object with given parameters and respect the accessible flag.

Parameters:
constructor - the constructor to use
parameters - the parameters for the constructor
Returns:
the new object.
Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

getGreediestSatisfiableConstructor

protected abstract java.lang.reflect.Constructor getGreediestSatisfiableConstructor(PicoContainer container)
                                                                             throws PicoIntrospectionException,
                                                                                    UnsatisfiableDependenciesException,
                                                                                    AmbiguousComponentResolutionException,
                                                                                    AssignabilityRegistrationException,
                                                                                    NotConcreteRegistrationException
Find and return the greediest satisfiable constructor.

Parameters:
container - the PicoContainer to resolve dependencies.
Returns:
the found constructor.
Throws:
PicoIntrospectionException
UnsatisfiableDependenciesException
AmbiguousComponentResolutionException
AssignabilityRegistrationException
NotConcreteRegistrationException