XADataSourceImpl.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  * --------------------------------------------------------------------------
00022  * $Id: XADataSourceImpl.java,v 1.12 2004/09/14 06:38:02 durieuxp Exp $
00023  * --------------------------------------------------------------------------
00024  */
00025 
00026 
00027 package org.objectweb.jonas.jdbc_xa;
00028 
00029 import java.io.PrintWriter;
00030 import java.sql.Connection;
00031 import java.sql.DriverManager;
00032 import java.sql.SQLException;
00033 import javax.sql.XAConnection;
00034 import javax.sql.XADataSource;
00035 import org.objectweb.jonas.common.Log;
00036 import org.objectweb.util.monolog.api.Logger;
00037 import org.objectweb.util.monolog.api.BasicLevel;
00038 
00049 public class XADataSourceImpl implements XADataSource {
00050 
00051     private static Logger logger = null;
00052 
00053     // properties
00054     private String dataSourceName = null;
00055     private String url = null;
00056     private String className = null;
00057     private String userName = null;
00058     private String password = null;
00059     private int isolationLevel = -1;
00060 
00061     // private
00062     private int loginTimeout = 60;
00063     private PrintWriter log = null;
00064     private Class driverClass = null;
00065 
00066     // -----------------------------------------------------------------
00067     // Constructors
00068     // -----------------------------------------------------------------
00069 
00073     public XADataSourceImpl() {
00074     }
00075 
00076     // -----------------------------------------------------------------
00077     // Properties
00078     // -----------------------------------------------------------------
00079     public String getDataSourceName() {
00080         return dataSourceName;
00081     }
00082     public void setDataSourceName(String s) {
00083         logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX);
00084         dataSourceName = s;
00085     }
00086 
00087     public String getUrl() {
00088         return url;
00089     }
00090     public void setUrl(String s) {
00091         url = s;
00092     }
00093 
00094     public String getClassName() {
00095         return className;
00096     }
00097 
00098     public void setClassName(String s) throws ClassNotFoundException {
00099         className = s;
00100 
00101         // Loads standard JDBC driver and keeps it loaded (via driverClass)
00102         logger.log(BasicLevel.DEBUG, "Load JDBC driver " + s);
00103         try {
00104             driverClass = Class.forName(className);
00105         } catch (java.lang.ClassNotFoundException e) {
00106             logger.log(BasicLevel.ERROR, "Cannot load JDBC driver : " + e);
00107             throw e;
00108         }
00109     }
00110 
00111     public String getUserName() {
00112         return userName;
00113     }
00114     public void setUserName(String s) {
00115         userName = s;
00116     }
00117 
00118     public String getPassword() {
00119         return password;
00120     }
00121     public void setPassword(String s) {
00122         password = s;
00123     }
00124 
00125     public void setTransactionIsolation(int level) {
00126         isolationLevel = level;
00127     }
00128 
00129     // -----------------------------------------------------------------
00130     // XADataSource implementation
00131     // -----------------------------------------------------------------
00132 
00140     public XAConnection getXAConnection() throws SQLException {
00141         return getXAConnection(userName, password);
00142     }
00143 
00144 
00156     public XAConnection getXAConnection(String user, String passwd) throws SQLException {
00157 
00158         // Create the actual connection in the std driver
00159         Connection conn = null;
00160         try {
00161             if (user.length() == 0) {
00162                 conn = DriverManager.getConnection(url);
00163                 logger.log(BasicLevel.DEBUG, "    * New Connection on " + url);
00164             } else {
00165                 // Accept password of zero length.
00166                 conn = DriverManager.getConnection(url, user, passwd);
00167                 logger.log(BasicLevel.DEBUG, "    * New Connection on " + url + " for " + user);
00168             }
00169         } catch (SQLException e) {
00170             logger.log(BasicLevel.ERROR, "Could not get Connection on " + url + ":", e);
00171             throw new SQLException("Could not get Connection on url : " + url
00172                                    + " for user : " + user + " inner exception: " + e.getMessage());
00173         }
00174 
00175         // Attempt to set the transaction isolation level
00176         // Depending on the underlaying database, this may not succeed.
00177         if (isolationLevel != -1) {
00178             try {
00179                 logger.log(BasicLevel.DEBUG, "set transaction isolation to " + isolationLevel);
00180                 conn.setTransactionIsolation(isolationLevel);
00181             } catch (SQLException e) {
00182                 String ilstr = "?";
00183                 switch (isolationLevel) {
00184                 case Connection.TRANSACTION_SERIALIZABLE:
00185                     ilstr = "SERIALIZABLE";
00186                     break;
00187                 case Connection.TRANSACTION_NONE:
00188                     ilstr = "NONE";
00189                     break;
00190                 case Connection.TRANSACTION_READ_COMMITTED:
00191                     ilstr = "READ_COMMITTED";
00192                     break;
00193                 case Connection.TRANSACTION_READ_UNCOMMITTED:
00194                     ilstr = "READ_UNCOMMITTED";
00195                     break;
00196                 case Connection.TRANSACTION_REPEATABLE_READ:
00197                     ilstr = "REPEATABLE_READ";
00198                     break;
00199                 }
00200                 logger.log(BasicLevel.ERROR, "Cannot set transaction isolation to " + ilstr + " for this DataSource:");
00201                 logger.log(BasicLevel.ERROR, url);
00202                 isolationLevel = -1;
00203             }
00204         }
00205 
00206         // Create the XAConnection object
00207         XAConnectionImpl xac = new XAConnectionImpl(conn, this);
00208 
00209         // return the XAConnection
00210         return (XAConnection) xac;
00211     }
00212 
00220     public PrintWriter getLogWriter() throws SQLException {
00221         return log;
00222     }
00223 
00231     public void setLogWriter(PrintWriter out) throws SQLException {
00232         log = out;
00233     }
00234 
00235 
00244     public void setLoginTimeout(int seconds) throws SQLException {
00245         loginTimeout = seconds;
00246     }
00247 
00256     public int getLoginTimeout() throws SQLException {
00257         return loginTimeout;
00258     }
00259 
00260 }

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