Skip to content

Commit 6c42f78

Browse files
committed
Finished woith pattern predicatse utilities
1 parent 0c1a244 commit 6c42f78

File tree

5 files changed

+81
-48
lines changed

5 files changed

+81
-48
lines changed

config-examples/src/main/java/io/scalecube/config/examples/PredicateOrderingConfigExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public static void main(String[] args) {
4646
.build());
4747

4848
StringConfigProperty orderedProp1 = configRegistry.stringProperty("orderedProp1");
49-
String foo = configRegistry.stringValue("foo", null);
50-
String bar = configRegistry.stringValue("bar", null);
51-
String sysFoo = configRegistry.stringValue("sys.foo", null);
49+
String foo = configRegistry.stringProperty("foo").valueOrThrow();
50+
String bar = configRegistry.stringProperty("bar").valueOrThrow();
51+
String sysFoo = configRegistry.stringProperty("sys.foo").valueOrThrow();
5252

5353
System.out.println(
5454
"### Matched by first predicate: orderedProp1=" + orderedProp1.value().get());
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.scalecube.config.examples;
2+
3+
import io.scalecube.config.ConfigRegistry;
4+
import io.scalecube.config.ConfigRegistrySettings;
5+
import io.scalecube.config.StringConfigProperty;
6+
import io.scalecube.config.source.ClassPathConfigSource;
7+
import io.scalecube.config.source.SystemPropertiesConfigSource;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
@SuppressWarnings("OptionalGetWithoutIsPresent")
12+
public class PredicateShortcutsConfigExample {
13+
14+
/**
15+
* Main method for example of predicate ordering.
16+
*
17+
* @param args program arguments
18+
*/
19+
public static void main(String[] args) {
20+
// Emulate scenario where sys.foo was also given from system properties
21+
// System.setProperty("sys.foo", "sys foo from java system properties");
22+
23+
String mask = ".*config\\.props";
24+
25+
ConfigRegistry configRegistry =
26+
ConfigRegistry.create(
27+
ConfigRegistrySettings.builder()
28+
.addLastSource("sysProps", new SystemPropertiesConfigSource())
29+
.addLastSource(
30+
"customSysProps",
31+
new SystemPropertiesConfigSource(
32+
ClassPathConfigSource.createWithPattern(
33+
mask, Stream.of("customSys").collect(Collectors.toList()))))
34+
.addLastSource(
35+
"classpath",
36+
ClassPathConfigSource.createWithPattern(
37+
mask, Stream.of("order1", "order2").collect(Collectors.toList())))
38+
.build());
39+
40+
StringConfigProperty orderedProp1 = configRegistry.stringProperty("orderedProp1");
41+
String foo = configRegistry.stringProperty("foo").valueOrThrow();
42+
String bar = configRegistry.stringProperty("bar").valueOrThrow();
43+
String sysFoo = configRegistry.stringProperty("sys.foo").valueOrThrow();
44+
45+
System.out.println(
46+
"### Matched by first predicate: orderedProp1=" + orderedProp1.value().get());
47+
System.out.println("### Regardeless of predicates: foo=" + foo + ", bar=" + bar);
48+
System.out.println(
49+
"### Custom system property: sysFoo="
50+
+ sysFoo
51+
+ ", System.getProperty(sysFoo)="
52+
+ System.getProperty("sys.foo"));
53+
}
54+
}

config/src/main/java/io/scalecube/config/source/ClassPathConfigSource.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.function.Predicate;
2828
import java.util.jar.JarEntry;
2929
import java.util.jar.JarFile;
30-
import java.util.regex.Pattern;
3130
import java.util.stream.Collectors;
3231

