org.apache.torque
Class TorqueInstance

java.lang.Object
  extended byorg.apache.torque.TorqueInstance

public class TorqueInstance
extends java.lang.Object

The core of Torque's implementation. Both the classic Torque static wrapper and the TorqueComponent Avalon implementation leverage this class.

Version:
$Id: TorqueInstance.java,v 1.5 2003/08/05 18:09:35 mpoeschl Exp $
Author:
Daniel Rall, Magnús Þór Torfason, Jason van Zyl, Rafal Krzewski, Martin Poeschl, Henning P. Schmiedehausen, Kurt Schrader

Field Summary
private  java.util.Map adapterMap
          The cache of DB adapter keys
private  org.apache.commons.configuration.Configuration conf
          Torque-specific configuration.
private  java.util.Map dbMaps
          The global cache of database maps
private static java.lang.String DEFAULT_NAME
          A constant for default.
private  java.lang.String defaultDBName
          The db name that is specified as the default in the property file
private  java.util.Map dsFactoryMap
          The cache of DataSourceFactory's
private  boolean isInit
          flag to set to true once this class has been initialized
private static org.apache.commons.logging.Log log
          Logging
private  java.util.Map managers
          A repository of Manager instances.
private  java.util.List mapBuilders
          Store mapbuilder classnames for peers that have been referenced prior to Torque being initialized.
 
Constructor Summary
TorqueInstance()
          Creates a new instance with default configuration.
 
Method Summary
 void closeConnection(java.sql.Connection con)
          Closes a connection.
 org.apache.commons.configuration.Configuration getConfiguration()
          Get the configuration for this component.
 java.sql.Connection getConnection()
          This method returns a Connection from the default pool.
 java.sql.Connection getConnection(java.lang.String name)
           
 java.sql.Connection getConnection(java.lang.String name, java.lang.String username, java.lang.String password)
          This method returns a Connecton using the given parameters.
 DatabaseMap getDatabaseMap()
          Returns the default database map information.
 DatabaseMap getDatabaseMap(java.lang.String name)
          Returns the database map information.
private  java.lang.String getDatabaseProperty(java.lang.String db, java.lang.String prop)
          Returns the specified property of the given database, or the empty string if no value is set for the property.
 DB getDB(java.lang.String name)
          Returns database adapter for a specific connection pool.
 java.lang.String getDefaultDB()
          Returns the name of the default database.
 AbstractBaseManager getManager(java.lang.String name)
          This method returns a Manager for the given name.
 AbstractBaseManager getManager(java.lang.String name, java.lang.String defaultClassName)
          This methods returns either the Manager from the configuration file, or the default one provided by the generated code.
 void init(org.apache.commons.configuration.Configuration conf)
          Initialization of Torque with a properties file.
 void init(java.lang.String configFile)
          Initialization of Torque with a properties file.
private  void initAdapters(org.apache.commons.configuration.Configuration conf)
           
private  DatabaseMap initDatabaseMap(java.lang.String name)
          Creates and initializes the mape for the named database.
private  void initDataSourceFactories(org.apache.commons.configuration.Configuration conf)
           
private  void initialize()
          Initializes this instance of Torque.
private  void initManager(java.lang.String name, java.lang.String className)
          Initialize a manager
protected  void initManagerMappings(org.apache.commons.configuration.Configuration conf)
          Creates a mapping between classes and their manager classes.
 boolean isInit()
          Determine whether Torque has already been initialized.
 void registerMapBuilder(java.lang.String className)
          Register a MapBuilder
private  void resetConfiguration()
          Resets some internal configuration variables to their defaults.
 void setConfiguration(org.apache.commons.configuration.Configuration conf)
          Sets the configuration for Torque and all dependencies.
private  void setupIdTable(DatabaseMap map)
          Setup IDBroker's table information within given database map.
 void shutdown()
          Shuts down the service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static org.apache.commons.logging.Log log
Logging


DEFAULT_NAME

private static final java.lang.String DEFAULT_NAME
A constant for default.

See Also:
Constant Field Values

defaultDBName

private java.lang.String defaultDBName
The db name that is specified as the default in the property file


dbMaps

private java.util.Map dbMaps
The global cache of database maps


dsFactoryMap

private java.util.Map dsFactoryMap
The cache of DataSourceFactory's


adapterMap

private java.util.Map adapterMap
The cache of DB adapter keys


managers

private java.util.Map managers
A repository of Manager instances.


conf

private org.apache.commons.configuration.Configuration conf
Torque-specific configuration.


isInit

private boolean isInit
flag to set to true once this class has been initialized


mapBuilders

private java.util.List mapBuilders
Store mapbuilder classnames for peers that have been referenced prior to Torque being initialized. This can happen if torque om/peer objects are serialized then unserialized prior to Torque being reinitialized. This condition exists in a normal catalina restart.

Constructor Detail

TorqueInstance

public TorqueInstance()
Creates a new instance with default configuration.

See Also:
resetConfiguration()
Method Detail

initialize

private void initialize()
                 throws TorqueException
Initializes this instance of Torque.

Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
See Also:
org.apache.stratum.lifecycle.Initializable

initAdapters

