Skip to content

Commit

Permalink
#42 : API/Compiler : ConfigHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmugra committed Apr 26, 2021
1 parent 7e59d6d commit 938a8c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
24 changes: 15 additions & 9 deletions core/src/main/java/net/cactusthorn/config/core/ConfigFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public ConfigFactory build() {
}

@SuppressWarnings("unchecked") public <T> T create(Class<T> sourceInterface) {
ConfigHolder configHolder = configHolder(sourceInterface);
ConfigHolder configHolder = configHolder(sourceInterface.getClassLoader());
try {
MethodHandle methodHandler = BUILDERS.computeIfAbsent(sourceInterface, this::findBuilderConstructor);
@SuppressWarnings("rawtypes") ConfigBuilder builder = (ConfigBuilder) methodHandler.invoke(configHolder);
Expand All @@ -141,40 +141,46 @@ public ConfigFactory build() {
}
}

public <T> ConfigHolder configHolder(Class<T> sourceInterface) {
public ConfigHolder configHolder(ClassLoader classLoader) {
Map<String, String> forBuilder = new HashMap<>();
forBuilder.putAll(load(sourceInterface));
forBuilder.putAll(load(classLoader));
forBuilder.putAll(props); // Map with properties is always has highest priority
return new ConfigHolder(forBuilder);
}

public ConfigHolder configHolder() {
return configHolder(ConfigFactory.class);
return configHolder(ConfigFactory.class.getClassLoader());
}

public static <T> T create(Class<T> sourceInterface, Map<String, String> properties) {
if (sourceInterface == null) {
throw new IllegalArgumentException(isNull(sourceInterface));
}
return ConfigFactory.builder().setSource(properties).build().create(sourceInterface);
}

public static <T> T create(Class<T> sourceInterface, URI... uri) {
if (sourceInterface == null) {
throw new IllegalArgumentException(isNull(sourceInterface));
}
return ConfigFactory.builder().addSource(uri).build().create(sourceInterface);
}

public static <T> T create(Class<T> sourceInterface, String... uri) {
if (sourceInterface == null) {
throw new IllegalArgumentException(isNull(uri));
}
return ConfigFactory.builder().addSource(uri).build().create(sourceInterface);
}

private <T> Map<String, String> load(Class<T> sourceInterface) {
if (sourceInterface == null) {
throw new IllegalArgumentException(isNull(sourceInterface));
}
private Map<String, String> load(ClassLoader classLoader) {
// TODO caching for properties by URI
List<Map<String, String>> values = new ArrayList<>();
for (UriTemplate template : templates) {
URI uri = template.uri();
Loader load = loaders.stream().filter(l -> l.accept(uri)).findFirst()
.orElseThrow(() -> new UnsupportedOperationException(msg(LOADER_NOT_FOUND, uri)));
values.add(load.load(uri, sourceInterface.getClassLoader()));
values.add(load.load(uri, classLoader));
}
return loadStrategy.combine(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public class UrlPropertiesLoaderTest {
}
URI uri = file.toUri();
uri = new URI(uri.getScheme(), uri.getSchemeSpecificPart(), "UTF-8");
System.out.println(uri);
Map<String, String> properties = LOADER.load(uri, CL);
assertEquals("bbb", properties.get("aaa"));
}
Expand Down

0 comments on commit 938a8c6

Please sign in to comment.