Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 70700 invoked from network); 11 Mar 2006 19:19:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 11 Mar 2006 19:19:23 -0000 Received: (qmail 23128 invoked by uid 500); 11 Mar 2006 19:19:18 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 22985 invoked by uid 500); 11 Mar 2006 19:19:18 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 22947 invoked by uid 500); 11 Mar 2006 19:19:18 -0000 Received: (qmail 22911 invoked by uid 99); 11 Mar 2006 19:19:18 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 11 Mar 2006 11:19:18 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sat, 11 Mar 2006 11:19:16 -0800 Received: (qmail 70565 invoked by uid 65534); 11 Mar 2006 19:18:55 -0000 Message-ID: <20060311191855.70562.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r385141 - in /jakarta/commons/sandbox/exec/trunk/src: main/java/org/apache/commons/exec/ main/java/org/apache/commons/exec/environment/ main/java/org/apache/commons/exec/launcher/ test/java/org/apache/commons/exec/ Date: Sat, 11 Mar 2006 19:18:52 -0000 To: commons-cvs@jakarta.apache.org From: ngn@apache.org X-Mailer: svnmailer-1.0.7 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: ngn Date: Sat Mar 11 11:18:50 2006 New Revision: 385141 URL: http://svn.apache.org/viewcvs?rev=385141&view=rev Log: Based on popular demand, CommandLine is back. Next step will be to further simplify the API. Added: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java - copied, changed from r377093, jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLineArgument.java - copied unchanged from r377093, jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLineArgument.java jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java - copied, changed from r377093, jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherProxy.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/TestUtil.java Copied: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java (from r377093, jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java) URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java?p2=jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java&p1=jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java&r1=377093&r2=385141&rev=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java Sat Mar 11 11:18:50 2006 @@ -17,59 +17,304 @@ package org.apache.commons.exec; +import java.io.File; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.StringTokenizer; +import java.util.Vector; + /** * CommandLine objects help handling command lines specifying processes to * execute. The class can be used to define a command line as nested elements or * as a helper to define a command line by an application. */ -public interface CommandLine { +public class CommandLine { + + private static final String SINGLE_QUOTE = "\'"; + + private static final String DOUBLE_QUOTE = "\""; + + /** + * The arguments of the command. + */ + private Vector arguments = new Vector(); + + /** + * The program to execute. + */ + private String executable = null; + + /** + * Create a command line from a string. + * + * @param toProcess + * the line: the first element becomes the executable, the rest + * the arguments + */ + public CommandLine(final String toProcess) { + super(); + String[] tmp = translateCommandline(toProcess); + if (tmp != null && tmp.length > 0) { + setExecutable(tmp[0]); + for (int i = 1; i < tmp.length; i++) { + createArgument(tmp[i]); + } + } + } + + /** + * Create an empty command line. + */ + public CommandLine() { + super(); + } + + /** + * Creates an argument object. + *

+ * Each commandline object has at most one instance of the argument class. + * This method calls this.createArgument(false). + *

