KickJava   Java API By Example, From Geeks To Geeks.

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


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.retouche.source.usages;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import org.netbeans.api.gsf.Index.SearchResult;
32 import org.netbeans.api.gsf.CancellableTask;
33 import org.netbeans.api.retouche.source.CompilationController;
34 import org.netbeans.api.retouche.source.CompilationInfo;
35 import org.netbeans.api.java.queries.SourceForBinaryQuery;
36 import org.netbeans.api.retouche.source.ClassIndex;
37 import org.netbeans.api.retouche.source.Phase;
38 import org.netbeans.api.retouche.source.Source;
39 import org.netbeans.modules.retouche.source.SourceAccessor;
40 import static org.netbeans.modules.retouche.source.usages.ClassIndexImpl.UsageType.*;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.URLMapper;
43 import org.openide.util.Exceptions;
44 import org.openide.util.Exceptions;
45
46 /**
47  * This file is originally from Retouche, the Java Support
48  * infrastructure in NetBeans. I have modified the file as little
49  * as possible to make merging Retouche fixes back as simple as
50  * possible.
51  *
52  *
53  * @author Petr Hrebejk, Tomas Zezula
54  */

55 public class PersistentClassIndex extends ClassIndexImpl {
56     
57     private final Index index;
58     private final URL JavaDoc root;
59     private final boolean isSource;
60     private WeakReference JavaDoc<Source> dirty;
61     private static final Logger JavaDoc LOGGER = Logger.getLogger(PersistentClassIndex.class.getName());
62     
63     /** Creates a new instance of ClassesAndMembersUQ */
64     private PersistentClassIndex(final URL JavaDoc root, final File JavaDoc cacheRoot, final boolean source)
65         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
66         assert root != null;
67         this.root = root;
68         this.index = LuceneIndex.create (cacheRoot, this);
69         this.isSource = source;
70     // BEGIN TOR MODIFICATIONS
71
this.cacheRoot = cacheRoot;
72     // END TOR MODIFICATIONS
73
}
74     
75     public BinaryAnalyser getBinaryAnalyser () {
76         return new BinaryAnalyser (this.index);
77     }
78     
79     public SourceAnalyser getSourceAnalyser () {
80         return new SourceAnalyser (this.index);
81     }
82     
83     public FileObject[] getSourceRoots () {
84         FileObject[] rootFos;
85         if (isSource) {
86             FileObject rootFo = URLMapper.findFileObject (this.root);
87             rootFos = rootFo == null ? new FileObject[0] : new FileObject[] {rootFo};
88         }
89         else {
90             rootFos = SourceForBinaryQuery.findSourceRoots(this.root).getRoots();
91         }
92         return rootFos;
93     }
94     
95
96     // Factory method
97

98     public static ClassIndexImpl create(URL JavaDoc root, final File JavaDoc cacheRoot, final boolean indexNow)
99         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
100         return new PersistentClassIndex(root, cacheRoot, indexNow);
101     }
102     
103     public synchronized void setDirty (final Source js) {
104         if (js == null) {
105             this.dirty = null;
106         }
107         else if (this.dirty == null || this.dirty.get() != js) {
108             this.dirty = new WeakReference JavaDoc (js);
109         }
110     }
111     
112     public @Override JavaDoc String JavaDoc toString () {
113         return "CompromiseUQ["+this.root.toExternalForm()+"]"; // NOI18N
114
}
115     
116     //Protected methods --------------------------------------------------------
117
protected final void close () throws IOException JavaDoc {
118         this.index.close();
119     }
120     
121     
122     // Private methods ---------------------------------------------------------
123

124     private void updateDirty () {
125         WeakReference JavaDoc<Source> jsRef;
126         synchronized (this) {
127             jsRef = this.dirty;
128         }
129         if (jsRef != null) {
130             final Source js = jsRef.get();
131             if (js != null) {
132                 final long startTime = System.currentTimeMillis();
133                 if (SourceAccessor.INSTANCE.isDispatchThread()) {
134                     //Already under javac's lock
135
try {
136                         ClassIndexManager.getDefault().writeLock(
137                             new ClassIndexManager.ExceptionAction<Void JavaDoc>() {
138                                 public Void JavaDoc run () throws IOException JavaDoc {
139                                     CompilationInfo compilationInfo = SourceAccessor.INSTANCE.getCurrentCompilationInfo (js, Phase.RESOLVED);
140                                     if (compilationInfo != null) {
141                                         //Not cancelled
142
final SourceAnalyser sa = getSourceAnalyser();
143                                         long st = System.currentTimeMillis();
144                                         sa.analyseUnitAndStore(compilationInfo.getParserResult(), SourceAccessor.INSTANCE.getParserTask(compilationInfo));
145                                         long et = System.currentTimeMillis();
146                                     }
147                                     return null;
148                                 }
149                         });
150                     } catch (IOException JavaDoc ioe) {
151                         Exceptions.printStackTrace(ioe);
152                     }
153                 }
154                 else {
155                     try {
156                         js.runUserActionTask(new CancellableTask<CompilationController>() {
157                             public void run (final CompilationController controller) {
158                                 try {
159                                     ClassIndexManager.getDefault().writeLock(
160                                         new ClassIndexManager.ExceptionAction<Void JavaDoc>() {
161                                             public Void JavaDoc run () throws IOException JavaDoc {
162                                                 controller.toPhase(Phase.RESOLVED);
163                                                 final SourceAnalyser sa = getSourceAnalyser();
164                                                 long st = System.currentTimeMillis();
165                                                 sa.analyseUnitAndStore(controller.getParserResult(), SourceAccessor.INSTANCE.getParserTask(controller));
166                                                 long et = System.currentTimeMillis();
167                                                 return null;
168                                             }
169                                     });
170                                 } catch (IOException JavaDoc ioe) {
171                                     Exceptions.printStackTrace(ioe);
172                                 }
173                             }
174
175                             public void cancel () {}
176                         }, true);
177                     } catch (IOException JavaDoc ioe) {
178                         Exceptions.printStackTrace(ioe);
179                     }
180                 }
181                 synchronized (this) {
182                     this.dirty = null;
183                 }
184                 final long endTime = System.currentTimeMillis();
185                 LOGGER.fine("PersistentClassIndex.updateDirty took: " + (endTime-startTime)+ " ms"); //NOI18N
186
}
187         }
188     }
189         
190     // BEGIN TOR MODIFICATIONS
191
public void gsfSearch(final String JavaDoc primaryField, final String JavaDoc name, final ClassIndex.NameKind kind,
192             final Set JavaDoc<ClassIndex.SearchScope> scope, final Set JavaDoc<SearchResult> result) throws IOException JavaDoc {
193         updateDirty();
194         try {
195             ClassIndexManager.getDefault().readLock(new ClassIndexManager.ExceptionAction<Void JavaDoc> () {
196                 public Void JavaDoc run () throws IOException JavaDoc {
197                     index.gsfSearch(primaryField, name, kind, scope, result);
198                     return null;
199                 }
200             });
201         } catch (IOException JavaDoc ioe) {
202             Exceptions.printStackTrace(ioe);
203         }
204     }
205     
206     /** For development purposes only */
207     public File JavaDoc getSegment() {
208         return cacheRoot;
209     }
210     
211     private File JavaDoc cacheRoot;
212     
213     public URL JavaDoc getRoot() {
214         return root;
215     }
216 // END TOR MODIFICATIONS
217

218 }
219
Popular Tags