1 /*******************************************************************************
2  *  Copyright (c) 2019 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  *     Andrew Obuchowicz <aobuchow@redhat.com> - Initial Implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.correction;
15 
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.jdt.launching.JavaRuntime;
18 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
21 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
22 import org.eclipse.pde.internal.core.text.bundle.*;
23 import org.osgi.framework.Constants;
24 
25 public class ReplaceExecEnvironment extends AbstractManifestMarkerResolution {
26 
27 	String fRequiredEE;
28 
ReplaceExecEnvironment(int type, IMarker marker)29 	public ReplaceExecEnvironment(int type, IMarker marker) {
30 		super(type, marker);
31 		this.fRequiredEE = super.marker.getAttribute(PDEMarkerFactory.REQUIRED_EXEC_ENV, "JavaSE-1.8"); //$NON-NLS-1$
32 	}
33 
34 	@Override
getLabel()35 	public String getLabel() {
36 		return NLS.bind(Messages.ReplaceExecEnvironment_Marker_Label, this.fRequiredEE);
37 	}
38 
39 	@Override
createChange(BundleModel model)40 	protected void createChange(BundleModel model) {
41 		IManifestHeader header = model.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
42 		if (header instanceof RequiredExecutionEnvironmentHeader) {
43 			RequiredExecutionEnvironmentHeader reqHeader = (RequiredExecutionEnvironmentHeader) header;
44 			ExecutionEnvironment[] bundleEnvs = reqHeader.getEnvironments();
45 			IExecutionEnvironment[] systemEnvs = JavaRuntime.getExecutionEnvironmentsManager()
46 					.getExecutionEnvironments();
47 
48 			for (IExecutionEnvironment systemEnv : systemEnvs) {
49 				if (systemEnv.getId().equals(fRequiredEE)) {
50 					for (ExecutionEnvironment bundleEnv : bundleEnvs) {
51 						reqHeader.removeExecutionEnvironment(bundleEnv);
52 					}
53 					reqHeader.addExecutionEnvironment(systemEnv);
54 				}
55 
56 			}
57 		}
58 
59 	}
60 
61 }
62