All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javasimon.jdbcx.SimonDataSource Maven / Gradle / Ivy

The newest version!
package org.javasimon.jdbcx;

import org.javasimon.jdbc.SimonConnection;

import javax.sql.DataSource;
import java.sql.*;
import java.sql.Connection;
import java.lang.reflect.Method;

/**
 * Wrapper class for real DataSource implementation, produces standard {@link Connection}
 * object.
 * 

* To use SimonDataSource, MUST properties are: *

    *
  • realDataSourceClassName - full qualified classname of real Datasource * implementation
  • *
  • url - JDBC connection URL (no special Simon added tags are needed)
  • *
  • user - DB user name
  • *
  • password - DB user name
  • *
* MAY properties are: *
    *
  • perfix - Simon prefix (default: org.javasimon.jdbcx
  • *
*

* As mentioned in package description all getConnection methods * just invokes real {@link javax.sql.DataSource} object methods and wraps obtained * {@link java.sql.Connection} with {@link org.javasimon.jdbc.SimonConnection} object. *

* Real {@link javax.sql.DataSource} is obtained in method {@link #datasource()}. It tries * to instantiate real datasource object by property realDataSourceClassName * (setters and getters for properties are in {@link AbstractSimonDataSource}) and then sets * basic properties (url, user, password). * * @author Radovan Sninsky * @version $Revision: 304 $ $Date: 2011-04-08 10:39:02 +0200 (Fri, 08 Apr 2011) $ * @since 1.0 */ public final class SimonDataSource extends AbstractSimonDataSource implements DataSource { private DataSource ds; private DataSource datasource() throws SQLException { if (ds == null) { if (realDataSourceClassName == null || realDataSourceClassName.length() == 0) { throw new SQLException("Property realdatasourceclassname is not set"); } Object o; try { o = Class.forName(realDataSourceClassName).newInstance(); } catch (Exception e) { throw new SQLException(e.getMessage()); } if (o instanceof DataSource) { ds = (DataSource) o; try { for (Method m : ds.getClass().getMethods()) { String methodName = m.getName(); if (methodName.equalsIgnoreCase("setUser")) { m.invoke(ds, user); } else if (methodName.equalsIgnoreCase("setPassword")) { m.invoke(ds, password); } else if (methodName.equalsIgnoreCase("setUrl")) { m.invoke(ds, url); } } } catch (Exception e) { throw new SQLException(e.getMessage()); } ds.setLogWriter(logWriter); ds.setLoginTimeout(loginTimeout); } else { throw new SQLException("Class in realdatasourceclassname is not a DataSource"); } } return ds; } /** *

Attempts to establish a connection with the data source that * this DataSource object represents. * * @return a connection to the data source * @throws SQLException if a database access error occurs */ public Connection getConnection() throws SQLException { return new SimonConnection(datasource().getConnection(), prefix); } /** *

Attempts to establish a connection with the data source that * this DataSource object represents. * * @param user the database user on whose behalf the connection is being made * @param password the user's password * @return a connection to the data source * @throws SQLException if a database access error occurs */ public Connection getConnection(String user, String password) throws SQLException { return new SimonConnection(datasource().getConnection(user, password), prefix); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy