com.townleyenterprises.persistence
Class PersistenceConfig

java.lang.Object
  extended bycom.townleyenterprises.persistence.PersistenceConfig

public final class PersistenceConfig
extends java.lang.Object

This class is used to provide a flexible mechanism to resolving the required database connection parameter values and encapsulate the differences between various database vendor connection URLs.

Properties

This class uses a set of properties to provide overridable values for making the connection to a specific server. The master property is the type of database to be used, as this value is used to provide overrides. This mechanism allows completely different database configurations to coexist enabling easy swapping of the connectivity information.

Property Description
database.type The symbolic name of the database type. This name is entirely dependent on the site, however the name or abbreviation of the database vendor is generally used, e.g. postgresql.
database.user The login name for the database connection.
database.password The password for the database connection.
database.host The host name for the database connection.
database.name The name of the database for the connection.
${database.type}.user The login name for the database connection for the given database type. This value overrides the default parameter.
${database.type}.password The password for the database connection for the given database type. This value overrides the default parameter.
${database.type}.host The host name for the database connection for the given database type. This value overrides the default parameter.
${database.type}.database.name The name of the database for the connection for the given database type. This value overrides the default parameter.
${database.type}.jdbc.params The parameter names necessary to supply to the JDBC driver URL. Parameter names can be anything, however the special parameter name database refers to the value of the database name. Each parameter should have value in the properties file for the current database type.
${database.type}.jdbc.url This property is a string processed by the java.text.MessageFormat class to generate the correct JDBC URL for the connection based on the parameter values specified by the ${database.type}.jdbc.params property.
${database.type}.jdbc.driver This property specifies the fully-qualified class name of the JDBC driver for this database type.
${database.type}.${param} For each token in the value of the ${database.type}.jdbc.params property, a parameter may be defined based on the database type and the specific token. The value of this parameter is used to specify the appropriate arguments to the ${database.type}.jdbc.url property value.

Example

The above description becomes clearer with a concrete example. Given the need to run an application against a test database server (PostgreSQL) and a production database server (Oracle), the following properties file could be supplied. Based on changing only the database type property value, the application can easily be switched from one configuration to another.

 database.type=postgresql
 #database.type=oracle
 database.user=appname
 database.password=secret
 database.name=data

 postgresql.host=testbox.myco.com
 postgresql.port=5432
 postgresql.jdbc.params=host port database
 postgresql.jdbc.url=jdbc:postgresql://{0}:{1}/{2}
 postgresql.jdbc.driver=org.postgresql.Driver

 oracle.host=production.myco.com
 oracle.port=1521
 oracle.database.name=production
 oracle.jdbc.params=host port database
 oracle.jdbc.url=jdbc:oracle:thin:@{0}:{1}:{2}
 oracle.jdbc.driver=oracle.jdbc.OracleDriver
 

Since:
2.1
Version:
$Id: PersistenceConfig.java,v 1.1 2004/01/28 20:03:17 atownley Exp $
Author:
Andrew S. Townley

Constructor Summary
PersistenceConfig()
          This version of the constructor works with the AppConfig class to provide the property information.
PersistenceConfig(java.util.Properties properties)
           
 
Method Summary
 java.lang.String getConnectionURL()
          This method returns the URL for the current connection, correctly populated with all of the parameter values.
 java.lang.String getDriverName()
          This method returns the driver for the current connection.
 java.lang.String getHost()
          This method returns the host for the current connection.
 java.lang.String getPassword()
          This method returns the password for the current connection.
 java.lang.String getUser()
          This method returns the username for the current connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PersistenceConfig

public PersistenceConfig(java.util.Properties properties)

PersistenceConfig

public PersistenceConfig()
This version of the constructor works with the AppConfig class to provide the property information.

Method Detail

getUser

public java.lang.String getUser()
This method returns the username for the current connection. If a user is set for the current database type, that user is returned, otherwise the default user is returned.

Returns:
the user or null if not found

getPassword

public java.lang.String getPassword()
This method returns the password for the current connection. If a password is set for the current database type, that password is returned, otherwise the default password is returned.

Returns:
the password or null if not found

getHost

public java.lang.String getHost()
This method returns the host for the current connection. If a host is set for the current database type, that host is returned, otherwise the default host is returned.

Returns:
the host or null if not found

getDriverName

public java.lang.String getDriverName()
This method returns the driver for the current connection. If a driver is set for the current database type, that driver is returned, otherwise the default driver is returned.

Returns:
the driver or null if not found

getConnectionURL

public java.lang.String getConnectionURL()
This method returns the URL for the current connection, correctly populated with all of the parameter values.

Returns:
the URL as a string or null


Copyright © 2002-2004, Andrew S. Townley and Townley Enterprises. All Rights Reserved.
This project is hosted on http://te-code.sourceforge.net.