KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 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  * --------------------------------------------------------------------------
22  * $Id: ManagedConnectionFactoryImpl.java,v 1.9 2005/05/04 14:09:00 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jdbc;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.naming.InitialContext JavaDoc;
34 import javax.resource.ResourceException JavaDoc;
35 import javax.resource.spi.ConnectionManager JavaDoc;
36 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
37 import javax.resource.spi.ManagedConnection JavaDoc;
38 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
39 import javax.security.auth.Subject JavaDoc;
40
41 import org.objectweb.jonas.common.Log;
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.api.Logger;
44 import org.objectweb.util.monolog.api.LoggerFactory;
45 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl;
46
47 /**
48  * @author Eric hardesty
49  */

50 public abstract class ManagedConnectionFactoryImpl implements ManagedConnectionFactory JavaDoc, Serializable JavaDoc {
51
52     // Factory config data property
53
MCFData mcfData = null;
54
55     int hashcode = 0;
56
57     PrintWriter JavaDoc pw;
58
59     String JavaDoc logTopic = "";
60
61     protected static final String JavaDoc LOGGER_FACTORY = "org.objectweb.util.monolog.loggerFactory";
62
63     public Logger trace = null;
64
65     /**
66      * Debug level ?
67      */

68     private boolean isEnabledDebug = false;
69
70
71     public ManagedConnectionFactoryImpl() {
72         pw = null;
73         mcfData = new MCFData();
74     }
75
76     /* Abstract methods, specific implementation is different
77      * for each type of MCF
78      */

79     public abstract ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cxReq)
80             throws ResourceException JavaDoc;
81
82     public abstract boolean equals(Object JavaDoc obj);
83
84     /* Common methods for each MCF type
85      */

