Skip to content

Commit

Permalink
Merge pull request #147 from scalecube/feature/146/predicate-shortcuts
Browse files Browse the repository at this point in the history
Feature/146/predicate shortcuts
  • Loading branch information
artem-v authored Feb 17, 2020
2 parents 472e454 + 6c42f78 commit b99631f
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.scalecube.config.audit.Slf4JConfigEventListener;
import io.scalecube.config.http.server.ConfigRegistryHttpServer;
import io.scalecube.config.source.ClassPathConfigSource;
import io.scalecube.config.source.DirectoryConfigSource;
import io.scalecube.config.source.FileDirectoryConfigSource;
import java.nio.file.Path;
import java.util.function.Predicate;

Expand All @@ -28,7 +28,7 @@ public static void main(String[] args) {
ConfigRegistrySettings.builder()
.addLastSource("classpath", new ClassPathConfigSource(propsPredicate))
.addLastSource(
"configDirectory", new DirectoryConfigSource(basePath, propsPredicate))
"configDirectory", new FileDirectoryConfigSource(basePath, propsPredicate))
.addListener(new Slf4JConfigEventListener())
.jmxEnabled(true)
.jmxMBeanName("config.exporter:name=ConfigRegistry")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.scalecube.config.mongo.MongoConfigConnector;
import io.scalecube.config.mongo.MongoConfigEventListener;
import io.scalecube.config.mongo.MongoConfigRepository;
import io.scalecube.config.source.DirectoryConfigSource;
import io.scalecube.config.source.FileDirectoryConfigSource;
import java.nio.file.Path;
import java.util.function.Predicate;

Expand Down Expand Up @@ -45,7 +45,7 @@ public static void main(String[] args) {
ConfigRegistry.create(
ConfigRegistrySettings.builder()
.addLastSource(
"ConfigDirectory", new DirectoryConfigSource(basePath, propsPredicate))
"ConfigDirectory", new FileDirectoryConfigSource(basePath, propsPredicate))
.addLastSource("MongoConfig", mongoConfigSource)
.addListener(new Slf4JConfigEventListener())
.addListener(new MongoConfigEventListener(connector, auditLogCollectionName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.scalecube.config.ConfigRegistrySettings;
import io.scalecube.config.StringConfigProperty;
import io.scalecube.config.source.ClassPathConfigSource;
import io.scalecube.config.source.DirectoryConfigSource;
import io.scalecube.config.source.FileDirectoryConfigSource;
import java.nio.file.Path;
import java.util.function.Predicate;

Expand All @@ -26,7 +26,7 @@ public static void main(String[] args) {
ConfigRegistrySettings.builder()
.addLastSource("classpath", new ClassPathConfigSource(propsPredicate))
.addLastSource(
"configDirectory", new DirectoryConfigSource(basePath, propsPredicate))
"configDirectory", new FileDirectoryConfigSource(basePath, propsPredicate))
.jmxEnabled(false)
.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.scalecube.config.source.SystemPropertiesConfigSource;
import java.nio.file.Path;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class PredicateOrderingConfigExample {
Expand Down Expand Up @@ -38,13 +40,15 @@ public static void main(String[] args) {
new SystemPropertiesConfigSource(new ClassPathConfigSource(customSysPredicate)))
.addLastSource(
"classpath",
new ClassPathConfigSource(firstPredicate, secondPredicate, rootPredicate))
new ClassPathConfigSource(
Stream.of(firstPredicate, secondPredicate, rootPredicate)
.collect(Collectors.toList())))
.build());

StringConfigProperty orderedProp1 = configRegistry.stringProperty("orderedProp1");
String foo = configRegistry.stringValue("foo", null);
String bar = configRegistry.stringValue("bar", null);
String sysFoo = configRegistry.stringValue("sys.foo", null);
String foo = configRegistry.stringProperty("foo").valueOrThrow();
String bar = configRegistry.stringProperty("bar").valueOrThrow();
String sysFoo = configRegistry.stringProperty("sys.foo").valueOrThrow();

System.out.println(
"### Matched by first predicate: orderedProp1=" + orderedProp1.value().get());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.scalecube.config.examples;

import io.scalecube.config.ConfigRegistry;
import io.scalecube.config.ConfigRegistrySettings;
import io.scalecube.config.StringConfigProperty;
import io.scalecube.config.source.ClassPathConfigSource;
import io.scalecube.config.source.SystemPropertiesConfigSource;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class PredicateShortcutsConfigExample {

/**
* Main method for example of predicate ordering.
*
* @param args program arguments
*/
public static void main(String[] args) {
// Emulate scenario where sys.foo was also given from system properties
// System.setProperty("sys.foo", "sys foo from java system properties");

String mask = ".*config\\.props";

ConfigRegistry configRegistry =
ConfigRegistry.create(
ConfigRegistrySettings.builder()
.addLastSource("sysProps", new SystemPropertiesConfigSource())
.addLastSource(
"customSysProps",
new SystemPropertiesConfigSource(
ClassPathConfigSource.createWithPattern(
mask, Stream.of("customSys").collect(Collectors.toList()))))
.addLastSource(
"classpath",
ClassPathConfigSource.createWithPattern(
mask, Stream.of("order1", "order2").collect(Collectors.toList())))
.build());

StringConfigProperty orderedProp1 = configRegistry.stringProperty("orderedProp1");
String foo = configRegistry.stringProperty("foo").valueOrThrow();
String bar = configRegistry.stringProperty("bar").valueOrThrow();
String sysFoo = configRegistry.stringProperty("sys.foo").valueOrThrow();

System.out.println(
"### Matched by first predicate: orderedProp1=" + orderedProp1.value().get());
System.out.println("### Regardeless of predicates: foo=" + foo + ", bar=" + bar);
System.out.println(
"### Custom system property: sysFoo="
+ sysFoo
+ ", System.getProperty(sysFoo)="
+ System.getProperty("sys.foo"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.scalecube.config.ObjectConfigProperty;
import io.scalecube.config.StringConfigProperty;
import io.scalecube.config.audit.Slf4JConfigEventListener;
import io.scalecube.config.source.DirectoryConfigSource;
import io.scalecube.config.source.FileDirectoryConfigSource;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
Expand All @@ -18,6 +18,8 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class ReloadableLocalResourceConfigExample {
Expand All @@ -39,7 +41,10 @@ public static void main(String[] args) throws Exception {
ConfigRegistrySettings.builder()
.addLastSource(
"configDirectory",
new DirectoryConfigSource(basePath, reloadablePropsPredicate, propsPredicate))
new FileDirectoryConfigSource(
basePath,
Stream.of(reloadablePropsPredicate, propsPredicate)
.collect(Collectors.toList())))
.addListener(new Slf4JConfigEventListener())
.reloadIntervalSec(1)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public void onEvents(Collection<ConfigEvent> events) {
if (!events.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append("[");
events
.stream()
events.stream()
.sorted(Comparator.comparing(ConfigEvent::getName))
.forEach(
event -> {
Expand All @@ -30,7 +29,7 @@ public void onEvents(Collection<ConfigEvent> events) {
sb.append(originAsString(event));
});
sb.append("\n").append("]");
LOGGER.info("Config property changed: {}", sb);
LOGGER.info(sb.toString());
}
}

Expand Down
Loading

0 comments on commit b99631f

Please sign in to comment.