From commits-return-2904-apmail-jmeter-commits-archive=jmeter.apache.org@jmeter.apache.org Sun Nov 17 14:18:15 2013 Return-Path: X-Original-To: apmail-jmeter-commits-archive@minotaur.apache.org Delivered-To: apmail-jmeter-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EEE6B10F14 for ; Sun, 17 Nov 2013 14:18:15 +0000 (UTC) Received: (qmail 50752 invoked by uid 500); 17 Nov 2013 14:18:15 -0000 Delivered-To: apmail-jmeter-commits-archive@jmeter.apache.org Received: (qmail 50728 invoked by uid 500); 17 Nov 2013 14:18:15 -0000 Mailing-List: contact commits-help@jmeter.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jmeter.apache.org Delivered-To: mailing list commits@jmeter.apache.org Received: (qmail 50721 invoked by uid 99); 17 Nov 2013 14:18:15 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 17 Nov 2013 14:18:15 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 17 Nov 2013 14:18:14 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 3001D23888CD; Sun, 17 Nov 2013 14:17:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1542727 - /jmeter/trunk/src/jorphan/org/apache/jorphan/exec/KeyToolUtils.java Date: Sun, 17 Nov 2013 14:17:54 -0000 To: commits@jmeter.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131117141754.3001D23888CD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sebb Date: Sun Nov 17 14:17:53 2013 New Revision: 1542727 URL: http://svn.apache.org/r1542727 Log: Add check to ensure that keytool application can be found Modified: jmeter/trunk/src/jorphan/org/apache/jorphan/exec/KeyToolUtils.java Modified: jmeter/trunk/src/jorphan/org/apache/jorphan/exec/KeyToolUtils.java URL: http://svn.apache.org/viewvc/jmeter/trunk/src/jorphan/org/apache/jorphan/exec/KeyToolUtils.java?rev=1542727&r1=1542726&r2=1542727&view=diff ============================================================================== --- jmeter/trunk/src/jorphan/org/apache/jorphan/exec/KeyToolUtils.java (original) +++ jmeter/trunk/src/jorphan/org/apache/jorphan/exec/KeyToolUtils.java Sun Nov 17 14:17:53 2013 @@ -31,6 +31,7 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.JavaVersion; import org.apache.commons.lang3.SystemUtils; import org.apache.jorphan.logging.LoggingManager; +import org.apache.jorphan.util.JOrphanUtils; import org.apache.log.Logger; /** @@ -47,6 +48,14 @@ public class KeyToolUtils { private static final String DNAME_ROOT_CA_KEY; + private static final String KEYTOOL_DIRECTORY = "keytool.directory"; // $NON-NLS-1$ + + /** + * Where to find the keytool application. + * If null, then keytool cannot be found. + */ + private static final String KEYTOOL_PATH; + private static void addElement(StringBuilder sb, String prefix, String value) { if (value != null) { sb.append(", "); @@ -61,6 +70,38 @@ public class KeyToolUtils { addElement(sb, "OU=Username: ", System.getProperty("user.name")); // $NON-NLS-1$ $NON-NLS-2$ addElement(sb, "C=", System.getProperty("user.country")); // $NON-NLS-1$ $NON-NLS-2$ DNAME_ROOT_CA_KEY = sb.toString(); + + // Try to find keytool application + // N.B. Cannot use JMeter property from jorphan jar. + final String keytoolDir = System.getProperty(KEYTOOL_DIRECTORY); + + String keytoolPath; // work field + if (keytoolDir != null) { + keytoolPath = new File(new File(keytoolDir),"keytool").getPath(); // $NON-NLS-1$ + if (!checkKeytool(keytoolPath)) { // $NON-NLS-1$ + log.error("Cannot find keytool using property " + KEYTOOL_DIRECTORY + "="+keytoolDir); + keytoolPath = null; // don't try anything else if the property is provided + } + } else { + keytoolPath = "keytool"; // $NON-NLS-1$ + if (!checkKeytool(keytoolPath)) { // Not found on PATH, check Java Home + File javaHome = JOrphanUtils.getJavaHome(); + if (javaHome != null) { + keytoolPath = new File(new File(javaHome,"bin"),"keytool").getPath(); // $NON-NLS-1$ $NON-NLS-2$ + if (!checkKeytool(keytoolPath)) { + keytoolPath = null; + } + } else { + keytoolPath = null; + } + } + } + if (keytoolPath == null) { + log.error("Unable to find keytool application. Check PATH or define system property " + KEYTOOL_DIRECTORY); + } else { + log.info("keytool found at '" + keytoolPath + "'"); + } + KEYTOOL_PATH = keytoolPath; } private static final String DNAME_INTERMEDIATE_CA_KEY = "cn=DO NOT INSTALL THIS CERTIFICATE (JMeter Intermediate CA)"; // $NON-NLS-1$ @@ -98,7 +139,7 @@ public class KeyToolUtils { final File workingDir = keystore.getParentFile(); final SystemCommand nativeCommand = new SystemCommand(workingDir, null); final List arguments = new ArrayList(); - arguments.add("keytool"); // $NON-NLS-1$ + arguments.add(getKeyToolPath()); arguments.add("-genkeypair"); // $NON-NLS-1$ arguments.add("-alias"); // $NON-NLS-1$ arguments.add(alias); @@ -252,7 +293,7 @@ public class KeyToolUtils { final File workingDir = keystore.getParentFile(); final SystemCommand nativeCommand = new SystemCommand(workingDir, null); final List arguments = new ArrayList(); - arguments.add("keytool"); // $NON-NLS-1$ + arguments.add(getKeyToolPath()); arguments.add("-list"); // $NON-NLS-1$ arguments.add("-v"); // $NON-NLS-1$ @@ -307,7 +348,7 @@ public class KeyToolUtils { final File workingDir = keystore.getParentFile(); final SystemCommand nativeCommand = new SystemCommand(workingDir, 0L, 0, null, input, output, null); final List arguments = new ArrayList(); - arguments.add("keytool"); // $NON-NLS-1$ + arguments.add(getKeyToolPath()); arguments.add(command); arguments.add("-keystore"); // $NON-NLS-1$ arguments.add(keystore.getName()); @@ -330,4 +371,35 @@ public class KeyToolUtils { throw new IOException("Command was interrupted\n" + nativeCommand.getOutResult(), e); } } + + public static boolean haveKeytool() { + return KEYTOOL_PATH != null; + } + + private static String getKeyToolPath() throws IOException { + if (KEYTOOL_PATH == null) { + throw new IOException("keytool application cannot be found"); + } + return "keytool"; + } + + /** + * Check if keytool can be found + * @param keytoolPath the path to check + */ + private static boolean checkKeytool(String keytoolPath) { + final SystemCommand nativeCommand = new SystemCommand(null, null); + final List arguments = new ArrayList(); + arguments.add(keytoolPath); + arguments.add("-help"); // $NON-NLS-1$ + try { + nativeCommand.run(arguments); + return true; + } catch (IOException ioe) { + return false; + } catch (InterruptedException e) { + log.error("Command was interrupted\n" + nativeCommand.getOutResult(), e); + return false; + } + } }