1 package org.apache.ojb.odmg; 2 3 17 18 import org.apache.ojb.broker.PBFactoryException; 19 import org.apache.ojb.broker.PBKey; 20 import org.apache.ojb.broker.PersistenceBroker; 21 import org.apache.ojb.broker.PersistenceBrokerFactory; 22 import org.apache.ojb.broker.util.BrokerHelper; 23 import org.apache.ojb.broker.util.logging.Logger; 24 import org.apache.ojb.broker.util.logging.LoggerFactory; 25 import org.odmg.DatabaseClosedException; 26 import org.odmg.DatabaseNotFoundException; 27 import org.odmg.DatabaseOpenException; 28 import org.odmg.ODMGException; 29 import org.odmg.ObjectNameNotFoundException; 30 import org.odmg.ObjectNameNotUniqueException; 31 import org.odmg.TransactionInProgressException; 32 import org.odmg.TransactionNotInProgressException; 33 34 42 public class DatabaseImpl implements org.odmg.Database 43 { 44 private Logger log = LoggerFactory.getLogger(DatabaseImpl.class); 45 46 private PBKey pbKey; 47 private boolean isOpen; 48 private ImplementationImpl odmg; 49 50 public DatabaseImpl(ImplementationImpl ojb) 51 { 52 isOpen = false; 53 this.odmg = ojb; 54 } 55 56 private TransactionImpl getTransaction() 57 { 58 Object result = odmg.currentTransaction(); 59 if(result instanceof NarrowTransaction) 63 { 64 return ((NarrowTransaction) result).getRealTransaction(); 65 } 66 return (TransactionImpl) result; 67 } 68 69 72 public PBKey getPBKey() 73 { 74 if (pbKey == null) 75 { 76 log.error("## PBKey not set, Database isOpen=" + isOpen + " ##"); 77 if (!isOpen) throw new DatabaseClosedException("Database is not open"); 78 } 79 return pbKey; 80 } 81 82 public boolean isOpen() 83 { 84 return this.isOpen; 85 } 86 87 100 public synchronized void open(String name, int accessMode) throws ODMGException 101 { 102 if (isOpen()) 103 { 104 throw new DatabaseOpenException("Database is already open"); 105 } 106 PersistenceBroker broker = null; 107 try 108 { 109 if (name == null) 110 { 111 log.info("Given argument was 'null', open default database"); 112 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 113 } 114 else 115 { 116 broker = PersistenceBrokerFactory.createPersistenceBroker( 117 BrokerHelper.extractAllTokens(name)); 118 } 119 pbKey = broker.getPBKey(); 120 isOpen = true; 121 odmg.registerOpenDatabase(this); 123 if (log.isDebugEnabled()) log.debug("Open database using PBKey " + pbKey); 124 } 125 catch (PBFactoryException ex) 126 { 127 log.error("Open database failed: " + ex.getMessage(), ex); 128 throw new DatabaseNotFoundException( 129 "OJB can't open database " + name + "\n" + ex.getMessage()); 130 } 131 finally 132 { 133 if (broker != null) 135 { 136 broker.close(); 137 } 138 } 139 } 140 141 149 public void close() throws ODMGException 150 { 151 154 if (!isOpen()) 155 { 156 throw new DatabaseClosedException("Database is not Open. Must have an open DB to call close."); 157 } 158 162 if (odmg.hasOpenTransaction() && 163 getTransaction().getAssociatedDatabase().equals(this)) 164 { 165 String msg = "Database cannot be closed, associated Tx is still open." + 166 " Transaction status is '" + TxUtil.getStatusString(getTransaction().getStatus()) + "'." + 167 " Used PBKey was "+getTransaction().getBroker().getPBKey(); 168 log.error(msg); 169 throw new TransactionInProgressException(msg); 170 } 171 isOpen = false; 172 pbKey = null; 174 if (this == odmg.getCurrentDatabase()) 176 { 177 odmg.setCurrentDatabase(null); 178 } 179 } 180 181 191 public void bind(Object object, String name) 192 throws ObjectNameNotUniqueException 193 { 194 197 if (!this.isOpen()) 198 { 199 throw new DatabaseClosedException("Database is not open. Must have an open DB to call bind."); 200 } 201 204 TransactionImpl tx = getTransaction(); 205 if (tx == null || !tx.isOpen()) 206 { 207 throw new TransactionNotInProgressException("Tx is not open. Must have an open TX to call bind."); 208 } 209 210 tx.getNamedRootsMap().bind(object, name); 211 } 212 213 220 public Object lookup(String name) throws ObjectNameNotFoundException 221 { 222 225 if (!this.isOpen()) 226 { 227 throw new DatabaseClosedException("Database is not open. Must have an open DB to call lookup"); 228 } 229 232 TransactionImpl tx = getTransaction(); 233 if (tx == null || !tx.isOpen()) 234 { 235 throw new TransactionNotInProgressException("Tx is not open. Must have an open TX to call lookup."); 236 } 237 238 return tx.getNamedRootsMap().lookup(name); 239 } 240 241 246 public void unbind(String name) throws ObjectNameNotFoundException 247 { 248 251 if (!this.isOpen()) 252 { 253 throw new DatabaseClosedException("Database is not open. Must have an open DB to call unbind"); 254 } 255 TransactionImpl tx = getTransaction(); 256 if (tx == null || !tx.isOpen()) 257 { 258 throw new TransactionNotInProgressException("Tx is not open. Must have an open TX to call lookup."); 259 } 260 261 tx.getNamedRootsMap().unbind(name); 262 } 263 264 277 public void makePersistent(Object object) 278 { 279 282 if (!this.isOpen()) 283 { 284 throw new DatabaseClosedException("Database is not open"); 285 } 286 289 TransactionImpl tx = getTransaction(); 290 if (tx == null || !tx.isOpen()) 291 { 292 throw new TransactionNotInProgressException("No transaction in progress, cannot persist"); 293 } 294 RuntimeObject rt = new RuntimeObject(object, getTransaction()); 295 tx.makePersistent(rt); 296 } 298 299 310 public void deletePersistent(Object object) 311 { 312 if (!this.isOpen()) 313 { 314 throw new DatabaseClosedException("Database is not open"); 315 } 316 TransactionImpl tx = getTransaction(); 317 if (tx == null || !tx.isOpen()) 318 { 319 throw new TransactionNotInProgressException("No transaction in progress, cannot delete persistent"); 320 } 321 RuntimeObject rt = new RuntimeObject(object, tx); 322 tx.deletePersistent(rt); 323 } 325 } 326 | Popular Tags |