KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jdbc > DriverManagerMCFImpl


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Eric HARDESTY
22  * --------------------------------------------------------------------------
23  * $Id: DriverManagerMCFImpl.java,v 1.6 2005/04/28 08:43:24 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas.jdbc;
27
28
29 import java.sql.DriverManager JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 import javax.resource.ResourceException JavaDoc;
33 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
34 import javax.resource.spi.ManagedConnection JavaDoc;
35 import javax.resource.spi.ResourceAllocationException JavaDoc;
36 import javax.resource.spi.security.PasswordCredential JavaDoc;
37 import javax.security.auth.Subject JavaDoc;
38
39 import org.objectweb.util.monolog.api.BasicLevel;
40
41 public class DriverManagerMCFImpl
42         extends ManagedConnectionFactoryImpl {
43
44     public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject,
45                                                       ConnectionRequestInfo JavaDoc cxReq)
46               throws ResourceException JavaDoc {
47
48         if (trace.isLoggable(BasicLevel.DEBUG)) {
49             trace.log(BasicLevel.DEBUG,"subject:"+subject+" connectionRequest:"+cxReq);
50         }
51       PasswordCredential JavaDoc pc = Utility.getPasswordCredential(this, subject, cxReq, pw);
52       String JavaDoc clsName = null;
53       DriverWrapper dWrap = null;
54       try
55       {
56           clsName = mcfData.getMCFData(MCFData.DSCLASS);
57           ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
58           java.sql.Driver JavaDoc d = (java.sql.Driver JavaDoc) Class.forName(clsName, true, loader).newInstance();
59           dWrap = new DriverWrapper(d);
60           DriverManager.registerDriver(dWrap);
61       }
62       catch(ClassNotFoundException JavaDoc cnfe)
63       {
64           throw new ResourceException JavaDoc("Class Name not found:" + clsName);
65       }
66       catch(Exception JavaDoc ex)
67       {
68           throw new ResourceException JavaDoc("Error loading driver manager: " + clsName+" "+ex.getMessage());
69       }
70
71       java.sql.Connection JavaDoc connection = null;
72       try {
73           String JavaDoc val = null;
74           if ((val = mcfData.getMCFData(MCFData.LOGINTIMEOUT)) != null) {
75               if (val.length() > 0) {
76                   DriverManager.setLoginTimeout(Integer.parseInt(val));
77               }
78           }
79           if(cxReq != null) {
80               ConnectionRequestInfoImpl cx = (ConnectionRequestInfoImpl) cxReq;
81               connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
82                                                        cx.getUser(), cx.getPassword());
83           } else if (pc != null){
84               connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
85                                             pc.getUserName(), new String JavaDoc(pc.getPassword()));
86           } else {
87               connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
88                                                        mcfData.getMCFData(MCFData.USER),
89                                                        mcfData.getMCFData(MCFData.PASSWORD));
90           }
91       }
92       catch(SQLException JavaDoc sqle)
93       {
94           sqle.printStackTrace();
95           throw new ResourceAllocationException JavaDoc("The connection could not be allocated: " + sqle.getMessage());
96       }
97       catch(Exception JavaDoc ex)
98       {
99           ex.printStackTrace();
100           throw new ResourceAllocationException JavaDoc("Error on allocation: " + ex.getMessage());
101       }
102       ManagedConnectionImpl mci = new ManagedConnectionImpl(this, pc, connection, null, null, dWrap);
103       if (trace.isLoggable(BasicLevel.DEBUG)) {
104           trace.log(BasicLevel.DEBUG, "Create Mc="+this+" with connection="+connection);
105       }
106       return mci;
107     }
108
109     /* Determine if the factories are equal
110      */

111     public boolean equals(Object JavaDoc obj) {
112         if (obj instanceof DriverManagerMCFImpl) {
113             return mcfData.equals(((DriverManagerMCFImpl)obj).mcfData);
114         }
115         else {
116             return false;
117         }
118     }
119
120     // JOnAS JDBC RA DriverManager config properties
121
public String JavaDoc getURL()
122     {
123         return mcfData.getMCFData(MCFData.URL);
124     }
125
126     public void setURL(String JavaDoc val)
127     {
128         mcfData.setMCFData(MCFData.URL, val);
129     }
130 }
Popular Tags