private final void initAdapters(org.apache.commons.configuration.Configuration conf)
                         throws TorqueException
Parameters:
conf - the Configuration representing the properties file
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

initDataSourceFactories

private void initDataSourceFactories(org.apache.commons.configuration.Configuration conf)
                              throws TorqueException
Parameters:
conf - the Configuration representing the properties file
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

init

public void init(java.lang.String configFile)
          throws TorqueException
Initialization of Torque with a properties file.

Parameters:
configFile - The absolute path to the configuration file.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

init

public void init(org.apache.commons.configuration.Configuration conf)
          throws TorqueException
Initialization of Torque with a properties file.

Parameters:
conf - The Torque configuration.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

initManagerMappings

protected void initManagerMappings(org.apache.commons.configuration.Configuration conf)
                            throws TorqueException
Creates a mapping between classes and their manager classes. The mapping is built according to settings present in properties file. The entries should have the following form:
 torque.managed_class.com.mycompany.Myclass.manager= \
          com.mycompany.MyManagerImpl
 services.managed_class.com.mycompany.Myotherclass.manager= \
          com.mycompany.MyOtherManagerImpl
 

Generic ServiceBroker provides no Services.

Parameters:
conf - the Configuration representing the properties file
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

initManager

private void initManager(java.lang.String name,
                         java.lang.String className)
                  throws TorqueException
Initialize a manager

Parameters:
name - name of the manager
className - name of the manager class
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

isInit

public boolean isInit()
Determine whether Torque has already been initialized.

Returns:
true if Torque is already initialized

setConfiguration

public void setConfiguration(org.apache.commons.configuration.Configuration conf)
Sets the configuration for Torque and all dependencies.

Parameters:
conf - the Configuration

getConfiguration

public org.apache.commons.configuration.Configuration getConfiguration()
Get the configuration for this component.

Returns:
the Configuration

getManager

public AbstractBaseManager getManager(java.lang.String name)
This method returns a Manager for the given name.

Parameters:
name - name of the manager
Returns:
a Manager

getManager

public AbstractBaseManager getManager(java.lang.String name,
                                      java.lang.String defaultClassName)
This methods returns either the Manager from the configuration file, or the default one provided by the generated code.

Parameters:
name - name of the manager
defaultClassName - the class to use if name has not been configured
Returns:
a Manager

shutdown

public void shutdown()
Shuts down the service. This method halts the IDBroker's daemon thread in all of the DatabaseMap's.


resetConfiguration

private void resetConfiguration()
Resets some internal configuration variables to their defaults.


getDatabaseMap

public DatabaseMap getDatabaseMap()
                           throws TorqueException
Returns the default database map information.

Returns:
A DatabaseMap.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

getDatabaseMap

public DatabaseMap getDatabaseMap(java.lang.String name)
                           throws TorqueException
Returns the database map information. Name relates to the name of the connection pool to associate with the map.

Parameters:
name - The name of the database corresponding to the DatabaseMap to retrieve.
Returns:
The named DatabaseMap.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

initDatabaseMap

private final DatabaseMap initDatabaseMap(java.lang.String name)
                                   throws TorqueException
Creates and initializes the mape for the named database. Assumes that dbMaps member is sync'd.

Parameters:
name - The name of the database to map.
Returns:
The desired map.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

registerMapBuilder

public void registerMapBuilder(java.lang.String className)
Register a MapBuilder

Parameters:
className - the MapBuilder

getDatabaseProperty

private java.lang.String getDatabaseProperty(java.lang.String db,
                                             java.lang.String prop)
Returns the specified property of the given database, or the empty string if no value is set for the property.

Parameters:
db - The name of the database whose property to get.
prop - The name of the property to get.
Returns:
The property's value.

setupIdTable

private final void setupIdTable(DatabaseMap map)
Setup IDBroker's table information within given database map. This method should be called on all new database map to ensure that IDBroker functionality is available in all databases used by the application.

Parameters:
map - the DataBaseMap to setup.

getConnection

public java.sql.Connection getConnection()
                                  throws TorqueException
This method returns a Connection from the default pool.

Returns:
The requested connection.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

getConnection

public java.sql.Connection getConnection(java.lang.String name)
                                  throws TorqueException
Parameters:
name - The database name.
Returns:
a database connection
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

getConnection

public java.sql.Connection getConnection(java.lang.String name,
                                         java.lang.String username,
                                         java.lang.String password)
                                  throws TorqueException
This method returns a Connecton using the given parameters. You should only use this method if you need user based access to the database!

Parameters:
name - The database name.
username - The name of the database user.
password - The password of the database user.
Returns:
A Connection.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

getDB

public DB getDB(java.lang.String name)
         throws TorqueException
Returns database adapter for a specific connection pool.

Parameters:
name - A pool name.
Returns:
The corresponding database adapter.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

getDefaultDB

public java.lang.String getDefaultDB()
Returns the name of the default database.

Returns:
name of the default DB

closeConnection

public void closeConnection(java.sql.Connection con)
Closes a connection.

Parameters:
con - A Connection to close.


Copyright © 2000-2003 Apache Software Foundation. All Rights Reserved.