4
4
import io .scalecube .config .ConfigRegistrySettings ;
5
5
import io .scalecube .config .StringConfigProperty ;
6
6
import io .scalecube .config .source .ClassPathConfigSource ;
7
+ import io .scalecube .config .source .SystemPropertiesConfigSource ;
7
8
import java .nio .file .Path ;
8
9
import java .util .function .Predicate ;
9
10
@@ -17,19 +18,41 @@ public class PredicateOrderingConfigExample {
17
18
*/
18
19
public static void main (String [] args ) {
19
20
Predicate <Path > propsPredicate = path -> path .toString ().endsWith (".props" );
21
+ Predicate <Path > rootPredicate =
22
+ propsPredicate .and (path -> path .toString ().contains ("config.props" ));
20
23
Predicate <Path > firstPredicate = propsPredicate .and (path -> path .toString ().contains ("order1" ));
21
24
Predicate <Path > secondPredicate =
22
25
propsPredicate .and (path -> path .toString ().contains ("order2" ));
26
+ Predicate <Path > customSysPredicate =
27
+ propsPredicate .and (path -> path .toString ().contains ("customSys" ));
28
+
29
+ // Emulate scenario where sys.foo was also given from system properties
30
+ // System.setProperty("sys.foo", "sys foo from java system properties");
23
31
24
32
ConfigRegistry configRegistry =
25
33
ConfigRegistry .create (
26
34
ConfigRegistrySettings .builder ()
35
+ .addLastSource ("sysProps" , new SystemPropertiesConfigSource ())
36
+ .addLastSource (
37
+ "customSysProps" ,
38
+ new SystemPropertiesConfigSource (new ClassPathConfigSource (customSysPredicate )))
27
39
.addLastSource (
28
- "classpath" , new ClassPathConfigSource (firstPredicate , secondPredicate ))
40
+ "classpath" ,
41
+ new ClassPathConfigSource (firstPredicate , secondPredicate , rootPredicate ))
29
42
.build ());
30
43
31
44
StringConfigProperty orderedProp1 = configRegistry .stringProperty ("orderedProp1" );
45
+ String foo = configRegistry .stringValue ("foo" , null );
46
+ String bar = configRegistry .stringValue ("bar" , null );
47
+ String sysFoo = configRegistry .stringValue ("sys.foo" , null );
32
48
33
- System .out .println ("### Matched by first predicate orderedProp1=" + orderedProp1 .value ().get ());
49
+ System .out .println (
50
+ "### Matched by first predicate: orderedProp1=" + orderedProp1 .value ().get ());
51
+ System .out .println ("### Regardeless of predicates: foo=" + foo + ", bar=" + bar );
52
+ System .out .println (
53
+ "### Custom system property: sysFoo="
54
+ + sysFoo
55
+ + ", System.getProperty(sysFoo)="
56
+ + System .getProperty ("sys.foo" ));
34
57
}
35
58
}
0 commit comments