86     public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc {
87         return new DataSourceImpl(this, null);
88     }
89
90     public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cxMgr) throws ResourceException JavaDoc {
91         return new DataSourceImpl(this, cxMgr);
92     }
93
94     public void getLogger(String JavaDoc _logTopic) throws Exception JavaDoc {
95         InitialContext JavaDoc ic = new InitialContext JavaDoc();
96
97         if (trace == null || !(logTopic.equals(_logTopic))) {
98             logTopic = _logTopic; // set the log topic value
99
// Get the trace module:
100
try {
101                 LoggerFactory mf = Log.getLoggerFactory();
102                 if (logTopic != null && logTopic.length() > 0) {
103                     trace = mf.getLogger(logTopic);
104                 } else if (pw != null) {
105                     trace = new LoggerImpl(pw);
106                 } else {
107                     trace = mf.getLogger("org.objectweb.jonas.jdbc.RA");
108                 }
109             } catch (Exception JavaDoc ex) {
110                 try {
111                     if (pw != null) {
112                         trace = new LoggerImpl(pw);
113                     }
114                 } catch (Exception JavaDoc e3) {
115                     throw new Exception JavaDoc("Cannot get logger");
116                 }
117             }
118         }
119         isEnabledDebug = trace.isLoggable(BasicLevel.DEBUG);
120         if (isEnabledDebug) {
121             trace.log(BasicLevel.DEBUG, "getLogger(" + _logTopic + ")");
122         }
123     }
124
125     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
126         return pw;
127     }
128
129     public int hashCode() {
130         if (hashcode == 0) {
131             hashcode = mcfData.hashCode();
132             try {
133                 getLogger(mcfData.getMCFData(MCFData.LOGTOPIC));
134             } catch (Exception JavaDoc ex) {
135             }
136         }
137
138         return hashcode;
139     }
140
141     public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc connectionSet, Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cxReq)
142             throws ResourceException JavaDoc {
143         if (isEnabledDebug) {
144             trace.log(BasicLevel.DEBUG, "matchManagedConnection(" + connectionSet + "," + subject + "," + cxReq + ")");
145         }
146         if (connectionSet == null) {
147             return null;
148         }
149         javax.resource.spi.security.PasswordCredential JavaDoc pc = Utility.getPasswordCredential(this, subject, cxReq, pw);
150         Iterator JavaDoc it = connectionSet.iterator();
151         Object JavaDoc obj = null;
152         ManagedConnectionImpl mc = null;
153         while (it.hasNext()) {
154             try {
155                 obj = it.next();
156                 if (obj instanceof ManagedConnectionImpl) {
157                     ManagedConnectionImpl mc1 = (ManagedConnectionImpl) obj;
158                     if (pc == null && equals(mc1.mcf)) {
159                         mc = mc1;
160                         break;
161                     }
162                     if (pc != null && pc.equals(mc.pc)) {
163                         mc = mc1;
164                         break;
165                     }
166                 }
167             } catch (Exception JavaDoc ex) {
168                 throw new ResourceException JavaDoc(ex.getMessage());
169             }
170         }
171         if (isEnabledDebug) {
172             trace.log(BasicLevel.DEBUG, "matchManagedConnection returns(" + mc + ")");
173         }
174         return mc;
175     }
176
177     public void setLogWriter(PrintWriter JavaDoc _pw) throws ResourceException JavaDoc {
178         pw = _pw;
179     }
180
181     /* Common getter/setters */
182     public String JavaDoc getDbSpecificMethods() {
183         return mcfData.getMCFData(MCFData.DBSPECIFICMETHODS);
184     }
185
186     public void setDbSpecificMethods(String JavaDoc val) {
187         mcfData.setMCFData(MCFData.DBSPECIFICMETHODS, val);
188     }
189
190     public String JavaDoc getDsClass() {
191         return mcfData.getMCFData(MCFData.DSCLASS);
192     }
193
194     public void setDsClass(String JavaDoc val) {
195         mcfData.setMCFData(MCFData.DSCLASS, val);
196     }
197
198     public String JavaDoc getIsolationLevel() {
199         String JavaDoc str = mcfData.getMCFData(MCFData.ISOLATIONLEVEL);
200         String JavaDoc retStr = "default";
201
202         if (str.length() == 0 || str.equals("-1")) {
203             return retStr;
204         }
205
206         int isolationLevel;
207         try {
208             isolationLevel = Integer.parseInt(str);
209         } catch (Exception JavaDoc ex) {
210             return retStr;
211         }
212
213         if (isolationLevel == Connection.TRANSACTION_SERIALIZABLE) {
214             retStr = "serializable";
215         } else if (isolationLevel == Connection.TRANSACTION_NONE) {
216             retStr = "none";
217         } else if (isolationLevel == Connection.TRANSACTION_READ_COMMITTED) {
218             retStr = "read_committed";
219         } else if (isolationLevel == Connection.TRANSACTION_READ_UNCOMMITTED) {
220             retStr = "read_uncommitted";
221         } else if (isolationLevel == Connection.TRANSACTION_REPEATABLE_READ) {
222             retStr = "repeatable_read";
223         }
224         return retStr;
225     }
226
227     public void setIsolationLevel(String JavaDoc val) {
228         int isolationLevel = -1;
229
230         if (val.equals("serializable")) {
231             isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
232         } else if (val.equals("none")) {
233             isolationLevel = Connection.TRANSACTION_NONE;
234         } else if (val.equals("read_committed")) {
235             isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
236         } else if (val.equals("read_uncommitted")) {
237             isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
238         } else if (val.equals("repeatable_read")) {
239             isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
240         }
241
242         mcfData.setMCFData(MCFData.ISOLATIONLEVEL, "" + isolationLevel);
243     }
244
245     public String JavaDoc getLoginTimeout() {
246         return mcfData.getMCFData(MCFData.LOGINTIMEOUT);
247     }
248
249     public void setLoginTimeout(String JavaDoc val) {
250         mcfData.setMCFData(MCFData.LOGINTIMEOUT, val);
251     }
252
253     public String JavaDoc getLogTopic() {
254         return mcfData.getMCFData(MCFData.LOGTOPIC);
255     }
256
257     public void setLogTopic(String JavaDoc val) {
258         mcfData.setMCFData(MCFData.LOGTOPIC, val);
259         try {
260             getLogger(val.trim());
261         } catch (Exception JavaDoc ex) {
262         }
263     }
264
265     public String JavaDoc getMapperName() {
266         return mcfData.getMCFData(MCFData.MAPPERNAME);
267     }
268
269     public void setMapperName(String JavaDoc val) {
270         mcfData.setMCFData(MCFData.MAPPERNAME, val);
271     }
272
273     public String JavaDoc getPassword() {
274         return mcfData.getMCFData(MCFData.PASSWORD);
275     }
276
277     public void setPassword(String JavaDoc val) {
278         mcfData.setMCFData(MCFData.PASSWORD, val);
279     }
280
281     public String JavaDoc getUser() {
282         return mcfData.getMCFData(MCFData.USER);
283     }
284
285     public void setUser(String JavaDoc val) {
286         mcfData.setMCFData(MCFData.USER, val);
287     }
288
289 }
Popular Tags