to.etc.iocular.container
Class BasicContainer

java.lang.Object
  extended by to.etc.iocular.container.BasicContainer
All Implemented Interfaces:
Container

public class BasicContainer
extends java.lang.Object
implements Container

This is a default implementation of an IOC container. While it exists it caches all instance objects. This object is threadsafe.

If an object really needs to be created we do this in two phases. The first phase creates a "build plan" for the object. The build plan considers all possible ways to create the given object, and will select the most optimal plan. The second phase will then execute the plan to actually create the object.

The separation into plan and create phase is needed because an object can be created across many ways (for instance because it has multiple constructors). We cannot try each method in turn because trying would force us to create objects that might not be needed after all when a given way to instantiate is not possible. We prevent this by first determining a plan which is guaranteed to work.

Author:
Frits Jalvingh Created on Mar 27, 2007

Constructor Summary
BasicContainer(ContainerDefinition def, Container parent)
           
 
Method Summary
 void destroy()
           
 void dump(java.lang.Class<?> theClass)
           
<T> T
findObject(java.lang.Class<T> theClass)
          Return the 'unnamed' object with the specified class from this container.
<T> T
findObject(java.lang.String name, java.lang.Class<T> theClass)
           
 java.lang.String getIdent()
           
<T> T
getObject(java.lang.Class<T> theClass)
          Return the 'unnamed' object with the specified class from this container.
<T> T
getObject(java.lang.String name, java.lang.Class<T> theClass)
           
 java.lang.Object retrieve(ComponentRef ref)
          Actually get the required thingy.
 void setParameter(java.lang.Class<?> theClass, java.lang.Object instance)
          Set the parameter as identified by it's target class to the specified instance.
 void setParameter(java.lang.Object instance)
          Set a container parameter object.
 void setParameter(java.lang.String name, java.lang.Object instance)
          Sets the parameter with the specified name to the instance passed.
 void start()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BasicContainer

public BasicContainer(ContainerDefinition def,
                      Container parent)
Method Detail

start

public void start()
Specified by:
start in interface Container

destroy

public void destroy()
Specified by:
destroy in interface Container

getIdent

public java.lang.String getIdent()

findObject

public <T> T findObject(java.lang.Class<T> theClass)
Description copied from interface: Container
Return the 'unnamed' object with the specified class from this container. If the object is not known this will return null!! This should not normally be used; use the 'getObject' call with the same signature instead: it throws an exception when the associated object is not found.

Specified by:
findObject in interface Container
Returns:

findObject

public <T> T findObject(java.lang.String name,
                        java.lang.Class<T> theClass)
Specified by:
findObject in interface Container

getObject

public <T> T getObject(java.lang.Class<T> theClass)
            throws java.lang.Exception
Description copied from interface: Container
Return the 'unnamed' object with the specified class from this container. If the object is not known this will throw a IocNotFoundException.

Specified by:
getObject in interface Container
Returns:
Throws:
java.lang.Exception

getObject

public <T> T getObject(java.lang.String name,
                       java.lang.Class<T> theClass)
            throws java.lang.Exception
Specified by:
getObject in interface Container
Throws:
java.lang.Exception

setParameter

public void setParameter(java.lang.Object instance)
Set a container parameter object. The parameter to set is inferred from the object type.

Specified by:
setParameter in interface Container
Parameters:
instance -

setParameter

public void setParameter(java.lang.Class<?> theClass,
                         java.lang.Object instance)
Set the parameter as identified by it's target class to the specified instance. This instance CAN be null, in which case null will be set into contructors and/or setters dependent on this parameter.

Specified by:
setParameter in interface Container
Parameters:
clz -
instance -

setParameter

public void setParameter(java.lang.String name,
                         java.lang.Object instance)
Sets the parameter with the specified name to the instance passed. This instance CAN be null, in which case null will be set into contructors and/or setters dependent on this parameter.

Specified by:
setParameter in interface Container
Parameters:
name -
instance -

retrieve

public java.lang.Object retrieve(ComponentRef ref)
                          throws java.lang.Exception
Actually get the required thingy. If it is already present in this container (and a singleton) it will be created atomically; if it is a prototype it will be created without lock.

Type Parameters:
T -
Parameters:
theClass -
ref -
Returns:
Throws:
java.lang.Exception

dump

public void dump(java.lang.Class<?> theClass)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object