DataSourceImpl.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Eric HARDESTY
00022  * --------------------------------------------------------------------------
00023  * $Id: DataSourceImpl.java,v 1.3 2003/11/25 07:50:10 durieuxp Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 package org.objectweb.jonas.jdbc;
00027 
00028 import java.io.PrintWriter;
00029 import java.io.Serializable;
00030 
00031 import java.sql.Connection;
00032 import java.sql.SQLException;
00033 
00034 import javax.naming.Reference;
00035 
00036 import javax.resource.Referenceable;
00037 import javax.resource.ResourceException;
00038 import javax.resource.spi.ConnectionManager;
00039 
00040 import org.objectweb.util.monolog.api.BasicLevel;
00041 
00042 
00050 public class DataSourceImpl
00051     implements javax.sql.DataSource, Serializable, Referenceable {
00052 
00053     private ConnectionManager cm;
00054     private ManagedConnectionFactoryImpl mcf;
00055     private PrintWriter pw;
00056     private Reference reference;
00057     
00058     int loginTimeout = 0;
00059 
00060     public DataSourceImpl(ManagedConnectionFactoryImpl _mcf, ConnectionManager _cm) {
00061         if(_cm == null) {
00062             cm = new ConnectionManagerImpl();
00063         } else {
00064             cm = _cm;
00065         }
00066         mcf = _mcf;
00067     }
00068 
00069     public Connection getConnection()
00070               throws SQLException {
00071         mcf.trace.log(BasicLevel.DEBUG,"");
00072         try {
00073             Connection con = (Connection) cm.allocateConnection(mcf, null);
00074             if (con == null) {
00075                 SQLException se = new SQLException("Null connection object returned");
00076                 throw se;
00077             }
00078             return con;
00079         } catch(ResourceException re) {
00080             throw new SQLException(re.getMessage());
00081         }
00082     }
00083 
00084     public Connection getConnection(String user, String pwd)
00085               throws SQLException {
00086         mcf.trace.log(BasicLevel.DEBUG,""+user);
00087         try {
00088             ConnectionRequestInfoImpl info = new ConnectionRequestInfoImpl(user, pwd);
00089             Connection con = (Connection) cm.allocateConnection(mcf, info);
00090             if (con == null) {
00091                 SQLException se = new SQLException("Null connection object returned");
00092                 throw se;
00093             }
00094             return con;
00095         } catch(ResourceException re) {
00096             throw new SQLException(re.getMessage());
00097         }
00098     }
00099 
00100     public int getLoginTimeout()
00101               throws SQLException {
00102         return loginTimeout;
00103     }
00104 
00105     public PrintWriter getLogWriter()
00106               throws SQLException {
00107         return pw;
00108     }
00109 
00110     public Reference getReference() {
00111         return reference;
00112     }
00113 
00114     public void setLoginTimeout(int _loginTimeout)
00115               throws SQLException {
00116         loginTimeout = _loginTimeout;
00117     }
00118 
00119     public void setLogWriter(PrintWriter _pw) 
00120               throws SQLException {
00121         pw = _pw;
00122     }
00123 
00124     public void setReference(Reference _ref) {
00125         reference = _ref;
00126     }
00127 
00128     /* JOnAS JDBC implementation for CMP*/
00129 
00130     public String getMapperName() {
00131         String mn = mcf.getMapperName();
00132         mcf.trace.log(BasicLevel.DEBUG, mn);
00133         return mn;
00134     }
00135 
00136 }

Generated on Tue Feb 15 15:05:16 2005 for JOnAS by  doxygen 1.3.9.1