Skip to content

Commit 69af1bd

Browse files
authored
Merge pull request #360 from dmlloyd/rl-base-url
Add resource loader method to get base URL
2 parents f12dee8 + 8438233 commit 69af1bd

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

resource/src/main/java/io/smallrye/common/resource/JarFileResourceLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public Resource findResource(final String path) {
7575
}
7676
}
7777

78+
public URL baseUrl() {
79+
return base;
80+
}
81+
7882
public void close() {
7983
try {
8084
jarFile.close();

resource/src/main/java/io/smallrye/common/resource/PathResourceLoader.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.Closeable;
44
import java.io.IOException;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
57
import java.nio.file.FileSystem;
68
import java.nio.file.Path;
79

@@ -42,6 +44,15 @@ public Resource findResource(final String path) {
4244
return new PathResource(canon, base.resolve(canon));
4345
}
4446

47+
public URL baseUrl() {
48+
try {
49+
return base.toUri().toURL();
50+
} catch (MalformedURLException e) {
51+
throw new UnsupportedOperationException(
52+
"Base URL is not supported for this loader because the path does not have a valid URL", e);
53+
}
54+
}
55+
4556
public void close() {
4657
Closeable c = this.c;
4758
if (c != null) {

resource/src/main/java/io/smallrye/common/resource/ResourceLoader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.Closeable;
44
import java.io.IOException;
5+
import java.net.URL;
56

67
/**
78
* A loader which can find resources by their path.
@@ -22,6 +23,13 @@ public interface ResourceLoader extends Closeable {
2223
*/
2324
Resource findResource(String path) throws IOException;
2425

26+
/**
27+
* {@return the base URL for this loader}
28+
*/
29+
default URL baseUrl() {
30+
throw new UnsupportedOperationException("Base URL is not supported by this resource loader");
31+
}
32+
2533
/**
2634
* Get a child resource loader for the given child path.
2735
* This method always returns a resource loader, even if the path does not exist.

resource/src/main/java/io/smallrye/common/resource/URLResourceLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ public Resource findResource(final String path) throws IOException {
2626
String canon = ResourceUtils.canonicalizeRelativePath(path);
2727
return new URLResource(canon, new URL(base, canon));
2828
}
29+
30+
public URL baseUrl() {
31+
return base;
32+
}
2933
}

0 commit comments

Comments
 (0)