1 /*******************************************************************************
2  * Copyright (c) 2007, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     EclipseSource Corporation - ongoing enhancements
14  *******************************************************************************/
15 package org.eclipse.pde.internal.launching.launcher;
16 
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.debug.core.*;
21 import org.eclipse.osgi.service.resolver.BundleDescription;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.internal.launching.*;
25 
26 public class EclipsePluginValidationOperation extends LaunchValidationOperation {
27 	public static final int CREATE_EXTENSION_ERROR_CODE = 1000;
28 
29 	private Map<Object, Object[]> fExtensionErrors = new HashMap<>(2);
30 	private static Object[] EMPTY = new Object[0];
31 
EclipsePluginValidationOperation(ILaunchConfiguration configuration)32 	public EclipsePluginValidationOperation(ILaunchConfiguration configuration) {
33 		super(configuration);
34 	}
35 
36 	@Override
getModels()37 	protected IPluginModelBase[] getModels() throws CoreException {
38 		return BundleLauncherHelper.getMergedBundles(fLaunchConfiguration, false);
39 	}
40 
41 	@Override
run(IProgressMonitor monitor)42 	public void run(IProgressMonitor monitor) throws CoreException {
43 		super.run(monitor);
44 		if (!fExtensionErrors.isEmpty())
45 			fExtensionErrors.clear();
46 		validateExtensions();
47 	}
48 
validateExtensions()49 	private void validateExtensions() {
50 		try {
51 			String[] required = RequirementHelper.getApplicationRequirements(fLaunchConfiguration);
52 			for (String element : required) {
53 				BundleDescription bundle = getState().getBundle(element, null);
54 				if (bundle == null) {
55 					String message = NLS.bind(PDEMessages.EclipsePluginValidationOperation_pluginMissing, element);
56 					Status status = new Status(IStatus.ERROR, IPDEConstants.PLUGIN_ID, CREATE_EXTENSION_ERROR_CODE, message, null);
57 					IStatusHandler statusHandler = DebugPlugin.getDefault().getStatusHandler(status);
58 					Object extensionError = null;
59 					if (statusHandler == null)
60 						extensionError = status.getMessage();
61 					else
62 						extensionError = statusHandler.handleStatus(status, element);
63 					fExtensionErrors.put(extensionError, EMPTY);
64 				}
65 			}
66 		} catch (CoreException e) {
67 			PDELaunchingPlugin.log(e);
68 		}
69 	}
70 
71 	@Override
hasErrors()72 	public boolean hasErrors() {
73 		return super.hasErrors() || fExtensionErrors.size() >= 1;
74 	}
75 
76 	@Override
getInput()77 	public Map<Object, Object[]> getInput() {
78 		Map<Object, Object[]> map = super.getInput();
79 		map.putAll(fExtensionErrors);
80 		return map;
81 	}
82 
83 }
84