Skip to content

Commit

Permalink
Merge branch 'main' into multiple-filter-subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Sep 14, 2023
2 parents 8c9f9e6 + 901fd0b commit d827d1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
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>
*
* <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

0 comments on commit d827d1f

Please sign in to comment.