KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > source > usages > Index


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.retouche.source.usages;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URISyntaxException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.zip.ZipEntry JavaDoc;
42 import java.util.zip.ZipInputStream JavaDoc;
43 import java.util.zip.ZipOutputStream JavaDoc;
44 import org.netbeans.api.retouche.source.ClassIndex;
45 import org.openide.ErrorManager;
46 import org.openide.filesystems.FileLock;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileSystem;
49 import org.openide.filesystems.FileUtil;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.filesystems.URLMapper;
52 import org.openide.util.Exceptions;
53
54 /**
55  * This file is originally from Retouche, the Java Support
56  * infrastructure in NetBeans. I have modified the file as little
57  * as possible to make merging Retouche fixes back as simple as
58  * possible.
59  *
60  * Index SPI. Represents an index for usages data
61  * @author Tomas Zezula
62  */

63 // BEGIN TOR MODIFICATIONS
64
public abstract class Index extends org.netbeans.api.gsf.Index {
65 // END TOR MODIFICATIONS
66

67     public enum BooleanOperator {
68         AND,
69         OR
70     };
71     
72     private static final int VERSION = 0;
73     private static final int SUBVERSION = 105;
74     private static final String JavaDoc NB_USER_DIR = "netbeans.user"; //NOI18N
75
private static final String JavaDoc SEGMENTS_FILE = "segments"; //NOI18N
76
private static final String JavaDoc CLASSES = "classes"; //NOI18N
77
private static final String JavaDoc SLICE_PREFIX = "s"; //NOI18N
78
private static final String JavaDoc INDEX_DIR = "var"+File.separatorChar+"cache"+File.separatorChar+"gsf-index"+File.separatorChar+VERSION+'.'+SUBVERSION; //NOI18N
79
// BEGIN TOR MODIFICATIONS
80
protected static final String JavaDoc PREINDEXED = "preindexed-" + VERSION + '.' + SUBVERSION; // NOI18N
81
private static final String JavaDoc PREINDEXED_MARKER = "preindexed";
82     // END TOR MODIFICATIONS
83

84     private static Properties JavaDoc segments;
85     private static Map JavaDoc<String JavaDoc, String JavaDoc> invertedSegments;
86     private static File JavaDoc cacheFolder;
87     private static File JavaDoc segmentsFile;
88     private static int index = 0;
89     
90     public abstract boolean isValid (boolean tryOpen) throws IOException JavaDoc;
91     public abstract boolean isUpToDate (String JavaDoc resourceName, long timeStamp) throws IOException JavaDoc;
92     public abstract void clear () throws IOException JavaDoc;
93     public abstract void close () throws IOException JavaDoc;
94     
95     
96     private static void loadSegments () throws IOException JavaDoc {
97         if (segments == null) {
98             File JavaDoc cacheFolder = getCacheFolder();
99             assert cacheFolder != null;
100             segments = new Properties JavaDoc ();
101             invertedSegments = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
102             segmentsFile = FileUtil.normalizeFile(new File JavaDoc (cacheFolder, SEGMENTS_FILE));
103             if (segmentsFile.exists()) {
104                 InputStream JavaDoc in = new FileInputStream JavaDoc (segmentsFile);
105                 try {
106                     segments.load (in);
107                 } finally {
108                     in.close();
109                 }
110             }
111             for (Map.Entry JavaDoc entry : segments.entrySet()) {
112                 String JavaDoc segment = (String JavaDoc) entry.getKey();
113                 String JavaDoc root = (String JavaDoc) entry.getValue();
114                 invertedSegments.put(root,segment);
115                 try {
116                     index = Math.max (index,Integer.parseInt(segment.substring(SLICE_PREFIX.length())));
117                 } catch (NumberFormatException JavaDoc nfe) {
118                     ErrorManager.getDefault().notify(nfe);
119                 }
120             }
121             assert segmentsFile != null;
122         }
123     }
124     
125     
126     private static void storeSegments () throws IOException JavaDoc {
127         assert segmentsFile != null;
128         OutputStream JavaDoc out = new FileOutputStream JavaDoc (segmentsFile);
129         try {
130             segments.store(out,null);
131         } finally {
132             out.close();
133         }
134     }
135     
136     
137     public static URL JavaDoc getSourceRootForClassFolder (final URL JavaDoc classFolder) {
138         if ("file".equals(classFolder.getProtocol())) { //NOI18N
139
try {
140                 final File JavaDoc file = FileUtil.normalizeFile(new File JavaDoc (classFolder.toURI()));
141                 final File JavaDoc segFolder = file.getParentFile();
142                 if (segFolder == null) {
143                     return null;
144                 }
145                 final Object JavaDoc cFolder = segFolder.getParentFile();
146                 if (cFolder == null || !cFolder.equals(cacheFolder)) {
147                     return null;
148                 }
149                 String JavaDoc source = segments.getProperty(segFolder.getName());
150                 if (source != null) {
151                     try {
152                         return new URL JavaDoc (source);
153                     } catch (IOException JavaDoc ioe) {
154                         ErrorManager.getDefault().notify(ioe);
155                     }
156                 }
157             } catch (URISyntaxException JavaDoc e) {
158                 ErrorManager.getDefault().notify(e);
159             }
160         }
161         return null;
162     }
163         
164     
165     public static synchronized File JavaDoc getDataFolder (final URL JavaDoc root) throws IOException JavaDoc {
166         loadSegments ();
167         final String JavaDoc rootName = root.toExternalForm();
168         String JavaDoc slice = invertedSegments.get (rootName);
169         // BEGIN TOR MODIFICATIONS
170
FileObject extract = null;
171         // END TOR MODIFICATIONS
172
if ( slice == null) {
173             slice = SLICE_PREFIX + (++index);
174             while (segments.getProperty(slice) != null) {
175                 slice = SLICE_PREFIX + (++index);
176             }
177             segments.put (slice,rootName);
178             invertedSegments.put(rootName, slice);
179             
180             // BEGIN TOR MODIFICATIONS
181
// See if I have pre-indexed data for this file
182
FileObject rootFo = URLMapper.findFileObject(root);
183             if (rootFo != null) {
184                 extract = rootFo.getFileObject(PREINDEXED, "zip"); // NOI18N
185
}
186             // END TOR MODIFICATIONS
187

188             storeSegments ();
189         }
190         File JavaDoc result = FileUtil.normalizeFile (new File JavaDoc (cacheFolder, slice));
191         if (!result.exists()) {
192             result.mkdir();
193             // BEGIN TOR MODIFICATIONS
194
if (extract != null) {
195                 File JavaDoc extractFile = FileUtil.toFile(extract);
196                 FileObject dest = FileUtil.toFileObject(result);
197                 if (dest != null) {
198                     extractZip(dest, new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(extractFile)));
199                 }
200             }
201             // END TOR MODIFICATIONS
202
}
203         return result;
204     }
205     
206     // BEGIN TOR MODIFICATIONS
207
static boolean isPreindexed(File JavaDoc dataDir) {
208        return new File JavaDoc(dataDir, PREINDEXED_MARKER).exists(); // NOI18N
209
}
210
211     // Based on openide/fs' FileUtil.extractJar
212
private static void extractZip(final FileObject fo, final InputStream JavaDoc is)
213     throws IOException JavaDoc {
214         FileSystem fs = fo.getFileSystem();
215
216         fs.runAtomicAction(
217             new FileSystem.AtomicAction() {
218                 public void run() throws IOException JavaDoc {
219                     extractZipImpl(fo, is);
220                 }
221             }
222         );
223     }
224
225     /** Does the actual extraction of the Jar file.
226      */

