net.mchaplin.ioc
Class DefaultContainer

java.lang.Object
  extended by net.mchaplin.commons.WmindObject
      extended by net.mchaplin.ioc.DefaultContainer
All Implemented Interfaces:
ComponentI, ContainerI

public class DefaultContainer
extends WmindObject
implements ContainerI

Default, ready-to-use ContainerI implementation Provides Component registration & management services.

Author:
mchaplin@users.sourceforge.net TODO Handle interfaces use in constructors TODO Move component lookup services to a ServiceLocator $Header: $Revision: $Date:

Field Summary
static int HERE
          reference this container
static int PARENT
          reference this container parent
static int SONS
          reference this container sons
static int SONS_DEFAULT_LENTGH
          default lentgh for sons array
 int sonsCount
          registered sons count
 
Fields inherited from class net.mchaplin.commons.WmindObject
EX_CAUSE, EX_STACK, log, METHOD_CALL, propertyHelper, STACK_PREFIX
 
Fields inherited from interface net.mchaplin.ioc.component.ComponentI
CMP_I
 
Constructor Summary
DefaultContainer(ContainerI parent, java.lang.String name)
          Instanciate & register this container as child of another container
DefaultContainer(java.lang.String name)
          Instanciate a container.
 
Method Summary
 void addSon(ContainerI container)
           
 ContainerI getContainer()
          Provide component dependencies lookup facility
 java.util.List<ContainerI> getContainers()
           
 java.lang.String getInstanceName()
           
 ContainerI getParent()
           
 java.util.Map<java.lang.String,java.util.List<ComponentI>> getRegisteredTypes()
           
 ContainerI[] getSons()
           
 java.lang.String getState()
          Return the current state of a Container instance, as formatted text.
 java.lang.String lookupComponent(java.lang.Class classType)
          Lookup for to see if a given component is currently registered in this Container, its parent or sons.
 java.lang.String lookupComponent(java.lang.Class classType, int where)
          Lookup for to see if a given component is currently registered.
 void makeComponentAvailable()
          Gives back a Component when you stop using it.
 void registerComponentImplementation(java.lang.Class component)
          Register a Component into the Container
 void registerComponentImplementation(java.lang.Class component, java.lang.Class factory)
          Custom component registration.
 void registerComponentImplementation(java.lang.Class component, java.lang.Class factory, java.lang.String type)
          Custom component registration.
 void registerComponentImplementation(java.lang.Class component, java.lang.String type)
          Component registration.
 void registerComponentInstance(ComponentI instance)
           
 void registerComponentInstance(ComponentI instance, java.lang.String key)
           
 void registerFactoryImplementation(java.lang.Class factory)
          Register a Factory for later use.
 void reset()
          Provide facility for runtime component redeployment
 ComponentI retrieveComponentInstance(java.lang.Class classType)
          Retrieve the first instance in List of given type.
 ComponentI retrieveComponentInstance(java.lang.String key)
          Retrieve the first instance in List of given type.
 ComponentI retrieveComponentInstance(java.lang.String key, java.lang.String location)
          Retrieve the first instance in List of given type.
 void retrieveComponentInstances()
          Retrieve a list of all registered components
 java.util.List retrieveComponentInstances(java.lang.String key)
          Retrieve a list of registered components for the given key
 ComponentFactoryI retrieveFactoryInstance(java.lang.Class classType)
          Retrieve the first instance in List of given type.
 ComponentFactoryI retrieveFactoryInstance(java.lang.String key)
          Retrieve the first instance in List of given type.
 void setContainer(ContainerI container)
          Dependency injection setter
 void setContainers(java.util.List<ContainerI> containers)
           
 void setInstanceName(java.lang.String instanceName)
           
 void setParent(ContainerI aParent)
           
 void unregisterComponentInstance(ComponentI instance)
           
 
Methods inherited from class net.mchaplin.commons.WmindObject
printInheritance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HERE

public static final int HERE
reference this container

See Also:
Constant Field Values

PARENT

public static final int PARENT
reference this container parent

See Also:
Constant Field Values

SONS

