1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.ListIterator ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.Stack ; 23 24 import org.eclipse.core.resources.IWorkspaceRunnable; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IPath; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.core.runtime.Preferences; 30 import org.eclipse.core.runtime.SubProgressMonitor; 31 import org.eclipse.core.runtime.jobs.Job; 32 import org.eclipse.core.variables.IStringVariableManager; 33 import org.eclipse.core.variables.VariablesPlugin; 34 import org.eclipse.jdt.launching.IVMInstall; 35 import org.eclipse.jdt.launching.IVMInstallType; 36 import org.eclipse.jdt.launching.JavaRuntime; 37 import org.eclipse.pde.core.plugin.IPluginModelBase; 38 import org.eclipse.pde.core.plugin.TargetPlatform; 39 import org.eclipse.pde.internal.core.ifeature.IFeature; 40 import org.eclipse.pde.internal.core.ifeature.IFeatureChild; 41 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 42 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin; 43 import org.eclipse.pde.internal.core.itarget.IAdditionalLocation; 44 import org.eclipse.pde.internal.core.itarget.IArgumentsInfo; 45 import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo; 46 import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo; 47 import org.eclipse.pde.internal.core.itarget.ILocationInfo; 48 import org.eclipse.pde.internal.core.itarget.ITarget; 49 import org.eclipse.pde.internal.core.itarget.ITargetFeature; 50 import org.eclipse.pde.internal.core.itarget.ITargetJRE; 51 import org.eclipse.pde.internal.core.itarget.ITargetPlugin; 52 53 public class LoadTargetOperation implements IWorkspaceRunnable { 54 55 private ITarget fTarget; 56 private Map fRequiredPlugins = new HashMap (); 57 private List fMissingFeatures = new ArrayList (); 58 private IPath fPath = null; 59 60 public LoadTargetOperation(ITarget target) { 61 this(target, (IPath)null); 62 } 63 64 public LoadTargetOperation(ITarget target, IPath workspaceLoc) { 65 fTarget = target; 66 fPath = workspaceLoc; 67 } 68 69 public void run(IProgressMonitor monitor) throws CoreException { 70 try { 71 Preferences preferences = PDECore.getDefault().getPluginPreferences(); 72 monitor.beginTask(PDECoreMessages.LoadTargetOperation_mainTaskName, 100); 73 loadEnvironmentInfo(preferences, new SubProgressMonitor(monitor, 5)); 74 loadProgramArgs(preferences, new SubProgressMonitor(monitor,5)); 75 loadJREInfo(preferences, new SubProgressMonitor(monitor, 15)); 76 loadImplicitPlugins(preferences, new SubProgressMonitor(monitor, 15)); 77 loadPlugins(preferences, new SubProgressMonitor(monitor, 60)); 78 loadAdditionalPreferences(preferences); 79 PDECore.getDefault().savePluginPreferences(); 80 } finally { 81 monitor.done(); 82 } 83 } 84 85 public Object [] getMissingPlugins() { 86 return fRequiredPlugins.values().toArray(); 87 } 88 89 public Object [] getMissingFeatures() { 90 return fMissingFeatures.toArray(); 91 } 92 93 protected void loadProgramArgs(Preferences pref, IProgressMonitor monitor) { 94 IArgumentsInfo args = fTarget.getArguments(); 95 monitor.beginTask(PDECoreMessages.LoadTargetOperation_argsTaskName, 2); 96 pref.setValue(ICoreConstants.PROGRAM_ARGS, (args != null) ? args.getProgramArguments() : ""); monitor.worked(1); 98 pref.setValue(ICoreConstants.VM_ARGS, (args != null) ? args.getVMArguments() : ""); monitor.done(); 100 } 101 102 protected void loadEnvironmentInfo(Preferences pref, IProgressMonitor monitor) { 103 IEnvironmentInfo env = fTarget.getEnvironment(); 104 monitor.beginTask(PDECoreMessages.LoadTargetOperation_envTaskName, 1); 105 if (env == null) { 106 pref.setToDefault(ICoreConstants.ARCH); 107 pref.setToDefault(ICoreConstants.NL); 108 pref.setToDefault(ICoreConstants.OS); 109 pref.setToDefault(ICoreConstants.WS); 110 } else { 111 pref.setValue(ICoreConstants.ARCH, env.getDisplayArch()); 112 pref.setValue(ICoreConstants.NL, env.getDisplayNL()); 113 pref.setValue(ICoreConstants.OS, env.getDisplayOS()); 114 pref.setValue(ICoreConstants.WS, env.getDisplayWS()); 115 } 116 monitor.done(); 117 } 118 119 protected void loadJREInfo(Preferences pref, IProgressMonitor monitor) { 120 ITargetJRE jreInfo = fTarget.getTargetJREInfo(); 121 monitor.beginTask(PDECoreMessages.LoadTargetOperation_jreTaskName, 1); 122 if (jreInfo != null) { 123 String jre = jreInfo.getCompatibleJRE(); 124 IVMInstall install = JavaRuntime.getDefaultVMInstall(); 125 if (install != null && !jre.equals(install.getName())) 126 try { 127 JavaRuntime.setDefaultVMInstall(getVMInstall(jre), null); 128 } catch (CoreException e) { 129 } 130 } 131 monitor.done(); 132 } 133 134 private IVMInstall getVMInstall(String name) { 135 IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); 136 for (int i = 0; i < types.length; i++) { 137 IVMInstall[] installs = types[i].getVMInstalls(); 138 for (int k = 0; k < installs.length; k++) { 139 if (installs[i].getName().equals(name)) 140 return installs[i]; 141 } 142 } 143 return JavaRuntime.getDefaultVMInstall(); 144 } 145 146 protected void loadImplicitPlugins(Preferences pref, IProgressMonitor monitor) { 147 IImplicitDependenciesInfo info = fTarget.getImplicitPluginsInfo(); 148 if (info != null) { 149 ITargetPlugin[] plugins = info.getPlugins(); 150 monitor.beginTask(PDECoreMessages.LoadTargetOperation_implicitPluginsTaskName, plugins.length + 1); 151 StringBuffer buffer = new StringBuffer (); 152 for (int i = 0; i < plugins.length; i++) { 153 buffer.append(plugins[i].getId()).append(','); 154 monitor.worked(1); 155 } 156 if (plugins.length > 0) 157 buffer.setLength(buffer.length() - 1); 158 pref.setValue(ICoreConstants.IMPLICIT_DEPENDENCIES, buffer.toString()); 159 } 160 monitor.done(); 161 } 162 163 protected void loadPlugins(Preferences pref, IProgressMonitor monitor) { 164 monitor.beginTask(PDECoreMessages.LoadTargetOperation_loadPluginsTaskName, 100); 165 ILocationInfo info = fTarget.getLocationInfo(); 166 String currentPath = pref.getString(ICoreConstants.PLATFORM_PATH); 167 String path; 168 if (info == null || info.useDefault()) { 169 path = TargetPlatform.getDefaultLocation(); 170 } else { 171 try { 172 IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 173 path = manager.performStringSubstitution(info.getPath()); 174 } catch (CoreException e) { 175 return; 176 } 177 } 178 monitor.worked(10); 179 List additional = getAdditionalLocs(); 183 handleReload(path, additional, pref, new SubProgressMonitor(monitor, 85)); 184 185 pref.setValue(ICoreConstants.PLATFORM_PATH, path); 187 String mode = 188 new Path(path).equals(new Path(TargetPlatform.getDefaultLocation())) 189 ? ICoreConstants.VALUE_USE_THIS 190 : ICoreConstants.VALUE_USE_OTHER; 191 pref.setValue(ICoreConstants.TARGET_MODE, mode); 192 193 ListIterator li = additional.listIterator(); 194 StringBuffer buffer = new StringBuffer (); 195 while (li.hasNext()) 196 buffer.append(li.next()).append(","); if (buffer.length() > 0) 198 buffer.setLength(buffer.length() - 1); 199 pref.setValue(ICoreConstants.ADDITIONAL_LOCATIONS, buffer.toString()); 200 201 String newValue = currentPath; 202 for (int i = 0; i < 4; i++) { 203 String value = pref.getString(ICoreConstants.SAVED_PLATFORM + i); 204 pref.setValue(ICoreConstants.SAVED_PLATFORM + i, newValue); 205 if (!value.equals(currentPath)) 206 newValue = value; 207 else 208 break; 209 } 210 monitor.done(); 226 } 227 228 protected void loadAdditionalPreferences(Preferences pref) { 229 if (fPath == null) 230 return; 231 String newValue = "${workspace_loc:".concat(fPath.toOSString()).concat("}"); pref.setValue(ICoreConstants.TARGET_PROFILE, newValue); 233 } 234 235 256 private List getAdditionalLocs() { 257 ArrayList additional = new ArrayList (); 258 IAdditionalLocation[] locations = fTarget.getAdditionalDirectories(); 259 IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 260 for (int i = 0; i < locations.length; i++) { 261 try { 262 additional.add(manager.performStringSubstitution(locations[i].getPath())); 263 } catch (CoreException e) { 264 additional.add(locations[i]); 265 } 266 } 267 return additional; 268 } 269 270 private void handleReload(String targetLocation, List additionalLocations, Preferences pref, IProgressMonitor monitor) { 271 monitor.beginTask(PDECoreMessages.LoadTargetOperation_reloadTaskName, 85); 272 URL [] paths = getURLs(targetLocation, additionalLocations); 273 PDEState state = new PDEState(paths, true, new SubProgressMonitor(monitor, 45)); 274 275 ExternalFeatureModelManager featureManager = getFeatureManager(targetLocation, additionalLocations); 276 IFeatureModel[] models = featureManager.getModels(); 277 Map features = new HashMap (); 278 for (int i = 0; i < models.length; i++) 279 features.put(models[i].getFeature().getId(), models[i]); 280 monitor.worked(5); 281 models = PDECore.getDefault().getFeatureModelManager().getWorkspaceModels(); 282 for (int i = 0; i < models.length; i++) 283 features.put(models[i].getFeature().getId(), models[i]); 284 monitor.worked(5); 285 286 handlePluginSelection(state, features, pref, new SubProgressMonitor(monitor,25)); 287 288 Job job = new TargetPlatformResetJob(state); 289 job.schedule(); 290 monitor.done(); 291 } 292 293 private URL [] getURLs(String targetLocation, List additionalLocations) { 294 int length = additionalLocations.size(); 295 File [] locations = new File [2 * length + 2]; 296 ListIterator li = additionalLocations.listIterator(); 297 while (li.hasNext()) { 298 File dir = new File ((String )li.next()); 299 locations[2 *length] = dir; 300 locations[2 * length + 1] = new File (dir, "plugins"); --length; 302 } 303 File targetDir = new File (targetLocation); 304 locations[0] = new File (targetLocation); 305 locations[1] = new File (targetDir, "plugins"); return PluginPathFinder.scanLocations(locations); 307 } 308 309 private ExternalFeatureModelManager getFeatureManager(String targetLocation, List additionalLocations) { 310 StringBuffer buffer = new StringBuffer (); 311 ListIterator li = additionalLocations.listIterator(); 312 while (li.hasNext()) 313 buffer.append(li.next()).append(','); 314 if (buffer.length() > 0) 315 buffer.setLength(buffer.length() - 1); 316 ExternalFeatureModelManager featureManager = new ExternalFeatureModelManager(); 317 featureManager.loadModels(targetLocation, buffer.toString()); 318 return featureManager; 319 } 320 321 protected IPluginModelBase[] handlePluginSelection(PDEState state, Map featureMap, Preferences pref, IProgressMonitor monitor) { 322 monitor.beginTask(PDECoreMessages.LoadTargetOperation_selectPluginsTaskName, 80); 323 Set optionalPlugins = new HashSet (); 324 getPluginIds(featureMap, null, optionalPlugins, new SubProgressMonitor(monitor, 40)); 325 return handlePluginSelection(state, optionalPlugins, pref, new SubProgressMonitor(monitor, 40)); 326 } 327 328 protected IPluginModelBase[] handlePluginSelection(PDEState state, FeatureModelManager manager, Preferences pref, IProgressMonitor monitor) { 329 monitor.beginTask(PDECoreMessages.LoadTargetOperation_selectPluginsTaskName, 80); 330 Set optionalPlugins = new HashSet (); 331 getPluginIds(null, manager, optionalPlugins, new SubProgressMonitor(monitor, 40)); 332 return handlePluginSelection(state, optionalPlugins, pref, new SubProgressMonitor(monitor, 40)); 333 } 334 335 private IPluginModelBase[] handlePluginSelection(PDEState state, Set optionalPlugins, Preferences pref, IProgressMonitor monitor) { 337 List changed = new ArrayList (); 338 boolean useAll = fTarget.useAllPlugins(); 339 340 IPluginModelBase[] models = state.getTargetModels(); 341 monitor.beginTask(PDECoreMessages.LoadTargetOperation_enablePluginsTaskName, models.length); 342 boolean anyPluginsEnabled = false; 343 for (int i = 0; i < models.length; i++) { 344 String id = models[i].getBundleDescription().getSymbolicName(); 345 if (models[i].isEnabled() != (useAll || optionalPlugins.contains(id) || fRequiredPlugins.containsKey(id))) { 346 changed.add(models[i]); 347 models[i].setEnabled(!models[i].isEnabled()); 348 } 349 fRequiredPlugins.remove(id); 350 if (!anyPluginsEnabled) 351 anyPluginsEnabled |= models[i].isEnabled(); 352 monitor.worked(1); 353 } 354 if (useAll) 355 pref.setValue(ICoreConstants.CHECKED_PLUGINS, ICoreConstants.VALUE_SAVED_ALL); 356 else if (!anyPluginsEnabled) 357 pref.setValue(ICoreConstants.CHECKED_PLUGINS, ICoreConstants.VALUE_SAVED_NONE); 358 monitor.done(); 359 return (IPluginModelBase[])changed.toArray(new IPluginModelBase[changed.size()]); 360 } 361 362 private void getPluginIds(Map featureMap, FeatureModelManager manager, Set optionalPlugins, IProgressMonitor monitor) { 363 ITargetFeature[] targetFeatures = fTarget.getFeatures(); 364 ITargetPlugin[] plugins = fTarget.getPlugins(); 365 366 monitor.beginTask(PDECoreMessages.LoadTargetOperation_findPluginsTaskName, targetFeatures.length + plugins.length); 367 if (fTarget.useAllPlugins()) { 368 monitor.done(); 369 return; 370 } 371 boolean useMap = featureMap != null; 372 Stack features = new Stack (); 373 374 for (int i = 0 ; i < targetFeatures.length; i++) { 375 IFeatureModel model = (useMap)? (IFeatureModel)featureMap.get(targetFeatures[i].getId()): 376 manager.findFeatureModel(targetFeatures[i].getId()); 377 if (model != null) 378 features.push(model); 379 else if (!targetFeatures[i].isOptional()) { 380 fMissingFeatures.add(targetFeatures[i]); 381 break; 382 } 383 while (!features.isEmpty()) { 384 IFeature feature = ((IFeatureModel) features.pop()).getFeature(); 385 IFeaturePlugin [] featurePlugins = feature.getPlugins(); 386 for (int j = 0; j < featurePlugins.length; j++) { 387 if (targetFeatures[i].isOptional() || featurePlugins[j].isFragment()) 388 optionalPlugins.add(featurePlugins[j].getId()); 389 else 390 fRequiredPlugins.put(featurePlugins[j].getId(), featurePlugins[j]); 391 } 392 IFeatureChild[] children = feature.getIncludedFeatures(); 393 for (int j = 0; j < children.length; j++) { 394 model = (useMap)? (IFeatureModel)featureMap.get(children[j].getId()): 395 manager.findFeatureModel(children[j].getId()); 396 if (model != null) 397 features.push(model); 398 } 399 } 400 monitor.worked(1); 401 } 402 403 for (int i = 0; i < plugins.length; i++) { 404 if (plugins[i].isOptional()) 405 optionalPlugins.add(plugins[i].getId()); 406 else 407 fRequiredPlugins.put(plugins[i].getId(), plugins[i]); 408 monitor.worked(1); 409 } 410 411 monitor.done(); 412 } 413 } 414 | Popular Tags |