227     // Based on openide/fs' FileUtil.extractJarImpl
228
private static void extractZipImpl(FileObject fo, InputStream JavaDoc is)
229     throws IOException JavaDoc {
230         ZipEntry JavaDoc je;
231
232         ZipInputStream JavaDoc jis = new ZipInputStream JavaDoc(is);
233
234         while ((je = jis.getNextEntry()) != null) {
235             String JavaDoc name = je.getName();
236
237             if (name.toLowerCase().startsWith("meta-inf/")) {
238                 continue; // NOI18N
239
}
240
241             if (je.isDirectory()) {
242                 FileUtil.createFolder(fo, name);
243
244                 continue;
245             }
246
247             // copy the file
248
FileObject fd = FileUtil.createData(fo, name);
249             FileLock lock = fd.lock();
250
251             try {
252                 OutputStream JavaDoc os = fd.getOutputStream(lock);
253
254                 try {
255                     FileUtil.copy(jis, os);
256                 } finally {
257                     os.close();
258                 }
259             } finally {
260                 lock.releaseLock();
261             }
262         }
263     }
264
265     // Only done at build time / ahead of time.
266
static void preindex(URL JavaDoc root) {
267         try {
268             FileObject rootFo = URLMapper.findFileObject(root);
269             File JavaDoc dataFile = Index.getDataFolder(root);
270             // Create "preindexed" file
271
// Zip contents of data folder up and store it as a preindexed file in the rootFo
272

273             File JavaDoc output = new File JavaDoc(FileUtil.toFile(rootFo), PREINDEXED + ".zip"); // NOI18N
274
OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(output));
275             
276             ZipEntry JavaDoc je;
277
278             ZipOutputStream JavaDoc jis = new ZipOutputStream JavaDoc(os);
279
280             je = new ZipEntry JavaDoc(PREINDEXED_MARKER);
281             jis.putNextEntry(je);
282             jis.closeEntry();
283             
284             File JavaDoc gsf = new File JavaDoc(dataFile, LuceneIndex.REFERENCES); // NOI18N
285
assert gsf.exists();
286             
287             File JavaDoc[] files = gsf.listFiles();
288             for (File JavaDoc f : files) {
289                 ZipEntry JavaDoc ze = new ZipEntry JavaDoc(LuceneIndex.REFERENCES + "/" + f.getName()); // NOI18N
290
jis.putNextEntry(ze);
291                 
292                 // Copy data
293
InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
294                 FileUtil.copy(is, jis);
295                 
296                 jis.closeEntry();
297             }
298             
299             jis.finish();
300             jis.close();
301         } catch (IOException JavaDoc ioe) {
302             Exceptions.printStackTrace(ioe);
303         }
304     }
305     // END TOR MODIFICATIONS
306

