Skip to content

Commit

Permalink
Merged pull request #781
Browse files Browse the repository at this point in the history
[WFCORE-723] Allow multiple server arguments with the same key.
  • Loading branch information
jamezp committed Jun 5, 2015
2 parents 8da45b5 + 196b398 commit e92a553
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 182 deletions.
Expand Up @@ -391,7 +391,7 @@ public T setAdminOnly() {
* @return the builder
*/
public T setBindAddressHint(final String address) {
addServerArg("-b", address);
setSingleServerArg("-b", address);
return getThis();
}

Expand All @@ -415,7 +415,7 @@ public T setBindAddressHint(final String interfaceName, final String address) {
if (interfaceName == null) {
throw LauncherMessages.MESSAGES.nullParam("interfaceName");
}
addServerArg("-b" + interfaceName, address);
setSingleServerArg("-b" + interfaceName, address);
return getThis();
}

Expand All @@ -432,14 +432,53 @@ public T setBindAddressHint(final String interfaceName, final String address) {
* @return the builder
*/
public T setMulticastAddressHint(final String address) {
addServerArg("-u", address);
setSingleServerArg("-u", address);
return getThis();
}

/**
* Sets the properties file to use for the server or {@code null} to remove the file. The file must exist.
* <p/>
* This will override any previous value set via {@link #addServerArgument(String)}..
* Adds a properties file to be passed to the server. Note that the file must exist.
*
* @param file the file to add
*
* @return the builder
*
* @throws java.lang.IllegalArgumentException if the file does not exist or is not a regular file
*/
public T addPropertiesFile(final String file) {
if (file != null) {
addPropertiesFile(Paths.get(file));
}
return getThis();
}

/**
* Adds a properties file to be passed to the server. Note that the file must exist.
*
* @param file the file to add
*
* @return the builder
*
* @throws java.lang.IllegalArgumentException if the file does not exist or is not a regular file
*/
public T addPropertiesFile(final Path file) {
if (file != null) {
if (Files.notExists(file)) {
throw LauncherMessages.MESSAGES.pathDoesNotExist(file);
}
if (!Files.isRegularFile(file)) {
throw LauncherMessages.MESSAGES.pathNotAFile(file);
}
addServerArg("-P", file.toAbsolutePath().normalize().toString());
}
return getThis();
}

/**
* Sets the properties file to use for the server or {@code null} to remove the file. Note that the file must exist.
* <p>
* This will override any previous values set.
* </p>
*
* @param file the properties file to use or {@code null}
*
Expand All @@ -458,9 +497,10 @@ public T setPropertiesFile(final String file) {
}

/**
* Sets the properties file to use for the server or {@code null} to remove the file. The file must exist.
* <p/>
* This will override any previous value set via {@link #addServerArgument(String)}..
* Sets the properties file to use for the server or {@code null} to remove the file. Note that the file must exist.
* <p>
* This will override any previous values set.
* </p>
*
* @param file the properties file to use or {@code null}
*
Expand All @@ -469,18 +509,8 @@ public T setPropertiesFile(final String file) {
* @throws java.lang.IllegalArgumentException if the file does not exist or is not a regular file
*/
public T setPropertiesFile(final Path file) {
if (file == null) {
addServerArg("-P", null);
} else {
if (Files.notExists(file)) {
throw LauncherMessages.MESSAGES.pathDoesNotExist(file);
}
if (!Files.isRegularFile(file)) {
throw LauncherMessages.MESSAGES.pathNotAFile(file);
}
addServerArg("-P", file.toAbsolutePath().normalize().toString());
}
return getThis();
serverArgs.remove("-P");
return addPropertiesFile(file);
}

/**
Expand Down Expand Up @@ -611,10 +641,21 @@ public List<String> getServerArguments() {
*/
protected abstract T getThis();

protected void setSingleServerArg(final String key, final String value) {
serverArgs.set(key, value);
}

protected void addServerArg(final String key, final String value) {
serverArgs.add(key, value);
}

/**
* Returns the first argument from the server arguments.
*
* @param key the key to the argument
*
* @return the first argument or {@code null}
*/
protected String getServerArg(final String key) {
return serverArgs.get(key);
}
Expand Down
86 changes: 76 additions & 10 deletions launcher/src/main/java/org/wildfly/core/launcher/Arguments.java
Expand Up @@ -23,6 +23,8 @@
package org.wildfly.core.launcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -34,7 +36,7 @@
*/
class Arguments {

private final Map<String, Argument> map;
private final Map<String, Collection<Argument>> map;

Arguments() {
this.map = new LinkedHashMap<>();
Expand All @@ -56,7 +58,7 @@ public void clear() {
public void add(final String arg) {
if (arg != null) {
final Argument argument = parse(arg);
map.put(argument.getKey(), argument);
add(argument);
}
}

Expand Down Expand Up @@ -85,7 +87,33 @@ public void add(final String key, final String value) {
argument = create(key, value);
}
}
map.put(key, argument);
add(argument);
}
}

/**
* Sets a key/value pair to the collection of arguments. This guarantees only one value will be assigned to the
* argument key. A {@code null} value indicates the key should be removed.
* <p/>
* If the key starts with {@code -D} it's assumed it's a system property argument and the prefix will be stripped
* from the key when checking for uniqueness.
*
* @param key the key for the argument
* @param value the value of the argument which may be {@code null}
*/
public void set(final String key, final String value) {
if (key != null) {
if (value == null) {
map.remove(key);
} else {
final Argument argument;
if (key.startsWith("-D")) {
argument = createSystemProperty(key, value);
} else {
argument = create(key, value);
}
map.put(argument.getKey(), Collections.singleton(argument));
}
}
}

Expand All @@ -105,29 +133,57 @@ public void addAll(final String... args) {
}

/**
* Gets the value for the key.
* Gets the first value for the key.
*
* @param key the key to check for the value
*
* @return the value or {@code null} if the key is not found or the value was {@code null}
*/
public String get(final String key) {
final Argument arg = map.get(key);
if (arg != null) {
return arg.getValue();
final Collection<Argument> args = map.get(key);
if (args != null) {
return args.iterator().hasNext() ? args.iterator().next().getValue() : null;
}
return null;
}

/**
* Gets the value for the key.
*
* @param key the key to check for the value
*
* @return the value or an empty collection if no values were set
*/
public Collection<Argument> getArguments(final String key) {
final Collection<Argument> args = map.get(key);
if (args != null) {
return new ArrayList<>(args);
}
return Collections.emptyList();
}

/**
* Removes the argument from the collection of arguments.
*
* @param key they key of the argument to remove
*
* @return the arguments or {@code null} if the argument was not found
*/
public Collection<Argument> remove(final String key) {
return map.remove(key);
}

/**
* Returns the arguments as a list in their command line form.
*
* @return the arguments for the command line
*/
public List<String> asList() {
final List<String> result = new ArrayList<>();
for (Argument arg : map.values()) {
result.add(arg.asCommandLineArgument());
for (Collection<Argument> args : map.values()) {
for (Argument arg : args) {
result.add(arg.asCommandLineArgument());
}
}
return result;
}
Expand All @@ -139,7 +195,12 @@ public List<String> asList() {
*/
void add(final Argument argument) {
if (argument != null) {
map.put(argument.getKey(), argument);
Collection<Argument> arguments = map.get(argument.getKey());
if (arguments == null) {
arguments = new ArrayList<>();
map.put(argument.getKey(), arguments);
}
arguments.add(argument);
}
}

Expand Down Expand Up @@ -243,6 +304,11 @@ public String getValue() {
* @return the command line argument
*/
public abstract String asCommandLineArgument();

@Override
public String toString() {
return asCommandLineArgument();
}
}


Expand Down

0 comments on commit e92a553

Please sign in to comment.