KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > LaunchPluginValidator


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.ui.launcher;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17 import java.util.TreeMap JavaDoc;
18 import java.util.TreeSet JavaDoc;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.debug.core.ILaunchConfiguration;
25 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
26 import org.eclipse.jdt.core.IJavaProject;
27 import org.eclipse.jdt.core.JavaCore;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.osgi.service.resolver.BundleDescription;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.core.plugin.PluginRegistry;
32 import org.eclipse.pde.internal.core.PDECore;
33 import org.eclipse.pde.internal.core.SearchablePluginsManager;
34 import org.eclipse.pde.internal.core.TargetPlatformHelper;
35 import org.eclipse.pde.internal.ui.IPDEUIConstants;
36 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
37 import org.eclipse.swt.widgets.Display;
38
39 public class LaunchPluginValidator {
40     
41     public static void checkBackwardCompatibility(ILaunchConfiguration configuration, boolean save) throws CoreException {
42         ILaunchConfigurationWorkingCopy wc = null;
43         if (configuration.isWorkingCopy()) {
44             wc = (ILaunchConfigurationWorkingCopy) configuration;
45         } else {
46             wc = configuration.getWorkingCopy();
47         }
48         
49         String JavaDoc value = configuration.getAttribute("wsproject", (String JavaDoc)null); //$NON-NLS-1$
50
if (value != null) {
51             wc.setAttribute("wsproject", (String JavaDoc)null); //$NON-NLS-1$
52
if (value.indexOf(';') != -1) {
53                 value = value.replace(';', ',');
54             } else if (value.indexOf(':') != -1) {
55                 value = value.replace(':', ',');
56             }
57             value = (value.length() == 0 || value.equals(",")) //$NON-NLS-1$
58
? null : value.substring(0, value.length() - 1);
59             
60             boolean automatic = configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true);
61             String JavaDoc attr = automatic
62                             ? IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS
63                             : IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS;
64             wc.setAttribute(attr, value);
65         }
66
67         String JavaDoc value2 = configuration.getAttribute("extplugins", (String JavaDoc)null); //$NON-NLS-1$
68
if (value2 != null) {
69             wc.setAttribute("extplugins", (String JavaDoc)null); //$NON-NLS-1$
70
if (value2.indexOf(';') != -1)
71                 value2 = value2.replace(';', ',');
72             else if (value2.indexOf(':') != -1)
73                 value2 = value2.replace(':', ',');
74             value2 = (value2.length() == 0 || value2.equals(",")) //$NON-NLS-1$
75
? null : value2.substring(0, value2.length() - 1);
76             wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, value2);
77         }
78         
79         String JavaDoc version = configuration.getAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, (String JavaDoc) null); //$NON-NLS-1$
80
boolean newApp = TargetPlatformHelper.usesNewApplicationModel();
81         boolean upgrade = !"3.3".equals(version) && newApp; //$NON-NLS-1$
82
if (!upgrade)
83             upgrade = TargetPlatformHelper.getTargetVersion() >= 3.2 && version == null; //$NON-NLS-1$
84
if (upgrade) {
85             wc.setAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, newApp ? "3.3" : "3.2a"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
86
boolean usedefault = configuration.getAttribute(IPDELauncherConstants.USE_DEFAULT, true);
87             boolean useFeatures = configuration.getAttribute(IPDELauncherConstants.USEFEATURES, false);
88             boolean automaticAdd = configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true);
89             if (!usedefault && !useFeatures) {
90                 ArrayList JavaDoc list = new ArrayList JavaDoc();
91                 if (version == null) {
92                     list.add("org.eclipse.core.contenttype"); //$NON-NLS-1$
93
list.add("org.eclipse.core.jobs"); //$NON-NLS-1$
94
list.add("org.eclipse.equinox.common"); //$NON-NLS-1$
95
list.add("org.eclipse.equinox.preferences"); //$NON-NLS-1$
96
list.add("org.eclipse.equinox.registry"); //$NON-NLS-1$
97
list.add("org.eclipse.core.runtime.compatibility.registry"); //$NON-NLS-1$
98
}
99                 if (!"3.3".equals(version) && newApp) //$NON-NLS-1$
100
list.add("org.eclipse.equinox.app"); //$NON-NLS-1$
101
StringBuffer JavaDoc extensions = new StringBuffer JavaDoc(configuration.getAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, "")); //$NON-NLS-1$
102
StringBuffer JavaDoc target = new StringBuffer JavaDoc(configuration.getAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, "")); //$NON-NLS-1$
103
for (int i = 0; i < list.size(); i++) {
104                     String JavaDoc plugin = list.get(i).toString();
105                     IPluginModelBase model = PluginRegistry.findModel(plugin);
106                     if (model == null)
107                         continue;
108                     if (model.getUnderlyingResource() != null) {
109                         if (automaticAdd)
110                             continue;
111                         if (extensions.length() > 0)
112                             extensions.append(","); //$NON-NLS-1$
113
extensions.append(plugin);
114                     } else {
115                         if (target.length() > 0)
116                             target.append(","); //$NON-NLS-1$
117
target.append(plugin);
118                     }
119                 }
120                 if (extensions.length() > 0)
121                     wc.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, extensions.toString());
122                 if (target.length() > 0)
123                     wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, target.toString());
124             }
125         }
126         
127         if (save && (value != null || value2 != null || upgrade))
128             wc.doSave();
129     }
130     
131     private static void addToMap(Map JavaDoc map, IPluginModelBase[] models) {
132         for (int i = 0; i < models.length; i++) {
133             addToMap(map, models[i]);
134         }
135     }
136
137     private static void addToMap(Map JavaDoc map, IPluginModelBase model) {
138         BundleDescription desc = model.getBundleDescription();
139         if (desc != null) {
140             String JavaDoc id = desc.getSymbolicName();
141             // the reason that we are using a map is to easily check
142
// if a plug-in with a certain id is among the plug-ins we are launching with.
143
// Therefore, now that we support multiple plug-ins by the same ID,
144
// once a particular ID is used up as a key, the rest can be entered
145
// with key == id_version, for easy retrieval of values later on,
146
// and without the need to create complicated data structures for values.
147
if (!map.containsKey(id)) {
148                 map.put(id, model);
149             } else {
150                 map.put(id + "_" + desc.getBundleId(), model); //$NON-NLS-1$
151
}
152         }
153     }
154
155     private static IPluginModelBase[] getSelectedWorkspacePlugins(ILaunchConfiguration configuration)
156             throws CoreException {
157         
158         boolean usedefault = configuration.getAttribute(IPDELauncherConstants.USE_DEFAULT, true);
159         boolean useFeatures = configuration.getAttribute(IPDELauncherConstants.USEFEATURES, false);
160         
161         IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
162         
163         if (usedefault || useFeatures || models.length == 0)
164             return models;
165         
166         ArrayList JavaDoc list = new ArrayList JavaDoc();
167         if (configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true)) {
168             TreeSet JavaDoc deselected = parsePlugins(configuration,
169                                     IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS);
170             if (deselected.size() == 0)
171                 return models;
172             for (int i = 0; i < models.length; i++) {
173                 String JavaDoc id = models[i].getPluginBase().getId();
174                 if (id != null && !deselected.contains(id))
175                     list.add(models[i]);
176             }
177         } else {
178             TreeSet JavaDoc selected = parsePlugins(configuration,
179                                     IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS);
180             for (int i = 0; i < models.length; i++) {
181                 String JavaDoc id = models[i].getPluginBase().getId();
182                 if (id != null && selected.contains(id))
183                     list.add(models[i]);
184             }
185         }
186         return (IPluginModelBase[])list.toArray(new IPluginModelBase[list.size()]);
187     }
188
189     public static TreeSet JavaDoc parsePlugins(ILaunchConfiguration configuration, String JavaDoc attribute)
190             throws CoreException {
191         TreeSet JavaDoc set = new TreeSet JavaDoc();
192         String JavaDoc ids = configuration.getAttribute(attribute, (String JavaDoc) null);
193         if (ids != null) {
194             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(ids, ","); //$NON-NLS-1$
195
while (tok.hasMoreTokens())
196                 set.add(tok.nextToken());
197         }
198         return set;
199     }
200
201     public static IPluginModelBase[] getPluginList(ILaunchConfiguration config) throws CoreException {
202         Map JavaDoc map = getPluginsToRun(config);
203         return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]);
204     }
205     
206     public static Map JavaDoc getPluginsToRun(ILaunchConfiguration config)
207             throws CoreException {
208
209         checkBackwardCompatibility(config, true);
210             
211         TreeMap JavaDoc map = new TreeMap JavaDoc();
212         if (config.getAttribute(IPDELauncherConstants.USE_DEFAULT, true)) {
213             addToMap(map, PluginRegistry.getActiveModels());
214             return map;
215         }
216         
217         if (config.getAttribute(IPDELauncherConstants.USEFEATURES, false)) {
218             addToMap(map, PluginRegistry.getWorkspaceModels());
219             return map;
220         }
221         
222         addToMap(map, getSelectedWorkspacePlugins(config));
223
224         Set JavaDoc exModels = parsePlugins(config, IPDELauncherConstants.SELECTED_TARGET_PLUGINS);
225         IPluginModelBase[] exmodels = PluginRegistry.getExternalModels();
226         for (int i = 0; i < exmodels.length; i++) {
227             String JavaDoc id = exmodels[i].getPluginBase().getId();
228             if (id != null && exModels.contains(id)) {
229                 IPluginModelBase existing = (IPluginModelBase)map.get(id);
230                 // only allow dups if plug-in existing in map is not a workspace plug-in
231
if (existing == null || existing.getUnderlyingResource() == null)
232                     addToMap(map, exmodels[i]);
233             }
234         }
235         return map;
236     }
237
238     public static IProject[] getAffectedProjects(ILaunchConfiguration config)
239             throws CoreException {
240         ArrayList JavaDoc projects = new ArrayList JavaDoc();
241         IPluginModelBase[] models = getSelectedWorkspacePlugins(config);
242         for (int i = 0; i < models.length; i++) {
243             IProject project = models[i].getUnderlyingResource().getProject();
244             if (project.hasNature(JavaCore.NATURE_ID))
245                 projects.add(project);
246         }
247
248         // add fake "Java Search" project
249
SearchablePluginsManager manager = PDECore.getDefault().getSearchablePluginsManager();
250         IJavaProject proxy = manager.getProxyProject();
251         if (proxy != null) {
252             projects.add(proxy.getProject());
253         }
254         return (IProject[]) projects.toArray(new IProject[projects.size()]);
255     }
256     
257     public static void runValidationOperation(final LaunchValidationOperation op, IProgressMonitor monitor) throws CoreException{
258         op.run(monitor);
259         if (op.hasErrors()) {
260             final int[] result = new int[1];
261             final Display display = LauncherUtils.getDisplay();
262             display.syncExec(new Runnable JavaDoc() {
263                 public void run() {
264                     PluginStatusDialog dialog = new PluginStatusDialog(display.getActiveShell());
265                     dialog.showCancelButton(true);
266                     dialog.setInput(op.getInput());
267                     result[0] = dialog.open();
268             }});
269             if (result[0] == IDialogConstants.CANCEL_ID)
270                 throw new CoreException(Status.CANCEL_STATUS);
271         }
272     }
273
274 }
275
Popular Tags