001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.File;
013: import java.net.URL;
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.List;
018: import java.util.ListIterator;
019: import java.util.Map;
020: import java.util.Set;
021: import java.util.Stack;
022:
023: import org.eclipse.core.resources.IWorkspaceRunnable;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IPath;
026: import org.eclipse.core.runtime.IProgressMonitor;
027: import org.eclipse.core.runtime.Path;
028: import org.eclipse.core.runtime.Preferences;
029: import org.eclipse.core.runtime.SubProgressMonitor;
030: import org.eclipse.core.runtime.jobs.Job;
031: import org.eclipse.core.variables.IStringVariableManager;
032: import org.eclipse.core.variables.VariablesPlugin;
033: import org.eclipse.jdt.launching.IVMInstall;
034: import org.eclipse.jdt.launching.IVMInstallType;
035: import org.eclipse.jdt.launching.JavaRuntime;
036: import org.eclipse.pde.core.plugin.IPluginModelBase;
037: import org.eclipse.pde.core.plugin.TargetPlatform;
038: import org.eclipse.pde.internal.core.ifeature.IFeature;
039: import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
040: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
041: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
042: import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
043: import org.eclipse.pde.internal.core.itarget.IArgumentsInfo;
044: import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
045: import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
046: import org.eclipse.pde.internal.core.itarget.ILocationInfo;
047: import org.eclipse.pde.internal.core.itarget.ITarget;
048: import org.eclipse.pde.internal.core.itarget.ITargetFeature;
049: import org.eclipse.pde.internal.core.itarget.ITargetJRE;
050: import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
051:
052: public class LoadTargetOperation implements IWorkspaceRunnable {
053:
054: private ITarget fTarget;
055: private Map fRequiredPlugins = new HashMap();
056: private List fMissingFeatures = new ArrayList();
057: private IPath fPath = null;
058:
059: public LoadTargetOperation(ITarget target) {
060: this (target, (IPath) null);
061: }
062:
063: public LoadTargetOperation(ITarget target, IPath workspaceLoc) {
064: fTarget = target;
065: fPath = workspaceLoc;
066: }
067:
068: public void run(IProgressMonitor monitor) throws CoreException {
069: try {
070: Preferences preferences = PDECore.getDefault()
071: .getPluginPreferences();
072: monitor.beginTask(
073: PDECoreMessages.LoadTargetOperation_mainTaskName,
074: 100);
075: loadEnvironmentInfo(preferences, new SubProgressMonitor(
076: monitor, 5));
077: loadProgramArgs(preferences, new SubProgressMonitor(
078: monitor, 5));
079: loadJREInfo(preferences,
080: new SubProgressMonitor(monitor, 15));
081: loadImplicitPlugins(preferences, new SubProgressMonitor(
082: monitor, 15));
083: loadPlugins(preferences,
084: new SubProgressMonitor(monitor, 60));
085: loadAdditionalPreferences(preferences);
086: PDECore.getDefault().savePluginPreferences();
087: } finally {
088: monitor.done();
089: }
090: }
091:
092: public Object[] getMissingPlugins() {
093: return fRequiredPlugins.values().toArray();
094: }
095:
096: public Object[] getMissingFeatures() {
097: return fMissingFeatures.toArray();
098: }
099:
100: protected void loadProgramArgs(Preferences pref,
101: IProgressMonitor monitor) {
102: IArgumentsInfo args = fTarget.getArguments();
103: monitor.beginTask(
104: PDECoreMessages.LoadTargetOperation_argsTaskName, 2);
105: pref.setValue(ICoreConstants.PROGRAM_ARGS,
106: (args != null) ? args.getProgramArguments() : ""); //$NON-NLS-1$
107: monitor.worked(1);
108: pref.setValue(ICoreConstants.VM_ARGS, (args != null) ? args
109: .getVMArguments() : ""); //$NON-NLS-1$
110: monitor.done();
111: }
112:
113: protected void loadEnvironmentInfo(Preferences pref,
114: IProgressMonitor monitor) {
115: IEnvironmentInfo env = fTarget.getEnvironment();
116: monitor.beginTask(
117: PDECoreMessages.LoadTargetOperation_envTaskName, 1);
118: if (env == null) {
119: pref.setToDefault(ICoreConstants.ARCH);
120: pref.setToDefault(ICoreConstants.NL);
121: pref.setToDefault(ICoreConstants.OS);
122: pref.setToDefault(ICoreConstants.WS);
123: } else {
124: pref.setValue(ICoreConstants.ARCH, env.getDisplayArch());
125: pref.setValue(ICoreConstants.NL, env.getDisplayNL());
126: pref.setValue(ICoreConstants.OS, env.getDisplayOS());
127: pref.setValue(ICoreConstants.WS, env.getDisplayWS());
128: }
129: monitor.done();
130: }
131:
132: protected void loadJREInfo(Preferences pref,
133: IProgressMonitor monitor) {
134: ITargetJRE jreInfo = fTarget.getTargetJREInfo();
135: monitor.beginTask(
136: PDECoreMessages.LoadTargetOperation_jreTaskName, 1);
137: if (jreInfo != null) {
138: String jre = jreInfo.getCompatibleJRE();
139: IVMInstall install = JavaRuntime.getDefaultVMInstall();
140: if (install != null && !jre.equals(install.getName()))
141: try {
142: JavaRuntime.setDefaultVMInstall(getVMInstall(jre),
143: null);
144: } catch (CoreException e) {
145: }
146: }
147: monitor.done();
148: }
149:
150: private IVMInstall getVMInstall(String name) {
151: IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
152: for (int i = 0; i < types.length; i++) {
153: IVMInstall[] installs = types[i].getVMInstalls();
154: for (int k = 0; k < installs.length; k++) {
155: if (installs[i].getName().equals(name))
156: return installs[i];
157: }
158: }
159: return JavaRuntime.getDefaultVMInstall();
160: }
161:
162: protected void loadImplicitPlugins(Preferences pref,
163: IProgressMonitor monitor) {
164: IImplicitDependenciesInfo info = fTarget
165: .getImplicitPluginsInfo();
166: if (info != null) {
167: ITargetPlugin[] plugins = info.getPlugins();
168: monitor
169: .beginTask(
170: PDECoreMessages.LoadTargetOperation_implicitPluginsTaskName,
171: plugins.length + 1);
172: StringBuffer buffer = new StringBuffer();
173: for (int i = 0; i < plugins.length; i++) {
174: buffer.append(plugins[i].getId()).append(',');
175: monitor.worked(1);
176: }
177: if (plugins.length > 0)
178: buffer.setLength(buffer.length() - 1);
179: pref.setValue(ICoreConstants.IMPLICIT_DEPENDENCIES, buffer
180: .toString());
181: }
182: monitor.done();
183: }
184:
185: protected void loadPlugins(Preferences pref,
186: IProgressMonitor monitor) {
187: monitor
188: .beginTask(
189: PDECoreMessages.LoadTargetOperation_loadPluginsTaskName,
190: 100);
191: ILocationInfo info = fTarget.getLocationInfo();
192: String currentPath = pref
193: .getString(ICoreConstants.PLATFORM_PATH);
194: String path;
195: if (info == null || info.useDefault()) {
196: path = TargetPlatform.getDefaultLocation();
197: } else {
198: try {
199: IStringVariableManager manager = VariablesPlugin
200: .getDefault().getStringVariableManager();
201: path = manager
202: .performStringSubstitution(info.getPath());
203: } catch (CoreException e) {
204: return;
205: }
206: }
207: monitor.worked(10);
208: // TODO in 3.4, check timestamp of plug-ins to see if anything changed and a reload is required (bug 181659)
209: // if (!new Path(path).equals(new Path(currentPath)) || !areAdditionalLocationsEqual(pref)) {
210: // reload required
211: List additional = getAdditionalLocs();
212: handleReload(path, additional, pref, new SubProgressMonitor(
213: monitor, 85));
214:
215: // update preferences (Note: some preferences updated in handleReload())
216: pref.setValue(ICoreConstants.PLATFORM_PATH, path);
217: String mode = new Path(path).equals(new Path(TargetPlatform
218: .getDefaultLocation())) ? ICoreConstants.VALUE_USE_THIS
219: : ICoreConstants.VALUE_USE_OTHER;
220: pref.setValue(ICoreConstants.TARGET_MODE, mode);
221:
222: ListIterator li = additional.listIterator();
223: StringBuffer buffer = new StringBuffer();
224: while (li.hasNext())
225: buffer.append(li.next()).append(","); //$NON-NLS-1$
226: if (buffer.length() > 0)
227: buffer.setLength(buffer.length() - 1);
228: pref.setValue(ICoreConstants.ADDITIONAL_LOCATIONS, buffer
229: .toString());
230:
231: String newValue = currentPath;
232: for (int i = 0; i < 4; i++) {
233: String value = pref.getString(ICoreConstants.SAVED_PLATFORM
234: + i);
235: pref.setValue(ICoreConstants.SAVED_PLATFORM + i, newValue);
236: if (!value.equals(currentPath))
237: newValue = value;
238: else
239: break;
240: }
241: // } else {
242: // PDECore core = PDECore.getDefault();
243: // IPluginModelBase[] changed = handlePluginSelection(TargetPlatformHelper.getPDEState(), core.getFeatureModelManager(),
244: // pref, new SubProgressMonitor(monitor,85));
245: // if (changed.length > 0) {
246: // ExternalModelManager pluginManager = core.getModelManager().getExternalModelManager();
247: // pluginManager.fireModelProviderEvent(
248: // new ModelProviderEvent(
249: // pluginManager,
250: // IModelProviderEvent.MODELS_CHANGED,
251: // null,
252: // null,
253: // changed));
254: // }
255: // }
256: monitor.done();
257: }
258:
259: protected void loadAdditionalPreferences(Preferences pref) {
260: if (fPath == null)
261: return;
262: String newValue = "${workspace_loc:".concat(fPath.toOSString()).concat("}"); //$NON-NLS-1$ //$NON-NLS-2$
263: pref.setValue(ICoreConstants.TARGET_PROFILE, newValue);
264: }
265:
266: // private boolean areAdditionalLocationsEqual(Preferences pref) {
267: // IAdditionalLocation[] addtionalLocs = fTarget.getAdditionalDirectories();
268: // String value = pref.getString(ICoreConstants.ADDITIONAL_LOCATIONS);
269: // StringTokenizer tokenzier = new StringTokenizer(value);
270: // if (addtionalLocs.length != tokenzier.countTokens())
271: // return false;
272: // while (tokenzier.hasMoreTokens()) {
273: // boolean found = false;
274: // String location = tokenzier.nextToken();
275: // for (int i = 0; i < addtionalLocs.length; i++) {
276: // if (addtionalLocs[i].getPath().equals(location)) {
277: // found = true;
278: // break;
279: // }
280: // }
281: // if (!found)
282: // return false;
283: // }
284: // return true;
285: // }
286:
287: private List getAdditionalLocs() {
288: ArrayList additional = new ArrayList();
289: IAdditionalLocation[] locations = fTarget
290: .getAdditionalDirectories();
291: IStringVariableManager manager = VariablesPlugin.getDefault()
292: .getStringVariableManager();
293: for (int i = 0; i < locations.length; i++) {
294: try {
295: additional.add(manager
296: .performStringSubstitution(locations[i]
297: .getPath()));
298: } catch (CoreException e) {
299: additional.add(locations[i]);
300: }
301: }
302: return additional;
303: }
304:
305: private void handleReload(String targetLocation,
306: List additionalLocations, Preferences pref,
307: IProgressMonitor monitor) {
308: monitor.beginTask(
309: PDECoreMessages.LoadTargetOperation_reloadTaskName, 85);
310: URL[] paths = getURLs(targetLocation, additionalLocations);
311: PDEState state = new PDEState(paths, true,
312: new SubProgressMonitor(monitor, 45));
313:
314: ExternalFeatureModelManager featureManager = getFeatureManager(
315: targetLocation, additionalLocations);
316: IFeatureModel[] models = featureManager.getModels();
317: Map features = new HashMap();
318: for (int i = 0; i < models.length; i++)
319: features.put(models[i].getFeature().getId(), models[i]);
320: monitor.worked(5);
321: models = PDECore.getDefault().getFeatureModelManager()
322: .getWorkspaceModels();
323: for (int i = 0; i < models.length; i++)
324: features.put(models[i].getFeature().getId(), models[i]);
325: monitor.worked(5);
326:
327: handlePluginSelection(state, features, pref,
328: new SubProgressMonitor(monitor, 25));
329:
330: Job job = new TargetPlatformResetJob(state);
331: job.schedule();
332: monitor.done();
333: }
334:
335: private URL[] getURLs(String targetLocation,
336: List additionalLocations) {
337: int length = additionalLocations.size();
338: File[] locations = new File[2 * length + 2];
339: ListIterator li = additionalLocations.listIterator();
340: while (li.hasNext()) {
341: File dir = new File((String) li.next());
342: locations[2 * length] = dir;
343: locations[2 * length + 1] = new File(dir, "plugins"); //$NON-NLS-1$
344: --length;
345: }
346: File targetDir = new File(targetLocation);
347: locations[0] = new File(targetLocation);
348: locations[1] = new File(targetDir, "plugins"); //$NON-NLS-1$
349: return PluginPathFinder.scanLocations(locations);
350: }
351:
352: private ExternalFeatureModelManager getFeatureManager(
353: String targetLocation, List additionalLocations) {
354: StringBuffer buffer = new StringBuffer();
355: ListIterator li = additionalLocations.listIterator();
356: while (li.hasNext())
357: buffer.append(li.next()).append(',');
358: if (buffer.length() > 0)
359: buffer.setLength(buffer.length() - 1);
360: ExternalFeatureModelManager featureManager = new ExternalFeatureModelManager();
361: featureManager.loadModels(targetLocation, buffer.toString());
362: return featureManager;
363: }
364:
365: protected IPluginModelBase[] handlePluginSelection(PDEState state,
366: Map featureMap, Preferences pref, IProgressMonitor monitor) {
367: monitor
368: .beginTask(
369: PDECoreMessages.LoadTargetOperation_selectPluginsTaskName,
370: 80);
371: Set optionalPlugins = new HashSet();
372: getPluginIds(featureMap, null, optionalPlugins,
373: new SubProgressMonitor(monitor, 40));
374: return handlePluginSelection(state, optionalPlugins, pref,
375: new SubProgressMonitor(monitor, 40));
376: }
377:
378: protected IPluginModelBase[] handlePluginSelection(PDEState state,
379: FeatureModelManager manager, Preferences pref,
380: IProgressMonitor monitor) {
381: monitor
382: .beginTask(
383: PDECoreMessages.LoadTargetOperation_selectPluginsTaskName,
384: 80);
385: Set optionalPlugins = new HashSet();
386: getPluginIds(null, manager, optionalPlugins,
387: new SubProgressMonitor(monitor, 40));
388: return handlePluginSelection(state, optionalPlugins, pref,
389: new SubProgressMonitor(monitor, 40));
390: }
391:
392: // returns changed Models
393: private IPluginModelBase[] handlePluginSelection(PDEState state,
394: Set optionalPlugins, Preferences pref,
395: IProgressMonitor monitor) {
396: List changed = new ArrayList();
397: boolean useAll = fTarget.useAllPlugins();
398:
399: IPluginModelBase[] models = state.getTargetModels();
400: monitor
401: .beginTask(
402: PDECoreMessages.LoadTargetOperation_enablePluginsTaskName,
403: models.length);
404: boolean anyPluginsEnabled = false;
405: for (int i = 0; i < models.length; i++) {
406: String id = models[i].getBundleDescription()
407: .getSymbolicName();
408: if (models[i].isEnabled() != (useAll
409: || optionalPlugins.contains(id) || fRequiredPlugins
410: .containsKey(id))) {
411: changed.add(models[i]);
412: models[i].setEnabled(!models[i].isEnabled());
413: }
414: fRequiredPlugins.remove(id);
415: if (!anyPluginsEnabled)
416: anyPluginsEnabled |= models[i].isEnabled();
417: monitor.worked(1);
418: }
419: if (useAll)
420: pref.setValue(ICoreConstants.CHECKED_PLUGINS,
421: ICoreConstants.VALUE_SAVED_ALL);
422: else if (!anyPluginsEnabled)
423: pref.setValue(ICoreConstants.CHECKED_PLUGINS,
424: ICoreConstants.VALUE_SAVED_NONE);
425: monitor.done();
426: return (IPluginModelBase[]) changed
427: .toArray(new IPluginModelBase[changed.size()]);
428: }
429:
430: private void getPluginIds(Map featureMap,
431: FeatureModelManager manager, Set optionalPlugins,
432: IProgressMonitor monitor) {
433: ITargetFeature[] targetFeatures = fTarget.getFeatures();
434: ITargetPlugin[] plugins = fTarget.getPlugins();
435:
436: monitor
437: .beginTask(
438: PDECoreMessages.LoadTargetOperation_findPluginsTaskName,
439: targetFeatures.length + plugins.length);
440: if (fTarget.useAllPlugins()) {
441: monitor.done();
442: return;
443: }
444: boolean useMap = featureMap != null;
445: Stack features = new Stack();
446:
447: for (int i = 0; i < targetFeatures.length; i++) {
448: IFeatureModel model = (useMap) ? (IFeatureModel) featureMap
449: .get(targetFeatures[i].getId()) : manager
450: .findFeatureModel(targetFeatures[i].getId());
451: if (model != null)
452: features.push(model);
453: else if (!targetFeatures[i].isOptional()) {
454: fMissingFeatures.add(targetFeatures[i]);
455: break;
456: }
457: while (!features.isEmpty()) {
458: IFeature feature = ((IFeatureModel) features.pop())
459: .getFeature();
460: IFeaturePlugin[] featurePlugins = feature.getPlugins();
461: for (int j = 0; j < featurePlugins.length; j++) {
462: if (targetFeatures[i].isOptional()
463: || featurePlugins[j].isFragment())
464: optionalPlugins.add(featurePlugins[j].getId());
465: else
466: fRequiredPlugins.put(featurePlugins[j].getId(),
467: featurePlugins[j]);
468: }
469: IFeatureChild[] children = feature
470: .getIncludedFeatures();
471: for (int j = 0; j < children.length; j++) {
472: model = (useMap) ? (IFeatureModel) featureMap
473: .get(children[j].getId()) : manager
474: .findFeatureModel(children[j].getId());
475: if (model != null)
476: features.push(model);
477: }
478: }
479: monitor.worked(1);
480: }
481:
482: for (int i = 0; i < plugins.length; i++) {
483: if (plugins[i].isOptional())
484: optionalPlugins.add(plugins[i].getId());
485: else
486: fRequiredPlugins.put(plugins[i].getId(), plugins[i]);
487: monitor.worked(1);
488: }
489:
490: monitor.done();
491: }
492: }
|