1 13 package org.eclipse.update.internal.search; 14 15 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Map ; 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 40 public class SiteSearchCategory extends BaseSearchCategory { 41 private IUpdateSearchQuery[] queries; 42 private boolean liteFeaturesAreOK; 43 private static final String CATEGORY_ID = 44 "org.eclipse.update.core.unified-search"; 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 [] categoriesToSkip, 70 IUpdateSearchFilter filter, 71 IUpdateSearchResultCollector collector, 72 IProgressMonitor monitor) { 73 74 ISiteFeatureReference[] refs = site.getFeatureReferences(); 75 HashSet ignores = new HashSet (); 76 Map liteFeatures = new HashMap (); 77 78 if (categoriesToSkip != null) { 79 for (int i = 0; i < categoriesToSkip.length; i++) { 80 ignores.add(categoriesToSkip[i]); 81 } 82 } 83 List siteFeatureReferences = new ArrayList (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); ThreadGroup featureDownloaders = new ThreadGroup ("FeatureDownloader"); int numberOfThreads = (refs.length > 5)? 5: refs.length; 105 106 Thread [] featureDownloader = new Thread [numberOfThreads]; 107 for( int i = 0; i < numberOfThreads; i++) { 108 featureDownloader[i] = new Thread (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 ie) { 125 } 126 } 127 } 128 129 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 { 154 155 private List siteFeatureReferences; 156 157 private IProgressMonitor monitor; 158 159 private IUpdateSearchFilter filter; 160 161 private IUpdateSearchResultCollector collector; 162 163 private HashSet ignores; 164 165 private boolean liteFeaturesAreOK; 166 167 private Map liteFeatures; 168 169 public FeatureDownloader(List siteFeatureReferences, IUpdateSearchResultCollector collector, IUpdateSearchFilter filter, HashSet 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 siteFeatureReferences, IUpdateSearchResultCollector collector, IUpdateSearchFilter filter, HashSet ignores, IProgressMonitor monitor, boolean liteFeaturesAreOK, Map 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 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 |