Merge branch 'dryrun-with-spaces'

This commit is contained in:
Joakim Erdfelt 2012-01-30 15:51:19 -07:00
commit f02244fbf6
4 changed files with 179 additions and 12 deletions

View File

@ -0,0 +1,124 @@
package org.eclipse.jetty.start;
import java.util.ArrayList;
import java.util.List;
public class CommandLineBuilder
{
private List<String> args;
public CommandLineBuilder(String bin)
{
args = new ArrayList<String>();
args.add(bin);
}
/**
* Add a simple argument to the command line.
* <p>
* Will quote arguments that have a space in them.
*
* @param arg
* the simple argument to add
*/
public void addArg(String arg)
{
args.add(quote(arg));
}
/**
* Similar to {@link #addArg(String)} but concats both name + value with an "=" sign, quoting were needed, and excluding the "=" portion if the value is
* undefined or empty.
* <p>
*
* <pre>
* addEqualsArg("-Dname", "value") = "-Dname=value"
* addEqualsArg("-Djetty.home", "/opt/company inc/jetty (7)/") = "-Djetty.home=/opt/company\ inc/jetty\ (7)/"
* addEqualsArg("-Djenkins.workspace", "/opt/workspaces/jetty jdk7/") = "-Djenkins.workspace=/opt/workspaces/jetty\ jdk7/"
* addEqualsArg("-Dstress", null) = "-Dstress"
* addEqualsArg("-Dstress", "") = "-Dstress"
* </pre>
*
* @param name
* the name
* @param value
* the value
*/
public void addEqualsArg(String name, String value)
{
if (value != null && value.length() > 0)
{
args.add(quote(name + "=" + value));
}
else
{
args.add(quote(name));
}
}
/**
* Add a simple argument to the command line.
* <p>
* Will <b>NOT</b> quote/escape arguments that have a space in them.
*
* @param arg
* the simple argument to add
*/
public void addRawArg(String arg)
{
args.add(arg);
}
public List<String> getArgs()
{
return args;
}
/**
* Perform an optional quoting of the argument, being intelligent with spaces and quotes as needed.
*
* @param arg
* @return
*/
public static String quote(String arg)
{
boolean needsQuoting = arg.indexOf(' ') >= 0 || arg.indexOf('"') >= 0;
if (!needsQuoting)
{
return arg;
}
StringBuilder buf = new StringBuilder();
// buf.append('"');
boolean escaped = false;
for (char c : arg.toCharArray())
{
if (!escaped && ((c == '"') || (c == ' ')))
{
buf.append("\\");
}
escaped = (c == '\\');
buf.append(c);
}
// buf.append('"');
return buf.toString();
}
@Override
public String toString()
{
StringBuilder buf = new StringBuilder();
boolean delim = false;
for (String arg : args)
{
if (delim)
{
buf.append(' ');
}
buf.append(arg);
delim = true;
}
return buf.toString();
}
}

View File

@ -661,20 +661,21 @@ public class Main
String buildCommandLine(Classpath classpath, List<String> xmls) throws IOException
{
StringBuilder cmd = new StringBuilder();
cmd.append(findJavaBin());
CommandLineBuilder cmd = new CommandLineBuilder(findJavaBin());
for (String x : _jvmArgs)
cmd.append(' ').append(x);
cmd.append(" -Djetty.home=").append(_jettyHome);
{
cmd.addArg(x);
}
cmd.addEqualsArg("-Djetty.home",_jettyHome);
for (String p : _sysProps)
{
cmd.append(" -D").append(p);
String v = System.getProperty(p);
if (v != null && v.length() > 0)
cmd.append('=').append(v);
cmd.addEqualsArg("-D" + p,v);
}
cmd.append(" -cp ").append(classpath.toString());
cmd.append(" ").append(_config.getMainClassname());
cmd.addArg("-cp");
cmd.addArg(classpath.toString());
cmd.addRawArg(_config.getMainClassname());
// Check if we need to pass properties as a file
Properties properties = Config.getProperties();
@ -684,12 +685,12 @@ public class Main
if (!_dryRun)
prop_file.deleteOnExit();
properties.store(new FileOutputStream(prop_file),"start.jar properties");
cmd.append(" ").append(prop_file.getAbsolutePath());
cmd.addArg(prop_file.getAbsolutePath());
}
for (String xml : xmls)
{
cmd.append(' ').append(xml);
cmd.addArg(xml);
}
return cmd.toString();

View File

@ -0,0 +1,42 @@
package org.eclipse.jetty.start;
import static org.hamcrest.Matchers.*;
import org.junit.Assert;
import org.junit.Test;
public class CommandLineBuilderTest
{
@Test
public void testSimpleCommandline()
{
CommandLineBuilder cmd = new CommandLineBuilder("java");
cmd.addEqualsArg("-Djava.io.tmpdir","/home/java/temp dir/");
cmd.addArg("--version");
Assert.assertThat(cmd.toString(), is("java -Djava.io.tmpdir=/home/java/temp\\ dir/ --version"));
}
@Test
public void testQuotingSimple()
{
assertQuoting("/opt/jetty","/opt/jetty");
}
@Test
public void testQuotingSpaceInPath()
{
assertQuoting("/opt/jetty 7/home","/opt/jetty\\ 7/home");
}
@Test
public void testQuotingSpaceAndQuotesInPath()
{
assertQuoting("/opt/jetty 7 \"special\"/home","/opt/jetty\\ 7\\ \\\"special\\\"/home");
}
private void assertQuoting(String raw, String expected)
{
String actual = CommandLineBuilder.quote(raw);
Assert.assertThat("Quoted version of [" + raw + "]",actual,is(expected));
}
}

View File

@ -20,7 +20,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j-version>1.6.1</slf4j-version>
<build-support-version>1.1</build-support-version>
<jetty.test.helper>1.6</jetty.test.helper>
<jetty.test.helper>1.6.1</jetty.test.helper>
<jetty.test.policy>1.2</jetty.test.policy>
</properties>
<scm>