Skip to content

Commit

Permalink
[KOGITO-9775] Classpath loading as fallback
Browse files Browse the repository at this point in the history
In case the file cannot be retrieved from file system or relative
classpath use absolute classpath to try to retrieve it.
  • Loading branch information
fjtirado committed Sep 6, 2023
1 parent 7f360e2 commit 0514160
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
import java.io.InputStream;
import java.net.URI;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class CachedContentLoader implements URIContentLoader {

private static final Logger logger = LoggerFactory.getLogger(CachedContentLoader.class);

private static class NoCopyByteArrayInputStream extends ByteArrayInputStream {
public NoCopyByteArrayInputStream(byte[] buf) {
super(buf);
Expand All @@ -39,14 +44,29 @@ public synchronized byte[] readAllBytes() {
}

private final URI uri;
private URIContentLoader[] fallbackContentLoaders;

protected CachedContentLoader(URI uri) {
protected CachedContentLoader(URI uri, URIContentLoader... fallbackContentLoaders) {
this.uri = uri;
this.fallbackContentLoaders = fallbackContentLoaders;
}

@Override
public InputStream getInputStream() {
return new NoCopyByteArrayInputStream(ResourceCacheFactory.getCache().get(uri, this::loadURI));
try {
return new NoCopyByteArrayInputStream(ResourceCacheFactory.getCache().get(uri, this::loadURI));
} catch (RuntimeException ex) {
for (URIContentLoader contentLoader : fallbackContentLoaders) {
try {
InputStream stream = contentLoader.getInputStream();
logger.warn("URI {} was retrieved using a fallback mechanism {} rather than original {}", uri, contentLoader.type(), type());
return stream;
} catch (RuntimeException supressed) {
ex.addSuppressed(supressed);
}
}
throw ex;
}
}

protected abstract byte[] loadURI(URI uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class ClassPathContentLoader extends CachedContentLoader {
private final Optional<URL> resource;
private final String path;

ClassPathContentLoader(URI uri, Optional<ClassLoader> cl) {
super(uri);
ClassPathContentLoader(URI uri, Optional<ClassLoader> cl, URIContentLoader... fallbackContentLoaders) {
super(uri, fallbackContentLoaders);
this.path = getPath(uri);
this.resource = Optional.ofNullable(cl.orElse(Thread.currentThread().getContextClassLoader()).getResource(path));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class FileContentLoader extends CachedContentLoader {

private final Path path;

FileContentLoader(URI uri) {
super(uri);
FileContentLoader(URI uri, URIContentLoader... fallbackContentLoaders) {
super(uri, fallbackContentLoaders);
this.path = Path.of(getPath(uri));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,16 @@ public Builder withBaseURI(URI baseURI) {
}

public URIContentLoader build() {
if (baseURI != null) {
uri = compoundURI(baseURI, uri);
}
switch (URIContentLoaderType.from(uri)) {
final URI finalURI = baseURI != null ? compoundURI(baseURI, uri) : uri;
switch (URIContentLoaderType.from(finalURI)) {
default:
case FILE:
return new FileContentLoader(uri);
return new FileContentLoader(finalURI, new ClassPathContentLoader(uri, Optional.ofNullable(cl)));
case HTTP:
return new HttpContentLoader(uri, Optional.ofNullable(workflow), authRef);
return new HttpContentLoader(finalURI, Optional.ofNullable(workflow), authRef);
case CLASSPATH:
return new ClassPathContentLoader(uri, Optional.ofNullable(cl));
Optional<ClassLoader> optionalCl = Optional.ofNullable(cl);
return finalURI != uri ? new ClassPathContentLoader(finalURI, optionalCl) : new ClassPathContentLoader(finalURI, optionalCl, new ClassPathContentLoader(uri, optionalCl));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"name": "publishEvent",
"type": "asyncapi",
"operation": "classpath:specs/asyncAPI.yaml#sendWait"
"operation": "specs/asyncAPI.yaml#sendWait"
}
],
"states": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"functions": [
{
"name": "echoFunction",
"operation": "classpath:specs/enum-parameter.yaml#echo"
"operation": "specs/enum-parameter.yaml#echo"
}
],
"states": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"name": "sayHello",
"type": "rpc",
"operation": "classpath:greeting.proto#Greeter#SayHello"
"operation": "greeting.proto#Greeter#SayHello"
}
],
"states": [
Expand Down

0 comments on commit 0514160

Please sign in to comment.