001: /*******************************************************************************
002: * Copyright (c) 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.ui.launcher;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.debug.core.ILaunchConfiguration;
018: import org.eclipse.osgi.service.resolver.BundleDescription;
019: import org.eclipse.osgi.util.NLS;
020: import org.eclipse.pde.core.plugin.IPluginAttribute;
021: import org.eclipse.pde.core.plugin.IPluginElement;
022: import org.eclipse.pde.core.plugin.IPluginExtension;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.core.plugin.PluginRegistry;
025: import org.eclipse.pde.core.plugin.TargetPlatform;
026: import org.eclipse.pde.internal.core.util.IdUtil;
027: import org.eclipse.pde.internal.ui.IPDEUIConstants;
028: import org.eclipse.pde.internal.ui.PDELabelProvider;
029: import org.eclipse.pde.internal.ui.PDEPlugin;
030: import org.eclipse.pde.internal.ui.PDEPluginImages;
031: import org.eclipse.pde.internal.ui.PDEUIMessages;
032: import org.eclipse.pde.internal.ui.elements.NamedElement;
033: import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
034: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
035: import org.eclipse.swt.graphics.Image;
036:
037: public class EclipsePluginValidationOperation extends
038: LaunchValidationOperation {
039:
040: private Map fExtensionErrors = new HashMap(2);
041: private static Object[] EMPTY = new Object[0];
042:
043: public EclipsePluginValidationOperation(
044: ILaunchConfiguration configuration) {
045: super (configuration);
046: }
047:
048: protected IPluginModelBase[] getModels() throws CoreException {
049: return LaunchPluginValidator
050: .getPluginList(fLaunchConfiguration);
051: }
052:
053: public void run(IProgressMonitor monitor) throws CoreException {
054: super .run(monitor);
055: if (fExtensionErrors.size() > 0)
056: fExtensionErrors.clear();
057: validateExtensions();
058: }
059:
060: private void validateExtensions() {
061: try {
062: if (fLaunchConfiguration.getAttribute(
063: IPDELauncherConstants.USE_PRODUCT, false)) {
064: String product = fLaunchConfiguration.getAttribute(
065: IPDELauncherConstants.PRODUCT, (String) null);
066: if (product != null) {
067: validateExtension(product);
068: String application = getApplication(product);
069: if (application != null)
070: validateExtension(application);
071: }
072: } else {
073: String configType = fLaunchConfiguration.getType()
074: .getIdentifier();
075: String attribute = configType
076: .equals(EclipseLaunchShortcut.CONFIGURATION_TYPE) ? IPDELauncherConstants.APPLICATION
077: : IPDELauncherConstants.APP_TO_TEST;
078: String application = fLaunchConfiguration.getAttribute(
079: attribute, TargetPlatform
080: .getDefaultApplication());
081: if (!IPDEUIConstants.CORE_TEST_APPLICATION
082: .equals(application)) {
083: validateExtension(application);
084: }
085: }
086: } catch (CoreException e) {
087: PDEPlugin.log(e);
088: }
089: }
090:
091: private String getApplication(String product) {
092: String bundleID = product
093: .substring(0, product.lastIndexOf('.'));
094: BundleDescription bundle = getState().getBundle(bundleID, null);
095: if (bundle != null) {
096: IPluginModelBase model = PluginRegistry.findModel(bundle);
097: if (model != null) {
098: IPluginExtension[] extensions = model.getPluginBase()
099: .getExtensions();
100: for (int i = 0; i < extensions.length; i++) {
101: IPluginExtension ext = extensions[i];
102: String point = ext.getPoint();
103: if ("org.eclipse.core.runtime.products".equals(point) //$NON-NLS-1$
104: && product.equals(IdUtil.getFullId(ext))) {
105: if (ext.getChildCount() == 1) {
106: IPluginElement prod = (IPluginElement) ext
107: .getChildren()[0];
108: if (prod.getName().equals("product")) { //$NON-NLS-1$
109: IPluginAttribute attr = prod
110: .getAttribute("application"); //$NON-NLS-1$
111: return attr != null ? attr.getValue()
112: : null;
113: }
114: }
115: }
116: }
117: }
118: }
119: return null;
120: }
121:
122: private void validateExtension(String id) {
123: String bundleID = id.substring(0, id.lastIndexOf('.'));
124: BundleDescription bundle = getState().getBundle(bundleID, null);
125: if (bundle == null) {
126: String name = NLS
127: .bind(
128: PDEUIMessages.EclipsePluginValidationOperation_pluginMissing,
129: bundleID);
130: PDELabelProvider provider = PDEPlugin.getDefault()
131: .getLabelProvider();
132: Image image = provider.get(PDEPluginImages.DESC_PLUGIN_OBJ);
133: fExtensionErrors.put(new NamedElement(name, image), EMPTY);
134: }
135: }
136:
137: public boolean hasErrors() {
138: return super .hasErrors() || fExtensionErrors.size() >= 1;
139: }
140:
141: public Map getInput() {
142: Map map = super.getInput();
143: map.putAll(fExtensionErrors);
144: return map;
145: }
146:
147: }
|