1 19 package org.netbeans.mdr.util; 20 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import org.netbeans.api.mdr.events.TransactionEvent; 24 import org.netbeans.mdr.NBMDRepositoryImpl; 25 import org.netbeans.mdr.storagemodel.MdrStorage; 26 import org.netbeans.mdr.persistence.StorageException; 27 28 34 public abstract class TransactionMutex { 35 36 private final MdrStorage storage; 38 39 private final EventNotifier notifier; 41 42 private final NBMDRepositoryImpl repository; 44 45 protected TransactionMutex(Object storage, Object notifier, Object repository) { 46 this.storage = (MdrStorage) storage; 47 this.notifier = (EventNotifier) notifier; 48 this.repository = (NBMDRepositoryImpl) repository; 49 } 50 51 public abstract boolean willFail(); 52 53 public abstract boolean pendingChanges(); 54 55 public abstract void enter(boolean writeAccess); 56 public abstract boolean leave(boolean fail); 57 58 public final void leave() { 59 leave(false); 60 } 61 62 protected final void start() { 63 TransactionEvent event = new TransactionEvent( 64 repository, 65 TransactionEvent.EVENT_TRANSACTION_START 66 ); 67 notifier.REPOSITORY.firePlannedChange(storage, event); 68 } 69 70 protected final void end(boolean fail) { 71 try { 72 TransactionEvent event = new TransactionEvent( 73 repository, 74 TransactionEvent.EVENT_TRANSACTION_END 75 ); 76 notifier.REPOSITORY.firePlannedChange(storage, event); 77 if (fail) { 78 notifier.fireCancelled(); 79 storage.rollback(); 80 } else { 82 notifier.fireChanged(); 83 storage.commit(); 84 } 87 } catch (StorageException e) { 88 try { 89 Logger.getDefault().notify(Logger.INFORMATIONAL, e); 90 storage.rollback(); 91 } catch (StorageException fatal) { 92 throw new DebugException("Fatal I/O error: " + fatal.toString()); 93 } 94 } 95 } 96 } 97 | Popular Tags |