Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d458412a7d1439068b65f36f8d16fafdb4f658e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 *  Copyright (c) 2015, 2019 SAP SE and others.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 * 
 *  Contributors:
 *      SAP SE - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.touchpoint.natives.actions;

import java.io.File;
import java.io.IOException;
import java.util.*;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.touchpoint.natives.*;
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.osgi.util.NLS;

public class CheckAndPromptNativePackageWindowsRegistry extends ProvisioningAction {
	public static final String ID = "checkAndPromptNativePackageWindowsRegistry"; //$NON-NLS-1$
	public static final String WINDOWS_DISTRO = "windows"; //$NON-NLS-1$
	private static final boolean IS_WINDOWS = java.io.File.separatorChar == '\\';
	private static final String IS_INSTALLED = "isInstalled.bat"; //$NON-NLS-1$
	private static final String SHELL = "cmd.exe"; //$NON-NLS-1$

	@Override
	public IStatus execute(Map<String, Object> parameters) {
		// If we are not running on Windows, do nothing and return
		if (!IS_WINDOWS)
			return Status.OK_STATUS;

		// Get and check the paremeters
		String packageName = (String) parameters.get(ActionConstants.PARM_LINUX_PACKAGE_NAME);
		String packageVersion = (String) parameters.get(ActionConstants.PARM_LINUX_PACKAGE_VERSION);
		String key = (String) parameters.get(ActionConstants.PARM_WINDOWS_REGISTRY_KEY);
		String attName = (String) parameters.get(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME);
		String attValue = (String) parameters.get(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE);
		IInstallableUnit iu = (IInstallableUnit) parameters.get(ActionConstants.PARM_IU);

		if (key == null || (attName != null && attValue == null))
			return new Status(IStatus.ERROR, Activator.ID, Messages.Incorrect_Command);

		// Check if the desired package is installed and collect information in the
		// touchpoint
		File scriptToExecute = NativeTouchpoint.getFileFromBundle(WINDOWS_DISTRO, IS_INSTALLED);
		if (scriptToExecute == null)
			return new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.Cannot_Find_status, WINDOWS_DISTRO));

		try {
			List<String> cmd = new ArrayList<>(6);
			cmd.add(SHELL);
			cmd.add("/c"); //$NON-NLS-1$
			cmd.add(scriptToExecute.getAbsolutePath());
			cmd.add(key);
			if (attName != null) {
				cmd.add(attName);
				cmd.add(attValue);
			}
			int exitValue = new ProcessBuilder(cmd).start().waitFor();
			switch (exitValue) {
			case 0:
				return Status.OK_STATUS;
			case 1:
			case 2:
			default:
				NativePackageEntry packageEntry = new NativePackageEntry(packageName, packageVersion, null);
				String downloadLink = (String) parameters.get(ActionConstants.PARM_DOWNLOAD_LINK);
				packageEntry.setDownloadLink(downloadLink);
				((NativeTouchpoint) getTouchpoint()).addPackageToInstall(packageEntry, iu);
				((NativeTouchpoint) getTouchpoint()).setDistro(WINDOWS_DISTRO);
				return Status.OK_STATUS;
			}
		} catch (IOException e) {
			return new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.Cannot_Check_Package,
					new String[] { packageName, packageVersion, WINDOWS_DISTRO }));
		} catch (InterruptedException e) {
			return new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.Cannot_Check_Package,
					new String[] { packageName, packageVersion, WINDOWS_DISTRO }));
		}
	}

	@Override
	public IStatus undo(Map<String, Object> parameters) {
		// Nothing to do since we are not modifying any state.
		return null;
	}

}

Back to the top