1 19 20 package org.netbeans.modules.retouche.source.usages; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.ref.WeakReference ; 25 import java.net.URL ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.logging.Logger ; 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 55 public class PersistentClassIndex extends ClassIndexImpl { 56 57 private final Index index; 58 private final URL root; 59 private final boolean isSource; 60 private WeakReference <Source> dirty; 61 private static final Logger LOGGER = Logger.getLogger(PersistentClassIndex.class.getName()); 62 63 64 private PersistentClassIndex(final URL root, final File cacheRoot, final boolean source) 65 throws IOException , IllegalArgumentException { 66 assert root != null; 67 this.root = root; 68 this.index = LuceneIndex.create (cacheRoot, this); 69 this.isSource = source; 70 this.cacheRoot = cacheRoot; 72 } 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 98 public static ClassIndexImpl create(URL root, final File cacheRoot, final boolean indexNow) 99 throws IOException , IllegalArgumentException { 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 (js); 109 } 110 } 111 112 public @Override String toString () { 113 return "CompromiseUQ["+this.root.toExternalForm()+"]"; } 115 116 protected final void close () throws IOException { 118 this.index.close(); 119 } 120 121 122 124 private void updateDirty () { 125 WeakReference <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 try { 136 ClassIndexManager.getDefault().writeLock( 137 new ClassIndexManager.ExceptionAction<Void >() { 138 public Void run () throws IOException { 139 CompilationInfo compilationInfo = SourceAccessor.INSTANCE.getCurrentCompilationInfo (js, Phase.RESOLVED); 140 if (compilationInfo != null) { 141 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 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 >() { 161 public Void run () throws IOException { 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 ioe) { 171 Exceptions.printStackTrace(ioe); 172 } 173 } 174 175 public void cancel () {} 176 }, true); 177 } catch (IOException 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"); } 187 } 188 } 189 190 public void gsfSearch(final String primaryField, final String name, final ClassIndex.NameKind kind, 192 final Set <ClassIndex.SearchScope> scope, final Set <SearchResult> result) throws IOException { 193 updateDirty(); 194 try { 195 ClassIndexManager.getDefault().readLock(new ClassIndexManager.ExceptionAction<Void > () { 196 public Void run () throws IOException { 197 index.gsfSearch(primaryField, name, kind, scope, result); 198 return null; 199 } 200 }); 201 } catch (IOException ioe) { 202 Exceptions.printStackTrace(ioe); 203 } 204 } 205 206 207 public File getSegment() { 208 return cacheRoot; 209 } 210 211 private File cacheRoot; 212 213 public URL getRoot() { 214 return root; 215 } 216 218 } 219 | Popular Tags |