KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > docscan > Cache


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.tasklist.docscan;
21
22 import org.netbeans.modules.tasklist.providers.SuggestionContext;
23 import org.openide.filesystems.FileObject;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.ErrorManager;
26
27 import java.util.*;
28 import java.io.*;
29
30 /**
31  * Cache of search results. It caches files that need not to be
32  * scanned because have not contained any match.
33  *
34  * @author Petr Kuzel
35  */

36 final class Cache {
37
38     private static Map cache;
39
40     public static void put(SuggestionContext ctx, List result) {
41         FileObject fo = ctx.getFileObject();
42         String JavaDoc name = createKey(fo);
43         if (name == null) return;
44         if (result == null || result.isEmpty()) {
45             long timestamp = fo.lastModified().getTime();
46             cache().put(name, new long[]{timestamp, System.currentTimeMillis()});
47         } else {
48             cache().remove(name);
49         }
50     }
51
52     public static List get(SuggestionContext ctx) {
53         FileObject fo = ctx.getFileObject();
54         String JavaDoc name = createKey(fo);
55         if (name == null) return null;
56         Object JavaDoc hit = cache().get(name);
57         if (hit != null) {
58             if (((long[])hit)[0] >= fo.lastModified().getTime()) {
59                 return Collections.EMPTY_LIST;
60             } else {
61                 cache().remove(name);
62             }
63         }
64         return null;
65     }
66
67     private static String JavaDoc createKey(FileObject fo) {
68         File file = FileUtil.toFile(fo);
69         if (file != null) {
70             try {
71                 return file.getCanonicalPath();
72             } catch (IOException e) {
73                 return null;
74             }
75         }
76         return null;
77     }
78
79     private static Map cache() {
80         if (cache == null) {
81             load();
82         }
83         return cache;
84     }
85
86     public static void load() {
87         ObjectInputStream ois = null;
88         try {
89             File file = getCacheFile(false);
90             InputStream in = new BufferedInputStream(new FileInputStream(file));
91             ois = new ObjectInputStream(in);
92             if (ois.readInt() == 1) {
93                 cache = (Map) ois.readObject();
94             }
95         } catch (IOException io) {
96             // null cache
97
} catch (ClassNotFoundException JavaDoc e) {
98             // null cache
99
} finally {
100             if (ois != null) {
101                 try {
102                     ois.close();
103                 } catch (IOException e) {
104                     // ignore
105
}
106             }
107         }
108         if (cache == null) {
109             cache = new HashMap(1113);
110         }
111
112         // eliminate very old entries and entries older than recent settings
113
long staleTime = System.currentTimeMillis() - 1000*60*60*24*17; //17 days
114
if (Settings.getDefault().getModificationTime() > staleTime) {
115             staleTime = Settings.getDefault().getModificationTime();
116         }
117
118         Iterator it = cache.entrySet().iterator();
119         while (it.hasNext()) {
120             Map.Entry entry = (Map.Entry) it.next();
121             if (((long[])entry.getValue())[1] < staleTime) {
122                 it.remove();
123             }
124         }
125
126     }
127
128     // XXX what are the right events to call this?
129
// now it uses SourceTasksView events and notifyFinished event (not called)
130
public static void store() {
131         if (cache == null) return;
132         try {
133             File file = getCacheFile(true);
134             OutputStream os = new FileOutputStream(file);
135             os = new BufferedOutputStream(os);
136             ObjectOutputStream oos = new ObjectOutputStream(os);
137             oos.writeInt(1);
138
139             // version 1 format
140
// map <string (canonical name), long[2] (timestamp, createdOn)>
141
oos.writeObject(cache);
142             oos.close();
143         } catch (IOException ex) {
144             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
145         } finally {
146             cache = null;
147         }
148     }
149
150     private static File getCacheFile(boolean create) throws IOException {
151         String JavaDoc loc = System.getProperty("netbeans.user") + // NOI18N
152
File.separatorChar + "var" + File.separatorChar + "cache" + File.separatorChar + "all-todos.ser";
153         File file = new File(loc);
154         if (create) {
155             if (!file.exists()) {
156                 File parent = file.getParentFile();
157                 parent.mkdirs();
158                 file.createNewFile();
159             }
160         }
161         return file;
162     }
163
164 }
165
Popular Tags