001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.search;
011:
012: import java.io.IOException;
013:
014: import org.eclipse.core.runtime.IPath;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.core.runtime.OperationCanceledException;
017: import org.eclipse.jdt.core.search.*;
018: import org.eclipse.jdt.internal.core.JavaModelManager;
019: import org.eclipse.jdt.internal.core.index.Index;
020: import org.eclipse.jdt.internal.core.search.indexing.IndexManager;
021: import org.eclipse.jdt.internal.core.search.indexing.ReadWriteMonitor;
022: import org.eclipse.jdt.internal.core.search.matching.MatchLocator;
023: import org.eclipse.jdt.internal.core.search.processing.IJob;
024: import org.eclipse.jdt.internal.core.search.processing.JobManager;
025: import org.eclipse.jdt.internal.core.util.Util;
026:
027: public class PatternSearchJob implements IJob {
028:
029: protected SearchPattern pattern;
030: protected IJavaSearchScope scope;
031: protected SearchParticipant participant;
032: protected IndexQueryRequestor requestor;
033: protected boolean areIndexesReady;
034: protected long executionTime = 0;
035:
036: public PatternSearchJob(SearchPattern pattern,
037: SearchParticipant participant, IJavaSearchScope scope,
038: IndexQueryRequestor requestor) {
039: this .pattern = pattern;
040: this .participant = participant;
041: this .scope = scope;
042: this .requestor = requestor;
043: }
044:
045: public boolean belongsTo(String jobFamily) {
046: return true;
047: }
048:
049: public void cancel() {
050: // search job is cancelled through progress
051: }
052:
053: public void ensureReadyToRun() {
054: if (!this .areIndexesReady)
055: getIndexes(null/*progress*/); // may trigger some index recreation
056: }
057:
058: public boolean execute(IProgressMonitor progressMonitor) {
059: if (progressMonitor != null && progressMonitor.isCanceled())
060: throw new OperationCanceledException();
061:
062: boolean isComplete = COMPLETE;
063: executionTime = 0;
064: Index[] indexes = getIndexes(progressMonitor);
065: try {
066: int max = indexes.length;
067: if (progressMonitor != null)
068: progressMonitor.beginTask("", max); //$NON-NLS-1$
069: for (int i = 0; i < max; i++) {
070: isComplete &= search(indexes[i], progressMonitor);
071: if (progressMonitor != null) {
072: if (progressMonitor.isCanceled())
073: throw new OperationCanceledException();
074: progressMonitor.worked(1);
075: }
076: }
077: if (JobManager.VERBOSE)
078: Util
079: .verbose("-> execution time: " + executionTime + "ms - " + this );//$NON-NLS-1$//$NON-NLS-2$
080: return isComplete;
081: } finally {
082: if (progressMonitor != null)
083: progressMonitor.done();
084: }
085: }
086:
087: public Index[] getIndexes(IProgressMonitor progressMonitor) {
088: // acquire the in-memory indexes on the fly
089: IPath[] indexLocations = this .participant.selectIndexes(
090: this .pattern, this .scope);
091: int length = indexLocations.length;
092: Index[] indexes = new Index[length];
093: int count = 0;
094: IndexManager indexManager = JavaModelManager.getIndexManager();
095: for (int i = 0; i < length; i++) {
096: if (progressMonitor != null && progressMonitor.isCanceled())
097: throw new OperationCanceledException();
098: // may trigger some index recreation work
099: IPath indexLocation = indexLocations[i];
100: Index index = indexManager.getIndex(indexLocation);
101: if (index == null) {
102: // only need containerPath if the index must be built
103: IPath containerPath = (IPath) indexManager.indexLocations
104: .keyForValue(indexLocation);
105: if (containerPath != null) // sanity check
106: index = indexManager.getIndex(containerPath,
107: indexLocation, true /*reuse index file*/,
108: false /*do not create if none*/);
109: }
110: if (index != null)
111: indexes[count++] = index; // only consider indexes which are ready
112: }
113: if (count == length)
114: this .areIndexesReady = true;
115: else
116: System.arraycopy(indexes, 0, indexes = new Index[count], 0,
117: count);
118: return indexes;
119: }
120:
121: public boolean search(Index index, IProgressMonitor progressMonitor) {
122: if (index == null)
123: return COMPLETE;
124: if (progressMonitor != null && progressMonitor.isCanceled())
125: throw new OperationCanceledException();
126:
127: ReadWriteMonitor monitor = index.monitor;
128: if (monitor == null)
129: return COMPLETE; // index got deleted since acquired
130: try {
131: monitor.enterRead(); // ask permission to read
132: long start = System.currentTimeMillis();
133: MatchLocator.findIndexMatches(this .pattern, index,
134: requestor, this .participant, this .scope,
135: progressMonitor);
136: executionTime += System.currentTimeMillis() - start;
137: return COMPLETE;
138: } catch (IOException e) {
139: if (e instanceof java.io.EOFException)
140: e.printStackTrace();
141: return FAILED;
142: } finally {
143: monitor.exitRead(); // finished reading
144: }
145: }
146:
147: public String toString() {
148: return "searching " + pattern.toString(); //$NON-NLS-1$
149: }
150: }
|