001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ManagedConnectionFactoryImpl.java 9932 2007-01-18 00:03:16Z ehardesty $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jdbc;
025:
026: import java.io.PrintWriter;
027: import java.io.Serializable;
028: import java.sql.Connection;
029: import java.util.Iterator;
030: import java.util.Set;
031:
032: import javax.naming.InitialContext;
033: import javax.resource.ResourceException;
034: import javax.resource.spi.ConnectionManager;
035: import javax.resource.spi.ConnectionRequestInfo;
036: import javax.resource.spi.ManagedConnection;
037: import javax.resource.spi.ManagedConnectionFactory;
038: import javax.security.auth.Subject;
039:
040: import org.objectweb.jonas.common.Log;
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043: import org.objectweb.util.monolog.api.LoggerFactory;
044: import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl;
045:
046: /**
047: * @author Eric hardesty
048: */
049: public abstract class ManagedConnectionFactoryImpl implements
050: ManagedConnectionFactory, Serializable {
051:
052: // Factory config data property
053: MCFData mcfData = null;
054:
055: int hashcode = 0;
056:
057: PrintWriter pw;
058:
059: String logTopic = "";
060:
061: protected static final String LOGGER_FACTORY = "org.objectweb.util.monolog.loggerFactory";
062:
063: public Logger trace = null;
064:
065: /**
066: * Debug level ?
067: */
068: private boolean isEnabledDebug = false;
069:
070: public ManagedConnectionFactoryImpl() {
071: pw = null;
072: mcfData = new MCFData();
073: }
074:
075: /* Abstract methods, specific implementation is different
076: * for each type of MCF
077: */
078: public abstract ManagedConnection createManagedConnection(
079: Subject subject, ConnectionRequestInfo cxReq)
080: throws ResourceException;
081:
082: public abstract boolean equals(Object obj);
083:
084: /* Common methods for each MCF type
085: */
086: public Object createConnectionFactory() throws ResourceException {
087: return new DataSourceImpl(this , null);
088: }
089:
090: public Object createConnectionFactory(ConnectionManager cxMgr)
091: throws ResourceException {
092: return new DataSourceImpl(this , cxMgr);
093: }
094:
095: public void getLogger(String _logTopic) throws Exception {
096: InitialContext ic = new InitialContext();
097:
098: if (trace == null || !(logTopic.equals(_logTopic))) {
099: logTopic = _logTopic; // set the log topic value
100: // Get the trace module:
101: try {
102: LoggerFactory mf = Log.getLoggerFactory();
103: if (logTopic != null && logTopic.length() > 0) {
104: trace = mf.getLogger(logTopic);
105: } else if (pw != null) {
106: trace = new LoggerImpl(pw);
107: } else {
108: trace = mf.getLogger("org.objectweb.jonas.jdbc.RA");
109: }
110: } catch (Exception ex) {
111: try {
112: if (pw != null) {
113: trace = new LoggerImpl(pw);
114: }
115: } catch (Exception e3) {
116: throw new Exception("Cannot get logger");
117: }
118: }
119: }
120: isEnabledDebug = trace.isLoggable(BasicLevel.DEBUG);
121: if (isEnabledDebug) {
122: trace.log(BasicLevel.DEBUG, "getLogger(" + _logTopic + ")");
123: }
124: }
125:
126: public PrintWriter getLogWriter() throws ResourceException {
127: return pw;
128: }
129:
130: public int hashCode() {
131: if (hashcode == 0) {
132: hashcode = mcfData.hashCode();
133: try {
134: getLogger(mcfData.getMCFData(MCFData.LOGTOPIC));
135: } catch (Exception ex) {
136: }
137: }
138:
139: return hashcode;
140: }
141:
142: public ManagedConnection matchManagedConnections(Set connectionSet,
143: Subject subject, ConnectionRequestInfo cxReq)
144: throws ResourceException {
145: if (isEnabledDebug) {
146: trace
147: .log(BasicLevel.DEBUG, "matchManagedConnection("
148: + connectionSet + "," + subject + ","
149: + cxReq + ")");
150: }
151: if (connectionSet == null) {
152: return null;
153: }
154: javax.resource.spi.security.PasswordCredential pc = Utility
155: .getPasswordCredential(this , subject, cxReq, pw);
156: Iterator it = connectionSet.iterator();
157: Object obj = null;
158: ManagedConnectionImpl mc = null;
159: while (it.hasNext()) {
160: try {
161: obj = it.next();
162: if (obj instanceof ManagedConnectionImpl) {
163: ManagedConnectionImpl mc1 = (ManagedConnectionImpl) obj;
164: if (pc == null && equals(mc1.mcf)) {
165: mc = mc1;
166: break;
167: }
168: if (pc != null && pc.equals(mc1.pc)) {
169: mc = mc1;
170: break;
171: }
172: }
173: } catch (Exception ex) {
174: throw new ResourceException(ex.getMessage());
175: }
176: }
177: if (isEnabledDebug) {
178: trace.log(BasicLevel.DEBUG,
179: "matchManagedConnection returns(" + mc + ")");
180: }
181: return mc;
182: }
183:
184: public void setLogWriter(PrintWriter _pw) throws ResourceException {
185: pw = _pw;
186: }
187:
188: /* Common getter/setters */
189: public String getDbSpecificMethods() {
190: return mcfData.getMCFData(MCFData.DBSPECIFICMETHODS);
191: }
192:
193: public void setDbSpecificMethods(String val) {
194: mcfData.setMCFData(MCFData.DBSPECIFICMETHODS, val);
195: }
196:
197: public String getDsClass() {
198: return mcfData.getMCFData(MCFData.DSCLASS);
199: }
200:
201: public void setDsClass(String val) {
202: mcfData.setMCFData(MCFData.DSCLASS, val);
203: }
204:
205: public String getIsolationLevel() {
206: String str = mcfData.getMCFData(MCFData.ISOLATIONLEVEL);
207: String retStr = "default";
208:
209: if (str.length() == 0 || str.equals("-1")) {
210: return retStr;
211: }
212:
213: int isolationLevel;
214: try {
215: isolationLevel = Integer.parseInt(str);
216: } catch (Exception ex) {
217: return retStr;
218: }
219:
220: if (isolationLevel == Connection.TRANSACTION_SERIALIZABLE) {
221: retStr = "serializable";
222: } else if (isolationLevel == Connection.TRANSACTION_NONE) {
223: retStr = "none";
224: } else if (isolationLevel == Connection.TRANSACTION_READ_COMMITTED) {
225: retStr = "read_committed";
226: } else if (isolationLevel == Connection.TRANSACTION_READ_UNCOMMITTED) {
227: retStr = "read_uncommitted";
228: } else if (isolationLevel == Connection.TRANSACTION_REPEATABLE_READ) {
229: retStr = "repeatable_read";
230: }
231: return retStr;
232: }
233:
234: public void setIsolationLevel(String val) {
235: int isolationLevel = -1;
236:
237: if (val.equals("serializable")) {
238: isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
239: } else if (val.equals("none")) {
240: isolationLevel = Connection.TRANSACTION_NONE;
241: } else if (val.equals("read_committed")) {
242: isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
243: } else if (val.equals("read_uncommitted")) {
244: isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
245: } else if (val.equals("repeatable_read")) {
246: isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
247: }
248:
249: mcfData.setMCFData(MCFData.ISOLATIONLEVEL, "" + isolationLevel);
250: }
251:
252: public String getLoginTimeout() {
253: return mcfData.getMCFData(MCFData.LOGINTIMEOUT);
254: }
255:
256: public void setLoginTimeout(String val) {
257: mcfData.setMCFData(MCFData.LOGINTIMEOUT, val);
258: }
259:
260: public String getLogTopic() {
261: return mcfData.getMCFData(MCFData.LOGTOPIC);
262: }
263:
264: public void setLogTopic(String val) {
265: mcfData.setMCFData(MCFData.LOGTOPIC, val);
266: try {
267: getLogger(val.trim());
268: } catch (Exception ex) {
269: }
270: }
271:
272: public String getMapperName() {
273: return mcfData.getMCFData(MCFData.MAPPERNAME);
274: }
275:
276: public void setMapperName(String val) {
277: mcfData.setMCFData(MCFData.MAPPERNAME, val);
278: }
279:
280: public String getPassword() {
281: return mcfData.getMCFData(MCFData.PASSWORD);
282: }
283:
284: public void setPassword(String val) {
285: mcfData.setMCFData(MCFData.PASSWORD, val);
286: }
287:
288: public String getUser() {
289: return mcfData.getMCFData(MCFData.USER);
290: }
291:
292: public void setUser(String val) {
293: mcfData.setMCFData(MCFData.USER, val);
294: }
295:
296: }
|