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