Add loader.args to PropertiesLauncher

Also new section of README for propery keys used by the
launcher.
This commit is contained in:
Dave Syer
2014-01-27 11:48:19 +00:00
parent 8d05cd1fb6
commit 91998d5942
3 changed files with 67 additions and 4 deletions

View File

@@ -79,7 +79,8 @@ public class PropertiesLauncher extends Launcher {
private final Logger logger = Logger.getLogger(Launcher.class.getName());
/**
* Properties key for main class
* Properties key for main class. As a manifest entry can also be specified as
* <code>Start-Class</code>.
*/
public static final String MAIN = "loader.main";
@@ -93,10 +94,16 @@ public class PropertiesLauncher extends Launcher {
* Properties key for home directory. This is the location of external configuration
* if not on classpath, and also the base path for any relative paths in the
* {@link #PATH loader path}. Defaults to current working directory (
* <code>${user.home}</code>).
* <code>${user.dir}</code>).
*/
public static final String HOME = "loader.home";
/**
* Properties key for default command line arguments. These arguments (if present) are
* prepended to the main method arguments before launching.
*/
public static final String ARGS = "loader.args";
/**
* Properties key for name of external configuration file (excluding suffix). Defaults
* to "application". Ignored if {@link #CONFIG_LOCATION loader config location} is
@@ -314,6 +321,19 @@ public class PropertiesLauncher extends Launcher {
return paths;
}
protected String[] getArgs(String... args) throws Exception {
String loaderArgs = getProperty(ARGS);
if (loaderArgs != null) {
String[] defaultArgs = loaderArgs.split("\\s+");
String[] additionalArgs = args;
args = new String[defaultArgs.length + additionalArgs.length];
System.arraycopy(defaultArgs, 0, args, 0, defaultArgs.length);
System.arraycopy(additionalArgs, 0, args, defaultArgs.length,
additionalArgs.length);
}
return args;
}
@Override
protected String getMainClass() throws Exception {
String mainClass = getProperty(MAIN, "Start-Class");
@@ -542,8 +562,10 @@ public class PropertiesLauncher extends Launcher {
return path;
}
public static void main(String[] args) {
new PropertiesLauncher().launch(args);
public static void main(String[] args) throws Exception {
PropertiesLauncher launcher = new PropertiesLauncher();
args = launcher.getArgs(args);
launcher.launch(args);
}
public static String toCamelCase(CharSequence string) {

View File

@@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import org.junit.After;
@@ -147,6 +148,13 @@ public class PropertiesLauncherTests {
assertEquals("demo.Application", System.getProperty("loader.main"));
}
@Test
public void testArgsEnhanced() throws Exception {
System.setProperty("loader.args", "foo");
PropertiesLauncher launcher = new PropertiesLauncher();
assertEquals("[foo, bar]", Arrays.asList(launcher.getArgs("bar")).toString());
}
private void waitFor(String value) throws Exception {
int count = 0;
boolean timeout = false;