Make DaemonParameters immutable

This commit is contained in:
Peter Palaga
2020-11-06 19:40:02 +01:00
parent 2a0ed8f554
commit 838691641b
4 changed files with 55 additions and 32 deletions

View File

@@ -24,6 +24,7 @@ import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@@ -55,10 +56,14 @@ public class DaemonParameters {
protected final Map<Path, Properties> mvndProperties = new ConcurrentHashMap<>(); protected final Map<Path, Properties> mvndProperties = new ConcurrentHashMap<>();
protected final Function<Path, Properties> provider = path -> mvndProperties.computeIfAbsent(path, protected final Function<Path, Properties> provider = path -> mvndProperties.computeIfAbsent(path,
p -> loadProperties(path)); p -> loadProperties(path));
protected final Properties properties; private final Map<String, String> properties;
public DaemonParameters(Properties properties) { public DaemonParameters() {
this.properties = properties; this.properties = Collections.emptyMap();
}
protected DaemonParameters(PropertiesBuilder propertiesBuilder) {
this.properties = propertiesBuilder.build();
} }
public List<String> getDaemonOpts() { public List<String> getDaemonOpts() {
@@ -239,10 +244,9 @@ public class DaemonParameters {
* @return a new {@link DaemonParameters} with {@code userDir} set to the given {@code newUserDir} * @return a new {@link DaemonParameters} with {@code userDir} set to the given {@code newUserDir}
*/ */
public DaemonParameters cd(Path newUserDir) { public DaemonParameters cd(Path newUserDir) {
Properties properties = new Properties(); return new DaemonParameters(new PropertiesBuilder()
properties.putAll(this.properties); .putAll(this.properties)
properties.put(Environment.USER_DIR.getProperty(), newUserDir.toString()); .put(Environment.USER_DIR.getProperty(), newUserDir.toString()));
return new DaemonParameters(properties);
} }
public int keepAliveMs() { public int keepAliveMs() {
@@ -282,7 +286,7 @@ public class DaemonParameters {
protected EnvValue value(Environment env) { protected EnvValue value(Environment env) {
return new EnvValue(env, new ValueSource( return new EnvValue(env, new ValueSource(
description -> description.append("value: ").append(env.getProperty()), description -> description.append("value: ").append(env.getProperty()),
() -> properties.getProperty(env.getProperty()))); () -> properties.get(env.getProperty())));
} }
public static EnvValue systemProperty(Environment env) { public static EnvValue systemProperty(Environment env) {
@@ -354,6 +358,35 @@ public class DaemonParameters {
return result; return result;
} }
public static class PropertiesBuilder {
private Map<String, String> properties = new LinkedHashMap<>();
public PropertiesBuilder put(String key, String value) {
properties.put(key, value);
return this;
}
public PropertiesBuilder put(Environment envKey, Object value) {
if (value == null) {
properties.remove(envKey.getProperty());
} else {
properties.put(envKey.getProperty(), value.toString());
}
return this;
}
public PropertiesBuilder putAll(Map<String, String> props) {
properties.putAll(props);
return this;
}
public Map<String, String> build() {
Map<String, String> props = properties;
properties = null;
return Collections.unmodifiableMap(props);
}
}
/** /**
* A source of an environment value with a description capability. * A source of an environment value with a description capability.
*/ */

View File

@@ -22,7 +22,6 @@ import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
import org.jboss.fuse.mvnd.common.BuildProperties; import org.jboss.fuse.mvnd.common.BuildProperties;
@@ -69,7 +68,7 @@ public class DefaultClient implements Client {
} }
try (TerminalOutput output = new TerminalOutput(logFile)) { try (TerminalOutput output = new TerminalOutput(logFile)) {
new DefaultClient(() -> new DaemonParameters(new Properties())).execute(output, args); new DefaultClient(() -> new DaemonParameters()).execute(output, args);
} }
} }

View File

@@ -16,9 +16,7 @@
package org.jboss.fuse.mvnd.it; package org.jboss.fuse.mvnd.it;
import java.io.IOException; import java.io.IOException;
import javax.inject.Inject; import javax.inject.Inject;
import org.jboss.fuse.mvnd.client.Client; import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.DaemonParameters; import org.jboss.fuse.mvnd.client.DaemonParameters;
import org.jboss.fuse.mvnd.common.logging.ClientOutput; import org.jboss.fuse.mvnd.common.logging.ClientOutput;

View File

@@ -16,7 +16,6 @@
package org.jboss.fuse.mvnd.junit; package org.jboss.fuse.mvnd.junit;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Properties;
import org.jboss.fuse.mvnd.client.DaemonParameters; import org.jboss.fuse.mvnd.client.DaemonParameters;
import org.jboss.fuse.mvnd.common.Environment; import org.jboss.fuse.mvnd.common.Environment;
@@ -28,27 +27,21 @@ public class TestParameters extends DaemonParameters {
Path multiModuleProjectDirectory, Path multiModuleProjectDirectory,
Path javaHome, Path localMavenRepository, Path settings, Path logbackConfigurationPath, Path javaHome, Path localMavenRepository, Path settings, Path logbackConfigurationPath,
int idleTimeout, int keepAlive, int maxLostKeepAlive) { int idleTimeout, int keepAlive, int maxLostKeepAlive) {
super(new Properties()); super(new PropertiesBuilder().put(Environment.MVND_PROPERTIES_PATH, mvndPropertiesPath)
.put(Environment.MVND_HOME, mavenHome)
.put(Environment.USER_HOME, userHome)
.put(Environment.USER_DIR, userDir)
.put(Environment.MAVEN_MULTIMODULE_PROJECT_DIRECTORY, multiModuleProjectDirectory)
.put(Environment.JAVA_HOME, javaHome)
.put(Environment.MAVEN_REPO_LOCAL, localMavenRepository)
.put(Environment.MAVEN_SETTINGS, settings)
.put(Environment.LOGBACK_CONFIGURATION_FILE, logbackConfigurationPath)
.put(Environment.DAEMON_IDLE_TIMEOUT_MS, idleTimeout)
.put(Environment.DAEMON_KEEP_ALIVE_MS, keepAlive)
.put(Environment.DAEMON_MAX_LOST_KEEP_ALIVE, maxLostKeepAlive)
.put(Environment.MVND_MIN_THREADS, TEST_MIN_THREADS));
this.testDir = testDir; this.testDir = testDir;
put(Environment.MVND_PROPERTIES_PATH, mvndPropertiesPath);
put(Environment.MVND_HOME, mavenHome);
put(Environment.USER_HOME, userHome);
put(Environment.USER_DIR, userDir);
put(Environment.MAVEN_MULTIMODULE_PROJECT_DIRECTORY, multiModuleProjectDirectory);
put(Environment.JAVA_HOME, javaHome);
put(Environment.MAVEN_REPO_LOCAL, localMavenRepository);
put(Environment.MAVEN_SETTINGS, settings);
put(Environment.LOGBACK_CONFIGURATION_FILE, logbackConfigurationPath);
put(Environment.DAEMON_IDLE_TIMEOUT_MS, idleTimeout);
put(Environment.DAEMON_KEEP_ALIVE_MS, keepAlive);
put(Environment.DAEMON_MAX_LOST_KEEP_ALIVE, maxLostKeepAlive);
put(Environment.MVND_MIN_THREADS, TEST_MIN_THREADS);
}
private void put(Environment env, Object value) {
if (value != null) {
this.properties.put(env.getProperty(), value.toString());
}
} }
public Path getTestDir() { public Path getTestDir() {