ProgramixGenericLib v4.0.0

com.programix.da2
Class DAFactory

java.lang.Object
  extended by com.programix.da2.DAFactory

public class DAFactory
extends Object

Used to construct and initialize instances of data access implementations using reflection.

Common usage is (where CustomerDA is an interface that extends the GenericDA interface):

 ValueMap config = //...
 CustomerDA cda = (CustomerDA) DAFactory.create(config, CustomerDA.class);
or
 String filename = //...
 CustomerDA cda = (CustomerDA) DAFactory.create(filename, CustomerDA.class);
The configuration ValueMap passed in must contain the class name of the class to be instantiated stored under the key DA_IMPLEMENTATION_CLASSNAME. Other key/value pairs in the map are populated with whatever the specific data access implementation needs for initialization.

Author:
Paul Hyde

Field Summary
static String DA_IMPLEMENTATION_CLASSNAME
          The reserved key that retrieves the fully-qualified name (as a String) of the data access implementation class that will be instantiated via reflection.
static String DA_SOURCE_PREFIX
          Used by this factory to facilitate the creation and initialization of a chain of data access' if the outer 'wrappers' implement DecoratorDA.
 
Method Summary
static GenericDA create(File file, Class targetType)
          Create a data access instance using an File whose data is in the format described by ValueMap (nearly identical to the format for a Properties file).
static GenericDA create(InputStream rawIn, Class targetType)
          Create a data access instance using an InputStream whose data is in the format described by ValueMap (nearly identical to the format for a Properties file).
static GenericDA create(Reader rawIn, Class targetType)
          Create a data access instance using a Reader whose data is in the format described by ValueMap (nearly identical to the format for a Properties file).
static GenericDA create(URL propertiesURL, Class targetType)
          Create a data access instance using the specified URL whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).
static GenericDA create(ValueMap config, Class daInterfaceType)
          Constructs an instance of the data access implementation specified in the configuration.
static GenericDA createFromFile(String filename, Class targetType)
          Create a data access instance using the specified filename whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).
static GenericDA createFromResource(String resourceLocation, Class targetType)
          Create a data access instance using the specified resourceLocation whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DA_IMPLEMENTATION_CLASSNAME

public static final String DA_IMPLEMENTATION_CLASSNAME
The reserved key that retrieves the fully-qualified name (as a String) of the data access implementation class that will be instantiated via reflection.

The value of this key is always:

 da.impl.classname
 

See Also:
create(ValueMap, Class), Constant Field Values

DA_SOURCE_PREFIX

public static final String DA_SOURCE_PREFIX
Used by this factory to facilitate the creation and initialization of a chain of data access' if the outer 'wrappers' implement DecoratorDA. The reserved key prefix that when present in the configuration ValueMap is used to extract a sub-map via the getNestedValueMap(prefix) on ValueMap. This sub-map is then passed off to the source of the DecoratorDA.

The value of this key is always (note the trailing dot):

 da.source.
 

See Also:
create(ValueMap, Class), Constant Field Values
Method Detail

create

public static GenericDA create(ValueMap config,
                               Class daInterfaceType)
                        throws DAException
Constructs an instance of the data access implementation specified in the configuration. One required key that must always be present is: DA_IMPLEMENTATION_CLASSNAME with a String value which is the full class name of the implementation of the data access interface.

Immediately after the zero-argument constructor is invoked, the init(ValueMap conf) of GenericDA is invoked to initialize the configuration.

If the targetType is not null, then an additional check is done to ensure that the constructed object can be type cast into that type (failures throw a DAException instead of a ClassCastException).

If the implementation happens to be not only a GenericDA but also a DecoratorDA and there are config map keys starting with the DA_SOURCE_PREFIX, then this create method will attempt to create the chain of decorated data access implementations. This create method will attempt to extract a sub-map using this reserved key prefix via the getNestedValueMap(prefix) on ValueMap. This sub-map is then used to create the source of the DecoratorDA.

Parameters:
config - key/value mapping with everything that is needed to initialize this data access implementation.
daInterfaceType - the class type that this instance is expected to be cast into. Use null to skip this check. If not null and the type does not match what is constructed, then a DAException is thrown.
Returns:
the constructed and initialized instance.
Throws:
DAException - if instance can not be constructed and initialized.
See Also:
create(Reader, Class), create(InputStream, Class), createFromFile(String, Class), create(File, Class), create(URL, Class)