public static final int SONS
reference this container sons

See Also:
Constant Field Values

sonsCount

public transient int sonsCount
registered sons count


SONS_DEFAULT_LENTGH

public static final int SONS_DEFAULT_LENTGH
default lentgh for sons array

See Also:
Constant Field Values
Constructor Detail

DefaultContainer

public DefaultContainer(java.lang.String name)
Instanciate a container.


DefaultContainer

public DefaultContainer(ContainerI parent,
                        java.lang.String name)
Instanciate & register this container as child of another container

Parameters:
parent -
Method Detail

registerComponentImplementation

public void registerComponentImplementation(java.lang.Class component)
Register a Component into the Container

Specified by:
registerComponentImplementation in interface ContainerI
Parameters:
component - the component to register

registerFactoryImplementation

public void registerFactoryImplementation(java.lang.Class factory)
Register a Factory for later use.

Specified by:
registerFactoryImplementation in interface ContainerI
Parameters:
factory - the factory to register

registerComponentImplementation

public void registerComponentImplementation(java.lang.Class component,
                                            java.lang.String type)
Component registration. Will try a newInstance() call on it & register if not already present.

Specified by:
registerComponentImplementation in interface ContainerI
Parameters:
component - the component Class to register
type - optional, the type to register this instance as. If set, the Component will be registered under its fully qualified class name

registerComponentImplementation

public void registerComponentImplementation(java.lang.Class component,
                                            java.lang.Class factory)
                                     throws ContainerException
Custom component registration. The factory will be instanciated through a Factory() Constructor. The component will then be instanciated through the Factory.retrieveInstance() method

Specified by:
registerComponentImplementation in interface ContainerI
Parameters:
component - the component to register
factory - the factory to register
Throws:
ContainerException
See Also:
ContainerI.registerComponentImplementation(java.lang.Class, java.lang.Class)

registerComponentImplementation

public void registerComponentImplementation(java.lang.Class component,
                                            java.lang.Class factory,
                                            java.lang.String type)
                                     throws ContainerException
Custom component registration. The factory will be instanciated through a Factory() Constructor. The component will then be instanciated through the Factory.retrieveInstance() method

Specified by:
registerComponentImplementation in interface ContainerI
Parameters:
component - the component to register
factory - the factory to register
type - the type to register this instance as. If set, the Component will be registered under its fully qualified class name
Throws:
ContainerException
See Also:
ContainerI.registerComponentImplementation(java.lang.Class, java.lang.Class)

registerComponentInstance

public void registerComponentInstance(ComponentI instance)
Specified by:
registerComponentInstance in interface ContainerI

registerComponentInstance

public void registerComponentInstance(ComponentI instance,
                                      java.lang.String key)
Specified by:
registerComponentInstance in interface ContainerI

retrieveComponentInstance

public ComponentI retrieveComponentInstance(java.lang.Class classType)
Retrieve the first instance in List of given type.

Specified by:
retrieveComponentInstance in interface ContainerI
Parameters:
classType - the Class type to retrieve an instance of.
Returns:
an instance of ComponentI, whose Class match classType

retrieveComponentInstance

public ComponentI retrieveComponentInstance(java.lang.String key)
Retrieve the first instance in List of given type.

Specified by:
retrieveComponentInstance in interface ContainerI
Parameters:
key - the component type to retrieve an instance of
Returns:
an instance of ComponentI, whose full name match key

retrieveComponentInstance

public ComponentI retrieveComponentInstance(java.lang.String key,
                                            java.lang.String location)
Retrieve the first instance in List of given type.

Specified by:
retrieveComponentInstance in interface ContainerI
Parameters:
key - the component type to retrieve an instance of
location - the location of the container where the component should be retrieved from.
Returns:
an instance of ComponentI, whose full name match key

retrieveFactoryInstance

public ComponentFactoryI retrieveFactoryInstance(java.lang.Class classType)
Retrieve the first instance in List of given type.

Specified by:
retrieveFactoryInstance in interface ContainerI
Parameters:
classType - the Class type to retrieve an instance of.
Returns:
an instance of ComponentI, whose Class match classType

