00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.controller.connection;
00026
00027 import java.io.Serializable;
00028 import java.sql.Connection;
00029 import java.sql.SQLException;
00030 import java.util.Hashtable;
00031
00032 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
00033 import org.objectweb.cjdbc.common.i18n.Translate;
00034 import org.objectweb.cjdbc.common.log.Trace;
00035 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00036 import org.objectweb.cjdbc.common.xml.XmlComponent;
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 public abstract class AbstractConnectionManager
00048 implements
00049 Serializable,
00050 XmlComponent,
00051 Cloneable
00052 {
00053
00054
00055
00056
00057
00058
00059 static Trace logger = Trace
00060 .getLogger("org.objectweb.cjdbc.controller.connection");
00061
00062
00063 protected String backendUrl;
00064
00065
00066
00067
00068 protected String backendName;
00069
00070
00071 protected String rLogin;
00072
00073
00074 protected String rPassword;
00075
00076
00077 protected String driverClassName;
00078
00079
00080
00081
00082 protected String driverPath;
00083
00084
00085 protected boolean initialized;
00086
00087
00088 private transient Hashtable connectionForTransaction;
00089
00090
00091 private String vLogin;
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 public AbstractConnectionManager(String backendUrl, String backendName,
00113 String rLogin, String rPassword, String driverPath, String driverClassName)
00114 {
00115 if (backendUrl == null)
00116 throw new IllegalArgumentException(
00117 "Illegal null database backend URL in AbstractConnectionManager constructor");
00118
00119 if (backendName == null)
00120 throw new IllegalArgumentException(
00121 "Illegal null database backend name in AbstractConnectionManager constructor");
00122
00123 if (rLogin == null)
00124 throw new IllegalArgumentException(
00125 "Illegal null database backend login in AbstractConnectionManager constructor");
00126
00127 if (rPassword == null)
00128 throw new IllegalArgumentException(
00129 "Illegal null database backend password in AbstractConnectionManager constructor");
00130
00131 if (driverPath != null)
00132 {
00133 if (driverClassName == null)
00134 {
00135 throw new IllegalArgumentException(
00136 "Illegal null database backend driverClassName in AbstractConnectionManager constructor");
00137 }
00138 }
00139 this.backendUrl = backendUrl;
00140 this.backendName = backendName;
00141 this.rLogin = rLogin;
00142 this.rPassword = rPassword;
00143 this.driverPath = driverPath;
00144 this.driverClassName = driverClassName;
00145 connectionForTransaction = new Hashtable();
00146
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 public AbstractConnectionManager copy(String url, String name)
00159 throws Exception
00160 {
00161 AbstractConnectionManager connectionManager = (AbstractConnectionManager) this
00162 .clone();
00163 connectionManager.backendName = name;
00164 connectionManager.backendUrl = url;
00165 return connectionManager;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 public abstract void initializeConnections() throws SQLException;
00179
00180
00181
00182
00183
00184
00185 public abstract void finalizeConnections() throws SQLException;
00186
00187
00188
00189
00190
00191
00192
00193 public Connection getConnectionFromDriver()
00194
00195 {
00196 try
00197 {
00198 return DriverManager.getConnection(backendUrl, rLogin, rPassword,
00199 driverPath, driverClassName);
00200 }
00201 catch (SQLException ignore)
00202 {
00203 if (logger.isDebugEnabled())
00204 {
00205 logger.debug("failed to get connection for driver ", ignore);
00206 }
00207 return null;
00208 }
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218 public abstract Connection getConnection() throws UnreachableBackendException;
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 public Connection getConnection(long transactionId)
00232 throws UnreachableBackendException
00233 {
00234 Long lTid = new Long(transactionId);
00235 Connection c = getConnection();
00236 if (c != null)
00237 {
00238 if (connectionForTransaction.put(lTid, c) != null)
00239 {
00240 logger
00241 .error("A new connection for transaction "
00242 + lTid
00243 + " has been opened but there was a remaining connection for this transaction that has not been closed.");
00244 }
00245 }
00246 return c;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 public Connection retrieveConnection(long transactionId)
00259 {
00260 Long id = new Long(transactionId);
00261 synchronized (connectionForTransaction)
00262 {
00263 return (Connection) connectionForTransaction.get(id);
00264 }
00265 }
00266
00267
00268
00269
00270
00271
00272 public abstract void releaseConnection(Connection connection);
00273
00274
00275
00276
00277
00278
00279
00280
00281 public void releaseConnection(long transactionId)
00282 {
00283 Connection c = (Connection) connectionForTransaction.remove(new Long(
00284 transactionId));
00285
00286 if (c == null)
00287 logger.error(Translate.get("connection.transaction.unknown",
00288 transactionId));
00289 else
00290 releaseConnection(c);
00291 }
00292
00293
00294
00295
00296
00297
00298 public abstract void deleteConnection(Connection connection);
00299
00300
00301
00302
00303
00304
00305
00306
00307 public void deleteConnection(long transactionId)
00308 {
00309 Connection c = (Connection) connectionForTransaction.remove(new Long(
00310 transactionId));
00311
00312 if (c == null)
00313 logger.error(Translate.get("connection.transaction.unknown",
00314 transactionId));
00315 else
00316 deleteConnection(c);
00317 }
00318
00319
00320
00321
00322
00323
00324 public boolean isInitialized()
00325 {
00326 return initialized;
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 public String getLogin()
00339 {
00340 return rLogin;
00341 }
00342
00343
00344
00345
00346
00347
00348 public void setLogin(String rLogin)
00349 {
00350 this.rLogin = rLogin;
00351 }
00352
00353
00354
00355
00356
00357
00358 public String getPassword()
00359 {
00360 return rPassword;
00361 }
00362
00363
00364
00365
00366
00367
00368 public void setPassword(String rPassword)
00369 {
00370 this.rPassword = rPassword;
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 public abstract String getXmlImpl();
00383
00384
00385
00386
00387 public String getXml()
00388 {
00389 StringBuffer info = new StringBuffer();
00390 info.append("<" + DatabasesXmlTags.ELT_ConnectionManager + " "
00391 + DatabasesXmlTags.ATT_vLogin + "=\"" + vLogin + "\" " + ""
00392 + DatabasesXmlTags.ATT_rLogin + "=\"" + rLogin + "\" " + ""
00393 + DatabasesXmlTags.ATT_rPassword + "=\"" + rPassword + "\" " + ">");
00394 info.append(this.getXmlImpl());
00395 info.append("</" + DatabasesXmlTags.ELT_ConnectionManager + ">");
00396 return info.toString();
00397 }
00398
00399
00400
00401
00402
00403
00404
00405 protected void finalize() throws Throwable
00406 {
00407 if (isInitialized())
00408 finalizeConnections();
00409 super.finalize();
00410 }
00411
00412
00413
00414
00415 public String getVLogin()
00416 {
00417 return vLogin;
00418 }
00419
00420
00421
00422
00423 public void setVLogin(String login)
00424 {
00425 vLogin = login;
00426 }
00427
00428
00429
00430
00431
00432
00433 public abstract int getCurrentNumberOfConnections();
00434
00435
00436
00437
00438
00439
00440 public String getDriverClassName()
00441 {
00442 return driverClassName;
00443 }
00444
00445
00446
00447
00448
00449
00450 public void setDriverClassName(String driverClassName)
00451 {
00452 this.driverClassName = driverClassName;
00453 }
00454
00455
00456
00457
00458
00459
00460 public String getDriverPath()
00461 {
00462 return driverPath;
00463 }
00464
00465
00466
00467
00468
00469
00470 public void setDriverPath(String driverPath)
00471 {
00472 this.driverPath = driverPath;
00473 }
00474
00475 protected abstract Object clone() throws CloneNotSupportedException;
00476 }