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

Author:
Brian Weaver , OpenNMS

Constructor Summary
DataSourceFactory()
           
 
Method Summary
static void close()
           
 Connection getConnection()
          Return a new database connection to the database configured in the opennms-database.xml.
 Connection getConnection(String dsName)
           
 Connection getConnection(String username, String password)
           
static DataSource getDataSource()
          Return the datasource configured for the database
static DataSource getDataSource(String dsName)
           
static DataSource getInstance()
           Return the singleton instance of this factory.
static DataSource getInstance(String name)
           
 int getLoginTimeout()
           
 int getLoginTimeout(String dsName)
           
 PrintWriter getLogWriter()
           
 PrintWriter getLogWriter(String dsName)
           
static void init()
          Load the config from the default config file and create the singleton instance of this factory.
static void init(String dsName)
           
 void initialize()
           
 boolean isWrapperFor(Class<?> iface)
           
static void setInstance(DataSource singleton)
           
static void setInstance(String dsName, DataSource singleton)
           
 void setLoginTimeout(int seconds)
           
 void setLoginTimeout(String dsName, int seconds)
           
 void setLogWriter(PrintWriter out)
           
 void setLogWriter(String dsName, PrintWriter out)
           
<T> T
unwrap(Class<T> iface)
           
 
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
PropertyVetoException
ClassNotFoundException

init

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

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)

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
Throws:
SQLException

setInstance

public static void setInstance(DataSource singleton)

setInstance

public static void setInstance(String dsName,
                               DataSource singleton)

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)

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
Specified by:
getConnection in interface DataSource
Throws:
SQLException

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Specified by:
getLogWriter in interface CommonDataSource
Throws:
SQLException

getLogWriter

public PrintWriter getLogWriter(String dsName)
                         throws SQLException
Throws:
SQLException

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
Throws:
SQLException

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
Throws:
SQLException

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Specified by:
getLoginTimeout in interface CommonDataSource
Throws:
SQLException

getLoginTimeout

public int getLoginTimeout(String dsName)
                    throws SQLException
Throws:
SQLException

initialize

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

close

public static void close()
                  throws SQLException
Throws:
SQLException

unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException
Specified by:
unwrap in interface Wrapper
Throws:
SQLException

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException
Specified by:
isWrapperFor in interface Wrapper
Throws:
SQLException


Copyright © 2009. All Rights Reserved.