Package org.apache.torque
Class Torque
java.lang.Object
org.apache.torque.Torque
A static facade wrapper around the Torque implementation (which is in
TorqueInstance).
- Version:
- $Id: Torque.java 1925092 2025-04-15 09:14:33Z mwriedt $
- Author:
- Daniel Rall, Magnús Ãór Torfason, Jason van Zyl, Rafal Krzewski, Martin Poeschl, Henning P. Schmiedehausen, Kurt Schrader
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringName of config property to determine whether caching is used.static final StringThe prefix for configuring the database adapters and the default database.static final StringThe key used to configure the name of the default database.static final Stringdefault schema name for the configurationstatic final String"defaults" Key for the configurationstatic final StringA prefix forManagerproperties in the configuration.static final StringAServiceproperty determining its implementing class name .static final String"schema" Key for the configurationstatic final StringThe prefix for all configuration keys used by Torque.static final StringThe prefix for configuring the transaction manger. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCloses a connection.static AdaptergetAdapter(String name) Returns the database adapter for a specific database name.static org.apache.commons.configuration2.ConfigurationGet the configuration for this component.static ConnectionThis method returns a Connection from the default pool.static ConnectiongetConnection(String name) This method returns a Connecton using the given database name.static ConnectiongetConnection(String name, String username, String password) This method returns a Connecton using the given parameters.static DatabasegetDatabase(String name) Returns the database for the given key.static DatabaseMapReturns the default database map information.static DatabaseMapgetDatabaseMap(String name) Returns the database map information for a given database.Returns a Map containing all Databases registered to Torque.static StringReturns the name of the default database.static TorqueInstanceRetrieves the singleTorqueInstanceused by this class.static <T extends AbstractBaseManager<? extends Persistent>>
TgetManager(String name) This method returns a Manager for the given name.static <T extends AbstractBaseManager<? extends Persistent>>
TgetManager(String name, String defaultClassName) This methods returns either the Manager from the configuration file, or the default one provided by the generated code.static DatabasegetOrCreateDatabase(String name) Returns the database for the keydatabaseName.static <T,P extends BasePeerImpl<T>>
PgetPeerInstance(Class<T> omClass) This method returns a PeerImpl for the given class.static StringThis method returns the current schema for a database connectionstatic voidInitialization of Torque with a path to a properties file.static voidinit(org.apache.commons.configuration2.Configuration conf) Initialization of Torque with a configuration.static booleanisInit()Determine whether Torque has already been initialized.static voidregisterIDBroker(IDBroker idBroker) Registers an id broker.static <T> voidregisterPeerInstance(Class<T> omClass, BasePeerImpl<T> peerInstance) This method registers a PeerImpl for a given class.static voidsetConfiguration(org.apache.commons.configuration2.Configuration conf) Sets the configuration for Torque and all dependencies.static voidsetInstance(TorqueInstance instance) Sets the singleTorqueInstanceused by this class.static voidSets the current schema for a database connectionstatic voidshutdown()Shuts down the service.
-
Field Details
-
TORQUE_KEY
The prefix for all configuration keys used by Torque.- See Also:
-
DATABASE_KEY
The prefix for configuring the database adapters and the default database.- See Also:
-
DEFAULT_KEY
The key used to configure the name of the default database.- See Also:
-
SCHEMA_KEY
"schema" Key for the configuration- See Also:
-
DEFAULTS_KEY
"defaults" Key for the configuration- See Also:
-
DEFAULT_SCHEMA_KEY
default schema name for the configuration- See Also:
-
MANAGER_PREFIX
A prefix forManagerproperties in the configuration.- See Also:
-
MANAGER_SUFFIX
AServiceproperty determining its implementing class name .- See Also:
-
CACHE_KEY
Name of config property to determine whether caching is used.- See Also:
-
TRANSACTION_MANAGER_KEY
The prefix for configuring the transaction manger.- See Also:
-
-
Method Details
-
getInstance
Retrieves the singleTorqueInstanceused by this class.- Returns:
- Our singleton.
-
setInstance
Sets the singleTorqueInstanceused by this class. This is used by the Avalon component to make sure that only one instance of Torque exists.- Parameters:
instance- Our singleton.
-
init
Initialization of Torque with a path to 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 static void init(org.apache.commons.configuration2.Configuration conf) throws TorqueException Initialization of Torque with a configuration.- Parameters:
conf- The Torque configuration.- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
isInit
public static boolean isInit()Determine whether Torque has already been initialized.- Returns:
- true if Torque is already initialized
-
setConfiguration
public static void setConfiguration(org.apache.commons.configuration2.Configuration conf) throws TorqueException Sets the configuration for Torque and all dependencies.- Parameters:
conf- the Configuration- Throws:
TorqueException- if the configuration does not contain any keys starting withTorque.TORQUE_KEY.
-
getConfiguration
public static org.apache.commons.configuration2.Configuration getConfiguration()Get the configuration for this component.- Returns:
- the Configuration
-
getManager
This method returns a Manager for the given name.- Type Parameters:
T- the type of the manager class- Parameters:
name- name of the manager.- Returns:
- The requested Manager.
-
getManager
public static <T extends AbstractBaseManager<? extends Persistent>> T getManager(String name, String defaultClassName) This methods returns either the Manager from the configuration file, or the default one provided by the generated code.- Type Parameters:
T- the type of the manager class- Parameters:
name- name of the manager.defaultClassName- the class to use if name has not been configured.- Returns:
- a Manager
-
registerPeerInstance
This method registers a PeerImpl for a given class.- Type Parameters:
T- the type of the OM class- Parameters:
omClass- the class of the associated OM objectpeerInstance- PeerImpl instance
-
getPeerInstance
This method returns a PeerImpl for the given class.- Type Parameters:
T- the type of the OM classP- the type of the peer instance class- Parameters:
omClass- the class of the associated OM object- Returns:
- a PeerImpl instance
-
shutdown
Shuts down the service. This method halts the IDBroker's daemon thread in all of the DatabaseMap's. It also closes all SharedPoolDataSourceFactories and PerUserPoolDataSourceFactories initialized by Torque.- Throws:
TorqueException- if a DataSourceFactory could not be closed cleanly. Only the first exception is rethrown, any following exceptions are logged but ignored.
-
getDatabaseMap
Returns the default database map information.- Returns:
- A DatabaseMap.
- Throws:
TorqueException- if Torque is not initialized.
-
getDatabaseMap
Returns the database map information for a given database.- Parameters:
name- The name of the database corresponding to theDatabaseMapto retrieve, or null for the default database.- Returns:
- The named
DatabaseMap, not null. - Throws:
TorqueException- if Torque is not initialized and name is null.
-
registerIDBroker
Registers an id broker. If Torque is already initialized, the id broker is started. If Torque is not initialized, the id broker will be started on initialization.- Parameters:
idBroker- the id broker to register, not null.- Throws:
NullPointerException- if idBroker is null.
-
getConnection
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
This method returns a Connecton using the given database name.- Parameters:
name- The database name.- Returns:
- a database connection to the database with the given name.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
getConnection
public static Connection getConnection(String name, String username, 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 to the database with the given name.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
getAdapter
Returns the database adapter for a specific database name.- Parameters:
name- the database name, or null for the default db.- Returns:
- The corresponding database adapter, or null if no database adapter is defined for the given database.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
getDefaultDB
Returns the name of the default database.- Returns:
- name of the default DB, or null if Torque is not initialized yet
-
closeConnection
Closes a connection.- Parameters:
con- A Connection to close.
-
setSchema
Sets the current schema for a database connection- Parameters:
name- The database name.schema- The current schema name.- Throws:
NullPointerException- if databaseName is null.
-
getSchema
This method returns the current schema for a database connection- Parameters:
name- The database name.- Returns:
- The current schema name. Null means, no schema has been set.
- Throws:
TorqueException- if Torque is not yet initialized.
-
getDatabase
Returns the database for the given key.- Parameters:
name- the key to get the database for, or null for the default database.- Returns:
- the Database for the given name, or null if no database exists for the given name.
- Throws:
TorqueException- if Torque is not yet initialized.
-
getOrCreateDatabase
Returns the database for the keydatabaseName. If no database is associated to the specified key, a new database is created, mapped to the specified key, and returned.- Parameters:
name- the key to get the database for, not null.- Returns:
- the database associated with specified key, or the newly created database, never null.
- Throws:
IllegalArgumentException- if databaseName is null.
-
getDatabases
Returns a Map containing all Databases registered to Torque. The key of the Map is the name of the database, and the value is the database instance.Note that in the very special case where a new database which is not configured in Torque's configuration gets known to Torque at a later time, the returned map may change, and there is no way to protect you against this. However, Databases should be initialized in the init() method, so this will not happen if Torque is used properly.
- Returns:
- a Map containing all Databases known to Torque, never null.
- Throws:
TorqueException- if Torque is not yet initialized.
-