KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > search > SiteSearchCategory


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  * James D Miles (IBM Corp.) - bug 181375, ArrayIndexOutOfBoundsException in SiteSearchCategory$Query
11  * James D Miles (IBM Corp.) - bug 191783, NullPointerException in FeatureDownloader
12  *******************************************************************************/

13 package org.eclipse.update.internal.search;
14
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.update.core.ICategory;
26 import org.eclipse.update.core.IFeature;
27 import org.eclipse.update.core.ISite;
28 import org.eclipse.update.core.ISiteFeatureReference;
29 import org.eclipse.update.internal.core.ExtendedSite;
30 import org.eclipse.update.internal.core.LiteFeature;
31 import org.eclipse.update.internal.core.UpdateCore;
32 import org.eclipse.update.search.IQueryUpdateSiteAdapter;
33 import org.eclipse.update.search.IUpdateSearchFilter;
34 import org.eclipse.update.search.IUpdateSearchQuery;
35 import org.eclipse.update.search.IUpdateSearchResultCollector;
36
37 /**
38  * Searches an update site
39  */

40 public class SiteSearchCategory extends BaseSearchCategory {
41     private IUpdateSearchQuery[] queries;
42     private boolean liteFeaturesAreOK;
43     private static final String JavaDoc CATEGORY_ID =
44         "org.eclipse.update.core.unified-search"; //$NON-NLS-1$
45

46     private static class Query implements IUpdateSearchQuery {
47         
48         private boolean liteFeaturesAreOK;
49         
50         
51         public Query() {
52             liteFeaturesAreOK = false;
53         }
54         
55         public Query(boolean liteFeaturesAreOK) {
56             this.liteFeaturesAreOK = liteFeaturesAreOK;
57         }
58         
59         public boolean isLiteFeaturesAreOK() {
60             return liteFeaturesAreOK;
61         }
62
63         public void setLiteFeaturesAreOK(boolean liteFeaturesAreOK) {
64             this.liteFeaturesAreOK = liteFeaturesAreOK;
65         }
66         
67         public void run(
68             ISite site,
69             String JavaDoc[] categoriesToSkip,
70             IUpdateSearchFilter filter,
71             IUpdateSearchResultCollector collector,
72             IProgressMonitor monitor) {
73             
74             ISiteFeatureReference[] refs = site.getFeatureReferences();
75             HashSet JavaDoc ignores = new HashSet JavaDoc();
76             Map JavaDoc liteFeatures = new HashMap JavaDoc();
77             
78             if (categoriesToSkip != null) {
79                 for (int i = 0; i < categoriesToSkip.length; i++) {
80                     ignores.add(categoriesToSkip[i]);
81                 }
82             }
83             List JavaDoc siteFeatureReferences = new ArrayList JavaDoc(Arrays.asList(refs));
84             
85             if (liteFeaturesAreOK && (site instanceof ExtendedSite) ) {
86                 
87                 ExtendedSite extendedSite = (ExtendedSite)site;
88                 LiteFeature[] liteFeaturesArray = extendedSite.getLiteFeatures();
89                 if ( (liteFeaturesArray != null) && ( liteFeaturesArray.length != 0)) {
90                     for(int i = 0; i < liteFeaturesArray.length; i++) {
91                         liteFeatures.put(liteFeaturesArray[i].getVersionedIdentifier(), liteFeaturesArray[i]);
92                     }
93                     (new FeatureDownloader(siteFeatureReferences, collector, filter, ignores, monitor, true, liteFeatures)).run();
94                     return;
95                 } else {
96                     liteFeaturesAreOK = false;
97                 }
98             }
99             
100
101             
102             monitor.beginTask("", refs.length); //$NON-NLS-1$
103
ThreadGroup JavaDoc featureDownloaders = new ThreadGroup JavaDoc("FeatureDownloader"); //$NON-NLS-1$
104
int numberOfThreads = (refs.length > 5)? 5: refs.length;
105
106             Thread JavaDoc[] featureDownloader = new Thread JavaDoc[numberOfThreads];
107             for( int i = 0; i < numberOfThreads; i++) {
108                 featureDownloader[i] = new Thread JavaDoc(featureDownloaders, new FeatureDownloader(siteFeatureReferences, collector, filter, ignores, monitor));
109                 featureDownloader[i].start();
110             }
111             
112             int i =0;
113             while(i < numberOfThreads){
114                 if (monitor.isCanceled()) {
115                     synchronized(siteFeatureReferences) {
116                         siteFeatureReferences.clear();
117                     }
118                 }
119                 try {
120                     featureDownloader[i].join(250);
121                     if(!featureDownloader[i].isAlive()){
122                         i++;
123                     }
124                 } catch (InterruptedException JavaDoc ie) {
125                 }
126             }
127         }
128
129         /* (non-Javadoc)
130          * @see org.eclipse.update.internal.ui.search.ISearchQuery#getSearchSite()
131          */

132         public IQueryUpdateSiteAdapter getQuerySearchSite() {
133             return null;
134         }
135     }
136
137     public SiteSearchCategory() {
138         super(CATEGORY_ID);
139         queries = new IUpdateSearchQuery[] { new Query()};
140     }
141
142     public SiteSearchCategory(boolean liteFeaturesAreOK) {
143         this();
144         this.liteFeaturesAreOK = liteFeaturesAreOK;
145         queries = new IUpdateSearchQuery[] { new Query(liteFeaturesAreOK)};
146     }
147
148     public IUpdateSearchQuery[] getQueries() {
149         return queries;
150     }
151     
152     
153     private static class FeatureDownloader implements Runnable JavaDoc {
154         
155         private List JavaDoc siteFeatureReferences;
156         
157         private IProgressMonitor monitor;
158         
159         private IUpdateSearchFilter filter;
160         
161         private IUpdateSearchResultCollector collector;
162         
163         private HashSet JavaDoc ignores;
164
165         private boolean liteFeaturesAreOK;
166
167         private Map JavaDoc liteFeatures;
168
169         public FeatureDownloader(List JavaDoc siteFeatureReferences, IUpdateSearchResultCollector collector, IUpdateSearchFilter filter, HashSet JavaDoc ignores, IProgressMonitor monitor) {
170             super();
171
172             this.collector = collector;
173             this.filter = filter;
174             this.ignores = ignores;
175             this.monitor = monitor;
176             this.siteFeatureReferences = siteFeatureReferences;
177         }
178         
179         public FeatureDownloader(List JavaDoc siteFeatureReferences, IUpdateSearchResultCollector collector, IUpdateSearchFilter filter, HashSet JavaDoc ignores, IProgressMonitor monitor, boolean liteFeaturesAreOK, Map JavaDoc liteFeatures) {
180             this(siteFeatureReferences, collector, filter, ignores, monitor);
181             this.liteFeaturesAreOK = liteFeaturesAreOK && (liteFeatures != null);
182             this.liteFeatures = liteFeatures;
183         }
184
185         public void run() {
186             
187             ISiteFeatureReference siteFeatureReference = null;
188             
189             while (siteFeatureReferences.size() != 0) {
190                 
191                 synchronized(siteFeatureReferences) {
192                     try{
193                         siteFeatureReference = (ISiteFeatureReference)siteFeatureReferences.remove(0);
194                     }catch(IndexOutOfBoundsException JavaDoc e){
195                         siteFeatureReference = null;
196                         break;
197                     }
198                 }
199                 if (siteFeatureReference != null) {
200                     boolean skipFeature = false;
201                     if (monitor.isCanceled())
202                         break;
203                     if (ignores.size() > 0) {
204                         ICategory[] categories = siteFeatureReference.getCategories();
205                         
206                         for (int j = 0; j < categories.length; j++) {
207                             ICategory category = categories[j];
208                             if (ignores.contains(category.getName())) {
209                                 skipFeature = true;
210                                 break;
211                             }
212                         }
213                     }
214                     try {
215                         if (!skipFeature) {
216                             if (filter.accept(siteFeatureReference)) {
217                                 IFeature feature = null;
218                                 if(liteFeaturesAreOK) {
219                                     feature = (IFeature)liteFeatures.get(siteFeatureReference.getVersionedIdentifier());
220                                 }
221                                 if (feature == null){
222                                     feature = siteFeatureReference.getFeature(null);
223                                 }
224                                 synchronized(siteFeatureReferences) {
225                                     if ( (feature != null) && (filter.accept(siteFeatureReference)) ) {
226                                         collector.accept(feature);
227                                         monitor.subTask(feature.getLabel());
228                                     }
229                                 }
230                             }
231                         }
232                     } catch (CoreException e) {
233                         UpdateCore.log(e);
234                     } finally {
235                         monitor.worked(1);
236                     }
237                 }
238             }
239             
240         }
241     }
242
243     public boolean isLiteFeaturesAreOK() {
244         return liteFeaturesAreOK;
245     }
246
247     public void setLiteFeaturesAreOK(boolean liteFeaturesAreOK) {
248         this.liteFeaturesAreOK = liteFeaturesAreOK;
249         for( int i = 0; i < queries.length; i++) {
250             ((Query)queries[i]).setLiteFeaturesAreOK(liteFeaturesAreOK);
251         }
252     }
253 }
254
Popular Tags