Skip to content

Commit e060dc7

Browse files
committed
Support MountableFile when build by GraalVM as native-build
We extract resource to temp path and then mount this path, as we already do with .jar resources.
1 parent 43c6a97 commit e060dc7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

core/src/main/java/org/testcontainers/utility/MountableFile.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ private static String unencodeResourceURIToFilePath(@NotNull final String resour
170170
.decode(resource.replaceAll("\\+", "%2B"), Charsets.UTF_8.name())
171171
.replaceFirst("jar:", "")
172172
.replaceFirst("file:", "")
173+
.replaceFirst("resource:", "")
173174
.replaceAll("!.*", "");
174175
} catch (UnsupportedEncodingException e) {
175176
throw new IllegalStateException(e);
@@ -217,6 +218,8 @@ private String resolveFilesystemPath() {
217218
private String getResourcePath() {
218219
if (path.contains(".jar!")) {
219220
resourcePath = extractClassPathResourceToTempLocation(this.path);
221+
} else if (this.path.startsWith("resource:")) {
222+
this.resourcePath = extractClassPathGraalVMResourceToTempLocation(this.path);
220223
} else {
221224
resourcePath = unencodeResourceURIToFilePath(path);
222225
}
@@ -270,6 +273,54 @@ private String extractClassPathResourceToTempLocation(final String hostPath) {
270273
}
271274
}
272275

276+
private String extractClassPathGraalVMResourceToTempLocation(String hostPath) {
277+
File tmpLocation = createTempDirectory();
278+
//noinspection ResultOfMethodCallIgnored
279+
tmpLocation.delete();
280+
281+
String urldecodedResourcePath = unencodeResourceURIToFilePath(hostPath);
282+
if (urldecodedResourcePath.startsWith("/")) {
283+
urldecodedResourcePath = urldecodedResourcePath.replaceFirst("/", "");
284+
}
285+
286+
try {
287+
log.debug(
288+
"Copying classpath GraalVM resource(s) from {} to {} to permit Docker to bind",
289+
hostPath,
290+
tmpLocation
291+
);
292+
293+
try (
294+
InputStream is = Thread
295+
.currentThread()
296+
.getContextClassLoader()
297+
.getResourceAsStream(urldecodedResourcePath)
298+
) {
299+
if (is == null) {
300+
throw new IllegalStateException(
301+
urldecodedResourcePath +
302+
" not found in classpath, probably need to update GraalVM's reachability-metadata.json"
303+
);
304+
}
305+
Files.copy(is, tmpLocation.toPath());
306+
}
307+
} catch (IOException e) {
308+
throw new IllegalStateException(
309+
"Failed to process GraalVM file when extracting classpath resource: " + hostPath,
310+
e
311+
);
312+
}
313+
314+
// Mark temporary files/dirs for deletion at JVM shutdown
315+
deleteOnExit(tmpLocation.toPath());
316+
317+
try {
318+
return tmpLocation.getCanonicalPath();
319+
} catch (IOException e) {
320+
throw new IllegalStateException(e);
321+
}
322+
}
323+
273324
private File createTempDirectory() {
274325
try {
275326
if (SystemUtils.IS_OS_MAC) {

0 commit comments

Comments
 (0)