com.fatwire.gst.foundation.controller.action.support
Class BaseFactory

java.lang.Object
  extended by com.fatwire.gst.foundation.controller.action.support.BaseFactory
All Implemented Interfaces:
Factory
Direct Known Subclasses:
ClassBasedFactory, GroovyFactory, IcsBackedObjectFactoryTemplate

public abstract class BaseFactory
extends Object
implements Factory

Factory making use to reflection (reflectionStrategy(String, Class) and ctorStrategy(String, Class)) to produce objects.

This class caches the produced objects for the lifetime of this object. Effectively this means the lifetime of the ICS object.

Author:
Dolf Dijkstra

Field Summary
protected  COM.FutureTense.Interfaces.ICS ics
           
protected static org.apache.commons.logging.Log LOG
           
 
Constructor Summary
BaseFactory(COM.FutureTense.Interfaces.ICS ics)
           
BaseFactory(COM.FutureTense.Interfaces.ICS ics, Factory... roots)
           
 
Method Summary
protected
<T> T
createFromMethod(String name, Class<T> c, Method m)
           
 COM.FutureTense.Interfaces.ICS createICS(COM.FutureTense.Interfaces.ICS ics)
           
protected
<T> T
ctorStrategy(String name, Class<T> c)
           
protected  Class<?>[] factoryClasses(COM.FutureTense.Interfaces.ICS ics)
          Method to find classes to use for the producer methods.
<T> T
getObject(String name, Class<T> fieldType)
           
protected  Object invokeCreateMethod(Method m, Object from, String name, Object... arguments)
           
protected
<T> T
locate(String askedName, Class<T> c)
          Internal method to check for Services or create Services.
protected
<T> T
namedAnnotationStrategy(String name, Class<T> c)
          Tries to create the object based on the ServiceProducer annotation where the names match.
protected
<T> T
reflectionStrategy(String name, Class<T> c)
          Reflection based producer method.
protected  boolean shouldCache(Method m)
           
protected  void throwRuntimeException(InvocationTargetException e)
           
 String toString()
           
protected
<T> T
unnamedAnnotationStrategy(String name, Class<T> c)
          Tries to create the object based on the ServiceProducer annotation without a name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOG

protected static final org.apache.commons.logging.Log LOG

ics

protected final COM.FutureTense.Interfaces.ICS ics
Constructor Detail

BaseFactory

public BaseFactory(COM.FutureTense.Interfaces.ICS ics)

BaseFactory

public BaseFactory(COM.FutureTense.Interfaces.ICS ics,
                   Factory... roots)
Method Detail

getObject

public final <T> T getObject(String name,
                             Class<T> fieldType)
Specified by:
getObject in interface Factory

locate

protected <T> T locate(String askedName,
                       Class<T> c)
            throws InvocationTargetException
Internal method to check for Services or create Services.

Parameters:
name -
c -
Returns:
the found service, null if no T can be created.
Throws:
InvocationTargetException

factoryClasses

protected Class<?>[] factoryClasses(COM.FutureTense.Interfaces.ICS ics)
Method to find classes to use for the producer methods. This implementation returns Object.getClass().

Subclasses can return and are encouraged to return other classes.

Parameters:
ics -
Returns:
array of classes to use for reflection

namedAnnotationStrategy

protected <T> T namedAnnotationStrategy(String name,
                                        Class<T> c)
                             throws InvocationTargetException
Tries to create the object based on the ServiceProducer annotation where the names match.

Parameters:
name -
c -
Returns:
Throws:
InvocationTargetException

unnamedAnnotationStrategy

protected <T> T unnamedAnnotationStrategy(String name,
                                          Class<T> c)
                               throws InvocationTargetException
Tries to create the object based on the ServiceProducer annotation without a name.

Parameters:
name -
c -
Returns:
Throws:
InvocationTargetException

reflectionStrategy

protected <T> T reflectionStrategy(String name,
                                   Class<T> c)
                        throws InvocationTargetException
Reflection based producer method.

This method uses reflection to find producer methods to the following rules:

If the non-static version is used the implementing class needs to have a public constructor that takes ICS and Factory as arguments. To this class the current ICS and this object will be passed.

Parameters:
name - the simple name of the object to produce
c - the class with the type information of the object to produce
Returns:
the created object, null if no producer method was found or when that method returned null.
Throws:
InvocationTargetException - when the create<Type> method throws an exception.

createFromMethod

protected <T> T createFromMethod(String name,
                                 Class<T> c,
                                 Method m)
                      throws InvocationTargetException
Parameters:
name - name of the object
c - the type of the object to create
m - the method to use to create the object
Returns:
Throws:
InvocationTargetException

invokeCreateMethod

protected Object invokeCreateMethod(Method m,
                                    Object from,
                                    String name,
                                    Object... arguments)
                             throws InvocationTargetException
Parameters:
m - method in invoke
from - object to invoke from
name - the name of the object
arguments - the arguments to pass to the method
Returns:
Throws:
InvocationTargetException

shouldCache

protected boolean shouldCache(Method m)

throwRuntimeException

protected void throwRuntimeException(InvocationTargetException e)
Parameters:
e -

ctorStrategy

protected <T> T ctorStrategy(String name,
                             Class<T> c)
                  throws InvocationTargetException
Parameters:
name -
c -
Returns:
Throws:
InvocationTargetException

createICS

public COM.FutureTense.Interfaces.ICS createICS(COM.FutureTense.Interfaces.ICS ics)

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2010-2013 Oracle Corporation. All Rights Reserved.