Skip to content

Commit

Permalink
Minor cleanup + the addition of a resource cache for jinjava java res…
Browse files Browse the repository at this point in the history
…ource streams

Signed-off-by: Greg Schohn <greg.schohn@gmail.com>
  • Loading branch information
gregschohn committed Dec 4, 2024
1 parent f5c4fed commit 5be3ddc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,43 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.io.Resources;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.loader.ClasspathResourceLocator;
import com.hubspot.jinjava.loader.ResourceNotFoundException;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NameMappingClasspathResourceLocator extends ClasspathResourceLocator {
@AllArgsConstructor
@Getter
@EqualsAndHashCode
private static class ResourceCacheKey {
private String fullName;
private Charset encoding;
}

private final LoadingCache<ResourceCacheKey, String> resourceCache = CacheBuilder.newBuilder()
.build(new CacheLoader<>() {
@Override
public String load(ResourceCacheKey key) throws IOException {
try {
String versionedName = getDefaultVersion("jinjava/" + key.getFullName());
return Resources.toString(Resources.getResource(versionedName), key.getEncoding());
} catch (IllegalArgumentException e) {
throw new ResourceNotFoundException("Couldn't find resource: " + key.getFullName());
}
}
});

private String getDefaultVersion(final String fullName) throws IOException {
try {
Expand All @@ -31,6 +59,10 @@ private String getDefaultVersion(final String fullName) throws IOException {

@Override
public String getString(String fullName, Charset encoding, JinjavaInterpreter interpreter) throws IOException {
return super.getString(getDefaultVersion("jinjava/" + fullName), encoding, interpreter);
try {
return resourceCache.get(new ResourceCacheKey(fullName, encoding));
} catch (ExecutionException e) {
throw new IOException("Failed to get resource content from cache", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {

testImplementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonTypeMappingsSanitizationTransformer')
testImplementation project(':coreUtilities')
testImplementation project(':TrafficCapture:trafficReplayer')
testImplementation testFixtures(project(path: ':coreUtilities'))
testImplementation testFixtures(project(path: ':testHelperFixtures'))
testImplementation testFixtures(project(path: ':TrafficCapture:trafficReplayer'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@ public void testSimpleTransform() throws JsonProcessingException {
+ "}\n";

var provider = new TypeMappingSanitizationTransformerProvider();

Map<String, Object> inputMap = mapper.readValue(TEST_INPUT_REQUEST, new TypeReference<>() {});
{
var transformedDocument = provider.createTransformer(config)
.transformJson(mapper.readValue(TEST_INPUT_REQUEST, new TypeReference<>() {}));
var transformedDocument = provider.createTransformer(config).transformJson(inputMap);
Assertions.assertEquals(JsonNormalizer.fromString(EXPECTED),
JsonNormalizer.fromObject(transformedDocument));
}
{
var resultFromNullConfig = provider.createTransformer(null)
.transformJson(mapper.readValue(TEST_INPUT_REQUEST, new TypeReference<>() {
}));
var resultFromNullConfig = provider.createTransformer(null).transformJson(inputMap);
Assertions.assertEquals(
JsonNormalizer.fromString(
EXPECTED.replace(
Expand Down

0 comments on commit 5be3ddc

Please sign in to comment.