KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > util > TransactionMutex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.util;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
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 /** This class implements mutex that controls storage transactions as well as
29  * MDR events firing.
30  *
31  * @author Martin Matula
32  * @version 0.1
33  */

34 public abstract class TransactionMutex {
35
36     // storage
37
private final MdrStorage storage;
38
39     // events notifier
40
private final EventNotifier notifier;
41
42     // the repository (used as event source for transaction events)
43
private final NBMDRepositoryImpl repository;
44     
45     protected TransactionMutex(Object JavaDoc storage, Object JavaDoc notifier, Object JavaDoc 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 // Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException("rollback"));
81
} else {
82                 notifier.fireChanged();
83                 storage.commit();
84                 //Logger.getDefault().log("commited");
85
// Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException("commit"));
86
}
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