From 901fd0b94c27a8283c950743630aef24395b8e87 Mon Sep 17 00:00:00 2001 From: Scott Fauerbach Date: Thu, 14 Sep 2023 13:06:23 -0400 Subject: [PATCH] Construct Options builder from properties file for user. (#971) --- src/main/java/io/nats/client/Options.java | 20 +++++++++++++------ .../java/io/nats/client/OptionsTests.java | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/nats/client/Options.java b/src/main/java/io/nats/client/Options.java index c4c2dcc97..d8a0ed382 100644 --- a/src/main/java/io/nats/client/Options.java +++ b/src/main/java/io/nats/client/Options.java @@ -23,11 +23,14 @@ import javax.net.ssl.SSLContext; import java.io.File; +import java.io.IOException; import java.lang.reflect.Constructor; import java.net.Proxy; import java.net.URI; import java.net.URISyntaxException; import java.nio.CharBuffer; +import java.nio.file.Files; +import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.security.NoSuchAlgorithmException; import java.time.Duration; @@ -694,19 +697,24 @@ public Builder() {} // ---------------------------------------------------------------------------------------------------- /** * Constructs a new {@code Builder} from a {@link Properties} object. - * - *

If {@link Options#PROP_SECURE PROP_SECURE} is set, the builder will - * try to get the default context{@link SSLContext#getDefault() getDefault()}. - * If a context can't be found, no context is set and an IllegalArgumentException is thrown.

- * *

Methods called on the builder after construction can override the properties.

- * * @param props the {@link Properties} object */ public Builder(Properties props) throws IllegalArgumentException { properties(props); } + /** + * Constructs a new {@code Builder} from a file that contains properties. + * @param propertiesFilePath a resolvable path to a file from the location the application is running, either relative or absolute + * @throws IOException if the properties file cannot be found, opened or read + */ + public Builder(String propertiesFilePath) throws IOException { + Properties props = new Properties(); + props.load(Files.newInputStream(Paths.get(propertiesFilePath))); + properties(props); + } + // ---------------------------------------------------------------------------------------------------- // BUILDER METHODS // ---------------------------------------------------------------------------------------------------- diff --git a/src/test/java/io/nats/client/OptionsTests.java b/src/test/java/io/nats/client/OptionsTests.java index 38187fc4b..6a8160402 100644 --- a/src/test/java/io/nats/client/OptionsTests.java +++ b/src/test/java/io/nats/client/OptionsTests.java @@ -26,6 +26,10 @@ import org.junit.jupiter.api.Test; import javax.net.ssl.SSLContext; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; @@ -418,6 +422,11 @@ public void testProperties() throws Exception { o = new Options.Builder(props).build(); _testProperties(o); + String propertiesFilePath = createTempPropertiesFile(props); + System.out.println(propertiesFilePath); + o = new Options.Builder(propertiesFilePath).build(); + _testProperties(o); + // intGtEqZeroProperty not gt zero gives default props.setProperty(Options.PROP_MAX_MESSAGES_IN_OUTGOING_QUEUE, "-1"); o = new Options.Builder(props).build(); @@ -437,6 +446,17 @@ public void testProperties() throws Exception { assertEquals(500, o.getMaxMessagesInOutgoingQueue()); } + public static String createTempPropertiesFile(Properties props) throws IOException { + File f = File.createTempFile("jnats", ".properties"); + BufferedWriter writer = new BufferedWriter(new FileWriter(f)); + for (String key : props.stringPropertyNames()) { + writer.write(key + "=" + props.getProperty(key) + System.lineSeparator()); + } + writer.flush(); + writer.close(); + return f.getAbsolutePath(); + } + private static void _testProperties(Options o) { assertEquals("name", o.getConnectionName()); assertNotNull(o.getUsernameChars());