+ * + * @return the argument object. + */ + private CommandLineArgument createArgument(final String value) { + CommandLineArgument argument = new CommandLineArgument(value); + arguments.addElement(argument); + return argument; + } + + public void setExecutable(final String executable) { + if (executable == null || executable.length() == 0) { + return; + } + this.executable = executable.replace('/', File.separatorChar).replace( + '\\', File.separatorChar); + } + + public String getExecutable() { + return executable; + } + + public void addArguments(final String[] line) { + for (int i = 0; i < line.length; i++) { + createArgument(line[i]); + } + } + + public void addArgument(final String arg) { + createArgument(arg); + } + + public String[] getCommandline() { + List commands = new LinkedList(); + ListIterator list = commands.listIterator(); + addCommandToList(list); + final String[] result = new String[commands.size()]; + return (String[]) commands.toArray(result); + } /** - * Sets the executable to run. All file separators in the string are - * converted to the platform specific value + * Add the entire command, including (optional) executable to a list. + * + * @param list */ - void setExecutable(final String executable); + private void addCommandToList(final ListIterator list) { + if (executable != null) { + list.add(executable); + } + addArgumentsToList(list); + } + + public String[] getArguments() { + List result = new ArrayList(arguments.size() * 2); + addArgumentsToList(result.listIterator()); + String[] res = new String[result.size()]; + return (String[]) result.toArray(res); + } /** - * Get the executable. + * append all the arguments to the tail of a supplied list * - * @return the program to run -null if not yet set + * @param list */ - String getExecutable(); + private void addArgumentsToList(final ListIterator list) { + for (int i = 0; i < arguments.size(); i++) { + CommandLineArgument arg = (CommandLineArgument) arguments + .elementAt(i); + String[] s = arg.getParts(); + if (s != null) { + for (int j = 0; j < s.length; j++) { + list.add(s[j]); + } + } + } + } /** - * Append the arguments to the existing command. + * Stringify operator returns the command line as a string. * - * @param line - * an array of arguments to append + * @return the command line */ - void addArguments(final String[] line); + public String toString() { + return toString(getCommandline()); + } /** - * Append a single argument to the current command. - * @param arg the argument to add. + * Put quotes around the given String if necessary. + *

+ * If the argument doesn't include spaces or quotes, return it as is. If it + * contains double quotes, use single quotes - else surround the argument by + * double quotes. + *

+ * */ - void addArgument(final String arg); + public static String quoteArgument(final String argument) { + final StringBuffer buf = new StringBuffer(); + if (argument.indexOf(DOUBLE_QUOTE) > -1) { + if (argument.indexOf(SINGLE_QUOTE) > -1) { + throw new IllegalArgumentException( + "Can\'t handle single and double quotes in same argument"); + } else { + return buf.append(SINGLE_QUOTE).append(argument).append(SINGLE_QUOTE).toString(); + } + } else if (argument.indexOf(SINGLE_QUOTE) > -1 || argument.indexOf(" ") > -1) { + return buf.append(DOUBLE_QUOTE).append(argument).append(DOUBLE_QUOTE).toString(); + } else { + return argument; + } + } /** - * Returns the executable and all defined arguments. + * Quotes the parts of the given array in way that makes them usable as + * command line arguments. + * + * @return empty string for null or no command, else every argument split by + * spaces and quoted by quoting rules */ - String[] getCommandline(); + public static String toString(final String[] line) { + // empty path return empty string + if (line == null || line.length == 0) { + return ""; + } + + // path containing one or more elements + final StringBuffer result = new StringBuffer(); + for (int i = 0; i < line.length; i++) { + if (i > 0) { + result.append(' '); + } + result.append(quoteArgument(line[i])); + } + return result.toString(); + } /** - * Returns all arguments defined by addLine, - * addValue or the argument object. + * Crack a command line. + * + * @param toProcess + * the command line to process + * @return the command line broken into strings. An empty or null toProcess + * parameter results in a zero sized array */ - String[] getArguments(); + public static String[] translateCommandline(final String toProcess) { + if (toProcess == null || toProcess.length() == 0) { + // no command? no string + return new String[0]; + } + + // parse with a simple finite state machine + + final int normal = 0; + final int inQuote = 1; + final int inDoubleQuote = 2; + int state = normal; + StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true); + Vector v = new Vector(); + StringBuffer current = new StringBuffer(); + boolean lastTokenHasBeenQuoted = false; + + while (tok.hasMoreTokens()) { + String nextTok = tok.nextToken(); + switch (state) { + case inQuote: + if ("\'".equals(nextTok)) { + lastTokenHasBeenQuoted = true; + state = normal; + } else { + current.append(nextTok); + } + break; + case inDoubleQuote: + if ("\"".equals(nextTok)) { + lastTokenHasBeenQuoted = true; + state = normal; + } else { + current.append(nextTok); + } + break; + default: + if ("\'".equals(nextTok)) { + state = inQuote; + } else if ("\"".equals(nextTok)) { + state = inDoubleQuote; + } else if (" ".equals(nextTok)) { + if (lastTokenHasBeenQuoted || current.length() != 0) { + v.addElement(current.toString()); + current = new StringBuffer(); + } + } else { + current.append(nextTok); + } + lastTokenHasBeenQuoted = false; + break; + } + } + + if (lastTokenHasBeenQuoted || current.length() != 0) { + v.addElement(current.toString()); + } + + if (state == inQuote || state == inDoubleQuote) { + throw new IllegalArgumentException( + "Unbalanced quotes in " + toProcess); + } + + String[] args = new String[v.size()]; + v.copyInto(args); + return args; + } /** - * Clear out the whole command line. + * size operator. This actually creates the command line, so it is not a + * zero cost operation. + * + * @return number of elements in the command, including the executable */ - void clear(); + public int size() { + return getCommandline().length; + } /** - * Clear out the arguments but leave the executable in place for another - * operation. + * Generate a deep clone of the contained object. + * + * @return a clone of the contained object + * @throws CloneNotSupportedException */ - void clearArgs(); + public Object clone() throws CloneNotSupportedException { + CommandLine c = (CommandLine) super.clone(); + c.arguments = (Vector) arguments.clone(); + return c; + } + + public void clear() { + executable = null; + arguments.removeAllElements(); + } + + public void clearArgs() { + arguments.removeAllElements(); + } } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java Sat Mar 11 11:18:50 2006 @@ -149,7 +149,6 @@ // try to find the executable File executableFile = new File(exec); if (executableFile.exists()) { - return executableFile.getAbsolutePath(); } @@ -201,22 +200,22 @@ *
  • this list is not exhaustive or limitative
  • * */ - public void execute(final String[] cl) throws IOException { + public void execute(final CommandLine cl) throws IOException { execute(cl, null, new LogOutputStream(1), new LogOutputStream(2)); } - public void execute(final String[] cl, final Map env) + public void execute(final CommandLine cl, final Map env) throws IOException { execute(cl, env, new LogOutputStream(1), new LogOutputStream(2)); } - public void execute(final String[] cl, final OutputStream out, + public void execute(final CommandLine cl, final OutputStream out, final OutputStream error) throws IOException { execute(cl, null, out, error); } - public void execute(final String[] cmdl, final Map env, + public void execute(final CommandLine cmdl, final Map env, final OutputStream out, final OutputStream error) throws IOException { File savedDir = dir; // possibly altered in prepareExec @@ -228,8 +227,7 @@ environment = env; } - cmdl[0] = resolveExecutable(cmdl[0], false); - + cmdl.setExecutable(resolveExecutable(executable, false)); checkConfiguration(cmdl); try { @@ -240,7 +238,7 @@ } } - public void execute(final String[] cmdl, final Map env, + public void execute(final CommandLine cmdl, final Map env, final InputStream in, final OutputStream out, final OutputStream error) throws IOException { File savedDir = dir; // possibly altered in prepareExec @@ -252,7 +250,7 @@ environment = env; } - cmdl[0] = resolveExecutable(cmdl[0], false); + cmdl.setExecutable(resolveExecutable(executable, false)); checkConfiguration(cmdl); try { @@ -269,9 +267,9 @@ * @throws ExecuteException * if there are missing required parameters */ - protected void checkConfiguration(final String[] cmdl) + protected void checkConfiguration(final CommandLine cmdl) throws IOException { - if (cmdl.length == 0) { + if (cmdl.getExecutable() == null) { throw new IOException("No executable specified"); } if (dir != null && !dir.exists()) { @@ -352,7 +350,7 @@ // redirector.complete(); if (Execute.isFailure(returnCode)) { - throw new ExecuteException(exe.getCommandline()[0] + throw new ExecuteException(exe.getCommandline().getExecutable() + " failed with return code", returnCode); } } else { @@ -370,7 +368,7 @@ * if the new process could not be started only if * failIfExecFails is set to true (the default) */ - protected void runExec(final Execute exe, final String[] cmdl) + protected void runExec(final Execute exe, final CommandLine cmdl) throws IOException { // show the command log.debug(cmdl.toString()); Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java Sat Mar 11 11:18:50 2006 @@ -40,7 +40,7 @@ /** Invalid exit code. * */ public static final int INVALID = Integer.MAX_VALUE; - private String[] cmdl = null; + private CommandLine cmdl = null; private Map environment = null; @@ -132,7 +132,7 @@ * * @return the command line used to create a subprocess */ - public String[] getCommandline() { + public CommandLine getCommandline() { return cmdl; } @@ -142,7 +142,7 @@ * @param commandline * the command line of the subprocess to launch */ - public void setCommandline(final String[] commandline) { + public void setCommandline(final CommandLine commandline) { cmdl = commandline; } @@ -211,7 +211,7 @@ * @throws IOException * forwarded from the particular launcher used */ - public static Process launch(final String[] command, final Map env, final File dir) + public static Process launch(final CommandLine command, final Map env, final File dir) throws IOException { CommandLauncher launcher = vmLauncher; @@ -419,7 +419,7 @@ * @throws ExecuteException * if the command does not return 0. */ - public static void runCommand(final String[] cmdline) + public static void runCommand(final CommandLine cmdline) throws IOException { try { LOG.debug(cmdline); @@ -428,12 +428,12 @@ exe.setCommandline(cmdline); int retval = exe.execute(); if (isFailure(retval)) { - throw new ExecuteException(cmdline[0] + throw new ExecuteException(cmdline.getExecutable() + " failed with return code", retval); } } catch (java.io.IOException exc) { throw new IOException("Could not launch " - + cmdline[0] + ": " + exc); + + cmdline.getExecutable() + ": " + exc); } } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java Sat Mar 11 11:18:50 2006 @@ -65,6 +65,15 @@ } /** + * @see #ExecuteWatchdog(long) + * @deprecated Use constructor with a long type instead. (1.4.x + * compatibility) + */ + public ExecuteWatchdog(final int timeout) { + this((long) timeout); + } + + /** * Watches the given process and terminates it, if it runs for too long. All * information from the previous run are reset. * Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java Sat Mar 11 11:18:50 2006 @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.Map; +import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.Execute; import org.apache.commons.exec.OS; import org.apache.commons.exec.PumpStreamHandler; @@ -114,39 +115,37 @@ return new BufferedReader(new StringReader(toString(out))); } - protected String[] getProcEnvCommand() { - String[] commandLine; + protected CommandLine getProcEnvCommand() { + CommandLine commandLine = new CommandLine(); if (OS.isFamilyOS2()) { // OS/2 - use same mechanism as Windows 2000 - commandLine = new String[]{"cmd", "/c", "set"}; + commandLine.setExecutable("cmd"); + commandLine.addArguments(new String[] {"/c", "set"}); } else if (OS.isFamilyWindows()) { // Determine if we're running under XP/2000/NT or 98/95 - commandLine = new String[3]; if (OS.isFamilyWin9x()) { - commandLine[0] = "command.com"; + commandLine.setExecutable("command.com"); // Windows 98/95 } else { - commandLine[0] = "cmd"; + commandLine.setExecutable("cmd"); // Windows XP/2000/NT/2003 } - commandLine[1] = "/c"; - commandLine[2] = "set"; + commandLine.addArguments(new String[] {"/c", "set"}); } else if (OS.isFamilyZOS() || OS.isFamilyUnix()) { // On most systems one could use: /bin/sh -c env // Some systems have /bin/env, others /usr/bin/env, just try - commandLine = new String[1]; if (new File("/bin/env").canRead()) { - commandLine[0] = "/bin/env"; + commandLine.setExecutable("/bin/env"); } else if (new File("/usr/bin/env").canRead()) { - commandLine[0] = "/usr/bin/env"; + commandLine.setExecutable("/usr/bin/env"); } else { // rely on PATH - commandLine[0] = "env"; + commandLine.setExecutable("env"); } } else if (OS.isFamilyNetware() || OS.isFamilyOS400()) { // rely on PATH - commandLine = new String[]{"env"}; + commandLine.setExecutable("env"); } else { // MAC OS 9 and previous // TODO: I have no idea how to get it, someone must fix it Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java Sat Mar 11 11:18:50 2006 @@ -23,6 +23,8 @@ import java.util.Iterator; import java.util.Map; +import org.apache.commons.exec.CommandLine; + public class OpenVmsProcessingEnvironment extends DefaultProcessingEnvironment { public synchronized Map getProcEnvironment() throws IOException { @@ -38,8 +40,11 @@ return procEnvironment; } - protected String[] getProcEnvCommand() { - return new String[]{"show", "logical"}; + protected CommandLine getProcEnvCommand() { + CommandLine commandLine = new CommandLine(); + commandLine.setExecutable("show"); + commandLine.addArgument("logical"); + return commandLine; } /** Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java Sat Mar 11 11:18:50 2006 @@ -21,6 +21,9 @@ import java.io.IOException; import java.util.Map; +import org.apache.commons.exec.CommandLine; +import org.apache.commons.exec.environment.EnvironmentUtil; + public interface CommandLauncher { /** @@ -34,7 +37,7 @@ * @throws IOException * if attempting to run a command in a specific directory */ - Process exec(final String[] cmd, final Map env) + Process exec(final CommandLine cmd, final Map env) throws IOException; /** @@ -52,6 +55,6 @@ * @throws IOException * if trying to change directory */ - Process exec(final String[] cmd, final Map env, + Process exec(final CommandLine cmd, final Map env, final File workingDir) throws IOException; } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java Sat Mar 11 11:18:50 2006 @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.Map; +import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.environment.EnvironmentUtil; /** @@ -30,17 +31,17 @@ */ public abstract class CommandLauncherImpl implements CommandLauncher { - public Process exec(final String[] cmd, final Map env) + public Process exec(final CommandLine cmd, final Map env) throws IOException { String[] envVar = null; if(env != null) { envVar = EnvironmentUtil.toStrings(env); } - return Runtime.getRuntime().exec(cmd, + return Runtime.getRuntime().exec(cmd.getCommandline(), envVar); } - public abstract Process exec(final String[] cmd, final Map env, + public abstract Process exec(final CommandLine cmd, final Map env, final File workingDir) throws IOException; } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherProxy.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherProxy.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherProxy.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherProxy.java Sat Mar 11 11:18:50 2006 @@ -20,6 +20,8 @@ import java.io.IOException; import java.util.Map; +import org.apache.commons.exec.CommandLine; + /** * A command launcher that proxies another command launcher. Sub-classes * override exec(args, env, workdir) @@ -43,7 +45,7 @@ * @throws IOException * forwarded from the exec method of the command launcher */ - public Process exec(final String[] cmd, final Map env) + public Process exec(final CommandLine cmd, final Map env) throws IOException { return myLauncher.exec(cmd, env); } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java Sat Mar 11 11:18:50 2006 @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.Map; +import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.environment.EnvironmentUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -48,7 +49,7 @@ * @throws IOException * probably forwarded from Runtime#exec */ - public Process exec(final String[] cmd, final Map env, + public Process exec(final CommandLine cmd, final Map env, final File workingDir) throws IOException { log.debug("Execute:Java13CommandLauncher: " + cmd); @@ -57,7 +58,7 @@ envVars = EnvironmentUtil.toStrings(env); } - return Runtime.getRuntime().exec(cmd, + return Runtime.getRuntime().exec(cmd.getCommandline(), envVars, workingDir); } } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java Sat Mar 11 11:18:50 2006 @@ -21,6 +21,8 @@ import java.io.IOException; import java.util.Map; +import org.apache.commons.exec.CommandLine; + /** * A command launcher for OS/2 that uses 'cmd.exe' when launching commands in * directories other than the current working directory. @@ -48,20 +50,16 @@ * @throws IOException * forwarded from the exec method of the command launcher */ - public Process exec(final String[] cmd, final Map env, + public Process exec(final CommandLine cmd, final Map env, final File workingDir) throws IOException { if (workingDir == null) { return exec(cmd, env); } - // TODO add code for switching to working dir - String[] newCmd = new String[cmd.length + 2]; - newCmd[0] = "cmd"; - newCmd[1] = "/c"; - - for (int i = 0; i < cmd.length; i++) { - newCmd[i + 2] = cmd[i]; - } + CommandLine newCmd = new CommandLine(); + newCmd.setExecutable("cmd"); + newCmd.addArgument("/c"); + newCmd.addArguments(cmd.getCommandline()); return exec(newCmd, env); } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java Sat Mar 11 11:18:50 2006 @@ -26,6 +26,8 @@ import java.util.Set; import java.util.Map.Entry; +import org.apache.commons.exec.CommandLine; + /** * A command launcher for VMS that writes the command to a temporary DCL script * before launching commands. This is due to limitations of both the DCL @@ -36,10 +38,10 @@ /** * Launches the given command in a new process. */ - public Process exec(final String[] cmd, final Map env) + public Process exec(final CommandLine cmd, final Map env) throws IOException { - String[] vmsCmd = new String[1]; - vmsCmd[0] = createCommandFile(cmd, env).getPath(); + CommandLine vmsCmd = new CommandLine(); + vmsCmd.setExecutable(createCommandFile(cmd, env).getPath()); return super.exec(vmsCmd, env); } @@ -50,10 +52,10 @@ * only works if workingDir is null or the logical * JAVA$FORK_SUPPORT_CHDIR needs to be set to TRUE. */ - public Process exec(final String[] cmd, final Map env, + public Process exec(final CommandLine cmd, final Map env, final File workingDir) throws IOException { - String[] vmsCmd = new String[1]; - vmsCmd[0] = createCommandFile(cmd, env).getPath(); + CommandLine vmsCmd = new CommandLine(); + vmsCmd.setExecutable(createCommandFile(cmd, env).getPath()); return super.exec(vmsCmd, env, workingDir); } @@ -62,9 +64,9 @@ * Writes the command into a temporary DCL script and returns the * corresponding File object. The script will be deleted on exit. */ - private File createCommandFile(final String[] cmd, final Map env) + private File createCommandFile(final CommandLine cmd, final Map env) throws IOException { - File script = File.createTempFile("EXEC", ".COM"); + File script = File.createTempFile("ANT", ".COM"); script.deleteOnExit(); PrintWriter out = null; try { @@ -84,15 +86,11 @@ } } - if(cmd.length == 0) { - throw new IOException("Can not execute empty command"); - } else { - out.print("$ " + cmd[0]); - - for (int i = 1; i < cmd.length; i++) { - out.println(" -"); - out.print(cmd[i]); - } + out.print("$ " + cmd.getExecutable()); + String[] args = cmd.getArguments(); + for (int i = 0; i < args.length; i++) { + out.println(" -"); + out.print(args[i]); } } finally { if (out != null) { Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java Sat Mar 11 11:18:50 2006 @@ -21,6 +21,8 @@ import java.io.IOException; import java.util.Map; +import org.apache.commons.exec.CommandLine; + /** * A command launcher for Windows XP/2000/NT that uses 'cmd.exe' when launching * commands in directories other than the current working directory. @@ -43,7 +45,7 @@ * @throws IOException * forwarded from the exec method of the command launcher */ - public Process exec(final String[] cmd, final Map env, + public Process exec(final CommandLine cmd, final Map env, final File workingDir) throws IOException { if (workingDir == null) { return exec(cmd, env); @@ -51,15 +53,10 @@ // Use cmd.exe to change to the specified directory before running // the command - // TODO add code for switching to working dir - - String[] newCmd = new String[cmd.length + 2]; - newCmd[0] = "cmd"; - newCmd[1] = "/c"; - - for (int i = 0; i < cmd.length; i++) { - newCmd[i + 2] = cmd[i]; - } + CommandLine newCmd = new CommandLine(); + newCmd.setExecutable("cmd"); + newCmd.addArgument("/c"); + newCmd.addArguments(cmd.getCommandline()); return exec(newCmd, env); } Copied: jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java (from r377093, jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java) URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java?p2=jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java&p1=jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java&r1=377093&r2=385141&rev=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java Sat Mar 11 11:18:50 2006 @@ -22,13 +22,13 @@ public class CommandLineTest extends TestCase { public void testSetExecutable() { - CommandLine cmdl = new CommandLineImpl(); + CommandLine cmdl = new CommandLine(); cmdl.setExecutable("test"); assertEquals("test", cmdl.toString()); } public void testSetArguments() { - CommandLine cmdl = new CommandLineImpl(); + CommandLine cmdl = new CommandLine(); cmdl.setExecutable("test"); cmdl.addArgument("foo"); cmdl.addArgument("bar"); @@ -36,7 +36,7 @@ } public void testSetArgumentsWithSpace() { - CommandLine cmdl = new CommandLineImpl(); + CommandLine cmdl = new CommandLine(); cmdl.setExecutable("test"); cmdl.addArgument("foo"); cmdl.addArgument("ba r"); @@ -44,7 +44,7 @@ } public void testSetArgumentsWithQuote() { - CommandLine cmdl = new CommandLineImpl(); + CommandLine cmdl = new CommandLine(); cmdl.setExecutable("test"); cmdl.addArgument("foo"); cmdl.addArgument("ba\"r"); @@ -52,7 +52,7 @@ } public void testSetArgumentsWithSingleQuote() { - CommandLine cmdl = new CommandLineImpl(); + CommandLine cmdl = new CommandLine(); cmdl.setExecutable("test"); cmdl.addArgument("foo"); cmdl.addArgument("ba'r"); Modified: jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java Sat Mar 11 11:18:50 2006 @@ -18,7 +18,6 @@ package org.apache.commons.exec; import java.io.ByteArrayOutputStream; -import java.io.File; import java.util.HashMap; import java.util.Map; @@ -37,7 +36,8 @@ public void testExecute() throws Exception { Exec exec = new Exec(); - String[] cl = new String[]{new File(testScript).getAbsolutePath()}; + CommandLine cl = new CommandLine(); + cl.setExecutable(testScript); exec.execute(cl, baos, baos); @@ -47,7 +47,9 @@ public void testExecuteWithArg() throws Exception { Exec exec = new Exec(); - String[] cl = new String[]{testScript, "BAR"}; + CommandLine cl = new CommandLine(); + cl.setExecutable(testScript); + cl.addArgument("BAR"); exec.execute(cl, baos, baos); assertEquals("FOO..BAR", baos.toString().trim()); @@ -57,7 +59,8 @@ Map env = new HashMap(); env.put("TEST_ENV_VAR", "XYZ"); - String[] cl = new String[]{testScript}; + CommandLine cl = new CommandLine(); + cl.setExecutable(testScript); Exec exec = new Exec(); Modified: jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/TestUtil.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/TestUtil.java?rev=385141&r1=385140&r2=385141&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/TestUtil.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/TestUtil.java Sat Mar 11 11:18:50 2006 @@ -17,7 +17,6 @@ package org.apache.commons.exec; -import java.io.File; import java.util.Arrays; import junit.framework.AssertionFailedError; @@ -29,17 +28,13 @@ } public static String resolveScriptForOS(String script) { - String scriptFileName; - if (OS.isFamilyWindows()) { - scriptFileName = script + ".bat"; + return script + ".bat"; } else if (OS.isFamilyUnix()) { - scriptFileName = script + ".sh"; + return script + ".sh"; } else { throw new AssertionFailedError("Test not supported for this OS"); } - - return new File(scriptFileName).getAbsolutePath(); } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org