create

public static GenericDA create(Reader rawIn,
                               Class targetType)
                        throws DAException
Create a data access instance using a Reader whose data is in the format described by ValueMap (nearly identical to the format for a Properties file). Internally, the stream will be buffered and the stream will be closed.

Parameters:
rawIn - a stream whose contents can be loaded into a ValueMap with everything that is needed to initialize the instance of data access.
targetType - the class type that this instance is expected to be cast into. Use null to skip this check. If not null and the type does not match what is constructed, then a DataAccessException is thrown.
Returns:
the constructed and initialized instance.
Throws:
DAException - if instance can not be constructed and initialized.
See Also:
create(ValueMap, Class), create(InputStream, Class), createFromFile(String, Class), create(File, Class), create(URL, Class)

create

public static GenericDA create(InputStream rawIn,
                               Class targetType)
                        throws DAException
Create a data access instance using an InputStream whose data is in the format described by ValueMap (nearly identical to the format for a Properties file). Internally, the stream will be buffered and the stream will be closed.

Parameters:
rawIn - a stream whose contents can be loaded into a ValueMap with everything that is needed to initialize the instance of data access.
targetType - the class type that this instance is expected to be cast into. Use null to skip this check. If not null and the type does not match what is constructed, then a DataAccessException is thrown.
Returns:
the constructed and initialized instance.
Throws:
DAException - if instance can not be constructed and initialized.
See Also:
create(ValueMap, Class), create(Reader, Class), createFromFile(String, Class), create(File, Class), create(URL, Class)

create

public static GenericDA create(File file,
                               Class targetType)
                        throws DAException
Create a data access instance using an File whose data is in the format described by ValueMap (nearly identical to the format for a Properties file).

Parameters:
file - file with key/value mapping with everything that is needed to initialize the instance of data access.
targetType - the class type that this instance is expected to be cast into. Use null to skip this check. If not null and the type does not match what is constructed, then a DataAccessException is thrown.
Returns:
the constructed and initialized instance.
Throws:
DAException - if instance can not be constructed and initialized.
See Also:
create(ValueMap, Class), create(Reader, Class), create(InputStream, Class), createFromFile(String, Class), create(URL, Class)

createFromFile

public static GenericDA createFromFile(String filename,
                                       Class targetType)
                                throws DAException
Create a data access instance using the specified filename whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).

Parameters:
filename - file with key/value mapping with everything that is needed to initialize the instance of data access.
targetType - the class type that this instance is expected to be cast into. Use null to skip this check. If not null and the type does not match what is constructed, then a DataAccessException is thrown.
Returns:
the constructed and initialized instance.
Throws:
DAException - if instance can not be constructed and initialized.
See Also:
create(ValueMap, Class), create(Reader, Class), create(InputStream, Class), create(File, Class), create(URL, Class)

create

public static GenericDA create(URL propertiesURL,
                               Class targetType)
                        throws DAException
Create a data access instance using the specified URL whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).

Parameters:
propertiesURL - a URL whose contents can be loaded into a ValueMap with everything that is needed to initialize the instance of data access.
targetType - the class type that this instance is expected to be cast into. Use null to skip this check. If not null and the type does not match what is constructed, then a DataAccessException is thrown.
Returns:
the constructed and initialized instance.
Throws:
DAException - if instance can not be constructed and initialized.
See Also:
create(ValueMap, Class), create(Reader, Class), create(InputStream, Class), createFromFile(String, Class), create(File, Class)

createFromResource

public static GenericDA createFromResource(String resourceLocation,
                                           Class targetType)
                                    throws DAException
Create a data access instance using the specified resourceLocation whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).

Parameters:
resourceLocation - resource with key/value mapping with everything that is needed to initialize the instance of data access.
targetType - the class type that this instance is expected to be cast into. Use null to skip this check. If not null and the type does not match what is constructed, then a DataAccessException is thrown.
Returns:
the constructed and initialized instance.
Throws:
DAException - if instance can not be constructed and initialized.
See Also:
create(ValueMap, Class), create(Reader, Class), create(InputStream, Class), create(File, Class), create(URL, Class)

ProgramixGenericLib v4.0.0

Copyright © 2001-2007 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.