001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jdbc;
023:
024: import java.io.PrintWriter;
025: import java.io.Serializable;
026: import java.sql.Connection;
027: import java.sql.SQLException;
028:
029: import javax.naming.Reference;
030: import javax.resource.Referenceable;
031: import javax.resource.ResourceException;
032: import javax.resource.spi.ConnectionManager;
033: import javax.resource.spi.ConnectionRequestInfo;
034: import javax.sql.DataSource;
035: import javax.transaction.RollbackException;
036:
037: import org.jboss.tm.TransactionTimeoutConfiguration;
038: import org.jboss.util.NestedSQLException;
039:
040: /**
041: * WrapperDataSource
042: *
043: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
044: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
045: * @version $Revision: 57189 $
046: */
047: public class WrapperDataSource implements Referenceable, DataSource,
048: Serializable {
049: static final long serialVersionUID = 3570285419164793501L;
050:
051: private final BaseWrapperManagedConnectionFactory mcf;
052: private final ConnectionManager cm;
053:
054: private Reference reference;
055:
056: public WrapperDataSource(
057: final BaseWrapperManagedConnectionFactory mcf,
058: final ConnectionManager cm) {
059: this .mcf = mcf;
060: this .cm = cm;
061: }
062:
063: public PrintWriter getLogWriter() throws SQLException {
064: // TODO: implement this javax.sql.DataSource method
065: return null;
066: }
067:
068: public void setLogWriter(PrintWriter param1) throws SQLException {
069: // TODO: implement this javax.sql.DataSource method
070: }
071:
072: public int getLoginTimeout() throws SQLException {
073: // TODO: implement this javax.sql.DataSource method
074: return 0;
075: }
076:
077: public void setLoginTimeout(int param1) throws SQLException {
078: // TODO: implement this javax.sql.DataSource method
079: }
080:
081: public Connection getConnection() throws SQLException {
082: try {
083: WrappedConnection wc = (WrappedConnection) cm
084: .allocateConnection(mcf, null);
085: wc.setDataSource(this );
086: return wc;
087: } catch (ResourceException re) {
088: throw new NestedSQLException(re);
089: }
090: }
091:
092: public Connection getConnection(String user, String password)
093: throws SQLException {
094: ConnectionRequestInfo cri = new WrappedConnectionRequestInfo(
095: user, password);
096: try {
097: WrappedConnection wc = (WrappedConnection) cm
098: .allocateConnection(mcf, cri);
099: wc.setDataSource(this );
100: return wc;
101: } catch (ResourceException re) {
102: throw new NestedSQLException(re);
103: }
104: }
105:
106: public void setReference(final Reference reference) {
107: this .reference = reference;
108: }
109:
110: public Reference getReference() {
111: return reference;
112: }
113:
114: protected int getTimeLeftBeforeTransactionTimeout()
115: throws SQLException {
116: try {
117: if (cm instanceof TransactionTimeoutConfiguration) {
118: long timeout = ((TransactionTimeoutConfiguration) cm)
119: .getTimeLeftBeforeTransactionTimeout(true);
120: // No timeout
121: if (timeout == -1)
122: return -1;
123: // Round up to the nearest second
124: long result = timeout / 1000;
125: if ((result % 1000) != 0)
126: ++result;
127: return (int) result;
128: } else
129: return -1;
130: } catch (RollbackException e) {
131: throw new NestedSQLException(e);
132: }
133: }
134: }
|