Skip to content

Commit

Permalink
[GR-23090] Support registration of directory resources from JARs with…
Browse files Browse the repository at this point in the history
… native image.

PullRequest: graal/6135
  • Loading branch information
Maja Vukasovic committed May 7, 2020
2 parents 6de8248 + 1895f5b commit 8b5f8a1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ public static void registerResource(String name, InputStream is) {
list.add(res);
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void registerDirectoryResource(String dir, String content) {
/*
* A directory content represents the names of all files and subdirectories located in the
* specified directory, separated with new line delimiter and joined into one string which
* is later converted into a byte array and placed into the resources map.
*/
ResourcesSupport support = ImageSingletons.lookup(ResourcesSupport.class);

byte[] arr = content.getBytes();
List<byte[]> list = support.resources.get(dir);
if (list == null) {
list = new ArrayList<>();
support.resources.put(dir, list);
}
list.add(arr);
}

public static List<byte[]> get(String name) {
return ImageSingletons.lookup(ResourcesSupport.class).resources.get(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -207,17 +212,40 @@ private void scanDirectory(DebugContext debugContext, File f, String relativePat
private static void scanJar(DebugContext debugContext, File element, Pattern... patterns) throws IOException {
JarFile jf = new JarFile(element);
Enumeration<JarEntry> en = jf.entries();

Map<String, List<String>> matchedDirectoryResources = new HashMap<>();
Set<String> allEntries = new HashSet<>();
while (en.hasMoreElements()) {
JarEntry e = en.nextElement();
if (e.getName().endsWith("/")) {
if (e.isDirectory()) {
String dirName = e.getName().substring(0, e.getName().length() - 1);
allEntries.add(dirName);
if (matches(patterns, dirName)) {
matchedDirectoryResources.put(dirName, new ArrayList<>());
}
continue;
}
allEntries.add(e.getName());
if (matches(patterns, e.getName())) {
try (InputStream is = jf.getInputStream(e)) {
registerResource(debugContext, e.getName(), is);
}
}
}

for (String entry : allEntries) {
int last = entry.lastIndexOf('/');
String key = last == -1 ? "" : entry.substring(0, last);
List<String> dirContent = matchedDirectoryResources.get(key);
if (dirContent != null && !dirContent.contains(entry)) {
dirContent.add(entry.substring(last + 1, entry.length()));
}
}

matchedDirectoryResources.forEach((dir, content) -> {
content.sort(Comparator.naturalOrder());
registerDirectoryResource(debugContext, dir, String.join(System.lineSeparator(), content));
});
}

private static boolean matches(Pattern[] patterns, String relativePath) {
Expand All @@ -236,4 +264,12 @@ private static void registerResource(DebugContext debugContext, String resourceN
Resources.registerResource(resourceName, resourceStream);
}
}

@SuppressWarnings("try")
private static void registerDirectoryResource(DebugContext debugContext, String dir, String content) {
try (DebugContext.Scope s = debugContext.scope("registerResource")) {
debugContext.log(DebugContext.VERBOSE_LEVEL, "ResourcesFeature: registerResource: " + dir);
Resources.registerDirectoryResource(dir, content);
}
}
}

0 comments on commit 8b5f8a1

Please sign in to comment.