KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > fileobjects > MutualExclusionSupport


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
20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects;
21
22
23 import org.openide.util.WeakSet;
24
25 import java.io.IOException JavaDoc;
26 import java.lang.ref.Reference JavaDoc;
27 import java.lang.ref.WeakReference JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.WeakHashMap JavaDoc;
31 import org.netbeans.modules.masterfs.filebasedfs.utils.FSException;
32
33 public final class MutualExclusionSupport {
34     private static final MutualExclusionSupport DEFAULT = new MutualExclusionSupport();
35     private static final Map JavaDoc exclusive = Collections.synchronizedMap(new WeakHashMap JavaDoc());
36     private static final Map JavaDoc shared = Collections.synchronizedMap(new WeakHashMap JavaDoc());
37
38     public static MutualExclusionSupport getDefault() {
39         return MutualExclusionSupport.DEFAULT;
40     }
41
42     private MutualExclusionSupport() {
43     }
44
45     public synchronized Closeable addResource(final Object JavaDoc key, final boolean isShared) throws IOException JavaDoc {
46         boolean isInUse = true;
47         final Map JavaDoc unexpected = (isShared) ? MutualExclusionSupport.exclusive : MutualExclusionSupport.shared;
48         final Map JavaDoc expected = (isShared) ? MutualExclusionSupport.shared : MutualExclusionSupport.exclusive;
49
50         final WeakSet unexpectedCounter = (WeakSet) unexpected.get(key);
51         WeakSet expectedCounter = (WeakSet) expected.get(key);;
52
53         for (int i = 0; i < 10 && isInUse; i++) {
54             isInUse = unexpectedCounter != null && unexpectedCounter.size() > 0;
55
56             if (!isInUse) {
57                 if (expectedCounter == null) {
58                     expectedCounter = new WeakSet();
59                     expected.put(key, expectedCounter);
60                 }
61                 isInUse = !isShared && expectedCounter.size() > 0;
62             }
63             
64             if (isInUse) {
65                 try {
66                     wait(200);
67                 } catch (InterruptedException JavaDoc e) {
68                     break;
69                 }
70             }
71         }
72
73         if (isInUse) {
74             if (isShared) {
75                 FSException.io("EXC_CannotGetSharedAccess", key.toString()); // NOI18N
76
} else {
77                 FSException.io("EXC_CannotGetExclusiveAccess", key.toString()); // NOI18N
78
}
79         }
80
81
82         final Closeable retVal = new Closeable(key, isShared);
83         expectedCounter.add(retVal);
84         return retVal;
85     }
86
87     private synchronized void removeResource(final Object JavaDoc key, final Object JavaDoc value, final boolean isShared) {
88         final Map JavaDoc expected = (isShared) ? MutualExclusionSupport.shared : MutualExclusionSupport.exclusive;
89
90         final WeakSet expectedCounter = (WeakSet) expected.get(key);
91         if (expectedCounter != null) {
92             expectedCounter.remove(value);
93         }
94     }
95
96
97     public final class Closeable {
98         private final boolean isShared;
99         private final Reference JavaDoc keyRef;
100         private boolean isClosed = false;
101
102         private Closeable(final Object JavaDoc key, final boolean isShared) {
103             this.isShared = isShared;
104             this.keyRef = new WeakReference JavaDoc(key);
105         }
106
107
108         public final void close() {
109             if (!isClosed()) {
110                 isClosed = true;
111                 final Object JavaDoc key = keyRef.get();
112                 if (key != null) {
113                     removeResource(key, this, isShared);
114                 }
115             }
116         }
117
118         public final boolean isClosed() {
119             return isClosed;
120         }
121     }
122 }
123
Popular Tags