00001
00026 package org.objectweb.jonas.jdbc;
00027
00028 import java.io.PrintWriter;
00029 import java.io.Serializable;
00030 import java.sql.Connection;
00031 import java.util.Iterator;
00032 import java.util.Set;
00033
00034 import javax.naming.InitialContext;
00035 import javax.resource.ResourceException;
00036 import javax.resource.spi.ConnectionManager;
00037 import javax.resource.spi.ConnectionRequestInfo;
00038 import javax.resource.spi.ManagedConnection;
00039 import javax.resource.spi.ManagedConnectionFactory;
00040 import javax.security.auth.Subject;
00041
00042 import org.objectweb.jonas.common.Log;
00043 import org.objectweb.util.monolog.api.BasicLevel;
00044 import org.objectweb.util.monolog.api.Logger;
00045 import org.objectweb.util.monolog.api.LoggerFactory;
00046 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl;
00047
00048 public abstract class ManagedConnectionFactoryImpl implements ManagedConnectionFactory, Serializable {
00049
00050
00051 MCFData mcfData = null;
00052
00053 int hashcode = 0;
00054
00055 PrintWriter pw;
00056
00057 String logTopic = "";
00058
00059 protected static final String LOGGER_FACTORY = "org.objectweb.util.monolog.loggerFactory";
00060
00061 public Logger trace = null;
00062
00063 public ManagedConnectionFactoryImpl() {
00064 pw = null;
00065 mcfData = new MCFData();
00066 }
00067
00068
00069
00070
00071 public abstract ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cxReq)
00072 throws ResourceException;
00073
00074 public abstract boolean equals(Object obj);
00075
00076
00077
00078 public Object createConnectionFactory() throws ResourceException {
00079 return new DataSourceImpl(this, null);
00080 }
00081
00082 public Object createConnectionFactory(ConnectionManager cxMgr) throws ResourceException {
00083 return new DataSourceImpl(this, cxMgr);
00084 }
00085
00086 public void getLogger(String _logTopic) throws Exception {
00087 InitialContext ic = new InitialContext();
00088
00089 if (trace == null || !(logTopic.equals(_logTopic))) {
00090 logTopic = _logTopic;
00091
00092 try {
00093 LoggerFactory mf = Log.getLoggerFactory();
00094 if (logTopic != null && logTopic.length() > 0) {
00095 trace = mf.getLogger(logTopic);
00096 } else if (pw != null) {
00097 trace = new LoggerImpl(pw);
00098 } else {
00099 trace = mf.getLogger("org.objectweb.jonas.jdbc.RA");
00100 }
00101 } catch (Exception ex) {
00102 try {
00103 if (pw != null) {
00104 trace = new LoggerImpl(pw);
00105 }
00106 } catch (Exception e3) {
00107 throw new Exception("Cannot get logger");
00108 }
00109 }
00110 }
00111 trace.log(BasicLevel.DEBUG, "getLogger(" + _logTopic + ")");
00112 }
00113
00114 public PrintWriter getLogWriter() throws ResourceException {
00115 return pw;
00116 }
00117
00118 public int hashCode() {
00119 if (hashcode == 0) {
00120 hashcode = mcfData.hashCode();
00121 try {
00122 getLogger(mcfData.getMCFData(MCFData.LOGTOPIC));
00123 } catch (Exception ex) {
00124 }
00125 }
00126
00127 return hashcode;
00128 }
00129
00130 public ManagedConnection matchManagedConnections(Set connectionSet, Subject subject, ConnectionRequestInfo cxReq)
00131 throws ResourceException {
00132 trace.log(BasicLevel.DEBUG, "matchManagedConnection(" + connectionSet + "," + subject + "," + cxReq + ")");
00133 if (connectionSet == null) {
00134 return null;
00135 }
00136 javax.resource.spi.security.PasswordCredential pc = Utility.getPasswordCredential(this, subject, cxReq, pw);
00137 Iterator it = connectionSet.iterator();
00138 Object obj = null;
00139 ManagedConnectionImpl mc = null;
00140 while (it.hasNext()) {
00141 try {
00142 obj = it.next();
00143 if (obj instanceof ManagedConnectionImpl) {
00144 ManagedConnectionImpl mc1 = (ManagedConnectionImpl) obj;
00145 if (pc == null && equals(mc1.mcf)) {
00146 mc = mc1;
00147 break;
00148 }
00149 if (pc != null && pc.equals(mc.pc)) {
00150 mc = mc1;
00151 break;
00152 }
00153 }
00154 } catch (Exception ex) {
00155 throw new ResourceException(ex.getMessage());
00156 }
00157 }
00158 trace.log(BasicLevel.DEBUG, "matchManagedConnection returns(" + mc + ")");
00159 return mc;
00160 }
00161
00162 public void setLogWriter(PrintWriter _pw) throws ResourceException {
00163 pw = _pw;
00164 }
00165
00166
00167 public String getDbSpecificMethods() {
00168 return mcfData.getMCFData(MCFData.DBSPECIFICMETHODS);
00169 }
00170
00171 public void setDbSpecificMethods(String val) {
00172 mcfData.setMCFData(MCFData.DBSPECIFICMETHODS, val);
00173 }
00174
00175 public String getDsClass() {
00176 return mcfData.getMCFData(MCFData.DSCLASS);
00177 }
00178
00179 public void setDsClass(String val) {
00180 mcfData.setMCFData(MCFData.DSCLASS, val);
00181 }
00182
00183 public String getIsolationLevel() {
00184 String str = mcfData.getMCFData(MCFData.ISOLATIONLEVEL);
00185 String retStr = "default";
00186
00187 if (str.length() == 0 || str.equals("-1")) {
00188 return retStr;
00189 }
00190
00191 int isolationLevel;
00192 try {
00193 isolationLevel = Integer.parseInt(str);
00194 } catch (Exception ex) {
00195 return retStr;
00196 }
00197
00198 if (isolationLevel == Connection.TRANSACTION_SERIALIZABLE) {
00199 retStr = "serializable";
00200 } else if (isolationLevel == Connection.TRANSACTION_NONE) {
00201 retStr = "none";
00202 } else if (isolationLevel == Connection.TRANSACTION_READ_COMMITTED) {
00203 retStr = "read_committed";
00204 } else if (isolationLevel == Connection.TRANSACTION_READ_UNCOMMITTED) {
00205 retStr = "read_uncommitted";
00206 } else if (isolationLevel == Connection.TRANSACTION_REPEATABLE_READ) {
00207 retStr = "repeatable_read";
00208 }
00209 return retStr;
00210 }
00211
00212 public void setIsolationLevel(String val) {
00213 int isolationLevel = -1;
00214
00215 if (val.equals("serializable")) {
00216 isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
00217 } else if (val.equals("none")) {
00218 isolationLevel = Connection.TRANSACTION_NONE;
00219 } else if (val.equals("read_committed")) {
00220 isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
00221 } else if (val.equals("read_uncommitted")) {
00222 isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
00223 } else if (val.equals("repeatable_read")) {
00224 isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
00225 }
00226
00227 mcfData.setMCFData(MCFData.ISOLATIONLEVEL, "" + isolationLevel);
00228 }
00229
00230 public String getLoginTimeout() {
00231 return mcfData.getMCFData(MCFData.LOGINTIMEOUT);
00232 }
00233
00234 public void setLoginTimeout(String val) {
00235 mcfData.setMCFData(MCFData.LOGINTIMEOUT, val);
00236 }
00237
00238 public String getLogTopic() {
00239 return mcfData.getMCFData(MCFData.LOGTOPIC);
00240 }
00241
00242 public void setLogTopic(String val) {
00243 mcfData.setMCFData(MCFData.LOGTOPIC, val);
00244 try {
00245 getLogger(val.trim());
00246 } catch (Exception ex) {
00247 }
00248 }
00249
00250 public String getMapperName() {
00251 return mcfData.getMCFData(MCFData.MAPPERNAME);
00252 }
00253
00254 public void setMapperName(String val) {
00255 mcfData.setMCFData(MCFData.MAPPERNAME, val);
00256 }
00257
00258 public String getPassword() {
00259 return mcfData.getMCFData(MCFData.PASSWORD);
00260 }
00261
00262 public void setPassword(String val) {
00263 mcfData.setMCFData(MCFData.PASSWORD, val);
00264 }
00265
00266 public String getUser() {
00267 return mcfData.getMCFData(MCFData.USER);
00268 }
00269
00270 public void setUser(String val) {
00271 mcfData.setMCFData(MCFData.USER, val);
00272 }
00273
00274 }