1 package org.apache.ojb.odmg; 2 3 17 18 import org.apache.commons.lang.SerializationUtils; 19 import org.apache.commons.lang.builder.ToStringBuilder; 20 import org.apache.commons.lang.builder.ToStringStyle; 21 import org.apache.ojb.broker.Identity; 22 import org.apache.ojb.broker.PBKey; 23 import org.apache.ojb.broker.PersistenceBroker; 24 import org.apache.ojb.broker.PersistenceBrokerFactory; 25 import org.apache.ojb.broker.util.collections.ManageableArrayList; 26 import org.apache.ojb.broker.util.configuration.Configuration; 27 import org.apache.ojb.broker.util.configuration.ConfigurationException; 28 import org.apache.ojb.broker.util.configuration.Configurator; 29 import org.apache.ojb.broker.util.factory.ConfigurableFactory; 30 import org.apache.ojb.broker.util.logging.Logger; 31 import org.apache.ojb.broker.util.logging.LoggerFactory; 32 import org.apache.ojb.odmg.locking.LockManager; 33 import org.apache.ojb.odmg.locking.LockManagerFactory; 34 import org.apache.ojb.odmg.oql.EnhancedOQLQuery; 35 import org.apache.ojb.odmg.oql.OQLQueryImpl; 36 import org.odmg.DArray; 37 import org.odmg.DBag; 38 import org.odmg.DList; 39 import org.odmg.DMap; 40 import org.odmg.DSet; 41 import org.odmg.Database; 42 import org.odmg.DatabaseClosedException; 43 import org.odmg.Implementation; 44 import org.odmg.ODMGRuntimeException; 45 import org.odmg.Transaction; 46 47 48 57 public class ImplementationImpl implements ImplementationExt 58 { 59 private Logger log = LoggerFactory.getLogger(ImplementationImpl.class); 60 61 private DatabaseImpl currentDatabase; 63 private Configurator configurator; 64 private OJBTxManager ojbTxManager; 65 private LockManager lockManager; 66 67 private Class oqlCollectionClass; 68 private boolean impliciteWriteLocks; 69 private boolean implicitLocking; 70 private boolean implicitLockingBackward; 71 private boolean ordering; 72 74 78 protected ImplementationImpl() 79 { 80 ojbTxManager = TxManagerFactory.instance(); 81 lockManager = LockManagerFactory.getLockManager(); 82 setConfigurator(PersistenceBrokerFactory.getConfigurator()); 83 Configuration conf = getConfigurator().getConfigurationFor(null); 84 oqlCollectionClass = conf.getClass("OqlCollectionClass", ManageableArrayList.class); 85 impliciteWriteLocks = (conf.getString("LockAssociations", "WRITE").equalsIgnoreCase("WRITE")); 86 implicitLocking = conf.getBoolean("ImplicitLocking", true); 87 ordering = conf.getBoolean("Ordering", true); 88 implicitLockingBackward = conf.getBoolean("ImplicitLockingBackward", false); 90 if(log.isEnabledFor(Logger.INFO)) 91 { 92 log.info("Settings: " + this.toString()); 93 } 94 } 95 96 public OJBTxManager getTxManager() 97 { 98 return ojbTxManager; 99 } 100 101 protected LockManager getLockManager() 102 { 103 return lockManager; 104 } 105 106 protected synchronized void setCurrentDatabase(DatabaseImpl curDB) 107 { 108 currentDatabase = curDB; 109 } 110 111 protected synchronized DatabaseImpl getCurrentDatabase() 112 { 113 return currentDatabase; 114 } 115 116 public PBKey getCurrentPBKey() 117 { 118 return currentDatabase.getPBKey(); 119 } 120 121 125 public Configurator getConfigurator() 126 { 127 return configurator; 128 } 129 130 134 public void setConfigurator(Configurator configurator) 135 { 136 this.configurator = configurator; 137 } 138 139 144 public Transaction newTransaction() 145 { 146 if ((getCurrentDatabase() == null)) 147 { 148 throw new DatabaseClosedException("Database is NULL, must have a DB in order to create a transaction"); 149 } 150 TransactionImpl tx = new TransactionImpl(this); 151 try 152 { 153 getConfigurator().configure(tx); 154 } 155 catch (ConfigurationException e) 156 { 157 throw new ODMGRuntimeException("Error in configuration of TransactionImpl instance: " + e.getMessage()); 158 } 159 return tx; 160 } 161 162 167 public Transaction currentTransaction() 168 { 169 if ((getCurrentDatabase() == null)) 170 { 171 throw new DatabaseClosedException("Database is NULL, must have a DB in order to create a transaction"); 172 } 173 return ojbTxManager.getTransaction(); 174 } 175 176 public boolean hasOpenTransaction() 177 { 178 TransactionImpl tx = ojbTxManager.getTransaction(); 179 return tx != null && tx.isOpen(); 180 } 181 182 187 public Database newDatabase() 188 { 189 return new DatabaseImpl(this); 190 } 191 192 197 public EnhancedOQLQuery newOQLQuery() 198 { 199 if ((getCurrentDatabase() == null) || !getCurrentDatabase().isOpen()) 200 { 201 throw new DatabaseClosedException("Database is not open"); 202 } 203 return new OQLQueryImpl(this); 204 } 205 206 211 public DList newDList() 212 { 213 if ((getCurrentDatabase() == null)) 214 { 215 throw new DatabaseClosedException("Database is NULL, cannot create a DList with a null database."); 216 } 217 return (DList) DListFactory.singleton.createCollectionOrMap(getCurrentPBKey()); 218 } 219 220 225 public DBag newDBag() 226 { 227 if ((getCurrentDatabase() == null)) 228 { 229 throw new DatabaseClosedException("Database is NULL, cannot create a DBag with a null database."); 230 } 231 return (DBag) DBagFactory.singleton.createCollectionOrMap(getCurrentPBKey()); 232 } 233 234 239 public DSet newDSet() 240 { 241 if ((getCurrentDatabase() == null)) 242 { 243 throw new DatabaseClosedException("Database is NULL, cannot create a DSet with a null database."); 244 } 245 return (DSet) DSetFactory.singleton.createCollectionOrMap(getCurrentPBKey()); 246 } 247 248 253 public DArray newDArray() 254 { 255 if ((getCurrentDatabase() == null)) 256 { 257 throw new DatabaseClosedException("Database is NULL, cannot create a DArray with a null database."); 258 } 259 return (DArray) DArrayFactory.singleton.createCollectionOrMap(getCurrentPBKey()); 260 } 261 262 267 public DMap newDMap() 268 { 269 if ((getCurrentDatabase() == null)) 270 { 271 throw new DatabaseClosedException("Database is NULL, cannot create a DMap with a null database."); 272 } 273 return (DMap) DMapFactory.singleton.createCollectionOrMap(getCurrentPBKey()); 274 } 275 276 282 public String getObjectId(Object obj) 283 { 284 Identity oid = null; 285 PersistenceBroker broker = null; 286 287 try 288 { 289 if (getCurrentDatabase() != null) 290 { 291 294 broker = PersistenceBrokerFactory.createPersistenceBroker(getCurrentDatabase().getPBKey()); 295 } 296 else 297 { 298 log.warn("Can't find open database, try to use the default configuration"); 299 302 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 303 } 304 305 oid = broker.serviceIdentity().buildIdentity(obj); 306 } 307 finally 308 { 309 if(broker != null) 310 { 311 broker.close(); 312 } 313 } 314 return new String (SerializationUtils.serialize(oid)); 315 } 316 317 320 public Database getDatabase(Object obj) 321 { 322 323 return getCurrentDatabase(); 324 } 325 326 329 protected synchronized void registerOpenDatabase(DatabaseImpl newDB) 330 { 331 DatabaseImpl old_db = getCurrentDatabase(); 332 if (old_db != null) 333 { 334 try 335 { 336 if (old_db.isOpen()) 337 { 338 log.warn("## There is still an opened database, close old one ##"); 339 old_db.close(); 340 } 341 } 342 catch (Throwable t) 343 { 344 } 346 } 347 if (log.isDebugEnabled()) log.debug("Set current database " + newDB + " PBKey was " + newDB.getPBKey()); 348 setCurrentDatabase(newDB); 349 } 351 352 360 public void setImplicitLocking(boolean value) 361 { 362 if(implicitLockingBackward) 363 { 364 ((TransactionExt)currentTransaction()).setImplicitLocking(value); 365 } 366 else 367 { 368 this.implicitLocking = value; 369 } 370 } 371 372 375 public boolean isImplicitLocking() 376 { 377 return implicitLocking; 378 } 379 380 383 public Class getOqlCollectionClass() 384 { 385 return oqlCollectionClass; 386 } 387 388 391 public void setOqlCollectionClass(Class oqlCollectionClass) 392 { 393 this.oqlCollectionClass = oqlCollectionClass; 394 } 395 396 399 public void setImpliciteWriteLocks(boolean impliciteWriteLocks) 400 { 401 this.impliciteWriteLocks = impliciteWriteLocks; 402 } 403 404 407 public boolean isImpliciteWriteLocks() 408 { 409 return impliciteWriteLocks; 410 } 411 412 public boolean isOrdering() 413 { 414 return ordering; 415 } 416 417 public void setOrdering(boolean ordering) 418 { 419 this.ordering = ordering; 420 } 421 422 432 439 public void setImplicitLockingBackward(boolean implicitLockingBackward) 440 { 441 this.implicitLockingBackward = implicitLockingBackward; 442 } 443 444 public String toString() 445 { 446 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) 447 .append("implicitLocking", isImplicitLocking()) 448 .append("implicitWriteLocks", isImpliciteWriteLocks()) 449 .append("ordering", isOrdering()) 450 .append("oqlCollectionClass", getOqlCollectionClass()) 451 .append("txManager", getTxManager()) 452 .append("lockManager", getLockManager()) 453 .toString(); 454 } 455 456 457 461 abstract static class BaseFactory extends ConfigurableFactory 462 { 463 Object createCollectionOrMap() 464 { 465 return this.createNewInstance(); 466 } 467 468 Object createCollectionOrMap(PBKey key) 469 { 470 return createNewInstance(PBKey.class, key); 471 } 472 } 473 474 static final class DListFactory extends BaseFactory 475 { 476 static final BaseFactory singleton = new DListFactory(); 477 protected String getConfigurationKey() 478 { 479 return "DListClass"; 480 } 481 } 482 483 static final class DArrayFactory extends BaseFactory 484 { 485 static final BaseFactory singleton = new DArrayFactory(); 486 protected String getConfigurationKey() 487 { 488 return "DArrayClass"; 489 } 490 } 491 492 static final class DBagFactory extends BaseFactory 493 { 494 static final BaseFactory singleton = new DBagFactory(); 495 protected String getConfigurationKey() 496 { 497 return "DBagClass"; 498 } 499 } 500 501 static final class DSetFactory extends BaseFactory 502 { 503 static final BaseFactory singleton = new DSetFactory(); 504 protected String getConfigurationKey() 505 { 506 return "DSetClass"; 507 } 508 } 509 510 static final class DMapFactory extends BaseFactory 511 { 512 static final BaseFactory singleton = new DMapFactory(); 513 protected String getConfigurationKey() 514 { 515 return "DMapClass"; 516 } 517 } 518 } 519 | Popular Tags |