Skip to content

Commit

Permalink
Allow resources inclusion from jar files in paths containing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Nov 13, 2023
1 parent b76e529 commit c39af7b
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,6 +27,7 @@

import static com.oracle.svm.core.jdk.Resources.RESOURCES_INTERNAL_PATH_SEPARATOR;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -281,12 +282,12 @@ private void processResourceFromModule(Module module, String resourcePath) {
}
}

InputStream is = module.getResourceAsStream(resourcePath);
boolean isDirectory = Files.isDirectory(Path.of(resourcePath));
if (isDirectory) {
String content = getDirectoryContent(resourcePath, false);
Resources.singleton().registerDirectoryResource(module, resourcePath, content, false);
} else {
InputStream is = module.getResourceAsStream(resourcePath);
registerResource(module, resourcePath, false, is);
}
} catch (IOException e) {
Expand All @@ -310,13 +311,13 @@ private void processResourceFromClasspath(String resourcePath) {
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
try {
InputStream is = url.openStream();
boolean fromJar = url.getProtocol().equalsIgnoreCase("jar");
boolean isDirectory = resourceIsDirectory(url, fromJar, resourcePath);
if (isDirectory) {
String content = getDirectoryContent(fromJar ? url.toString() : Paths.get(url.toURI()).toString(), fromJar);
Resources.singleton().registerDirectoryResource(null, resourcePath, content, fromJar);
} else {
InputStream is = url.openStream();
registerResource(null, resourcePath, fromJar, is);
}
} catch (IOException e) {
Expand All @@ -343,17 +344,17 @@ private void registerResource(Module module, String resourcePath, boolean fromJa
}

/* Util functions for resource attributes calculations */
private String urlToJarPath(URL url) {
private JarFile urlToJarFile(URL url) {
try {
return ((JarURLConnection) url.openConnection()).getJarFileURL().getFile();
return ((JarURLConnection) url.openConnection()).getJarFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private boolean resourceIsDirectory(URL url, boolean fromJar, String resourcePath) throws IOException, URISyntaxException {
if (fromJar) {
try (JarFile jf = new JarFile(urlToJarPath(url))) {
try (JarFile jf = urlToJarFile(url)) {
return jf.getEntry(resourcePath).isDirectory();
}
} else {
Expand All @@ -364,7 +365,7 @@ private boolean resourceIsDirectory(URL url, boolean fromJar, String resourcePat
private String getDirectoryContent(String path, boolean fromJar) throws IOException {
Set<String> content = new TreeSet<>();
if (fromJar) {
try (JarFile jf = new JarFile(urlToJarPath(URI.create(path).toURL()))) {
try (JarFile jf = urlToJarFile(URI.create(path).toURL())) {
String pathSeparator = FileSystems.getDefault().getSeparator();
String directoryPath = path.split("!")[1];

Expand Down

0 comments on commit c39af7b

Please sign in to comment.