307     public static File JavaDoc getClassFolder (final URL JavaDoc url) throws IOException JavaDoc {
308         return getClassFolderImpl(url);
309     }
310     
311     public static File JavaDoc getClassFolder (final File JavaDoc root) throws IOException JavaDoc {
312         try {
313             return getClassFolderImpl(root.toURI().toURL());
314         } catch (MalformedURLException JavaDoc mue) {
315             ErrorManager.getDefault().notify (mue);
316             return null;
317         }
318     }
319     
320     private static File JavaDoc getClassFolderImpl (final URL JavaDoc url) throws IOException JavaDoc {
321         final File JavaDoc dataFolder = getDataFolder (url);
322         final File JavaDoc result= new File JavaDoc (dataFolder, CLASSES);
323         if (!result.exists()) {
324             result.mkdir();
325         }
326         return result;
327     }
328     
329     private static synchronized File JavaDoc getCacheFolder () {
330         if (cacheFolder == null) {
331             final String JavaDoc nbUserProp = System.getProperty(NB_USER_DIR);
332             assert nbUserProp != null;
333             final File JavaDoc nbUserDir = new File JavaDoc (nbUserProp);
334             cacheFolder = FileUtil.normalizeFile(new File JavaDoc (nbUserDir, INDEX_DIR));
335             if (!cacheFolder.exists()) {
336                 boolean created = cacheFolder.mkdirs();
337                 assert created : "Cannot create cache folder"; //NOI18N
338
}
339             else {
340                 assert cacheFolder.isDirectory() && cacheFolder.canRead() && cacheFolder.canWrite();
341             }
342         }
343         return cacheFolder;
344     }
345     
346     /**
347      * Only for unit tests!
348      *
349      */

350     static synchronized void setCacheFolder (final File JavaDoc folder) {
351         assert folder != null && folder.exists() && folder.canRead() && folder.canWrite();
352         cacheFolder = folder;
353     }
354     
355 }
356
Popular Tags