DriverManagerMCFImpl.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: DriverManagerMCFImpl.java,v 1.5 2004/12/14 16:13:51 ehardesty Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 package org.objectweb.jonas.jdbc;
00027 
00028 
00029 import java.sql.DriverManager;
00030 import java.sql.SQLException;
00031 
00032 import javax.resource.ResourceException;
00033 import javax.resource.spi.ConnectionRequestInfo;
00034 import javax.resource.spi.ManagedConnection;
00035 import javax.resource.spi.ResourceAllocationException;
00036 import javax.resource.spi.security.PasswordCredential;
00037 import javax.security.auth.Subject;
00038 
00039 import org.objectweb.util.monolog.api.BasicLevel;
00040 
00041 public class DriverManagerMCFImpl
00042         extends ManagedConnectionFactoryImpl { 
00043 
00044     public ManagedConnection createManagedConnection(Subject subject, 
00045                                                       ConnectionRequestInfo cxReq)
00046               throws ResourceException {
00047  
00048       trace.log(BasicLevel.DEBUG,"subject:"+subject+" connectionRequest:"+cxReq);
00049       PasswordCredential pc = Utility.getPasswordCredential(this, subject, cxReq, pw);
00050       String clsName = null;
00051       DriverWrapper dWrap = null;
00052       try
00053       {
00054           clsName = mcfData.getMCFData(MCFData.DSCLASS);
00055           ClassLoader loader = Thread.currentThread().getContextClassLoader();
00056           java.sql.Driver d = (java.sql.Driver) Class.forName(clsName, true, loader).newInstance();
00057           dWrap = new DriverWrapper(d);
00058           DriverManager.registerDriver(dWrap);
00059       }
00060       catch(ClassNotFoundException cnfe)
00061       {
00062           throw new ResourceException("Class Name not found:" + clsName);
00063       }
00064       catch(Exception ex)
00065       {
00066           throw new ResourceException("Error loading driver manager: " + clsName+" "+ex.getMessage());
00067       }
00068         
00069       java.sql.Connection connection = null;
00070       try {
00071           String val = null;
00072           if ((val = mcfData.getMCFData(MCFData.LOGINTIMEOUT)) != null) {
00073               if (val.length() > 0) {
00074                   DriverManager.setLoginTimeout(Integer.parseInt(val));
00075               }
00076           }
00077           if(cxReq != null) {
00078               ConnectionRequestInfoImpl cx = (ConnectionRequestInfoImpl) cxReq; 
00079               connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
00080                                                        cx.getUser(), cx.getPassword());
00081           } else if (pc != null){
00082               connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
00083                                             pc.getUserName(), new String(pc.getPassword()));
00084           } else {
00085               connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
00086                                                        mcfData.getMCFData(MCFData.USER), 
00087                                                        mcfData.getMCFData(MCFData.PASSWORD));
00088           }
00089       }
00090       catch(SQLException sqle)
00091       {
00092           sqle.printStackTrace();
00093           throw new ResourceAllocationException("The connection could not be allocated: " + sqle.getMessage());
00094       }
00095       catch(Exception ex)
00096       {
00097           ex.printStackTrace();
00098           throw new ResourceAllocationException("Error on allocation: " + ex.getMessage());
00099       }
00100       ManagedConnectionImpl mci = new ManagedConnectionImpl(this, pc, connection, null, null, dWrap);
00101       trace.log(BasicLevel.DEBUG, "Create Mc="+this+" with connection="+connection);
00102       return mci;
00103     }
00104 
00105     /* Determine if the factories are equal
00106      */
00107     public boolean equals(Object obj) {
00108         if (obj instanceof DriverManagerMCFImpl) {
00109             return mcfData.equals(((DriverManagerMCFImpl)obj).mcfData);
00110         }
00111         else {
00112             return false;
00113         }
00114     }
00115 
00116     // JOnAS JDBC RA DriverManager config properties
00117     public String getURL()
00118     {
00119         return mcfData.getMCFData(MCFData.URL);
00120     }
00121 
00122     public void setURL(String val)
00123     {
00124         mcfData.setMCFData(MCFData.URL, val);
00125     }
00126 }

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