org.opennms.netmgt.config
Class DataSourceFactory

java.lang.Object
  extended by org.opennms.netmgt.config.DataSourceFactory
All Implemented Interfaces:
javax.sql.DataSource

public final class DataSourceFactory
extends java.lang.Object
implements javax.sql.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()
           
 java.sql.Connection getConnection()
          Return a new database connection to the database configured in the opennms-database.xml.
 java.sql.Connection getConnection(java.lang.String dsName)
           
 java.sql.Connection getConnection(java.lang.String username, java.lang.String password)
           
 org.exolab.castor.jdo.conf.Database getDatabase()
          Return the database that was configured in the config file
static javax.sql.DataSource getDataSource()
          Return the datasource configured for the database
static javax.sql.DataSource getDataSource(java.lang.String dsName)
           
static javax.sql.DataSource getInstance()
           Return the singleton instance of this factory.
static javax.sql.DataSource getInstance(java.lang.String name)
           
 int getLoginTimeout()
           
 int getLoginTimeout(java.lang.String dsName)
           
 java.io.PrintWriter getLogWriter()
           
 java.io.PrintWriter getLogWriter(java.lang.String dsName)
           
static void init()
          Load the config from the default config file and create the singleton instance of this factory.
static void init(java.lang.String dsName)
           
 void initialize()
           
 boolean isWrapperFor(java.lang.Class<?> iface)
           
static void setInstance(javax.sql.DataSource singleton)
           
static void setInstance(java.lang.String dsName, javax.sql.DataSource singleton)
           
 void setLoginTimeout(int seconds)
           
 void setLoginTimeout(java.lang.String dsName, int seconds)
           
 void setLogWriter(java.io.PrintWriter out)
           
 void setLogWriter(java.lang.String dsName, java.io.PrintWriter out)
           
<T> T
unwrap(java.lang.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 java.io.IOException,
                        org.exolab.castor.xml.MarshalException,
                        org.exolab.castor.xml.ValidationException,
                        java.lang.ClassNotFoundException,
                        java.beans.PropertyVetoException,
                        java.sql.SQLException
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.sql.SQLException
java.beans.PropertyVetoException
java.lang.ClassNotFoundException

init

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

getInstance

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

getInstance

public static javax.sql.DataSource getInstance(java.lang.String name)

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 javax.sql.DataSource
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.

getConnection

public java.sql.Connection getConnection(java.lang.String dsName)
                                  throws java.sql.SQLException
Throws:
java.sql.SQLException

setInstance

public static void setInstance(javax.sql.DataSource singleton)

setInstance

public static void setInstance(java.lang.String dsName,
                               javax.sql.DataSource 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 static javax.sql.DataSource getDataSource()
Return the datasource configured for the database

Returns:
the datasource configured for the database

getDataSource

public static javax.sql.DataSource getDataSource(java.lang.String dsName)

getConnection

public java.sql.Connection getConnection(java.lang.String username,
                                         java.lang.String password)
                                  throws java.sql.SQLException
Specified by:
getConnection in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException
Specified by:
getLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getLogWriter

public java.io.PrintWriter getLogWriter(java.lang.String dsName)
                                 throws java.sql.SQLException
Throws:
java.sql.SQLException

setLogWriter

public void setLogWriter(java.io.PrintWriter out)
                  throws java.sql.SQLException
Specified by:
setLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLogWriter

public void setLogWriter(java.lang.String dsName,
                         java.io.PrintWriter out)
                  throws java.sql.SQLException
Throws:
java.sql.SQLException

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws java.sql.SQLException
Specified by:
setLoginTimeout in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLoginTimeout

public void setLoginTimeout(java.lang.String dsName,
                            int seconds)
                     throws java.sql.SQLException
Throws:
java.sql.SQLException

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException
Specified by:
getLoginTimeout in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getLoginTimeout

public int getLoginTimeout(java.lang.String dsName)
                    throws java.sql.SQLException
Throws:
java.sql.SQLException

initialize

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

close

public static void close()
                  throws java.sql.SQLException
Throws:
java.sql.SQLException

unwrap

public <T> T unwrap(java.lang.Class<T> iface)
         throws java.sql.SQLException
Throws:
java.sql.SQLException

isWrapperFor

public boolean isWrapperFor(java.lang.Class<?> iface)
                     throws java.sql.SQLException
Throws:
java.sql.SQLException


Copyright © 2007. All Rights Reserved.