1 19 package org.netbeans.modules.editor.hints; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.OutputStream ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.WeakHashMap ; 32 import org.openide.ErrorManager; 33 import org.openide.filesystems.FileLock; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.util.WeakSet; 37 38 42 public class PersistentCache { 43 44 private static PersistentCache INSTANCE = new PersistentCache(); 45 46 public static PersistentCache getDefault() { 47 return INSTANCE; 48 } 49 50 private static Map <FileObject, Set <FileObject>> fo2ContainedErrorFiles = new WeakHashMap <FileObject, Set <FileObject>>(); private static Set <FileObject> knownObjects = new WeakSet<FileObject>(); 52 53 54 private PersistentCache() { 55 } 56 57 public void addErrorFile(FileObject key, FileObject error) { 58 Set <FileObject> contained = fo2ContainedErrorFiles.get(key); 59 60 if (contained == null) { 61 fo2ContainedErrorFiles.put(key, contained = new HashSet <FileObject>()); 62 } 63 64 if (contained.add(error)) { 65 } 67 68 knownObjects.add(error); 69 } 70 71 public void removeErrorFile(FileObject key, FileObject error) { 72 Set <FileObject> contained = fo2ContainedErrorFiles.get(key); 73 74 if (contained == null) { 75 fo2ContainedErrorFiles.put(key, contained = new HashSet <FileObject>()); 76 } 77 78 if (contained.remove(error)) { 79 } 81 82 knownObjects.add(error); 83 } 84 85 public boolean hasErrors(FileObject file) { 86 Set <FileObject> contained = fo2ContainedErrorFiles.get(file); 87 88 return contained != null && !contained.isEmpty(); 89 } 90 91 public boolean isKnown(FileObject file) { 92 return knownObjects.contains(file); 93 } 94 95 private FileObject cache = null; 96 97 private synchronized FileObject cacheFile() { 98 if (cache == null) { 99 try { 100 String nbuser = System.getProperty("netbeans.user"); 101 File userDir = new File (nbuser); 102 FileObject userDirFO = FileUtil.toFileObject(userDir); 103 104 cache = FileUtil.createFolder(userDirFO, "var/cache/errors"); 105 } catch (IOException e) { 106 ErrorManager.getDefault().notify(e); 107 } 108 } 109 110 return cache; 111 } 112 113 private FileObject getCacheFileFor(FileObject file) throws IOException { 114 File f = FileUtil.toFile(file); 115 116 if (f == null) 117 return null; 118 119 String path = f.getAbsolutePath().replace(':', '-').replace(File.separatorChar, '-'); 120 FileObject directory = FileUtil.createFolder(cacheFile(), path); 121 FileObject errors = directory.getFileObject("errors"); 122 123 if (errors == null) 124 errors = directory.createData("errors"); 125 126 return errors; 127 } 128 129 void saveCache() { 130 for (FileObject file : fo2ContainedErrorFiles.keySet()) { 131 try { 132 FileObject cache = getCacheFileFor(file); 133 134 if (cache == null) 135 continue; 136 137 FileLock lock = cache.lock(); 138 OutputStream out = cache.getOutputStream(lock); 139 140 try { 141 Set <FileObject> errors = fo2ContainedErrorFiles.get(file); 142 143 for (Iterator e = errors.iterator(); e.hasNext(); ) { 144 FileObject f = (FileObject) e.next(); 145 146 out.write(FileUtil.toFile(f).getAbsolutePath().getBytes()); 147 out.write('\n'); 148 } 149 } finally { 150 out.close(); 151 lock.releaseLock(); 152 } 153 } catch (IOException e) { 154 ErrorManager.getDefault().notify(e); 155 } 156 } 157 } 158 159 void loadCache() { 160 loadCacheRecursive(""); 161 } 162 163 private void loadCacheRecursive(String path) { 164 FileObject f = cacheFile().getFileObject(path); 165 FileObject[] children = f.getChildren(); 166 167 for (int cntr = 0; cntr < children.length; cntr++) { 168 FileObject c = children[cntr]; 169 170 if (c.isFolder()) { 171 loadCacheRecursive(path + "/" + c.getNameExt()); 172 } 173 } 174 175 try { 176 FileObject errors = f.getFileObject("errors"); 177 178 if (errors == null) 179 return ; 180 181 InputStream ins = errors.getInputStream(); 182 BufferedReader reader = new BufferedReader (new InputStreamReader (ins)); 183 Set <FileObject> errorFiles = new HashSet <FileObject>(); 184 185 try { 186 String line; 187 188 while ((line = reader.readLine()) != null) { 189 File resolved = new File (line); 190 FileObject resolvedFO = FileUtil.toFileObject(FileUtil.normalizeFile(resolved)); 191 192 if (resolvedFO != null) { 193 errorFiles.add(resolvedFO); 194 } 195 } 196 }finally { 197 reader.close(); 198 } 199 200 FileObject original = FileUtil.toFileObject(new File ("/", path)); 201 202 if (original != null) { 203 fo2ContainedErrorFiles.put(original, errorFiles); 204 knownObjects.add(original); 205 } 206 } catch (IOException e) { 207 ErrorManager.getDefault().notify(e); 208 } 209 } 210 211 } 212 | Popular Tags |