KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > SearchManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.help.internal.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.help.internal.base.HelpBasePlugin;
25 import org.eclipse.help.internal.base.remote.RemoteHelp;
26 import org.eclipse.help.internal.base.remote.RemoteSearchManager;
27 import org.eclipse.help.internal.search.federated.FederatedSearchEntry;
28 import org.eclipse.help.internal.search.federated.FederatedSearchJob;
29
30 /*
31  * Manages both local and remote searching, as well as merging of results.
32  */

33 public class SearchManager {
34
35     private LocalSearchManager localManager = new LocalSearchManager();
36     private RemoteSearchManager remoteManager = new RemoteSearchManager();
37     
38     private IProgressMonitor localMonitor;
39     private IProgressMonitor remoteMonitor;
40     
41     private ISearchQuery searchQuery;
42     private ISearchHitCollector collector;
43     private BufferedSearchHitCollector bufferedCollector = new BufferedSearchHitCollector();
44     
45     private Job localSearchJob;
46     private Job remoteSearchJob;
47
48     /*
49      * Constructs a new SearchManager.
50      */

51     public SearchManager() {
52         /*
53          * We use these jobs to perform the local and remote searches in parallel
54          * in the background.
55          */

56         localSearchJob = new Job("localSearchJob") { //$NON-NLS-1$
57
protected IStatus run(IProgressMonitor monitor) {
58                 localManager.search(searchQuery, bufferedCollector, localMonitor);
59                 return Status.OK_STATUS;
60             }
61         };
62         remoteSearchJob = new Job("remoteSearchJob") { //$NON-NLS-1$
63
protected IStatus run(IProgressMonitor monitor) {
64                 remoteManager.search(searchQuery, bufferedCollector, remoteMonitor);
65                 return Status.OK_STATUS;
66             }
67         };
68         localSearchJob.setSystem(true);
69         remoteSearchJob.setSystem(true);
70     }
71     
72     /*
73      * Perform the given search both locally and remotely if configured.
74      */

75     public void search(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm)
76             throws QueryTooComplexException {
77         if (RemoteHelp.isEnabled()) {
78             searchLocalAndRemote(searchQuery, collector, pm);
79         }
80         else {
81             searchLocal(searchQuery, collector, pm);
82         }
83     }
84
85     /*
86      * Perform the given search locally only.
87      */

88     public void searchLocal(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm)
89             throws QueryTooComplexException {
90         localManager.search(searchQuery, collector, pm);
91     }
92
93     /*
94      * Perform the given search both locally and remotely.
95      */

96     public void searchLocalAndRemote(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm)
97             throws QueryTooComplexException {
98         this.searchQuery = searchQuery;
99         this.collector = collector;
100         
101         pm.beginTask("", 100); //$NON-NLS-1$
102

103         // allocate half of the progress bar for each
104
localMonitor = new SubProgressMonitor(pm, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
105         remoteMonitor = new SubProgressMonitor(pm, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
106         
107         // start both searches in parallel
108
localSearchJob.schedule();
109         remoteSearchJob.schedule();
110         
111         // wait until finished
112
try {
113             localSearchJob.join();
114             remoteSearchJob.join();
115         }
116         catch (InterruptedException JavaDoc e) {
117             String JavaDoc msg = "Unexpected InterruptedException while waiting for help search jobs to finish"; //$NON-NLS-1$
118
HelpBasePlugin.logError(msg, e);
119         }
120
121         // results are in; send them off to the collector
122
bufferedCollector.flush();
123         pm.done();
124     }
125     
126     /**
127      * Performs the federated search.
128      */

129     public void search(String JavaDoc expression, FederatedSearchEntry[] entries) {
130         for (int i = 0; i < entries.length; i++) {
131             FederatedSearchJob job = new FederatedSearchJob(expression, entries[i]);
132             job.schedule();
133         }
134     }
135     
136     /*
137      * Returns the manager responsible for handling local searching.
138      */

139     public LocalSearchManager getLocalSearchManager() {
140         return localManager;
141     }
142
143     /*
144      * Returns the manager responsible for handling remote searching.
145      */

146     public RemoteSearchManager getRemoteSearchManager() {
147         return remoteManager;
148     }
149
150     /*
151      * Performs any necessary cleanup (workbench is shutting down).
152      */

153     public void close() {
154         localManager.close();
155     }
156     
157     /*
158      * Buffers hits, and only sends them off to the wrapped collector
159      * when flush() is called.
160      */

161     private class BufferedSearchHitCollector implements ISearchHitCollector {
162         private Set JavaDoc allHits = new HashSet JavaDoc();
163         private String JavaDoc wordsSearched = null;
164         
165         /* (non-Javadoc)
166          * @see org.eclipse.help.internal.search.ISearchHitCollector#addHits(java.util.List, java.lang.String)
167          */

168         public void addHits(List JavaDoc hits, String JavaDoc wordsSearched) {
169             if (wordsSearched != null) {
170                 this.wordsSearched = wordsSearched;
171             }
172             allHits.addAll(hits);
173         }
174         
175         /*
176          * Send all the buffered hits to the underlying collector,
177          * and reset the buffers.
178          */

179         public void flush() {
180             // sort by score
181
List JavaDoc hitsList = new ArrayList JavaDoc(allHits);
182             Collections.sort(hitsList);
183             collector.addHits(hitsList, wordsSearched);
184             allHits.clear();
185             wordsSearched = null;
186         }
187     }
188 }
189
Free Books   Free Magazines  
Popular Tags