1 package org.apache.ojb.broker.prevayler; 2 3 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 26 import org.apache.ojb.broker.Identity; 27 import org.apache.ojb.broker.ManageableCollection; 28 import org.apache.ojb.broker.MtoNImplementor; 29 import org.apache.ojb.broker.PBKey; 30 import org.apache.ojb.broker.PersistenceBrokerException; 31 import org.apache.ojb.broker.PersistenceBrokerFactory; 32 import org.apache.ojb.broker.TransactionAbortedException; 33 import org.apache.ojb.broker.TransactionInProgressException; 34 import org.apache.ojb.broker.TransactionNotInProgressException; 35 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF; 36 import org.apache.ojb.broker.core.PersistenceBrokerImpl; 37 import org.apache.ojb.broker.query.Query; 38 import org.apache.ojb.broker.query.QueryByIdentity; 39 import org.apache.ojb.broker.util.BrokerHelper; 40 import org.apache.ojb.broker.util.ObjectModification; 41 import org.prevayler.Command; 42 import org.prevayler.Prevayler; 43 import org.prevayler.implementation.SnapshotPrevayler; 44 45 46 57 public class PBPrevaylerImpl extends PersistenceBrokerImpl 58 { 59 60 private transient Database db; 61 62 private Prevayler prevayler; 63 64 private ArrayList commandLog = new ArrayList (100); 65 66 private boolean inTransaction = false; 67 68 69 74 public PBPrevaylerImpl(PBKey key, PersistenceBrokerFactoryIF pbf) 75 { 76 super(key, pbf); 77 refresh(); 78 if(key == null) throw new PersistenceBrokerException("Could not instantiate broker with PBKey 'null'"); 79 this.pbf = pbf; 80 this.setPBKey(key); 81 82 brokerHelper = new BrokerHelper(this); 83 90 try 92 { 93 prevayler = new SnapshotPrevayler(new Database(), "PrevalenceBase" + File.separator + "Database"); 94 db = (Database) prevayler.system(); 95 db.setBroker(this); 96 } 97 catch (Exception e) 98 { 99 } 100 } 101 102 103 106 public void abortTransaction() throws TransactionNotInProgressException 107 { 108 if (! isInTransaction()) 109 { 110 throw new TransactionNotInProgressException(); 111 } 112 inTransaction = false; 113 commandLog.clear(); 114 } 115 116 119 public void beginTransaction() 120 throws TransactionInProgressException, TransactionAbortedException 121 { 122 if (this.isInTransaction()) 123 { 124 throw new TransactionInProgressException(); 125 } 126 inTransaction = true; 127 commandLog.clear(); 128 } 129 130 133 public void commitTransaction() 134 throws TransactionNotInProgressException, TransactionAbortedException 135 { 136 if (!isInTransaction()) 137 { 138 throw new TransactionNotInProgressException(); 139 } 140 141 Iterator iter = commandLog.iterator(); 142 try 143 { 144 while (iter.hasNext()) 145 { 146 Command cmd = (Command) iter.next(); 147 prevayler.executeCommand(cmd); 148 } 149 } 150 catch (Exception e) 151 { 152 this.abortTransaction(); 153 } 154 inTransaction = false; 155 commandLog.clear(); 156 } 157 158 161 public boolean isInTransaction() throws PersistenceBrokerException 162 { 163 return inTransaction; 164 } 165 166 169 public boolean close() 170 { 171 if (isInTransaction()) 172 { 173 abortTransaction(); 174 } 175 try 176 { 177 ((SnapshotPrevayler)prevayler).takeSnapshot(); 178 } 179 catch (IOException e) 180 { 181 } 182 setClosed(true); 183 return true; 184 } 185 186 189 public void clearCache() throws PersistenceBrokerException 190 { 191 } 192 193 196 public void removeFromCache(Object obj) throws PersistenceBrokerException 197 { 198 } 199 200 203 public void store(Object obj, ObjectModification modification) 204 throws PersistenceBrokerException 205 { 206 this.store(obj); 207 } 208 209 212 public void store(Object obj) throws PersistenceBrokerException 213 { 214 if (! (obj instanceof Serializable )) 215 { 216 throw new PersistenceBrokerException(obj.getClass().getName() + "does not implement java.io.Serializable."); 217 } 218 219 CommandStore cmd = new CommandStore(obj); 220 commandLog.add(cmd); 221 } 222 223 226 public void delete(Object obj) throws PersistenceBrokerException 227 { 228 Command cmd = new CommandDelete(obj); 229 commandLog.add(cmd); 230 } 231 232 235 public void deleteMtoNImplementor(MtoNImplementor m2nImpl) 236 throws PersistenceBrokerException 237 { 238 } 239 240 243 public void addMtoNImplementor(MtoNImplementor m2nImpl) 244 throws PersistenceBrokerException 245 { 246 } 247 248 251 public void deleteByQuery(Query query) throws PersistenceBrokerException 252 { 253 throw new PersistenceBrokerException("not yet implemented"); 254 } 255 256 259 public void retrieveAllReferences(Object pInstance) 260 throws PersistenceBrokerException 261 { 262 } 263 264 267 public void retrieveReference(Object pInstance, String pAttributeName) 268 throws PersistenceBrokerException 269 { 270 } 271 272 275 public int getCount(Query query) throws PersistenceBrokerException 276 { 277 throw new PersistenceBrokerException("not yet implemented"); 278 } 279 280 283 public Collection getCollectionByQuery(Query query) 284 throws PersistenceBrokerException 285 { 286 return db.getTable().values(); 288 } 289 290 293 public ManageableCollection getCollectionByQuery( 294 Class collectionClass, 295 Query query) 296 throws PersistenceBrokerException 297 { 298 throw new PersistenceBrokerException("not yet implemented"); 299 } 300 301 304 public Iterator getIteratorByQuery(Query query) 305 throws PersistenceBrokerException 306 { 307 throw new PersistenceBrokerException("not yet implemented"); 308 } 309 310 313 public Iterator getReportQueryIteratorByQuery(Query query) 314 throws PersistenceBrokerException 315 { 316 throw new PersistenceBrokerException("not yet implemented"); 317 } 318 319 322 public Object getObjectByIdentity(Identity id) 323 throws PersistenceBrokerException 324 { 325 return db.lookupObjectByIdentity(id); 326 } 327 328 331 public Object getObjectByQuery(Query query) 332 throws PersistenceBrokerException 333 { 334 if (query instanceof QueryByIdentity) 335 { 336 Object id = ((QueryByIdentity) query).getExampleObject(); 337 if (! (id instanceof Identity)) 338 { 339 id = new Identity(id,PersistenceBrokerFactory.defaultPersistenceBroker()); 340 } 341 Identity oid = (Identity) id; 342 return db.lookupObjectByIdentity(oid); 343 } 344 else 345 { 346 throw new PersistenceBrokerException("not yet implemented"); 347 } 348 } 349 350 353 public Enumeration getPKEnumerationByQuery( 354 Class PrimaryKeyClass, 355 Query query) 356 throws PersistenceBrokerException 357 { 358 throw new PersistenceBrokerException("not yet implemented"); 359 } 360 361 362 } 363 | Popular Tags |