001: /*
002: * Licensed under the X license (see http://www.x.org/terms.htm)
003: */
004: package org.ofbiz.minerva.pool.jdbc;
005:
006: import java.sql.*;
007: import java.util.Properties;
008:
009: import javax.sql.DataSource;
010:
011: /**
012: * JDBC Driver to access pooled JDBC connections. Supports both JDBC 1.0
013: * connections and JDBC 2.0 transactional XAConnections. You will get a
014: * java.sql.Connection back in any case (in the case of XAConnections, the
015: * transactional-ness is handled under the covers). You must create the pools
016: * ahead of time by creating and initializing the appropriate DataSource.
017: * <P>You should use a URL of the form <B>jdbc:minerva:<I>PoolName</I></B>
018: * to get a connection from the pool. This will check for both a JDBC Pool
019: * and XA Pool if necessary, so don't create one pool of each type with the
020: * same name!</P>
021: * <P>This driver will be loaded automatically whenever a JDBC Pool or XA
022: * Pool is created, but you can also use the standard <CODE>Class.forName</CODE>
023: * call to load it.</P>
024: * @see org.ofbiz.minerva.pool.jdbc.JDBCPoolDataSource
025: * @see org.ofbiz.minerva.pool.jdbc.xa.XAPoolDataSource
026:
027: * @author Aaron Mulder (ammulder@alumni.princeton.edu)
028: */
029: public class PoolDriver implements Driver {
030:
031: private final static String URL_START = "jdbc:minerva:";
032: private final static PoolDriver instance;
033:
034: static {
035: instance = new PoolDriver();
036: try {
037: DriverManager.registerDriver(PoolDriver.instance());
038: } catch (SQLException e) {
039: System.out
040: .println("Unable to register Minerva DB pool driver!");
041: e.printStackTrace();
042: }
043: }
044:
045: /**
046: * Gets the singleton driver instance.
047: */
048: public static PoolDriver instance() {
049: return instance;
050: }
051:
052: private PoolDriver() {
053: }
054:
055: /**
056: * Tells which URLs this driver can handle.
057: */
058: public boolean acceptsURL(String url) throws java.sql.SQLException {
059: return url.startsWith(URL_START);
060: }
061:
062: /**
063: * Retrieves a connection from a connection pool.
064: */
065: public Connection connect(String url, Properties props)
066: throws java.sql.SQLException {
067: if (url.startsWith(URL_START)) {
068: return getJDBCConnection(url.substring(URL_START.length()));
069: }
070: return null; // No SQL Exception here!
071: }
072:
073: private Connection getJDBCConnection(String name) {
074: Connection con = null;
075: try {
076: DataSource source = JDBCPoolDataSource.getDataSource(name);
077: if (source != null)
078: con = source.getConnection();
079: } catch (Exception e) {
080: e.printStackTrace();
081: }
082: return con;
083: }
084:
085: /**
086: * Returns the driver version.
087: */
088: public int getMajorVersion() {
089: return 2;
090: }
091:
092: /**
093: * Returns the driver version.
094: */
095: public int getMinorVersion() {
096: return 0;
097: }
098:
099: /**
100: * Returns no properties. You do not need properties to connect to the
101: * pool, and the properties for the underlying driver are not managed here.
102: */
103: public DriverPropertyInfo[] getPropertyInfo(String url,
104: Properties info) throws SQLException {
105: return new DriverPropertyInfo[0];
106: }
107:
108: /**
109: * Returns <B>false</B> since it is not known which underlying driver will
110: * be used and what its capabilities are.
111: */
112: public boolean jdbcCompliant() {
113: return false;
114: }
115: }
|