Bug 486446 - StandInException in DockerMachine.execute

Checking that the 'docker-machine' command is found before
  attempting to run it, and validating the Docker Mahcine
  preference page by checking that the command is found there, too.

Change-Id: I872008d5c49510624b23bf98e1944fb157163556
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
Reviewed-on: https://git.eclipse.org/r/68663
Tested-by: Hudson CI
Reviewed-by: Roland Grunberg <rgrunber@redhat.com>
Tested-by: Roland Grunberg <rgrunber@redhat.com>
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
Reviewed-on: https://git.eclipse.org/r/68774
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java
index 2ffe5a7..9073a07 100644
--- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java
@@ -24,6 +24,7 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.linuxtools.docker.core.Activator;
+import org.eclipse.osgi.util.NLS;
 
 /**
  * Utility class to discover Docker machines using the 'docker-machine' command
@@ -88,13 +89,14 @@
 	 * 
 	 * @param args
 	 *            command arguments
-	 * @return the lines read in the {@link Process}' {@link InputStream}
+	 * @return the lines read in the {@link Process}' {@link InputStream} or an
+	 *         empty array if the {@code docker-machine} command could not be
+	 *         found in the {@code PATH}.
 	 */
 	private static String[] execute(final String dockerMachineInstallDir,
 			final String[] args, final String... extraPaths) {
 		try {
 			final String[] command = new String[args.length + 1];
-
 			command[0] = Paths.get(dockerMachineInstallDir, "docker-machine").toString(); //$NON-NLS-1$
 			final String envPath = System.getenv("PATH"); //$NON-NLS-1$
 			if (envPath != null) {
@@ -117,6 +119,18 @@
 			}
 			String newEnvPath = environment.get("PATH") + path.toString(); //$NON-NLS-1$
 			environment.put("PATH", newEnvPath); //$NON-NLS-1$
+			// check that the 'docker-machine' can be found in PATH
+			final boolean dockerMachineCommandExists = checkPathToDockerMachine(
+					newEnvPath);
+			if (!dockerMachineCommandExists) {
+				// log a warning and exit
+				Activator
+						.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID,
+								NLS.bind(
+										Messages.Docker_Machine_Command_Not_Found,
+										newEnvPath)));
+				return new String[0];
+			}
 			final Process p = processBuilder.start();
 			p.waitFor();
 			if (p.exitValue() == 0) {
@@ -147,4 +161,23 @@
 		return new String[0];
 	}
 
+	/**
+	 * Checks that the 'docker-machine' command exists in the given {@code path}
+	 * 
+	 * @param path
+	 *            to path to use to look for the 'docker-machine' command
+	 * @return <code>true</code> if the command was found, <code>false</code>
+	 *         otherwise.
+	 */
+	public static boolean checkPathToDockerMachine(final String path) {
+		for (String pathFragment : path.split(File.pathSeparator)) {
+			final File pathToDockerMachine = new File(pathFragment,
+					"docker-machine"); //$NON-NLS-1$
+			if (pathToDockerMachine.exists()) {
+				return true;
+			}
+		}
+		return false;
+	}
+
 }
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/Messages.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/Messages.java
new file mode 100644
index 0000000..16f4ba9
--- /dev/null
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/Messages.java
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Red Hat.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Red Hat - Initial Contribution
+ *******************************************************************************/
+package org.eclipse.linuxtools.internal.docker.core;
+
+import org.eclipse.osgi.util.NLS;
+
+public class Messages extends NLS {
+
+	public static String Docker_Machine_Command_Not_Found;
+
+	static {
+		// Initialize resource bundle.
+		NLS.initializeMessages(Messages.class.getName(), Messages.class);
+	}
+
+	private Messages() {
+	}
+
+}
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/Messages.properties b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/Messages.properties
new file mode 100644
index 0000000..596ae83
--- /dev/null
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/Messages.properties
@@ -0,0 +1,11 @@
+#################################################################################
+# Copyright (c) 2016 Red Hat Inc.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+#     Red Hat - Initial Contribution
+#################################################################################
+Docker_Machine_Command_Not_Found=The '''docker-machine''' command could not be located in {0}
\ No newline at end of file
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerMachinePreferencePage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerMachinePreferencePage.java
index 3bc9d4f..357df2d 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerMachinePreferencePage.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerMachinePreferencePage.java
@@ -15,6 +15,8 @@
 import org.eclipse.jface.preference.StringFieldEditor;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.linuxtools.docker.ui.Activator;
+import org.eclipse.linuxtools.internal.docker.core.DockerMachine;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
@@ -81,6 +83,22 @@
 	}
 
 	/**
+	 * Checks that the {@code docker-machine} command exists in the specified
+	 * directory
+	 */
+	@Override
+	public boolean isValid() {
+		if (!DockerMachine.checkPathToDockerMachine(
+				this.dockerMachineInstallDir.getStringValue())) {
+			setErrorMessage(NLS.bind(
+					org.eclipse.linuxtools.internal.docker.core.Messages.Docker_Machine_Command_Not_Found,
+					this.dockerMachineInstallDir.getStringValue()));
+			return false;
+		}
+		return super.isValid();
+	}
+
+	/**
 	 * Subclass of the {@link DirectoryFieldEditor} but with the
 	 * {@link StringFieldEditor#VALIDATE_ON_KEY_STROKE} validation strategy.
 	 */
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
index 1b78e86..2e5224c 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
@@ -664,8 +664,9 @@
 			
 			@Override
 			public void widgetSelected(SelectionEvent e) {
-				if (getDockerMachineInstallDir().equals("")
-						|| getVMDriverInstallDir().equals("")) {
+				if (!DockerMachine
+						.checkPathToDockerMachine(getDockerMachineInstallDir())
+						|| getVMDriverInstallDir().equals("")) { //$NON-NLS-1$
 					final boolean confirm = MessageDialog.openConfirm(
 							getShell(),
 									WizardMessages.getString(
@@ -679,8 +680,8 @@
 										DOCKER_MACHINE_PREFERENCE_PAGE_ID },
 								null).open();
 						// after user input data, check again
-						if (getDockerMachineInstallDir().equals("")
-								|| getVMDriverInstallDir().equals("")) {
+						if (getDockerMachineInstallDir().equals("") //$NON-NLS-1$
+								|| getVMDriverInstallDir().equals("")) { //$NON-NLS-1$
 							return;
 						}
 					} else {