Skip to content

Commit

Permalink
feat: Load System properties from configuration file
Browse files Browse the repository at this point in the history
Closes #254, #255
  • Loading branch information
mthmulders committed Sep 11, 2023
1 parent 5b3d7d3 commit 066fe68
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ Next, point MCS to that trust store like so
mcs -Djavax.net.ssl.trustStore=/path/to/keystore search something
```

Or, even easier: create a directory **.mcs** in your user directory (typically **C:\Users\<your-user-name>** on 🪟, **/home/<your-user-name>** on 🐧 or **/Users/<your-user-name>** on 🍎).
Inside that folder, create a file **mcs.config** and write the following line in it:

```
javax.net.ssl.trustStore=/path/to/keystore
```

This way, you don't have to remember passing the `-Djavax.net.ssl.trustStore=`.

## Contributing
Probably the easiest way to get a working development environment is to use Gitpod:

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/it/mulders/mcs/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.mulders.mcs.cli.Cli;
import it.mulders.mcs.cli.CommandClassFactory;
import it.mulders.mcs.cli.SystemPropertyLoader;
import it.mulders.mcs.common.McsExecutionExceptionHandler;
import picocli.CommandLine;

Expand All @@ -12,6 +13,7 @@ public static void main(final String... args) {

// Visible for testing
static int doMain(final String... args) {
System.setProperties(new SystemPropertyLoader().getProperties());
var cli = new Cli();
var program = new CommandLine(cli, new CommandClassFactory(cli))
.setExecutionExceptionHandler(new McsExecutionExceptionHandler());
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/it/mulders/mcs/cli/SystemPropertyLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package it.mulders.mcs.cli;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
* Loads additional system properties from a predefined file on disk.
* <br />
* Copies those properties over the existing ones @{{@link System#getProperties()}} so the result
* is a drop-in replacement that could be passed for {@link System#setProperties(Properties)}.
* This class does not modify the System properties itself.
*/
public class SystemPropertyLoader {
private static final Path MCS_PROPERTIES_FILE = Paths.get(
System.getProperty("user.home"),
".mcs",
"mcs.config"
);

private final Properties properties;

public SystemPropertyLoader() {
this(MCS_PROPERTIES_FILE);
}

protected SystemPropertyLoader(final Path source) {
var input = new Properties();

if (Files.exists(source) && Files.isRegularFile(source)) {
try (var reader = Files.newBufferedReader(source)) {
input.load(reader);
} catch (IOException ioe) {
System.err.printf("Failed to load %s: %s%n", source, ioe.getLocalizedMessage());
}
}

this.properties = new Properties();
properties.putAll(System.getProperties());
properties.putAll(input);
}

public Properties getProperties() {
return properties;
}
}
43 changes: 43 additions & 0 deletions src/test/java/it/mulders/mcs/cli/SystemPropertyLoaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package it.mulders.mcs.cli;

import java.nio.file.Path;
import java.nio.file.Paths;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class SystemPropertyLoaderTest implements WithAssertions {
private static final Path SAMPLE = Paths.get("src", "test", "resources", "sample-mcs.config");
@Test
void should_load_if_file_exists() {
var loader = new SystemPropertyLoader(SAMPLE);

assertThat(loader.getProperties())
.containsEntry("example.a", "foo");
}

@Test
void should_not_fail_if_file_does_not_exist() {
var loader = new SystemPropertyLoader(Paths.get("src", "test", "resources", "non-existing-mcs.config"));

assertThat(loader.getProperties().isEmpty()).isFalse();
}

@Test
void should_delegate_to_System_properties() {
var loader = new SystemPropertyLoader(SAMPLE);

// user.dir is not overridden in the sample configuration file
assertThat(loader.getProperties()).containsKey("user.dir");
}

@Test
void should_override_System_properties() {
var loader = new SystemPropertyLoader(SAMPLE);

// user.home is recklessly overridden in the sample configuration file
assertThat(loader.getProperties()).containsEntry("user.home", "whatever");
}
}
4 changes: 4 additions & 0 deletions src/test/resources/sample-mcs.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
example.a=foo
example.b=${example.a} bar
example.c=${example.a} ${example.a} baz
user.home=whatever

0 comments on commit 066fe68

Please sign in to comment.