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

Construct Options builder from properties file for user. #971

Merged
merged 1 commit into from
Sep 14, 2023
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
20 changes: 14 additions & 6 deletions src/main/java/io/nats/client/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -694,19 +697,24 @@ public Builder() {}
// ----------------------------------------------------------------------------------------------------
/**
* Constructs a new {@code Builder} from a {@link Properties} object.
*
* <p>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.</p>
*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This text was moved to the build method in a previous PR

* <p>Methods called on the builder after construction can override the properties.</p>
*
* @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
// ----------------------------------------------------------------------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/io/nats/client/OptionsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand Down