ProgramixGenericLib v5.0.1

com.programix.da
Class DataAccessFactory

java.lang.Object
  extended by com.programix.da.DataAccessFactory

public class DataAccessFactory
extends Object

Used to construct and initialize instances of DataAccess using reflection.

Common usage is (where CustomerDataAccess is an interface that extends the DataAccess interface):

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

Author:
Paul Hyde

Field Summary
static String DATA_ACCESS_CLASSNAME_KEY
          The reserved key that retrieves the fully-qualified name (as a String) of the DataAccess implementation class that will be instantiated via reflection.
 
Method Summary
static
<T extends DataAccess>
T
create(File file, Class<T> targetType)
          Create a DataAccess instance using an File whose data is in the format described by ValueMap (nearly identical to the format for a Properties file).
static
<T extends DataAccess>
T
create(InputStream rawIn, Class<T> targetType)
          Create a DataAccess instance using an InputStream whose data is in the format described by ValueMap (nearly identical to the format for a Properties file).
static
<T extends DataAccess>
T
create(Reader rawIn, Class<T> targetType)
          Create a DataAccess instance using a Reader whose data is in the format described by ValueMap (nearly identical to the format for a Properties file).
static
<T extends DataAccess>
T
create(URL propertiesURL, Class<T> targetType)
          Create a DataAccess instance using the specified URL whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).
static
<T extends DataAccess>
T
create(ValueMap config, Class<T> targetType)
          Constructs an instance of the DataAccess implementation specified in the configuration.
static
<T extends DataAccess>
T
createFromFile(String filename, Class<T> targetType)
          Create a DataAccess instance using the specified filename whose data is in the format described by ValueMap(nearly identical to the format for a Properties file).
static
<T extends DataAccess>
T
createFromResource(String resourceLocation, Class<T> targetType)
          Create a DataAccess 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

DATA_ACCESS_CLASSNAME_KEY

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

The value of this key is always:

 data.access.classname
 

See Also:
Constant Field Values
Method Detail

create

public static <T extends DataAccess> T create(ValueMap config,
                                              Class<T> targetType)
                                   throws DataAccessException
Constructs an instance of the DataAccess implementation specified in the configuration. One required key that must always be present is: DATA_ACCESS_CLASSNAME_KEY with a String value which is the full class name.

Immediately after the zero-argument constructor is invoked, the init(ValueMap conf) of DataAccess 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 DataAccessException instead of a ClassCastException).

Parameters:
config - key/value mapping with everything that is needed to initialize the instance of DataAccess.
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:
DataAccessException - 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 <T extends DataAccess> T create(Reader rawIn,
                                              Class<T> targetType)
                                   throws DataAccessException
Create a DataAccess 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 DataAccess.
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:
DataAccessException - 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 <T extends DataAccess> T create(InputStream rawIn,
                                              Class<T> targetType)
                                   throws DataAccessException
Create a DataAccess 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 DataAccess.
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:
DataAccessException - 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 <T extends DataAccess> T create(File file,
                                              Class<T> targetType)
                                   throws DataAccessException
Create a DataAccess 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 DataAccess.
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:
DataAccessException - 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 <T extends DataAccess> T createFromFile(String filename,
                                                      Class<T> targetType)
                                           throws DataAccessException
Create a DataAccess 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 DataAccess.
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:
DataAccessException - 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 <T extends DataAccess> T create(URL propertiesURL,
                                              Class<T> targetType)
                                   throws DataAccessException
Create a DataAccess 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 DataAccess.
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:
DataAccessException - 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 <T extends DataAccess> T createFromResource(String resourceLocation,
                                                          Class<T> targetType)
                                               throws DataAccessException
Create a DataAccess 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 DataAccess.
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:
DataAccessException - 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 v5.0.1

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