3332
public final class ClassPathConfigSource extends FilteredPathConfigSource {
@@ -65,19 +64,7 @@ public ClassPathConfigSource(List<Predicate<Path>> predicates) {
6564
public static ClassPathConfigSource createWithPattern(String mask, List<String> prefixes) {
6665
Objects.requireNonNull(mask, "ClassPathConfigSource: mask is required");
6766
Objects.requireNonNull(prefixes, "ClassPathConfigSource: prefixes is required");
68-
69-
Pattern pattern = Pattern.compile(mask);
70-
71-
Predicate<Path> patternPredicate =
72-
path -> pattern.matcher(path.getFileName().toString()).matches();
73-
74-
List<Predicate<Path>> predicates =
75-
prefixes.stream()
76-
.map(p -> (Predicate<Path>) path -> path.getFileName().startsWith(p))
77-
.map(patternPredicate::and)
78-
.collect(Collectors.toList());
79-
80-
return new ClassPathConfigSource(predicates);
67+
return new ClassPathConfigSource(preparePatternPredicates(mask, prefixes));
8168
}
8269

8370
@Override
@@ -106,7 +93,6 @@ public Map<String, ConfigProperty> loadConfig() {
10693
});
10794

10895
Map<String, ConfigProperty> result = new TreeMap<>();
109-
11096
filterAndCollectInOrder(
11197
predicates.iterator(),
11298
loadConfigMap(pathCollection),
@@ -119,11 +105,10 @@ public Map<String, ConfigProperty> loadConfig() {
119105
LoadedConfigProperty.withNameAndValue(entry)
120106
.origin(path.toString())
121107
.build())));
122-
123108
return loadedConfig = result;
124109
}
125110

