Skip to content

Commit 35163f8

Browse files
committed
Add Java-side support for -D arguments
In most cases, there is no need to parse these on the native side; Java can handle it and set the relevant system property. It is only an issue if the system property needs to be set before the JVM starts up for some reason. There are not too many of these (though java.library.path comes to mind...).
1 parent 9a343ec commit 35163f8

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.console;
33+
34+
import java.util.LinkedList;
35+
import java.util.regex.Matcher;
36+
import java.util.regex.Pattern;
37+
38+
import org.scijava.plugin.Plugin;
39+
40+
/**
41+
* Handles the {@code -D} command line argument, in an analogous way to tools
42+
* such as Java and Maven.
43+
*
44+
* @author Curtis Rueden
45+
*/
46+
@Plugin(type = ConsoleArgument.class)
47+
public class SystemPropertyArgument extends AbstractConsoleArgument {
48+
49+
private static final String SYS_PROP_REGEX = "-D([\\w\\._-]+)(=(.*))?";
50+
private static final Pattern SYS_PROP_PAT = Pattern.compile(SYS_PROP_REGEX);
51+
52+
// -- ConsoleArgument methods --
53+
54+
@Override
55+
public void handle(final LinkedList<String> args) {
56+
if (!supports(args)) return;
57+
58+
final String arg = args.removeFirst();
59+
final Matcher m = SYS_PROP_PAT.matcher(arg);
60+
if (!m.matches()) {
61+
throw new IllegalArgumentException("Invalid argument: " + arg);
62+
}
63+
final String key = m.group(1);
64+
final String value = m.group(3);
65+
System.setProperty(key, value == null ? "" : value);
66+
}
67+
68+
// -- Typed methods --
69+
70+
@Override
71+
public boolean supports(final LinkedList<String> args) {
72+
if (args == null || args.isEmpty()) return false;
73+
final String arg = args.getFirst();
74+
if (!arg.startsWith("-D")) return false;
75+
return SYS_PROP_PAT.matcher(arg).matches();
76+
}
77+
78+
79+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.console;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNull;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.util.LinkedList;
39+
40+
import org.junit.Test;
41+
42+
/**
43+
* Tests {@link SystemPropertyArgument}.
44+
*
45+
* @author Curtis Rueden
46+
*/
47+
public class SystemPropertyArgumentTest {
48+
49+
@Test
50+
public void testSystemProperties() {
51+
assertPropertySet("foo", "bar");
52+
assertPropertySet("prop.with.dots", "true");
53+
assertPropertySet("prop-with-dashes", "whiz");
54+
assertPropertySet("prop_with_underscores", "bang");
55+
assertPropertySet("empty.value", "");
56+
assertPropertySet("unspecified.value", null);
57+
assertPropertySet("_-_", "legal");
58+
assertPropertySet("-_-", "also legal");
59+
}
60+
61+
private void assertPropertySet(final String key, final String value) {
62+
final SystemPropertyArgument spa = new SystemPropertyArgument();
63+
final LinkedList<String> args = new LinkedList<String>();
64+
args.add(value == null ? "-D" + key : "-D" + key + "=" + value);
65+
assertTrue(spa.supports(args));
66+
assertNull(System.getProperty(key));
67+
spa.handle(args);
68+
assertTrue(args.isEmpty());
69+
assertEquals(value == null ? "" : value, System.getProperty(key));
70+
}
71+
72+
}

0 commit comments

Comments
 (0)