OpenNMS API 1.2.3

org.opennms.netmgt.config
Class DatabaseConnectionFactory

java.lang.Object
  extended byorg.opennms.netmgt.config.DatabaseConnectionFactory
All Implemented Interfaces:
DbConnectionFactory

public final class DatabaseConnectionFactory
extends java.lang.Object
implements DbConnectionFactory

This is the singleton class used to load the OpenNMS database configuration from the opennms-database.xml. This provides convenience methods to create database connections to the database configured in this default xml

Note: Users of this class should make sure the init() is called before calling any other method to ensure the config is loaded before accessing other convenience methods

Author:
Brian Weaver , OpenNMS

Nested Class Summary
(package private) static class DatabaseConnectionFactory.CachedConnection
          This class is used to represent a cached database connection within this factory.
 
Field Summary
private  org.exolab.castor.jdo.conf.Database m_database
          The database class loaded from the config file
private  java.util.LinkedList m_dbcCache
          The linked list of cached connections that are reused if the garbage collector has not finalized them yet.
private  java.lang.String m_driverPass
          The cached database password.
private  java.lang.String m_driverUrl
          The cached database URL
private  java.lang.String m_driverUser
          The cached database user
private static boolean m_loaded
          This member is set to true if the configuration file has been loaded.
private static DbConnectionFactory m_singleton
          The singleton instance of this factory
private static int MAX_AGE
          The maximum age before a connection is closed
 
Constructor Summary
private DatabaseConnectionFactory(java.lang.String configFile)
          Private constructor
 
Method Summary
 java.sql.Connection getConnection()
          Return a new database connection to the database configured in the opennms-database.xml.
 org.exolab.castor.jdo.conf.Database getDatabase()
          Return the database that was configured in the config file
 org.exolab.castor.jdo.conf.DataSource getDataSource()
          Return the datasource configured for the database
 org.exolab.castor.jdo.conf.Driver getDriver()
          Return the driver configured for the database
 java.lang.String getEngine()
          Return the engine configured for the database
static DbConnectionFactory getInstance()
           Return the singleton instance of this factory.
 org.exolab.castor.jdo.conf.Jndi getJndi()
          Return the JNDI configuration for the database
 org.exolab.castor.jdo.conf.Mapping[] getMapping()
          Return the mapping configured for the database.
static void init()
          Load the config from the default config file and create the singleton instance of this factory.
private static void reload()
          Reload the config from the default config file
static void setInstance(DbConnectionFactory singleton)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_AGE

private static final int MAX_AGE
The maximum age before a connection is closed

See Also:
Constant Field Values

m_singleton

private static DbConnectionFactory m_singleton
The singleton instance of this factory


m_loaded

private static boolean m_loaded
This member is set to true if the configuration file has been loaded.


m_database

private org.exolab.castor.jdo.conf.Database m_database
The database class loaded from the config file


m_driverUrl

private java.lang.String m_driverUrl
The cached database URL


m_driverUser

private java.lang.String m_driverUser
The cached database user


m_driverPass

private java.lang.String m_driverPass
The cached database password.


m_dbcCache

private java.util.LinkedList m_dbcCache
The linked list of cached connections that are reused if the garbage collector has not finalized them yet.

Constructor Detail

DatabaseConnectionFactory

private DatabaseConnectionFactory(java.lang.String configFile)
                           throws java.io.IOException,
                                  org.exolab.castor.xml.MarshalException,
                                  org.exolab.castor.xml.ValidationException,
                                  java.lang.ClassNotFoundException
Private constructor

Throws:
java.io.IOException - Thrown if the specified config file cannot be read
org.exolab.castor.xml.MarshalException - Thrown if the file does not conform to the schema.
org.exolab.castor.xml.ValidationException - Thrown if the contents do not match the required schema.
java.lang.ClassNotFoundException
Method Detail

init

public static void init()
                 throws java.io.IOException,
                        org.exolab.castor.xml.MarshalException,
                        org.exolab.castor.xml.ValidationException,
                        java.lang.ClassNotFoundException
Load the config from the default config file and create the singleton instance of this factory.

Throws:
java.io.IOException - Thrown if the specified config file cannot be read
org.exolab.castor.xml.MarshalException - Thrown if the file does not conform to the schema.
org.exolab.castor.xml.ValidationException - Thrown if the contents do not match the required schema.
java.lang.ClassNotFoundException

reload

private static void reload()
                    throws java.io.IOException,
                           org.exolab.castor.xml.MarshalException,
                           org.exolab.castor.xml.ValidationException,
                           java.lang.ClassNotFoundException
Reload the config from the default config file

Throws:
java.io.IOException - Thrown if the specified config file cannot be read/loaded
org.exolab.castor.xml.MarshalException - Thrown if the file does not conform to the schema.
org.exolab.castor.xml.ValidationException - Thrown if the contents do not match the required schema.
java.lang.ClassNotFoundException

getInstance

public static DbConnectionFactory getInstance()

Return the singleton instance of this factory. This is the instance of the factory that was last created when the init or reload method was invoked. The instance will not change unless a reload method is invoked.

Returns:
The current factory instance.
Throws:
java.lang.IllegalStateException - Thrown if the factory has not yet been initialized.

setInstance

public static void setInstance(DbConnectionFactory singleton)

getDatabase

public org.exolab.castor.jdo.conf.Database getDatabase()
Return the database that was configured in the config file

Returns:
the database that was configured in the config file

getDataSource

public org.exolab.castor.jdo.conf.DataSource getDataSource()
Return the datasource configured for the database

Returns:
the datasource configured for the database

getDriver

public org.exolab.castor.jdo.conf.Driver getDriver()
Return the driver configured for the database

Returns:
the driver configured for the database

getEngine

public java.lang.String getEngine()
Return the engine configured for the database

Returns:
the engine configured for the database

getJndi

public org.exolab.castor.jdo.conf.Jndi getJndi()
Return the JNDI configuration for the database

Returns:
the JNDI configuration for the database

getMapping

public org.exolab.castor.jdo.conf.Mapping[] getMapping()
Return the mapping configured for the database.

Returns:
the mapping configured for the database.

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Return a new database connection to the database configured in the opennms-database.xml. The database connection is not managed by the factory and must be release by the caller by using the close method.

Specified by:
getConnection in interface DbConnectionFactory
Returns:
a new database connection to the database configured in the opennms-database.xml
Throws:
java.sql.SQLException - Thrown if there is an error opening the connection to the database.

OpenNMS API 1.2.3

Generated by eevans on May 29 2005 2015.