1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.LinkedList ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.Iterator ; 30 import java.util.concurrent.Callable ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import org.openide.filesystems.*; 34 35 40 public final class LocalFileSystemEx extends LocalFileSystem { 41 42 43 private static HashMap <String ,FileObject> allLocks = new HashMap <String ,FileObject> (7); 44 private static HashSet <String > pLocks = new HashSet <String > (7); 45 47 public static String [] getLocks () { 48 synchronized (allLocks) { 49 removeInvalid (pLocks); 50 LinkedList <String > l = new LinkedList <String > (); 51 l.addAll (allLocks.keySet ()); 52 l.addAll (pLocks); 53 return l.toArray (new String [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 name) { 65 synchronized (allLocks) { 66 pLocks.add (name); 67 } 68 } 69 70 public static void potentialLock (String o, String n) { 71 synchronized (allLocks) { 72 if (pLocks.remove (o)) { 73 pLocks.add (n); 74 } 75 } 76 } 77 78 private static void removeInvalid (Set names) { 79 FileSystem sfs = Repository.getDefault ().getDefaultFileSystem (); 80 Iterator i = names.iterator (); 81 while (i.hasNext ()) { 82 String name = (String ) i.next (); 83 if (null == sfs.findResource (name)) { 84 i.remove (); 88 } 89 } 90 } 91 92 93 public LocalFileSystemEx () { 94 this( false ); 95 } 96 97 100 LocalFileSystemEx( boolean supportRemoveWritablesAttr ) { 101 if( supportRemoveWritablesAttr ) { 102 attr = new DelegatingAttributes( attr ); 103 } 104 } 105 106 protected void lock (String name) throws IOException { 107 super.lock (name); 108 synchronized (allLocks) { 109 FileObject fo = findResource (name); 110 allLocks.put (name, fo); 111 pLocks.remove (name); 112 } 114 } 115 116 protected void unlock (String name) { 117 synchronized (allLocks) { 118 if (allLocks.containsKey (name)) { 119 allLocks.remove (name); 120 } else { 122 FileObject fo = findResource (name); 123 if (fo != null) { 124 for (Map.Entry entry: allLocks.entrySet()) { 125 if (fo.equals (entry.getValue ())) { 126 allLocks.remove (entry.getKey ()); 127 break; 129 } 130 } 131 } else { 132 Logger.getLogger(LocalFileSystemEx.class.getName()).log(Level.WARNING, null, 133 new Throwable ("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 readAttribute(String name, String 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 name, String attrName, Object value) throws IOException { 157 a.writeAttribute( name, attrName, value ); 158 } 159 160 public Enumeration <String > attributes(String name) { 161 return a.attributes( name ); 162 } 163 164 public void renameAttributes(String oldName, String newName) { 165 a.readAttribute( oldName, newName ); 166 } 167 168 public void deleteAttributes(String name) { 169 a.deleteAttributes( name ); 170 } 171 } 172 173 private class WritableRemover implements Callable { 174 private String name; 175 public WritableRemover( String name ) { 176 this.name = name; 177 } 178 179 public Object call() throws Exception { 180 FileObject fo = findResource( name ); 181 if( null != fo ) { 182 fo.delete(); 183 } 184 return null; 185 } 186 187 } 188 } 189 | Popular Tags |