KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > PatternSearchJob


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search;
12
13 import java.io.IOException JavaDoc;
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 JavaDoc jobFamily) {
44     return true;
45 }
46 public void cancel() {
47     // search job is cancelled through progress
48
}
49 public void ensureReadyToRun() {
50     if (!this.areIndexesReady)
51         getIndexes(null/*progress*/); // may trigger some index recreation
52
}
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); //$NON-NLS-1$
63
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);//$NON-NLS-1$//$NON-NLS-2$
72
return isComplete;
73     } finally {
74         if (progressMonitor != null)
75             progressMonitor.done();
76     }
77 }
78 public Index[] getIndexes(IProgressMonitor progressMonitor) {
79     // acquire the in-memory indexes on the fly
80
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         // may trigger some index recreation work
88
IPath indexLocation = indexLocations[i];
89         Index index = indexManager.getIndex(indexLocation);
90         if (index == null) {
91             // only need containerPath if the index must be built
92
IPath containerPath = (IPath) indexManager.indexLocations.keyForValue(indexLocation);
93             if (containerPath != null) // sanity check
94
index = indexManager.getIndex(containerPath, indexLocation, true /*reuse index file*/, false /*do not create if none*/);
95         }
96         if (index != null)
97             indexes[count++] = index; // only consider indexes which are ready
98
}
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; // index got deleted since acquired
111
try {
112         monitor.enterRead(); // ask permission to read
113
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 JavaDoc e) {
118         if (e instanceof java.io.EOFException JavaDoc)
119             e.printStackTrace();
120         return FAILED;
121     } finally {
122         monitor.exitRead(); // finished reading
123
}
124 }
125 public String JavaDoc toString() {
126     return "searching " + pattern.toString(); //$NON-NLS-1$
127
}
128 }
129
Popular Tags