001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: DataSourceImpl.java 6697 2005-05-04 14:09:00Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jdbc;
025:
026: import java.io.PrintWriter;
027: import java.io.Serializable;
028:
029: import java.sql.Connection;
030: import java.sql.SQLException;
031:
032: import javax.naming.Reference;
033:
034: import javax.resource.Referenceable;
035: import javax.resource.ResourceException;
036: import javax.resource.spi.ConnectionManager;
037:
038: import org.objectweb.util.monolog.api.BasicLevel;
039:
040: /**
041: * Description of the DataSourceImpl. This is the dataSource for the JDBC RA.
042: * @author Eric Hardesty
043: * @created 22 May 2003
044: */
045: public class DataSourceImpl implements javax.sql.DataSource,
046: Serializable, Referenceable {
047:
048: private ConnectionManager cm;
049:
050: private ManagedConnectionFactoryImpl mcf;
051:
052: private PrintWriter pw;
053:
054: private Reference reference;
055:
056: int loginTimeout = 0;
057:
058: /**
059: * Logging (debug enabled)
060: */
061: private final boolean isDebugOn;
062:
063: public DataSourceImpl(ManagedConnectionFactoryImpl _mcf,
064: ConnectionManager _cm) {
065: if (_cm == null) {
066: cm = new ConnectionManagerImpl();
067: } else {
068: cm = _cm;
069: }
070: mcf = _mcf;
071: isDebugOn = mcf.trace.isLoggable(BasicLevel.DEBUG);
072: }
073:
074: public Connection getConnection() throws SQLException {
075: if (isDebugOn) {
076: mcf.trace.log(BasicLevel.DEBUG, "");
077: }
078: try {
079: Connection con = (Connection) cm.allocateConnection(mcf,
080: null);
081: if (con == null) {
082: SQLException se = new SQLException(
083: "Null connection object returned");
084: throw se;
085: }
086: return con;
087: } catch (ResourceException re) {
088: throw new SQLException(re.getMessage());
089: }
090: }
091:
092: public Connection getConnection(String user, String pwd)
093: throws SQLException {
094: if (isDebugOn) {
095: mcf.trace.log(BasicLevel.DEBUG, "" + user);
096: }
097: try {
098: ConnectionRequestInfoImpl info = new ConnectionRequestInfoImpl(
099: user, pwd);
100: Connection con = (Connection) cm.allocateConnection(mcf,
101: info);
102: if (con == null) {
103: SQLException se = new SQLException(
104: "Null connection object returned");
105: throw se;
106: }
107: return con;
108: } catch (ResourceException re) {
109: throw new SQLException(re.getMessage());
110: }
111: }
112:
113: public int getLoginTimeout() throws SQLException {
114: return loginTimeout;
115: }
116:
117: public PrintWriter getLogWriter() throws SQLException {
118: return pw;
119: }
120:
121: public Reference getReference() {
122: return reference;
123: }
124:
125: public void setLoginTimeout(int _loginTimeout) throws SQLException {
126: loginTimeout = _loginTimeout;
127: }
128:
129: public void setLogWriter(PrintWriter _pw) throws SQLException {
130: pw = _pw;
131: }
132:
133: public void setReference(Reference _ref) {
134: reference = _ref;
135: }
136:
137: /* JOnAS JDBC implementation for CMP */
138:
139: public String getMapperName() {
140: String mn = mcf.getMapperName();
141: if (isDebugOn) {
142: mcf.trace.log(BasicLevel.DEBUG, mn);
143: }
144: return mn;
145: }
146:
147: }
|