1 11 package org.eclipse.jdt.internal.core.search; 12 13 import java.io.IOException ; 14 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.OperationCanceledException; 18 import org.eclipse.jdt.core.search.*; 19 import org.eclipse.jdt.internal.core.JavaModelManager; 20 import org.eclipse.jdt.internal.core.index.Index; 21 import org.eclipse.jdt.internal.core.search.indexing.IndexManager; 22 import org.eclipse.jdt.internal.core.search.indexing.ReadWriteMonitor; 23 import org.eclipse.jdt.internal.core.search.matching.MatchLocator; 24 import org.eclipse.jdt.internal.core.search.processing.IJob; 25 import org.eclipse.jdt.internal.core.search.processing.JobManager; 26 import org.eclipse.jdt.internal.core.util.Util; 27 28 public class PatternSearchJob implements IJob { 29 30 protected SearchPattern pattern; 31 protected IJavaSearchScope scope; 32 protected SearchParticipant participant; 33 protected IndexQueryRequestor requestor; 34 protected boolean areIndexesReady; 35 protected long executionTime = 0; 36 37 public PatternSearchJob(SearchPattern pattern, SearchParticipant participant, IJavaSearchScope scope, IndexQueryRequestor requestor) { 38 this.pattern = pattern; 39 this.participant = participant; 40 this.scope = scope; 41 this.requestor = requestor; 42 } 43 public boolean belongsTo(String jobFamily) { 44 return true; 45 } 46 public void cancel() { 47 } 49 public void ensureReadyToRun() { 50 if (!this.areIndexesReady) 51 getIndexes(null); } 53 public boolean execute(IProgressMonitor progressMonitor) { 54 if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException(); 55 56 boolean isComplete = COMPLETE; 57 executionTime = 0; 58 Index[] indexes = getIndexes(progressMonitor); 59 try { 60 int max = indexes.length; 61 if (progressMonitor != null) 62 progressMonitor.beginTask("", max); for (int i = 0; i < max; i++) { 64 isComplete &= search(indexes[i], progressMonitor); 65 if (progressMonitor != null) { 66 if (progressMonitor.isCanceled()) throw new OperationCanceledException(); 67 progressMonitor.worked(1); 68 } 69 } 70 if (JobManager.VERBOSE) 71 Util.verbose("-> execution time: " + executionTime + "ms - " + this); return isComplete; 73 } finally { 74 if (progressMonitor != null) 75 progressMonitor.done(); 76 } 77 } 78 public Index[] getIndexes(IProgressMonitor progressMonitor) { 79 IPath[] indexLocations = this.participant.selectIndexes(this.pattern, this.scope); 81 int length = indexLocations.length; 82 Index[] indexes = new Index[length]; 83 int count = 0; 84 IndexManager indexManager = JavaModelManager.getJavaModelManager().getIndexManager(); 85 for (int i = 0; i < length; i++) { 86 if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException(); 87 IPath indexLocation = indexLocations[i]; 89 Index index = indexManager.getIndex(indexLocation); 90 if (index == null) { 91 IPath containerPath = (IPath) indexManager.indexLocations.keyForValue(indexLocation); 93 if (containerPath != null) index = indexManager.getIndex(containerPath, indexLocation, true , false ); 95 } 96 if (index != null) 97 indexes[count++] = index; } 99 if (count == length) 100 this.areIndexesReady = true; 101 else 102 System.arraycopy(indexes, 0, indexes=new Index[count], 0, count); 103 return indexes; 104 } 105 public boolean search(Index index, IProgressMonitor progressMonitor) { 106 if (index == null) return COMPLETE; 107 if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException(); 108 109 ReadWriteMonitor monitor = index.monitor; 110 if (monitor == null) return COMPLETE; try { 112 monitor.enterRead(); long start = System.currentTimeMillis(); 114 MatchLocator.findIndexMatches(this.pattern, index, requestor, this.participant, this.scope, progressMonitor); 115 executionTime += System.currentTimeMillis() - start; 116 return COMPLETE; 117 } catch (IOException e) { 118 if (e instanceof java.io.EOFException ) 119 e.printStackTrace(); 120 return FAILED; 121 } finally { 122 monitor.exitRead(); } 124 } 125 public String toString() { 126 return "searching " + pattern.toString(); } 128 } 129 | Popular Tags |