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.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -55,10 +56,14 @@ public class DaemonParameters {
protected final Map<Path, Properties> mvndProperties = new ConcurrentHashMap<>();
protected final Function<Path, Properties> provider = path -> mvndProperties.computeIfAbsent(path,
p -> loadProperties(path));
protected final Properties properties;
private final Map<String, String> properties;
public DaemonParameters(Properties properties) {
this.properties = properties;
public DaemonParameters() {
this.properties = Collections.emptyMap();
}
protected DaemonParameters(PropertiesBuilder propertiesBuilder) {
this.properties = propertiesBuilder.build();
}
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}
*/
public DaemonParameters cd(Path newUserDir) {
Properties properties = new Properties();
properties.putAll(this.properties);
properties.put(Environment.USER_DIR.getProperty(), newUserDir.toString());
return new DaemonParameters(properties);
return new DaemonParameters(new PropertiesBuilder()
.putAll(this.properties)
.put(Environment.USER_DIR.getProperty(), newUserDir.toString()));
}
public int keepAliveMs() {
@@ -282,7 +286,7 @@ public class DaemonParameters {
protected EnvValue value(Environment env) {
return new EnvValue(env, new ValueSource(
description -> description.append("value: ").append(env.getProperty()),
() -> properties.getProperty(env.getProperty())));
() -> properties.get(env.getProperty())));
}
public static EnvValue systemProperty(Environment env) {
@@ -354,6 +358,35 @@ public class DaemonParameters {
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.
*/

View File

@@ -22,7 +22,6 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.Supplier;
import org.fusesource.jansi.Ansi;
import org.jboss.fuse.mvnd.common.BuildProperties;
@@ -69,7 +68,7 @@ public class DefaultClient implements Client {
}
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;
import java.io.IOException;
import javax.inject.Inject;
import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.DaemonParameters;
import org.jboss.fuse.mvnd.common.logging.ClientOutput;

View File

@@ -16,7 +16,6 @@
package org.jboss.fuse.mvnd.junit;
import java.nio.file.Path;
import java.util.Properties;
import org.jboss.fuse.mvnd.client.DaemonParameters;
import org.jboss.fuse.mvnd.common.Environment;
@@ -28,27 +27,21 @@ public class TestParameters extends DaemonParameters {
Path multiModuleProjectDirectory,
Path javaHome, Path localMavenRepository, Path settings, Path logbackConfigurationPath,
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;
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() {