1 24 25 package org.objectweb.cjdbc.controller.connection; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.Hashtable ; 30 31 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 32 import org.objectweb.cjdbc.common.i18n.Translate; 33 import org.objectweb.cjdbc.common.log.Trace; 34 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 35 import org.objectweb.cjdbc.common.xml.XmlComponent; 36 37 46 public abstract class AbstractConnectionManager 47 implements 48 XmlComponent, 49 Cloneable 50 { 51 55 56 57 static Trace logger = Trace 58 .getLogger("org.objectweb.cjdbc.controller.connection"); 59 60 61 protected String backendUrl; 62 63 66 protected String backendName; 67 68 69 protected String rLogin; 70 71 72 protected String rPassword; 73 74 75 protected String driverClassName; 76 77 80 protected String driverPath; 81 82 83 protected boolean initialized; 84 85 86 private transient Hashtable connectionForTransaction; 87 88 89 private String vLogin; 90 91 94 95 110 public AbstractConnectionManager(String backendUrl, String backendName, 111 String rLogin, String rPassword, String driverPath, String driverClassName) 112 { 113 if (backendUrl == null) 114 throw new IllegalArgumentException ( 115 "Illegal null database backend URL in AbstractConnectionManager constructor"); 116 117 if (backendName == null) 118 throw new IllegalArgumentException ( 119 "Illegal null database backend name in AbstractConnectionManager constructor"); 120 121 if (rLogin == null) 122 throw new IllegalArgumentException ( 123 "Illegal null database backend login in AbstractConnectionManager constructor"); 124 125 if (rPassword == null) 126 throw new IllegalArgumentException ( 127 "Illegal null database backend password in AbstractConnectionManager constructor"); 128 129 if (driverPath != null) 130 { 131 if (driverClassName == null) 132 { 133 throw new IllegalArgumentException ( 134 "Illegal null database backend driverClassName in AbstractConnectionManager constructor"); 135 } 136 } 137 this.backendUrl = backendUrl; 138 this.backendName = backendName; 139 this.rLogin = rLogin; 140 this.rPassword = rPassword; 141 this.driverPath = driverPath; 142 this.driverClassName = driverClassName; 143 connectionForTransaction = new Hashtable (); 144 145 } 146 147 156 public AbstractConnectionManager copy(String url, String name) 157 throws Exception 158 { 159 AbstractConnectionManager connectionManager = (AbstractConnectionManager) this 160 .clone(); 161 connectionManager.backendName = name; 162 connectionManager.backendUrl = url; 163 return connectionManager; 164 } 165 166 169 170 176 public abstract void initializeConnections() throws SQLException ; 177 178 183 public abstract void finalizeConnections() throws SQLException ; 184 185 191 public Connection getConnectionFromDriver() 192 193 { 194 try 195 { 196 return DriverManager.getConnection(backendUrl, rLogin, rPassword, 197 driverPath, driverClassName); 198 } 199 catch (SQLException ignore) 200 { 201 if (logger.isDebugEnabled()) 202 { 203 logger.debug("failed to get connection for driver ", ignore); 204 } 205 return null; 206 } 207 } 208 209 216 public abstract Connection getConnection() throws UnreachableBackendException; 217 218 229 public Connection getConnection(long transactionId) 230 throws UnreachableBackendException 231 { 232 Long lTid = new Long (transactionId); 233 Connection c = getConnection(); 234 if (c != null) 235 { 236 if (connectionForTransaction.put(lTid, c) != null) 237 { 238 logger 239 .error("A new connection for transaction " 240 + lTid 241 + " has been opened but there was a remaining connection for this transaction that has not been closed."); 242 } 243 } 244 return c; 245 } 246 247 256 public Connection retrieveConnection(long transactionId) 257 { 258 Long id = new Long (transactionId); 259 synchronized (connectionForTransaction) 260 { 261 return (Connection ) connectionForTransaction.get(id); 262 } 263 } 264 265 270 public abstract void releaseConnection(Connection connection); 271 272 279 public void releaseConnection(long transactionId) 280 { 281 Connection c = (Connection ) connectionForTransaction.remove(new Long ( 282 transactionId)); 283 284 if (c == null) 285 logger.error(Translate.get("connection.transaction.unknown", 286 transactionId)); 287 else 288 releaseConnection(c); 289 } 290 291 296 public abstract void deleteConnection(Connection connection); 297 298 305 public void deleteConnection(long transactionId) 306 { 307 Connection c = (Connection ) connectionForTransaction.remove(new Long ( 308 transactionId)); 309 310 if (c == null) 311 logger.error(Translate.get("connection.transaction.unknown", 312 transactionId)); 313 else 314 deleteConnection(c); 315 } 316 317 322 public boolean isInitialized() 323 { 324 return initialized; 325 } 326 327 330 331 336 public String getLogin() 337 { 338 return rLogin; 339 } 340 341 346 public void setLogin(String rLogin) 347 { 348 this.rLogin = rLogin; 349 } 350 351 356 public String getPassword() 357 { 358 return rPassword; 359 } 360 361 366 public void setPassword(String rPassword) 367 { 368 this.rPassword = rPassword; 369 } 370 371 374 375 380 public abstract String getXmlImpl(); 381 382 385 public String getXml() 386 { 387 StringBuffer info = new StringBuffer (); 388 info.append("<" + DatabasesXmlTags.ELT_ConnectionManager + " " 389 + DatabasesXmlTags.ATT_vLogin + "=\"" + vLogin + "\" " + "" 390 + DatabasesXmlTags.ATT_rLogin + "=\"" + rLogin + "\" " + "" 391 + DatabasesXmlTags.ATT_rPassword + "=\"" + rPassword + "\" " + ">"); 392 info.append(this.getXmlImpl()); 393 info.append("</" + DatabasesXmlTags.ELT_ConnectionManager + ">"); 394 return info.toString(); 395 } 396 397 403 protected void finalize() throws Throwable 404 { 405 if (isInitialized()) 406 finalizeConnections(); 407 super.finalize(); 408 } 409 410 413 public String getVLogin() 414 { 415 return vLogin; 416 } 417 418 421 public void setVLogin(String login) 422 { 423 vLogin = login; 424 } 425 426 431 public abstract int getCurrentNumberOfConnections(); 432 433 438 public String getDriverClassName() 439 { 440 return driverClassName; 441 } 442 443 448 public void setDriverClassName(String driverClassName) 449 { 450 this.driverClassName = driverClassName; 451 } 452 453 458 public String getDriverPath() 459 { 460 return driverPath; 461 } 462 463 468 public void setDriverPath(String driverPath) 469 { 470 this.driverPath = driverPath; 471 } 472 473 protected abstract Object clone() throws CloneNotSupportedException ; 474 } | Popular Tags |