All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.scalecube.config.examples.PredicateOrderingConfigExample Maven / Gradle / Ivy

The newest version!
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.nio.file.Path;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class PredicateOrderingConfigExample {

  /**
   * Main method for example of predicate ordering.
   *
   * @param args program arguments
   */
  public static void main(String[] args) {
    Predicate propsPredicate = path -> path.toString().endsWith(".props");
    Predicate rootPredicate =
        propsPredicate.and(path -> path.toString().contains("config.props"));
    Predicate firstPredicate = propsPredicate.and(path -> path.toString().contains("order1"));
    Predicate secondPredicate =
        propsPredicate.and(path -> path.toString().contains("order2"));
    Predicate customSysPredicate =
        propsPredicate.and(path -> path.toString().contains("customSys"));

    // Emulate scenario where sys.foo was also given from system properties
    // System.setProperty("sys.foo", "sys foo from java system properties");

    ConfigRegistry configRegistry =
        ConfigRegistry.create(
            ConfigRegistrySettings.builder()
                .addLastSource("sysProps", new SystemPropertiesConfigSource())
                .addLastSource(
                    "customSysProps",
                    new SystemPropertiesConfigSource(new ClassPathConfigSource(customSysPredicate)))
                .addLastSource(
                    "classpath",
                    new ClassPathConfigSource(
                        Stream.of(firstPredicate, secondPredicate, rootPredicate)
                            .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"));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy