001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.sql;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.L10N;
033:
034: import javax.resource.ResourceException;
035: import javax.resource.spi.ConnectionManager;
036: import javax.sql.DataSource;
037: import java.io.PrintWriter;
038: import java.sql.Connection;
039: import java.sql.SQLException;
040: import java.util.logging.Logger;
041:
042: /**
043: * The wrapped data source.
044: */
045: public class DataSourceImpl implements DataSource {
046: protected static final Logger log = Log.open(DataSourceImpl.class);
047: private static final L10N L = new L10N(DataSourceImpl.class);
048:
049: private final ManagedFactoryImpl _managedFactory;
050: private final ConnectionManager _connManager;
051:
052: DataSourceImpl(ManagedFactoryImpl factory, ConnectionManager cm) {
053: _managedFactory = factory;
054: _connManager = cm;
055: }
056:
057: /**
058: * Returns a connection.
059: */
060: public Connection getConnection() throws SQLException {
061: try {
062: return (Connection) _connManager.allocateConnection(
063: _managedFactory, null);
064: } catch (ResourceException e) {
065: Throwable cause;
066:
067: for (cause = e; cause != null; cause = cause.getCause()) {
068: if (cause instanceof SQLException)
069: throw (SQLException) cause;
070: }
071:
072: throw new SQLExceptionWrapper(e);
073: }
074: }
075:
076: /**
077: * Returns a connection.
078: */
079: public Connection getConnection(String username, String password)
080: throws SQLException {
081: try {
082: Credential credential = null;
083:
084: if (username != null || password != null)
085: credential = new Credential(username, password);
086:
087: return (Connection) _connManager.allocateConnection(
088: _managedFactory, credential);
089: } catch (ResourceException e) {
090: Throwable cause;
091:
092: for (cause = e; cause != null; cause = cause.getCause()) {
093: if (cause instanceof SQLException)
094: throw (SQLException) cause;
095: }
096:
097: throw new SQLExceptionWrapper(e);
098: }
099: }
100:
101: /**
102: * Returns the login timeout.
103: */
104: public int getLoginTimeout() {
105: return 0;
106: }
107:
108: /**
109: * Returns the login timeout.
110: */
111: public void setLoginTimeout(int seconds) {
112: }
113:
114: /**
115: * Returns the log writer.
116: */
117: public PrintWriter getLogWriter() {
118: return null;
119: }
120:
121: /**
122: * Sets the log writer.
123: */
124: public void setLogWriter(PrintWriter out) {
125: }
126:
127: /**
128: * Returns true if the impl has closed.
129: */
130: boolean isClosed() {
131: return false;
132: }
133:
134: public <T> T unwrap(Class<T> iface) throws SQLException {
135: throw new UnsupportedOperationException("Not supported yet.");
136: }
137:
138: public boolean isWrapperFor(Class<?> iface) throws SQLException {
139: throw new UnsupportedOperationException("Not supported yet.");
140: }
141: }
|