Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactorings #192

Merged
merged 4 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public DaemonClientConnection(DaemonConnection connection, DaemonInfo daemon,
this.parameters = parameters;
}

public DaemonInfo getDaemon() {
return daemon;
}

public void dispatch(Message message) throws DaemonException.ConnectException {
LOG.debug("thread {}: dispatching {}", Thread.currentThread().getId(), message.getClass());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -239,10 +244,9 @@ public Path mavenRepoLocal() {
* @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, newUserDir));
}

public int keepAliveMs() {
Expand Down Expand Up @@ -282,7 +286,7 @@ public EnvValue property(Environment env) {
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) {
Expand Down Expand Up @@ -354,6 +358,30 @@ private static Properties loadProperties(Path path) {
return result;
}

public static class PropertiesBuilder {
private Map<String, String> properties = new LinkedHashMap<>();

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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
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;
import org.jboss.fuse.mvnd.common.DaemonInfo;
Expand All @@ -48,7 +46,7 @@ public class DefaultClient implements Client {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);

private final Supplier<DaemonParameters> lazyParameters;
private final DaemonParameters parameters;

public static void main(String[] argv) throws Exception {
final List<String> args = new ArrayList<>(argv.length);
Expand All @@ -69,12 +67,12 @@ public static void main(String[] argv) throws Exception {
}

try (TerminalOutput output = new TerminalOutput(logFile)) {
new DefaultClient(() -> new DaemonParameters(new Properties())).execute(output, args);
new DefaultClient(new DaemonParameters()).execute(output, args);
}
}

public DefaultClient(Supplier<DaemonParameters> lazyParameters) {
this.lazyParameters = lazyParameters;
public DefaultClient(DaemonParameters parameters) {
this.parameters = parameters;
}

@Override
Expand Down Expand Up @@ -144,7 +142,6 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
*/
}

final DaemonParameters parameters = lazyParameters.get();
try (DaemonRegistry registry = new DaemonRegistry(parameters.registry())) {
boolean status = args.remove("--status");
if (status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Client newClient(boolean isNative, DaemonParameters parameters, long timeoutMs)
}
return new NativeTestClient(parameters, mvndNativeExecutablePath, timeoutMs);
} else {
return new DefaultClient(() -> parameters);
return new DefaultClient(parameters);
}
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public static MvndResource create(String className, String rawProjectDir, boolea
final Path mvndPropertiesPath = testDir.resolve("mvnd.properties");
final Path localMavenRepository = deleteDir(testDir.resolve("local-maven-repo"));
final Path settingsPath = createSettings(testDir.resolve("settings.xml"));
final Path logback = Paths.get("src/test/resources/logback.xml").toAbsolutePath();
final Path logback = Paths.get("src/test/resources/logback/logback.xml").toAbsolutePath();
final Path home = deleteDir(testDir.resolve("home"));
final TestParameters parameters = new TestParameters(
testDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,27 +27,21 @@ public TestParameters(Path testDir, Path mvndPropertiesPath, Path mavenHome, Pat
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() {
Expand Down