org.opennms.netmgt.config
Class DataSourceFactory

java.lang.Object
  extended by org.opennms.netmgt.config.DataSourceFactory
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource

public final class DataSourceFactory
extends Object
implements DataSource

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

Version:
$Id: $
Author:
Brian Weaver , OpenNMS , Brian Weaver , OpenNMS

Constructor Summary
DataSourceFactory()
           
 
Method Summary
static void close()
          close
 Connection getConnection()
          Return a new database connection to the database configured in the opennms-database.xml.
 Connection getConnection(String dsName)
          getConnection
 Connection getConnection(String username, String password)
          
static DataSource getDataSource()
          Return the datasource configured for the database
static DataSource getDataSource(String dsName)
          getDataSource
static DataSource getInstance()
           Return the singleton instance of this factory.
static DataSource getInstance(String name)
          getInstance
 int getLoginTimeout()
          getLoginTimeout
 int getLoginTimeout(String dsName)
          getLoginTimeout
 PrintWriter getLogWriter()
          getLogWriter
 PrintWriter getLogWriter(String dsName)
          getLogWriter
static void init()
          Load the config from the default config file and create the singleton instance of this factory.
static void init(String dsName)
          init
 void initialize()
          initialize
 boolean isWrapperFor(Class<?> iface)
          isWrapperFor
static void setInstance(DataSource singleton)
          setInstance
static void setInstance(String dsName, DataSource singleton)
          setInstance
 void setLoginTimeout(int seconds)
          
 void setLoginTimeout(String dsName, int seconds)
          setLoginTimeout
 void setLogWriter(PrintWriter out)
          
 void setLogWriter(String dsName, PrintWriter out)
          setLogWriter
<T> T
unwrap(Class<T> iface)
          unwrap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataSourceFactory

public DataSourceFactory()
Method Detail

init

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

Throws:
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.
SQLException - if any.
PropertyVetoException - if any.
IOException - if any.
org.exolab.castor.xml.MarshalException - if any.
org.exolab.castor.xml.ValidationException - if any.
ClassNotFoundException - if any.

init

public static void init(String dsName)
                 throws IOException,
                        org.exolab.castor.xml.MarshalException,
                        org.exolab.castor.xml.ValidationException,
                        ClassNotFoundException,
                        PropertyVetoException,
                        SQLException

init

Parameters:
dsName - a String object.
Throws:
IOException - if any.
org.exolab.castor.xml.MarshalException - if any.
org.exolab.castor.xml.ValidationException - if any.
ClassNotFoundException - if any.
PropertyVetoException - if any.
SQLException - if any.

getInstance

public static DataSource 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:
IllegalStateException - Thrown if the factory has not yet been initialized.

getInstance

public static DataSource getInstance(String name)

getInstance

Parameters:
name - a String object.
Returns:
a DataSource object.

getConnection

public Connection getConnection()
                         throws 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 DataSource
Returns:
a new database connection to the database configured in the opennms-database.xml
Throws:
SQLException - Thrown if there is an error opening the connection to the database.

getConnection

public Connection getConnection(String dsName)
                         throws SQLException

getConnection

Parameters:
dsName - a String object.
Returns:
a Connection object.
Throws:
SQLException - if any.

setInstance

public static void setInstance(DataSource singleton)

setInstance

Parameters:
singleton - a DataSource object.

setInstance

public static void setInstance(String dsName,
                               DataSource singleton)

setInstance

Parameters:
dsName - a String object.
singleton - a DataSource object.

getDataSource

public static DataSource getDataSource()
Return the datasource configured for the database

Returns:
the datasource configured for the database

getDataSource

public static DataSource getDataSource(String dsName)

getDataSource

Parameters:
dsName - a String object.
Returns:
a DataSource object.

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException

Specified by:
getConnection in interface DataSource
Throws:
SQLException

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException

getLogWriter

Specified by:
getLogWriter in interface CommonDataSource
Returns:
a PrintWriter object.
Throws:
SQLException - if any.

getLogWriter

public PrintWriter getLogWriter(String dsName)
                         throws SQLException

getLogWriter

Parameters:
dsName - a String object.
Returns:
a PrintWriter object.
Throws:
SQLException - if any.

setLogWriter

public void setLogWriter(PrintWriter out)
                  throws SQLException

Specified by:
setLogWriter in interface CommonDataSource
Throws:
SQLException

setLogWriter

public void setLogWriter(String dsName,
                         PrintWriter out)
                  throws SQLException

setLogWriter

Parameters:
dsName - a String object.
out - a PrintWriter object.
Throws:
SQLException - if any.

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws SQLException

Specified by:
setLoginTimeout in interface CommonDataSource
Throws:
SQLException

setLoginTimeout

public void setLoginTimeout(String dsName,
                            int seconds)
                     throws SQLException

setLoginTimeout

Parameters:
dsName - a String object.
seconds - a int.
Throws:
SQLException - if any.

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException

getLoginTimeout

Specified by:
getLoginTimeout in interface CommonDataSource
Returns:
a int.
Throws:
SQLException - if any.

getLoginTimeout

public int getLoginTimeout(String dsName)
                    throws SQLException

getLoginTimeout

Parameters:
dsName - a String object.
Returns:
a int.
Throws:
SQLException - if any.

initialize

public void initialize()
                throws org.exolab.castor.xml.MarshalException,
                       org.exolab.castor.xml.ValidationException,
                       IOException,
                       ClassNotFoundException

initialize

Throws:
org.exolab.castor.xml.MarshalException - if any.
org.exolab.castor.xml.ValidationException - if any.
IOException - if any.
ClassNotFoundException - if any.

close

public static void close()
                  throws SQLException

close

Throws:
SQLException - if any.

unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException

unwrap

Specified by:
unwrap in interface Wrapper
Type Parameters:
T - a T object.
Parameters:
iface - a Class object.
Returns:
a T object.
Throws:
SQLException - if any.

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException

isWrapperFor

Specified by:
isWrapperFor in interface Wrapper
Parameters:
iface - a Class object.
Returns:
a boolean.
Throws:
SQLException - if any.


Copyright © 2011. All Rights Reserved.