1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.util.ArrayList ; 14 import java.util.Map ; 15 import java.util.Set ; 16 import java.util.StringTokenizer ; 17 import java.util.TreeMap ; 18 import java.util.TreeSet ; 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 value = configuration.getAttribute("wsproject", (String )null); if (value != null) { 51 wc.setAttribute("wsproject", (String )null); 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(",")) ? null : value.substring(0, value.length() - 1); 59 60 boolean automatic = configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true); 61 String attr = automatic 62 ? IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS 63 : IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS; 64 wc.setAttribute(attr, value); 65 } 66 67 String value2 = configuration.getAttribute("extplugins", (String )null); if (value2 != null) { 69 wc.setAttribute("extplugins", (String )null); 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(",")) ? null : value2.substring(0, value2.length() - 1); 76 wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, value2); 77 } 78 79 String version = configuration.getAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, (String ) null); boolean newApp = TargetPlatformHelper.usesNewApplicationModel(); 81 boolean upgrade = !"3.3".equals(version) && newApp; if (!upgrade) 83 upgrade = TargetPlatformHelper.getTargetVersion() >= 3.2 && version == null; if (upgrade) { 85 wc.setAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, newApp ? "3.3" : "3.2a"); 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 list = new ArrayList (); 91 if (version == null) { 92 list.add("org.eclipse.core.contenttype"); list.add("org.eclipse.core.jobs"); list.add("org.eclipse.equinox.common"); list.add("org.eclipse.equinox.preferences"); list.add("org.eclipse.equinox.registry"); list.add("org.eclipse.core.runtime.compatibility.registry"); } 99 if (!"3.3".equals(version) && newApp) list.add("org.eclipse.equinox.app"); StringBuffer extensions = new StringBuffer (configuration.getAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, "")); StringBuffer target = new StringBuffer (configuration.getAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, "")); for (int i = 0; i < list.size(); i++) { 104 String 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(","); extensions.append(plugin); 114 } else { 115 if (target.length() > 0) 116 target.append(","); 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 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 map, IPluginModelBase model) { 138 BundleDescription desc = model.getBundleDescription(); 139 if (desc != null) { 140 String id = desc.getSymbolicName(); 141 if (!map.containsKey(id)) { 148 map.put(id, model); 149 } else { 150 map.put(id + "_" + desc.getBundleId(), model); } 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 list = new ArrayList (); 167 if (configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true)) { 168 TreeSet 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 id = models[i].getPluginBase().getId(); 174 if (id != null && !deselected.contains(id)) 175 list.add(models[i]); 176 } 177 } else { 178 TreeSet selected = parsePlugins(configuration, 179 IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS); 180 for (int i = 0; i < models.length; i++) { 181 String 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 parsePlugins(ILaunchConfiguration configuration, String attribute) 190 throws CoreException { 191 TreeSet set = new TreeSet (); 192 String ids = configuration.getAttribute(attribute, (String ) null); 193 if (ids != null) { 194 StringTokenizer tok = new StringTokenizer (ids, ","); 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 map = getPluginsToRun(config); 203 return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]); 204 } 205 206 public static Map getPluginsToRun(ILaunchConfiguration config) 207 throws CoreException { 208 209 checkBackwardCompatibility(config, true); 210 211 TreeMap map = new TreeMap (); 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 exModels = parsePlugins(config, IPDELauncherConstants.SELECTED_TARGET_PLUGINS); 225 IPluginModelBase[] exmodels = PluginRegistry.getExternalModels(); 226 for (int i = 0; i < exmodels.length; i++) { 227 String id = exmodels[i].getPluginBase().getId(); 228 if (id != null && exModels.contains(id)) { 229 IPluginModelBase existing = (IPluginModelBase)map.get(id); 230 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 projects = new ArrayList (); 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 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 () { 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 |