com.tomgibara.pronto.util
Class FactoryHelper<T>

java.lang.Object
  extended by com.tomgibara.pronto.util.FactoryHelper<T>
Type Parameters:
T - the type of object produced by the factory

public class FactoryHelper<T>
extends java.lang.Object

This class can be extended to provide a factory implementation that:

Note that instantiation of the instance is eager, but any exception is recorded and repeatedly rethrown on each call to getInstance. The eager instantiation means that this factory implementation is fast, requires no synchronization, and is thread-safe.

It is expected that this class will be used by extension, or delegation: supplying the appropriate strings to the constructor and wrapping the getInstance method to narrow the thrown Exception.

Author:
Tom Gibara

Constructor Summary
FactoryHelper(java.lang.String className, java.lang.ClassLoader classLoader, java.lang.String propertyName)
          Constructs a new factory which will create an instance of the class specified by either the value of the specified system property, or the supplied class name - exactly one of which may be null.
 
Method Summary
 T getInstance()
          The object instance created by this factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FactoryHelper

public FactoryHelper(java.lang.String className,
                     java.lang.ClassLoader classLoader,
                     java.lang.String propertyName)
Constructs a new factory which will create an instance of the class specified by either the value of the specified system property, or the supplied class name - exactly one of which may be null. Any exception which arises from attempting to instantiate the class is cached and thrown on any subsequent call to getInstance(). If no class loader is supplied, then the context class loader is used; If the context class loader is null the class loader of this class is used.

Parameters:
className - the class to which this factory is to create, may be null
classLoader - the class loader from which to load the class, may be null
propertyName - a system property which overrides the specified class name
Method Detail

getInstance

public T getInstance()
              throws java.lang.Exception
The object instance created by this factory. The same object is returned on each call to this method. Note that the exception thrown by this method is 'recycled'.

Returns:
the instance created by this factory
Throws:
java.lang.Exception - the exception which occured trying to create the instance.


Copyright © 2006-2007 Tom Gibara. All Rights Reserved.