1 25 package org.objectweb.jonas.jdbc; 26 27 import java.io.PrintWriter ; 28 import java.io.Serializable ; 29 import java.sql.Connection ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 33 import javax.naming.InitialContext ; 34 import javax.resource.ResourceException ; 35 import javax.resource.spi.ConnectionManager ; 36 import javax.resource.spi.ConnectionRequestInfo ; 37 import javax.resource.spi.ManagedConnection ; 38 import javax.resource.spi.ManagedConnectionFactory ; 39 import javax.security.auth.Subject ; 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 50 public abstract class ManagedConnectionFactoryImpl implements ManagedConnectionFactory , Serializable { 51 52 MCFData mcfData = null; 54 55 int hashcode = 0; 56 57 PrintWriter pw; 58 59 String logTopic = ""; 60 61 protected static final String LOGGER_FACTORY = "org.objectweb.util.monolog.loggerFactory"; 62 63 public Logger trace = null; 64 65 68 private boolean isEnabledDebug = false; 69 70 71 public ManagedConnectionFactoryImpl() { 72 pw = null; 73 mcfData = new MCFData(); 74 } 75 76 79 public abstract ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cxReq) 80 throws ResourceException ; 81 82 public abstract boolean equals(Object obj); 83 84 86 public Object createConnectionFactory() throws ResourceException { 87 return new DataSourceImpl(this, null); 88 } 89 90 public Object createConnectionFactory(ConnectionManager cxMgr) throws ResourceException { 91 return new DataSourceImpl(this, cxMgr); 92 } 93 94 public void getLogger(String _logTopic) throws Exception { 95 InitialContext ic = new InitialContext (); 96 97 if (trace == null || !(logTopic.equals(_logTopic))) { 98 logTopic = _logTopic; 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 ex) { 110 try { 111 if (pw != null) { 112 trace = new LoggerImpl(pw); 113 } 114 } catch (Exception e3) { 115 throw new Exception ("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 getLogWriter() throws ResourceException { 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 ex) { 135 } 136 } 137 138 return hashcode; 139 } 140 141 public ManagedConnection matchManagedConnections(Set connectionSet, Subject subject, ConnectionRequestInfo cxReq) 142 throws ResourceException { 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 pc = Utility.getPasswordCredential(this, subject, cxReq, pw); 150 Iterator it = connectionSet.iterator(); 151 Object 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 ex) { 168 throw new ResourceException (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 _pw) throws ResourceException { 178 pw = _pw; 179 } 180 181 182 public String getDbSpecificMethods() { 183 return mcfData.getMCFData(MCFData.DBSPECIFICMETHODS); 184 } 185 186 public void setDbSpecificMethods(String val) { 187 mcfData.setMCFData(MCFData.DBSPECIFICMETHODS, val); 188 } 189 190 public String getDsClass() { 191 return mcfData.getMCFData(MCFData.DSCLASS); 192 } 193 194 public void setDsClass(String val) { 195 mcfData.setMCFData(MCFData.DSCLASS, val); 196 } 197 198 public String getIsolationLevel() { 199 String str = mcfData.getMCFData(MCFData.ISOLATIONLEVEL); 200 String 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 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 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 getLoginTimeout() { 246 return mcfData.getMCFData(MCFData.LOGINTIMEOUT); 247 } 248 249 public void setLoginTimeout(String val) { 250 mcfData.setMCFData(MCFData.LOGINTIMEOUT, val); 251 } 252 253 public String getLogTopic() { 254 return mcfData.getMCFData(MCFData.LOGTOPIC); 255 } 256 257 public void setLogTopic(String val) { 258 mcfData.setMCFData(MCFData.LOGTOPIC, val); 259 try { 260 getLogger(val.trim()); 261 } catch (Exception ex) { 262 } 263 } 264 265 public String getMapperName() { 266 return mcfData.getMCFData(MCFData.MAPPERNAME); 267 } 268 269 public void setMapperName(String val) { 270 mcfData.setMCFData(MCFData.MAPPERNAME, val); 271 } 272 273 public String getPassword() { 274 return mcfData.getMCFData(MCFData.PASSWORD); 275 } 276 277 public void setPassword(String val) { 278 mcfData.setMCFData(MCFData.PASSWORD, val); 279 } 280 281 public String getUser() { 282 return mcfData.getMCFData(MCFData.USER); 283 } 284 285 public void setUser(String val) { 286 mcfData.setMCFData(MCFData.USER, val); 287 } 288 289 } | Popular Tags |