KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > 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.java.source.usages;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URISyntaxException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Set JavaDoc;
35 import org.netbeans.api.java.source.ClassIndex;
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileUtil;
38
39 /**
40  * Index SPI. Represents an index for usages data
41  * @author Tomas Zezula
42  */

43 public abstract class Index {
44     
45     public enum BooleanOperator {
46         AND,
47         OR
48     };
49     
50     private static final int VERSION = 0;
51     private static final int SUBVERSION = 1;
52     private static final String JavaDoc NB_USER_DIR = "netbeans.user"; //NOI18N
53
private static final String JavaDoc SEGMENTS_FILE = "segments"; //NOI18N
54
private static final String JavaDoc CLASSES = "classes"; //NOI18N
55
private static final String JavaDoc SLICE_PREFIX = "s"; //NOI18N
56
private static final String JavaDoc INDEX_DIR = "var"+File.separatorChar+"cache"+File.separatorChar+"index"+File.separatorChar+VERSION+'.'+SUBVERSION; //NOI18N
57

58     private static Properties JavaDoc segments;
59     private static Map JavaDoc<String JavaDoc, String JavaDoc> invertedSegments;
60     private static File JavaDoc cacheFolder;
61     private static File JavaDoc segmentsFile;
62     private static int index = 0;
63     
64     public abstract boolean isValid (boolean tryOpen) throws IOException JavaDoc;
65     public abstract List JavaDoc<String JavaDoc> getUsagesData (String JavaDoc resourceName, Set JavaDoc<ClassIndexImpl.UsageType> mask, BooleanOperator operator) throws IOException JavaDoc;
66     public abstract List JavaDoc<String JavaDoc> getUsagesFQN (String JavaDoc resourceName, Set JavaDoc<ClassIndexImpl.UsageType> mask, BooleanOperator operator) throws IOException JavaDoc;
67     public abstract List JavaDoc<String JavaDoc> getReferencesData (String JavaDoc resourceName) throws IOException JavaDoc;
68     public abstract <T> void getDeclaredTypes (String JavaDoc simpleName, ClassIndex.NameKind kind, ResultConvertor<T> convertor, Set JavaDoc<? super T> result) throws IOException JavaDoc;
69     public abstract void getPackageNames (String JavaDoc prefix, boolean directOnly, Set JavaDoc<String JavaDoc> result) throws IOException JavaDoc;
70     public abstract void store (Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> refs, Set JavaDoc<String JavaDoc> toDelete) throws IOException JavaDoc;
71     public abstract void store (Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> refs, List JavaDoc<String JavaDoc> topLevels) throws IOException JavaDoc;
72     public abstract boolean isUpToDate (String JavaDoc resourceName, long timeStamp) throws IOException JavaDoc;
73     public abstract void clear () throws IOException JavaDoc;
74     public abstract void close () throws IOException JavaDoc;
75     
76     
77     private static void loadSegments () throws IOException JavaDoc {
78         if (segments == null) {
79             File JavaDoc cacheFolder = getCacheFolder();
80             assert cacheFolder != null;
81             segments = new Properties JavaDoc ();
82             invertedSegments = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
83             segmentsFile = FileUtil.normalizeFile(new File JavaDoc (cacheFolder, SEGMENTS_FILE));
84             if (segmentsFile.exists()) {
85                 InputStream JavaDoc in = new FileInputStream JavaDoc (segmentsFile);
86                 try {
87                     segments.load (in);
88                 } finally {
89                     in.close();
90                 }
91             }
92             for (Map.Entry JavaDoc entry : segments.entrySet()) {
93                 String JavaDoc segment = (String JavaDoc) entry.getKey();
94                 String JavaDoc root = (String JavaDoc) entry.getValue();
95                 invertedSegments.put(root,segment);
96                 try {
97                     index = Math.max (index,Integer.parseInt(segment.substring(SLICE_PREFIX.length())));
98                 } catch (NumberFormatException JavaDoc nfe) {
99                     ErrorManager.getDefault().notify(nfe);
100                 }
101             }
102             assert segmentsFile != null;
103         }
104     }
105     
106     
107     private static void storeSegments () throws IOException JavaDoc {
108         assert segmentsFile != null;
109         OutputStream JavaDoc out = new FileOutputStream JavaDoc (segmentsFile);
110         try {
111             segments.store(out,null);
112         } finally {
113             out.close();
114         }
115     }
116     
117     
118     public static URL JavaDoc getSourceRootForClassFolder (final URL JavaDoc classFolder) {
119         if ("file".equals(classFolder.getProtocol())) { //NOI18N
120
try {
121                 final File JavaDoc file = FileUtil.normalizeFile(new File JavaDoc (classFolder.toURI()));
122                 final File JavaDoc segFolder = file.getParentFile();
123                 if (segFolder == null) {
124                     return null;
125                 }
126                 final Object JavaDoc cFolder = segFolder.getParentFile();
127                 if (cFolder == null || !cFolder.equals(cacheFolder)) {
128                     return null;
129                 }
130                 String JavaDoc source = segments.getProperty(segFolder.getName());
131                 if (source != null) {
132                     try {
133                         return new URL JavaDoc (source);
134                     } catch (IOException JavaDoc ioe) {
135                         ErrorManager.getDefault().notify(ioe);
136                     }
137                 }
138             } catch (URISyntaxException JavaDoc e) {
139                 ErrorManager.getDefault().notify(e);
140             }
141         }
142         return null;
143     }
144         
145     
146     public static synchronized File JavaDoc getDataFolder (final URL JavaDoc root) throws IOException JavaDoc {
147         loadSegments ();
148         final String JavaDoc rootName = root.toExternalForm();
149         String JavaDoc slice = invertedSegments.get (rootName);
150         if ( slice == null) {
151             slice = SLICE_PREFIX + (++index);
152             while (segments.getProperty(slice) != null) {
153                 slice = SLICE_PREFIX + (++index);
154             }
155             segments.put (slice,rootName);
156             invertedSegments.put(rootName, slice);
157             storeSegments ();
158         }
159         File JavaDoc result = FileUtil.normalizeFile (new File JavaDoc (cacheFolder, slice));
160         if (!result.exists()) {
161             result.mkdir();
162         }
163         return result;
164     }
165     
166     public static File JavaDoc getClassFolder (final URL JavaDoc url) throws IOException JavaDoc {
167         return getClassFolderImpl(url);
168     }
169     
170     public static File JavaDoc getClassFolder (final File JavaDoc root) throws IOException JavaDoc {
171         try {
172             return getClassFolderImpl(root.toURI().toURL());
173         } catch (MalformedURLException JavaDoc mue) {
174             ErrorManager.getDefault().notify (mue);
175             return null;
176         }
177     }
178     
179     private static File JavaDoc getClassFolderImpl (final URL JavaDoc url) throws IOException JavaDoc {
180         final File JavaDoc dataFolder = getDataFolder (url);
181         final File JavaDoc result= new File JavaDoc (dataFolder, CLASSES);
182         if (!result.exists()) {
183             result.mkdir();
184         }
185         return result;
186     }
187     
188     private static synchronized File JavaDoc getCacheFolder () {
189         if (cacheFolder == null) {
190             final String JavaDoc nbUserProp = System.getProperty(NB_USER_DIR);
191             assert nbUserProp != null;
192             final File JavaDoc nbUserDir = new File JavaDoc (nbUserProp);
193             cacheFolder = FileUtil.normalizeFile(new File JavaDoc (nbUserDir, INDEX_DIR));
194             if (!cacheFolder.exists()) {
195                 boolean created = cacheFolder.mkdirs();
196                 assert created : "Cannot create cache folder"; //NOI18N
197
}
198             else {
199                 assert cacheFolder.isDirectory() && cacheFolder.canRead() && cacheFolder.canWrite();
200             }
201         }
202         return cacheFolder;
203     }
204     
205     /**
206      * Only for unit tests!
207      *
208      */

209     static synchronized void setCacheFolder (final File JavaDoc folder) {
210         assert folder != null && folder.exists() && folder.canRead() && folder.canWrite();
211         cacheFolder = folder;
212     }
213     
214 }
215
Popular Tags