KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > hints > PersistentCache


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.modules.editor.hints;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.WeakHashMap JavaDoc;
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 /**
39  *
40  * @author Jan Lahoda
41  */

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 JavaDoc<FileObject, Set JavaDoc<FileObject>> fo2ContainedErrorFiles = new WeakHashMap JavaDoc<FileObject, Set JavaDoc<FileObject>>(); //TODO: persistent! (?)
51
private static Set JavaDoc<FileObject> knownObjects = new WeakSet<FileObject>();
52     
53     /** Creates a new instance of PersistentCache */
54     private PersistentCache() {
55     }
56     
57     public void addErrorFile(FileObject key, FileObject error) {
58         Set JavaDoc<FileObject> contained = fo2ContainedErrorFiles.get(key);
59         
60         if (contained == null) {
61             fo2ContainedErrorFiles.put(key, contained = new HashSet JavaDoc<FileObject>());
62         }
63         
64         if (contained.add(error)) {
65             //TODO: fireFileStatusChanged(key);
66
}
67         
68         knownObjects.add(error);
69     }
70     
71     public void removeErrorFile(FileObject key, FileObject error) {
72         Set JavaDoc<FileObject> contained = fo2ContainedErrorFiles.get(key);
73         
74         if (contained == null) {
75             fo2ContainedErrorFiles.put(key, contained = new HashSet JavaDoc<FileObject>());
76         }
77         
78         if (contained.remove(error)) {
79             //TODO: fireFileStatusChanged(key);
80
}
81         
82         knownObjects.add(error);
83     }
84     
85     public boolean hasErrors(FileObject file) {
86         Set JavaDoc<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 JavaDoc nbuser = System.getProperty("netbeans.user");
101                 File JavaDoc userDir = new File JavaDoc(nbuser);
102                 FileObject userDirFO = FileUtil.toFileObject(userDir);
103                 
104                 cache = FileUtil.createFolder(userDirFO, "var/cache/errors");
105             } catch (IOException JavaDoc e) {
106                 ErrorManager.getDefault().notify(e);
107             }
108         }
109         
110         return cache;
111     }
112     
113     private FileObject getCacheFileFor(FileObject file) throws IOException JavaDoc {
114         File JavaDoc f = FileUtil.toFile(file);
115         
116         if (f == null)
117             return null;
118         
119         String JavaDoc 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 JavaDoc out = cache.getOutputStream(lock);
139                 
140                 try {
141                     Set JavaDoc<FileObject> errors = fo2ContainedErrorFiles.get(file);
142                     
143                     for (Iterator JavaDoc 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 JavaDoc e) {
154                 ErrorManager.getDefault().notify(e);
155             }
156         }
157     }
158     
159     void loadCache() {
160         loadCacheRecursive("");
161     }
162
163     private void loadCacheRecursive(String JavaDoc 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 JavaDoc ins = errors.getInputStream();
182             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(ins));
183             Set JavaDoc<FileObject> errorFiles = new HashSet JavaDoc<FileObject>();
184             
185             try {
186                 String JavaDoc line;
187                 
188                 while ((line = reader.readLine()) != null) {
189                     File JavaDoc resolved = new File JavaDoc(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 JavaDoc("/", path));
201             
202             if (original != null) {
203                 fo2ContainedErrorFiles.put(original, errorFiles);
204                 knownObjects.add(original);
205             }
206         } catch (IOException JavaDoc e) {
207             ErrorManager.getDefault().notify(e);
208         }
209     }
210     
211 }
212
Popular Tags