001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: * Initial developer(s): Eric HARDESTY
022: * --------------------------------------------------------------------------
023: * $Id: DriverManagerMCFImpl.java 6661 2005-04-28 08:43:27Z benoitf $
024: * --------------------------------------------------------------------------
025: */
026: package org.objectweb.jonas.jdbc;
027:
028: import java.sql.DriverManager;
029: import java.sql.SQLException;
030:
031: import javax.resource.ResourceException;
032: import javax.resource.spi.ConnectionRequestInfo;
033: import javax.resource.spi.ManagedConnection;
034: import javax.resource.spi.ResourceAllocationException;
035: import javax.resource.spi.security.PasswordCredential;
036: import javax.security.auth.Subject;
037:
038: import org.objectweb.util.monolog.api.BasicLevel;
039:
040: public class DriverManagerMCFImpl extends ManagedConnectionFactoryImpl {
041:
042: public ManagedConnection createManagedConnection(Subject subject,
043: ConnectionRequestInfo cxReq) throws ResourceException {
044:
045: if (trace.isLoggable(BasicLevel.DEBUG)) {
046: trace.log(BasicLevel.DEBUG, "subject:" + subject
047: + " connectionRequest:" + cxReq);
048: }
049: PasswordCredential pc = Utility.getPasswordCredential(this ,
050: subject, cxReq, pw);
051: String clsName = null;
052: DriverWrapper dWrap = null;
053: try {
054: clsName = mcfData.getMCFData(MCFData.DSCLASS);
055: ClassLoader loader = Thread.currentThread()
056: .getContextClassLoader();
057: java.sql.Driver d = (java.sql.Driver) Class.forName(
058: clsName, true, loader).newInstance();
059: dWrap = new DriverWrapper(d);
060: DriverManager.registerDriver(dWrap);
061: } catch (ClassNotFoundException cnfe) {
062: throw new ResourceException("Class Name not found:"
063: + clsName);
064: } catch (Exception ex) {
065: throw new ResourceException(
066: "Error loading driver manager: " + clsName + " "
067: + ex.getMessage());
068: }
069:
070: java.sql.Connection connection = null;
071: try {
072: String val = null;
073: if ((val = mcfData.getMCFData(MCFData.LOGINTIMEOUT)) != null) {
074: if (val.length() > 0) {
075: DriverManager
076: .setLoginTimeout(Integer.parseInt(val));
077: }
078: }
079: if (cxReq != null) {
080: ConnectionRequestInfoImpl cx = (ConnectionRequestInfoImpl) cxReq;
081: connection = DriverManager.getConnection(mcfData
082: .getMCFData(MCFData.URL), cx.getUser(), cx
083: .getPassword());
084: } else if (pc != null) {
085: connection = DriverManager.getConnection(mcfData
086: .getMCFData(MCFData.URL), pc.getUserName(),
087: new String(pc.getPassword()));
088: } else {
089: connection = DriverManager.getConnection(mcfData
090: .getMCFData(MCFData.URL), mcfData
091: .getMCFData(MCFData.USER), mcfData
092: .getMCFData(MCFData.PASSWORD));
093: }
094: } catch (SQLException sqle) {
095: sqle.printStackTrace();
096: throw new ResourceAllocationException(
097: "The connection could not be allocated: "
098: + sqle.getMessage());
099: } catch (Exception ex) {
100: ex.printStackTrace();
101: throw new ResourceAllocationException(
102: "Error on allocation: " + ex.getMessage());
103: }
104: ManagedConnectionImpl mci = new ManagedConnectionImpl(this , pc,
105: connection, null, null, dWrap);
106: if (trace.isLoggable(BasicLevel.DEBUG)) {
107: trace.log(BasicLevel.DEBUG, "Create Mc=" + this
108: + " with connection=" + connection);
109: }
110: return mci;
111: }
112:
113: /* Determine if the factories are equal
114: */
115: public boolean equals(Object obj) {
116: if (obj instanceof DriverManagerMCFImpl) {
117: return mcfData.equals(((DriverManagerMCFImpl) obj).mcfData);
118: } else {
119: return false;
120: }
121: }
122:
123: // JOnAS JDBC RA DriverManager config properties
124: public String getURL() {
125: return mcfData.getMCFData(MCFData.URL);
126: }
127:
128: public void setURL(String val) {
129: mcfData.setMCFData(MCFData.URL, val);
130: }
131: }
|