KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > layers > LocalFileSystemEx


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.core.startup.layers;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.concurrent.Callable JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import org.openide.filesystems.*;
34
35 /** Extends LocalFileSystem by useful features. It is used as
36  * delegates being part of SystemFileSystem.
37  *
38  * @author Vita Stejskal
39  */

40 public final class LocalFileSystemEx extends LocalFileSystem {
41
42     /** name -> FileObject */
43     private static HashMap JavaDoc<String JavaDoc,FileObject> allLocks = new HashMap JavaDoc<String JavaDoc,FileObject> (7);
44     private static HashSet JavaDoc<String JavaDoc> pLocks = new HashSet JavaDoc<String JavaDoc> (7);
45 // private static HashMap allThreads = new HashMap (7);
46

47     public static String JavaDoc [] getLocks () {
48         synchronized (allLocks) {
49             removeInvalid (pLocks);
50             LinkedList JavaDoc<String JavaDoc> l = new LinkedList JavaDoc<String JavaDoc> ();
51             l.addAll (allLocks.keySet ());
52             l.addAll (pLocks);
53             return l.toArray (new String JavaDoc [l.size ()]);
54         }
55     }
56
57     public static boolean hasLocks () {
58         synchronized (allLocks) {
59             removeInvalid (pLocks);
60             return !allLocks.isEmpty () || !pLocks.isEmpty ();
61         }
62     }
63     
64     public static void potentialLock (String JavaDoc name) {
65         synchronized (allLocks) {
66             pLocks.add (name);
67         }
68     }
69     
70     public static void potentialLock (String JavaDoc o, String JavaDoc n) {
71         synchronized (allLocks) {
72             if (pLocks.remove (o)) {
73                 pLocks.add (n);
74             }
75         }
76     }
77
78     private static void removeInvalid (Set JavaDoc names) {
79         FileSystem sfs = Repository.getDefault ().getDefaultFileSystem ();
80         Iterator JavaDoc i = names.iterator ();
81         while (i.hasNext ()) {
82             String JavaDoc name = (String JavaDoc) i.next ();
83             if (null == sfs.findResource (name)) {
84                 // file lock recorded in potentialLock has been used
85
// in operation which masked file as hidden and nothing
86
// was actually locked
87
i.remove ();
88             }
89         }
90     }
91
92     /** Creates new LocalFileSystemEx */
93     public LocalFileSystemEx () {
94         this( false );
95     }
96     
97     /**
98      * @since 1.8
99      */

100     LocalFileSystemEx( boolean supportRemoveWritablesAttr ) {
101         if( supportRemoveWritablesAttr ) {
102             attr = new DelegatingAttributes( attr );
103         }
104     }
105
106     protected void lock (String JavaDoc name) throws IOException JavaDoc {
107         super.lock (name);
108         synchronized (allLocks) {
109             FileObject fo = findResource (name);
110             allLocks.put (name, fo);
111             pLocks.remove (name);
112 // allThreads.put (name, new Throwable ("LocalFileSystemEx.lock() is locking file: " + name));
113
}
114     }
115     
116     protected void unlock (String JavaDoc name) {
117         synchronized (allLocks) {
118             if (allLocks.containsKey (name)) {
119                 allLocks.remove (name);
120 // allThreads.remove (name);
121
} else {
122                 FileObject fo = findResource (name);
123                 if (fo != null) {
124             for (Map.Entry JavaDoc entry: allLocks.entrySet()) {
125                         if (fo.equals (entry.getValue ())) {
126                             allLocks.remove (entry.getKey ());
127 // allThreads.remove (entry.getKey ());
128
break;
129                         }
130                     }
131                 } else {
132                     Logger.getLogger(LocalFileSystemEx.class.getName()).log(Level.WARNING, null,
133                                       new Throwable JavaDoc("Can\'t unlock file " + name +
134                                                     ", it\'s lock was not found or it wasn\'t locked."));
135                 }
136             }
137         }
138         super.unlock (name);
139     }
140     
141     private class DelegatingAttributes implements AbstractFileSystem.Attr {
142         
143         private AbstractFileSystem.Attr a;
144         
145         public DelegatingAttributes( AbstractFileSystem.Attr a ) {
146             this.a = a;
147         }
148
149         public Object JavaDoc readAttribute(String JavaDoc name, String JavaDoc attrName) {
150             if( "removeWritables".equals( attrName ) ) {
151                 return new WritableRemover( name );
152             }
153             return a.readAttribute( name, attrName );
154         }
155
156         public void writeAttribute(String JavaDoc name, String JavaDoc attrName, Object JavaDoc value) throws IOException JavaDoc {
157             a.writeAttribute( name, attrName, value );
158         }
159
160         public Enumeration JavaDoc<String JavaDoc> attributes(String JavaDoc name) {
161             return a.attributes( name );
162         }
163
164         public void renameAttributes(String JavaDoc oldName, String JavaDoc newName) {
165             a.readAttribute( oldName, newName );
166         }
167
168         public void deleteAttributes(String JavaDoc name) {
169             a.deleteAttributes( name );
170         }
171     }
172
173     private class WritableRemover implements Callable JavaDoc {
174         private String JavaDoc name;
175         public WritableRemover( String JavaDoc name ) {
176             this.name = name;
177         }
178         
179         public Object JavaDoc call() throws Exception JavaDoc {
180             FileObject fo = findResource( name );
181             if( null != fo ) {
182                 fo.delete();
183             }
184             return null;
185         }
186         
187     }
188 }
189
Popular Tags