1 11 package org.eclipse.pde.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.pde.core.IModel; 20 import org.eclipse.pde.core.IModelProviderEvent; 21 import org.eclipse.pde.core.IModelProviderListener; 22 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 23 import org.eclipse.pde.internal.core.util.VersionUtil; 24 import org.osgi.framework.Version; 25 26 32 33 public class FeatureModelManager { 34 35 39 private FeatureTable fActiveModels; 40 41 44 private FeatureTable fInactiveModels; 45 46 private ExternalFeatureModelManager fExternalManager; 47 48 private boolean fReloadExternalNeeded = false; 49 50 private WorkspaceFeatureModelManager fWorkspaceManager; 51 52 private IModelProviderListener fProviderListener; 53 54 57 private ArrayList fListeners; 58 59 public FeatureModelManager() { 60 fWorkspaceManager = new WorkspaceFeatureModelManager(); 61 fListeners = new ArrayList (); 62 } 63 64 public synchronized void shutdown() { 65 if (fWorkspaceManager != null) 66 fWorkspaceManager.removeModelProviderListener(fProviderListener); 67 if (fExternalManager != null){ 68 fExternalManager.removeModelProviderListener(fProviderListener); 69 fExternalManager.shutdown(); 70 } 71 } 72 73 private synchronized void init() { 74 if (fActiveModels != null) { 75 if (fReloadExternalNeeded) { 76 fReloadExternalNeeded = false; 77 fExternalManager.reload(); 78 } 79 return; 80 } 81 82 fActiveModels = new FeatureTable(); 83 fInactiveModels = new FeatureTable(); 84 85 fProviderListener = new IModelProviderListener() { 86 public void modelsChanged(IModelProviderEvent e) { 87 handleModelsChanged(e); 88 } 89 }; 90 fWorkspaceManager.addModelProviderListener(fProviderListener); 91 92 IFeatureModel[] models = fWorkspaceManager.getFeatureModels(); 93 for (int i = 0; i < models.length; i++) { 94 fActiveModels.add(models[i]); 97 } 98 99 fExternalManager = new ExternalFeatureModelManager(); 100 fExternalManager.addModelProviderListener(fProviderListener); 101 fReloadExternalNeeded = false; 102 fExternalManager.startup(); 103 } 104 105 108 public IFeatureModel[] getModels() { 109 init(); 110 IFeatureModel[] allModels = fActiveModels.getAll(); 111 ArrayList valid = new ArrayList (allModels.length); 112 for( int i =0; i< allModels.length; i++){ 113 if(allModels[i].isValid()){ 114 valid.add(allModels[i]); 115 } 116 } 117 return (IFeatureModel[])valid.toArray(new IFeatureModel[valid.size()]); 118 } 119 120 public IFeatureModel[] getWorkspaceModels() { 121 init(); 122 return fWorkspaceManager.getFeatureModels(); 123 } 124 125 public IFeatureModel getFeatureModel(IProject project) { 126 init(); 127 return fWorkspaceManager.getFeatureModel(project); 128 } 129 130 137 public IFeatureModel findFeatureModel(String id, String version) { 138 init(); 139 IFeatureModel[] models = fActiveModels.get(id, version); 140 for (int i = 0; i < models.length; i++) { 141 if (models[i].isValid()) { 142 return models[i]; 143 } 144 } 145 if (models.length == 0 && "0.0.0".equals(version)) { return findFeatureModel(id); 147 } 148 return null; 149 } 150 151 160 public IFeatureModel findFeatureModelRelaxed(String id, String version) { 161 IFeatureModel model = findFeatureModel(id, version); 162 if (model != null) { 163 return model; 164 } 165 Version pvi = Version.parseVersion(version); 166 return findFeatureModel(id, pvi.getMajor() + "." + pvi.getMinor() + "." + pvi.getMicro() + ".qualifier"); } 170 171 178 public IFeatureModel[] findFeatureModels(String id) { 179 init(); 180 IFeatureModel[] models = fActiveModels.get(id); 181 ArrayList valid = new ArrayList (models.length); 182 for( int i =0; i< models.length; i++){ 183 if(models[i].isValid()){ 184 valid.add(models[i]); 185 } 186 } 187 return (IFeatureModel[])valid.toArray(new IFeatureModel[valid.size()]); 188 } 189 190 public IFeatureModel findFeatureModel(String id) { 191 IFeatureModel[] models = findFeatureModels(id); 192 IFeatureModel model = null; 193 for (int i = 0; i < models.length; i++) { 194 if (model == null) { 195 model = models[i]; 196 } else { 197 String version = model.getFeature().getVersion(); 198 String version2 = models[i].getFeature().getVersion(); 199 Version vid = Version.parseVersion(version); 200 Version vid2 = Version.parseVersion(version2); 201 if (VersionUtil.isGreaterOrEqualTo(vid2, vid)) { 202 model = models[i]; 203 } 204 } 205 } 206 return model; 207 } 208 209 private void handleModelsChanged(IModelProviderEvent e) { 210 init(); 211 IFeatureModelDelta delta = processEvent(e); 212 213 Object [] entries = fListeners.toArray(); 214 for (int i = 0; i < entries.length; i++) { 215 ((IFeatureModelListener) entries[i]).modelsChanged(delta); 216 } 217 } 218 219 223 private synchronized IFeatureModelDelta processEvent(IModelProviderEvent e) { 224 FeatureModelDelta delta = new FeatureModelDelta(); 225 229 Set affectedIdVers = null; 230 if ((e.getEventTypes() & IModelProviderEvent.MODELS_REMOVED) != 0) { 231 IModel[] removed = e.getRemovedModels(); 232 for (int i = 0; i < removed.length; i++) { 233 if (!(removed[i] instanceof IFeatureModel)) 234 continue; 235 IFeatureModel model = (IFeatureModel) removed[i]; 236 FeatureTable.Idver idver = fActiveModels.remove(model); 237 if (idver != null) { 238 if (affectedIdVers == null) 240 affectedIdVers = new HashSet (); 241 affectedIdVers.add(idver); 242 delta.add(model, IFeatureModelDelta.REMOVED); 243 } else { 244 fInactiveModels.remove(model); 245 } 246 } 247 } 248 if ((e.getEventTypes() & IModelProviderEvent.MODELS_ADDED) != 0) { 249 IModel[] added = e.getAddedModels(); 250 for (int i = 0; i < added.length; i++) { 251 if (!(added[i] instanceof IFeatureModel)) 252 continue; 253 IFeatureModel model = (IFeatureModel) added[i]; 254 if (model.getUnderlyingResource() != null) { 255 FeatureTable.Idver idver = fActiveModels.add(model); 256 delta.add(model, IFeatureModelDelta.ADDED); 257 if (affectedIdVers == null) 259 affectedIdVers = new HashSet (); 260 affectedIdVers.add(idver); 261 } else { 262 if (!model.isValid()) { 263 continue; 265 } 266 String id = model.getFeature().getId(); 267 String version = model.getFeature().getVersion(); 268 if (fInactiveModels.get(id, version).length > 0) { 269 continue; 271 } 272 IFeatureModel[] activeModels = fActiveModels.get(id, 273 version); 274 for (int j = 0; j < activeModels.length; j++) { 275 if (activeModels[j].getUnderlyingResource() == null) { 276 continue; 278 } 279 } 280 FeatureTable.Idver idver = fInactiveModels.add(model); 281 if (affectedIdVers == null) 283 affectedIdVers = new HashSet (); 284 affectedIdVers.add(idver); 285 } 286 } 287 } 288 289 290 if ((e.getEventTypes() & IModelProviderEvent.MODELS_CHANGED) != 0) { 291 IModel[] changed = e.getChangedModels(); 292 for (int i = 0; i < changed.length; i++) { 293 if (!(changed[i] instanceof IFeatureModel)) 294 continue; 295 IFeatureModel model = (IFeatureModel) changed[i]; 296 297 String id = model.getFeature().getId(); 298 String version = model.getFeature().getVersion(); 299 300 FeatureTable.Idver oldIdver = fActiveModels.get(model); 301 if (oldIdver != null && !oldIdver.equals(id, version)) { 302 FeatureTable.Idver idver = fActiveModels.add(model); 304 if (affectedIdVers == null) 305 affectedIdVers = new HashSet (); 306 affectedIdVers.add(oldIdver); 307 affectedIdVers.add(idver); 308 } 309 313 314 } 315 } 316 317 adjustExternalVisibility(delta, affectedIdVers); 318 322 if ((e.getEventTypes() & IModelProviderEvent.MODELS_CHANGED) != 0) { 323 IModel[] changed = e.getChangedModels(); 324 for (int i = 0; i < changed.length; i++) { 325 if (!(changed[i] instanceof IFeatureModel)) 326 continue; 327 IFeatureModel model = (IFeatureModel) changed[i]; 328 if (!delta.contains(model, IFeatureModelDelta.ADDED 329 | IFeatureModelDelta.REMOVED)) { 330 delta.add(model, IFeatureModelDelta.CHANGED); 331 } 332 333 } 334 } 335 return delta; 336 } 337 338 342 private void adjustExternalVisibility(FeatureModelDelta delta, 343 Set affectedIdVers) { 344 if (affectedIdVers != null) { 345 for (Iterator it = affectedIdVers.iterator(); it.hasNext();) { 346 FeatureTable.Idver idver = (FeatureTable.Idver) it.next(); 347 IFeatureModel[] affectedModels = fActiveModels.get(idver); 348 if (affectedModels.length > 1) { 349 353 for (int j = 0; j < affectedModels.length; j++) { 354 if (affectedModels[j].getUnderlyingResource() == null) { 355 fActiveModels.remove(affectedModels[j]); 357 fInactiveModels.add(affectedModels[j]); 358 delta.add(affectedModels[j], 359 IFeatureModelDelta.REMOVED); 360 } 361 } 362 } 363 364 if (affectedModels.length <= 0) { 365 IFeatureModel[] models = fInactiveModels.get(idver); 367 if (models.length > 0) { 368 fInactiveModels.remove(models[0]); 370 fActiveModels.add(models[0]); 371 delta.add(models[0], IFeatureModelDelta.ADDED); 372 } 373 } 374 } 375 } 376 } 377 378 public void addFeatureModelListener(IFeatureModelListener listener) { 379 if (!fListeners.contains(listener)) 380 fListeners.add(listener); 381 } 382 383 public void removeFeatureModelListener(IFeatureModelListener listener) { 384 if (fListeners.contains(listener)) 385 fListeners.remove(listener); 386 } 387 388 public void targetReloaded() { 389 fReloadExternalNeeded = true; 390 } 391 392 public IFeatureModel getDeltaPackFeature() { 393 IFeatureModel model = findFeatureModel("org.eclipse.equinox.executable"); if (model == null) 395 model = findFeatureModel("org.eclipse.platform.launchers"); return model; 397 } 398 399 } 400 | Popular Tags |