Skip to content

Commit

Permalink
#62 : API: caching API "nocache:" URI
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmugra committed May 6, 2021
1 parent 99fb5cd commit 58ae73a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 18 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ In this case generated class will also get methods for this interface:
### Caching
By default, `ConfigFactory` caches loaded properties using source-URI (after resolving system properties and/or environment variable in it) as a cache key. To not cache properties related to the URI(s), use the `addSourceNoCache` methods instead of `addSource`.

Alternative, it's possible to use URI-prefix `nocache:` this also will switch off caching for the URI.
e.g.
`nocache:system:properties`
`nocache:file:~/my.properties`

### Logging
The runtime part of the library is using [Java Logging API](https://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html).
That's because one of the requirements is that external libraries must not be used, and JUL is only option in this case.
Expand Down
39 changes: 21 additions & 18 deletions core/src/main/java/net/cactusthorn/config/core/loader/Loaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static net.cactusthorn.config.core.ApiMessages.Key.LOADER_NOT_FOUND;

import java.net.URI;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
Expand All @@ -23,21 +24,19 @@ public static final class UriTemplate {
private boolean cachable = true;

public UriTemplate(URI uri, boolean cachable) {
this.uri = replaceUserHome(uri);
this.cachable = cachable;
this.uri = replace(uri, cachable);
}

public UriTemplate(String template, boolean cachable) {
this.template = replaceUserHome(template);
this.cachable = cachable;
this.template = replace(template, cachable);
if (template.indexOf("{") != -1) {
variable = true;
} else {
uri = URI.create(this.template);
}
}

@SuppressWarnings({ "rawtypes", "unchecked" }) private URI uri() {
@SuppressWarnings({ "rawtypes", "unchecked" }) URI uri() {
if (!variable) {
return uri;
}
Expand All @@ -46,29 +45,33 @@ public UriTemplate(String template, boolean cachable) {
return URI.create(new VariablesParser(template).replace(values));
}

private boolean cachable() {
boolean cachable() {
return cachable;
}

private static final String USERHOME_PREFIX = "file:~/";

private URI replaceUserHome(URI u) {
String tmp = replaceUserHome(u.toString());
private URI replace(URI u, boolean cache) {
String tmp = replace(u.toString(), cache);
return URI.create(tmp);
}

private String replaceUserHome(String str) {
if (str.indexOf(USERHOME_PREFIX) == -1) {
return str;
}
String userHome = userHome().toString();
return str.replace(USERHOME_PREFIX, userHome);
}

private static final String NOCACHE = "nocache:";
private static final String USER_HOME = "user.home";

private URI userHome() {
return java.nio.file.Paths.get(System.getProperty(USER_HOME)).toUri();
private String replace(String str, boolean cache) {
String result = str;
if (result.indexOf(NOCACHE) == 0) {
this.cachable = false;
result = result.substring(NOCACHE.length());
} else {
this.cachable = cache;
}
if (result.indexOf(USERHOME_PREFIX) == -1) {
return result;
}
String userHome = Paths.get(System.getProperty(USER_HOME)).toUri().toString();
return result.replace(USERHOME_PREFIX, userHome);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.cactusthorn.config.core.loader;

import static org.junit.jupiter.api.Assertions.*;

import java.net.URI;

import org.junit.jupiter.api.Test;

public class UriTemplateTest {

@Test public void nocacheStr() {
Loaders.UriTemplate template = new Loaders.UriTemplate("nocache:system:properties", true);
assertFalse(template.cachable());
assertEquals(URI.create("system:properties"), template.uri());
}

@Test public void nocacheURI() {
Loaders.UriTemplate template = new Loaders.UriTemplate(URI.create("nocache:system:properties"), true);
assertFalse(template.cachable());
assertEquals(URI.create("system:properties"), template.uri());
}

@Test public void cacheStr() {
Loaders.UriTemplate template = new Loaders.UriTemplate("system:properties", true);
assertTrue(template.cachable());
assertEquals(URI.create("system:properties"), template.uri());
}

@Test public void cacheURI() {
Loaders.UriTemplate template = new Loaders.UriTemplate(URI.create("system:properties"), true);
assertTrue(template.cachable());
assertEquals(URI.create("system:properties"), template.uri());
}

@Test public void nocacheParamStr() {
Loaders.UriTemplate template = new Loaders.UriTemplate("system:properties", false);
assertFalse(template.cachable());
assertEquals(URI.create("system:properties"), template.uri());
}

@Test public void nocacheParamURI() {
Loaders.UriTemplate template = new Loaders.UriTemplate(URI.create("system:properties"), false);
assertFalse(template.cachable());
assertEquals(URI.create("system:properties"), template.uri());
}
}

0 comments on commit 58ae73a

Please sign in to comment.