KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > LoadTargetOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.ListIterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.Stack JavaDoc;
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 JavaDoc fRequiredPlugins = new HashMap JavaDoc();
57     private List JavaDoc fMissingFeatures = new ArrayList JavaDoc();
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 JavaDoc[] getMissingPlugins() {
86         return fRequiredPlugins.values().toArray();
87     }
88     
89     public Object JavaDoc[] 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() : ""); //$NON-NLS-1$
97
monitor.worked(1);
98         pref.setValue(ICoreConstants.VM_ARGS, (args != null) ? args.getVMArguments() : ""); //$NON-NLS-1$
99
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 JavaDoc 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 JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc currentPath = pref.getString(ICoreConstants.PLATFORM_PATH);
167         String JavaDoc 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         // TODO in 3.4, check timestamp of plug-ins to see if anything changed and a reload is required (bug 181659)
180
// if (!new Path(path).equals(new Path(currentPath)) || !areAdditionalLocationsEqual(pref)) {
181
// reload required
182
List JavaDoc additional = getAdditionalLocs();
183             handleReload(path, additional, pref, new SubProgressMonitor(monitor, 85));
184             
185             // update preferences (Note: some preferences updated in handleReload())
186
pref.setValue(ICoreConstants.PLATFORM_PATH, path);
187             String JavaDoc 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 JavaDoc li = additional.listIterator();
194             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
195             while (li.hasNext())
196                 buffer.append(li.next()).append(","); //$NON-NLS-1$
197
if (buffer.length() > 0)
198                 buffer.setLength(buffer.length() - 1);
199             pref.setValue(ICoreConstants.ADDITIONAL_LOCATIONS, buffer.toString());
200             
201             String JavaDoc newValue = currentPath;
202             for (int i = 0; i < 4; i++) {
203                 String JavaDoc 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 // } else {
211
// PDECore core = PDECore.getDefault();
212
// IPluginModelBase[] changed = handlePluginSelection(TargetPlatformHelper.getPDEState(), core.getFeatureModelManager(),
213
// pref, new SubProgressMonitor(monitor,85));
214
// if (changed.length > 0) {
215
// ExternalModelManager pluginManager = core.getModelManager().getExternalModelManager();
216
// pluginManager.fireModelProviderEvent(
217
// new ModelProviderEvent(
218
// pluginManager,
219
// IModelProviderEvent.MODELS_CHANGED,
220
// null,
221
// null,
222
// changed));
223
// }
224
// }
225
monitor.done();
226     }
227     
228     protected void loadAdditionalPreferences(Preferences pref) {
229         if (fPath == null)
230             return;
231         String JavaDoc newValue = "${workspace_loc:".concat(fPath.toOSString()).concat("}"); //$NON-NLS-1$ //$NON-NLS-2$
232
pref.setValue(ICoreConstants.TARGET_PROFILE, newValue);
233     }
234     
235 // private boolean areAdditionalLocationsEqual(Preferences pref) {
236
// IAdditionalLocation[] addtionalLocs = fTarget.getAdditionalDirectories();
237
// String value = pref.getString(ICoreConstants.ADDITIONAL_LOCATIONS);
238
// StringTokenizer tokenzier = new StringTokenizer(value);
239
// if (addtionalLocs.length != tokenzier.countTokens())
240
// return false;
241
// while (tokenzier.hasMoreTokens()) {
242
// boolean found = false;
243
// String location = tokenzier.nextToken();
244
// for (int i = 0; i < addtionalLocs.length; i++) {
245
// if (addtionalLocs[i].getPath().equals(location)) {
246
// found = true;
247
// break;
248
// }
249
// }
250
// if (!found)
251
// return false;
252
// }
253
// return true;
254
// }
255

256     private List JavaDoc getAdditionalLocs() {
257         ArrayList JavaDoc additional = new ArrayList JavaDoc();
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 JavaDoc targetLocation, List JavaDoc additionalLocations, Preferences pref, IProgressMonitor monitor) {
271         monitor.beginTask(PDECoreMessages.LoadTargetOperation_reloadTaskName, 85);
272         URL JavaDoc[] 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 JavaDoc features = new HashMap JavaDoc();
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 JavaDoc[] getURLs(String JavaDoc targetLocation, List JavaDoc additionalLocations) {
294         int length = additionalLocations.size();
295         File JavaDoc[] locations = new File JavaDoc[2 * length + 2];
296         ListIterator JavaDoc li = additionalLocations.listIterator();
297         while (li.hasNext()) {
298             File JavaDoc dir = new File JavaDoc((String JavaDoc)li.next());
299             locations[2 *length] = dir;
300             locations[2 * length + 1] = new File JavaDoc(dir, "plugins"); //$NON-NLS-1$
301
--length;
302         }
303         File JavaDoc targetDir = new File JavaDoc(targetLocation);
304         locations[0] = new File JavaDoc(targetLocation);
305         locations[1] = new File JavaDoc(targetDir, "plugins"); //$NON-NLS-1$
306
return PluginPathFinder.scanLocations(locations);
307     }
308     
309     private ExternalFeatureModelManager getFeatureManager(String JavaDoc targetLocation, List JavaDoc additionalLocations) {
310         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
311         ListIterator JavaDoc 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 JavaDoc featureMap, Preferences pref, IProgressMonitor monitor) {
322         monitor.beginTask(PDECoreMessages.LoadTargetOperation_selectPluginsTaskName, 80);
323         Set JavaDoc optionalPlugins = new HashSet JavaDoc();
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 JavaDoc optionalPlugins = new HashSet JavaDoc();
331         getPluginIds(null, manager, optionalPlugins, new SubProgressMonitor(monitor, 40));
332         return handlePluginSelection(state, optionalPlugins, pref, new SubProgressMonitor(monitor, 40));
333     }
334
335     // returns changed Models
336
private IPluginModelBase[] handlePluginSelection(PDEState state, Set JavaDoc optionalPlugins, Preferences pref, IProgressMonitor monitor) {
337         List JavaDoc changed = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc featureMap, FeatureModelManager manager, Set JavaDoc 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 JavaDoc features = new Stack JavaDoc();
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