126-
static Collection<URI> getClassPathEntries(ClassLoader classloader) {
111+
private static Collection<URI> getClassPathEntries(ClassLoader classloader) {
127112
Collection<URI> entries = new LinkedHashSet<>();
128113
ClassLoader parent = classloader.getParent();
129114
if (parent != null) {
@@ -174,7 +159,7 @@ private static Collection<URL> parseJavaClassPath() {
174159
return new LinkedHashSet<>(urls);
175160
}
176161

177-
private void scanDirectory(
162+
private static void scanDirectory(
178163
File directory, String prefix, Set<File> ancestors, Collection<Path> collector)
179164
throws IOException {
180165
File canonical = directory.getCanonicalFile();
@@ -198,7 +183,7 @@ private void scanDirectory(
198183
}
199184
}
200185

201-
private void scanJar(File file, Collection<Path> collector) throws IOException {
186+
private static void scanJar(File file, Collection<Path> collector) throws IOException {
202187
JarFile jarFile;
203188
try {
204189
jarFile = new JarFile(file);

config/src/main/java/io/scalecube/config/source/FileDirectoryConfigSource.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.StringJoiner;
1616
import java.util.TreeMap;
1717
import java.util.function.Predicate;
18-
import java.util.regex.Pattern;
1918
import java.util.stream.Collectors;
2019

2120
public final class FileDirectoryConfigSource extends FilteredPathConfigSource {
@@ -56,20 +55,8 @@ public static FileDirectoryConfigSource createWithPattern(
5655
String directory, String mask, List<String> prefixes) {
5756
Objects.requireNonNull(directory, "FileDirectoryConfigSource: directory is required");
5857
Objects.requireNonNull(mask, "FileDirectoryConfigSource: mask is required");
59-
Objects.requireNonNull(mask, "FileDirectoryConfigSource: prefixes is required");
60-
61-
Pattern pattern = Pattern.compile(mask);
62-
63-
Predicate<Path> patternPredicate =
64-
path -> pattern.matcher(path.getFileName().toString()).matches();
65-
66-
List<Predicate<Path>> predicates =
67-
prefixes.stream()
68-
.map(p -> (Predicate<Path>) path -> path.getFileName().startsWith(p))
69-
.map(patternPredicate::and)
70-
.collect(Collectors.toList());
71-
72-
return new FileDirectoryConfigSource(directory, predicates);
58+
Objects.requireNonNull(prefixes, "FileDirectoryConfigSource: prefixes is required");
59+
return new FileDirectoryConfigSource(directory, preparePatternPredicates(mask, prefixes));
7360
}
7461

7562
@Override
@@ -88,7 +75,6 @@ public Map<String, ConfigProperty> loadConfig() {
8875
List<Path> pathCollection = Arrays.stream(files).map(File::toPath).collect(Collectors.toList());
8976

9077
Map<String, ConfigProperty> result = new TreeMap<>();
91-
9278
filterAndCollectInOrder(
9379
predicates.iterator(),
9480
loadConfigMap(pathCollection),
@@ -101,7 +87,6 @@ public Map<String, ConfigProperty> loadConfig() {
10187
LoadedConfigProperty.withNameAndValue(entry)
10288
.origin(path.toString())
10389
.build())));
104-
10590
return result;
10691
}
10792

config/src/main/java/io/scalecube/config/source/FilteredPathConfigSource.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
import java.util.Properties;
1515
import java.util.function.BiConsumer;
1616
import java.util.function.Predicate;
17+
import java.util.regex.Pattern;
1718
import java.util.stream.Collectors;
18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
19+
import java.util.stream.Stream;
2020

2121
public abstract class FilteredPathConfigSource implements ConfigSource {
22-
private static final Logger LOGGER = LoggerFactory.getLogger(FilteredPathConfigSource.class);
23-
2422
protected final List<Predicate<Path>> predicates;
2523

2624
protected FilteredPathConfigSource(List<Predicate<Path>> predicates) {
@@ -31,10 +29,22 @@ protected FilteredPathConfigSource(List<Predicate<Path>> predicates) {
3129
protected final Map<Path, Map<String, String>> loadConfigMap(Collection<Path> pathCollection) {
3230
return pathCollection.stream()
3331
.filter(path -> predicates.stream().anyMatch(predicate -> predicate.test(path)))
34-
.collect(Collectors.toMap(path -> path, this::loadProperties));
32+
.collect(Collectors.toMap(path -> path, FilteredPathConfigSource::loadProperties));
33+
}
34+
35+
static List<Predicate<Path>> preparePatternPredicates(String mask, List<String> prefixes) {
36+
Pattern pattern = Pattern.compile(mask);
37+
Predicate<Path> patternPredicate = path -> pattern.matcher(path.toString()).matches();
38+
39+
Stream<Predicate<Path>> stream =
40+
prefixes.stream()
41+
.map(p -> (Predicate<Path>) path -> path.getFileName().toString().startsWith(p))
42+
.map(p -> p.and(patternPredicate));
43+
44+
return Stream.concat(stream, Stream.of(patternPredicate)).collect(Collectors.toList());
3545
}
3646

37-
protected final void filterAndCollectInOrder(
47+
static void filterAndCollectInOrder(
3848
Iterator<Predicate<Path>> predicateIterator,
3949
Map<Path, Map<String, String>> configMap,
4050
BiConsumer<Path, Map<String, String>> configCollector) {
@@ -56,18 +66,17 @@ protected final void filterAndCollectInOrder(
5666
filterAndCollectInOrder(predicateIterator, configMap, configCollector);
5767
}
5868

59-
private Map<String, String> loadProperties(Path input) {
69+
private static Map<String, String> loadProperties(Path input) {
6070
try (InputStream is = input.toUri().toURL().openStream()) {
6171
Properties properties = new Properties();
6272
properties.load(is);
6373
return fromProperties(properties);
6474
} catch (Exception e) {
65-
LOGGER.error("Exception at loading props from '{}', cause: {}", input, e);
6675
throw ThrowableUtil.propagate(e);
6776
}
6877
}
6978

70-
private Map<String, String> fromProperties(Properties properties) {
79+
private static Map<String, String> fromProperties(Properties properties) {
7180
Map<String, String> map = new HashMap<>();
7281
for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) {
7382
String key = (String) e.nextElement();

0 commit comments

Comments
 (0)