retrieveFactoryInstance

public ComponentFactoryI retrieveFactoryInstance(java.lang.String key)
Retrieve the first instance in List of given type.

Specified by:
retrieveFactoryInstance in interface ContainerI
Parameters:
key - the key to retrieve associated Factory instance.
Returns:
an instance of ComponentI, whose full name match key

retrieveComponentInstances

public java.util.List retrieveComponentInstances(java.lang.String key)
Retrieve a list of registered components for the given key

Specified by:
retrieveComponentInstances in interface ContainerI

retrieveComponentInstances

public void retrieveComponentInstances()
Retrieve a list of all registered components

Specified by:
retrieveComponentInstances in interface ContainerI

makeComponentAvailable

public void makeComponentAvailable()
Gives back a Component when you stop using it. TODO : manage component lock/unlock mode

Specified by:
makeComponentAvailable in interface ContainerI

unregisterComponentInstance

public void unregisterComponentInstance(ComponentI instance)
Specified by:
unregisterComponentInstance in interface ContainerI

getContainer

public ContainerI getContainer()
Description copied from interface: ComponentI
Provide component dependencies lookup facility

Specified by:
getContainer in interface ComponentI
Specified by:
getContainer in interface ContainerI
Returns:
the Container that holds this Component.
See Also:
net.mchaplin.ioc.ComponentI#getContainer()

lookupComponent

public java.lang.String lookupComponent(java.lang.Class classType)
Lookup for to see if a given component is currently registered in this Container, its parent or sons. Use if you have no knowledge of where the component is located

Specified by:
lookupComponent in interface ContainerI
Parameters:
classType - the Class type to lookup for.
Returns:
the name of the container where a Component instance has been found or null.

lookupComponent

public java.lang.String lookupComponent(java.lang.Class classType,
                                        int where)
Lookup for to see if a given component is currently registered.

Specified by:
lookupComponent in interface ContainerI
Parameters:
classType - the Class type to lookup for.
where - where to lookup the component : here, parent or sons
Returns:
the name of the container where a Component instance has been found or null.

getContainers

public java.util.List<ContainerI> getContainers()
Returns:
Returns the containers.

setContainers

public void setContainers(java.util.List<ContainerI> containers)
Parameters:
containers - The containers to set.

getInstanceName

public java.lang.String getInstanceName()
Specified by:
getInstanceName in interface ContainerI
Returns:
Returns the instanceName.

setInstanceName

public void setInstanceName(java.lang.String instanceName)
Specified by:
setInstanceName in interface ContainerI
Parameters:
instanceName - The instanceName to set.

getParent

public ContainerI getParent()
Specified by:
getParent in interface ContainerI
See Also:
ContainerI.getParent()

setParent

public final void setParent(ContainerI aParent)
Specified by:
setParent in interface ContainerI
See Also:
ContainerI.setParent(net.mchaplin.ioc.ContainerI)

getRegisteredTypes

public java.util.Map<java.lang.String,java.util.List<ComponentI>> getRegisteredTypes()
Specified by:
getRegisteredTypes in interface ContainerI
Returns:
Returns the registeredTypes.

getSons

public ContainerI[] getSons()
Specified by:
getSons in interface ContainerI
Returns:
Returns the sons.

getState

public java.lang.String getState()
Return the current state of a Container instance, as formatted text.

Specified by:
getState in interface ContainerI
Returns:
the current Container State as formatted text.

addSon

public void addSon(ContainerI container)
Specified by:
addSon in interface ContainerI

setContainer

public void setContainer(ContainerI container)
Description copied from interface: ComponentI
Dependency injection setter

Specified by:
setContainer in interface ComponentI
See Also:
ComponentI.setContainer(net.mchaplin.ioc.ContainerI)

reset

public void reset()
Description copied from interface: ComponentI
Provide facility for runtime component redeployment

Specified by:
reset in interface ComponentI
Specified by:
reset in interface ContainerI
See Also:
net.mchaplin.ioc.ComponentI#reset()


Copyright © 2004-2005 mchaplin.net. All Rights Reserved.