Skip to content

Commit 28446e7

Browse files
Fixed bug with classpath search.
1 parent c802f6a commit 28446e7

File tree

9 files changed

+77
-40
lines changed

9 files changed

+77
-40
lines changed

substratevm/mx.substratevm/ce-dev

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
LINKY_LAYOUT=*.jar
2+
DISABLE_INSTALLABLES=true
3+
FORCE_BASH_LAUNCHERS=true
4+
EXCLUDE_COMPONENTS=nju,nic,LibGraal
5+
SKIP_LIBRARIES=polyglot

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_ClassLoader.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ private static <T> T urlToResource(String name, URL url) {
8787
}
8888
}
8989

90-
static <T> T urlToResource(String name) {
90+
static <T> T nameToResource(String name) {
9191
return urlToResource(name, Resources.createURL(name));
9292
}
9393

94-
static <T> Enumeration<T> findResources(String name) {
94+
static <T> Enumeration<T> nameToResources(String name) {
9595
Enumeration<URL> urls = Resources.createURLs(name);
9696
List<T> resourceURLs = new ArrayList<>();
9797
while (urls.hasMoreElements()) {
@@ -148,27 +148,27 @@ public Enumeration<URL> findResources(final String name, final boolean check) {
148148
@Substitute
149149
@TargetElement(name = "getResource", onlyWith = JDK11OrLater.class)
150150
public Target_jdk_internal_loader_Resource_JDK11OrLater getResourceJDK11OrLater(String name, boolean check) {
151-
return URLClassPathHelper.urlToResource(name);
151+
return URLClassPathHelper.nameToResource(name);
152152
}
153153

154154
@Substitute
155155
@TargetElement(name = "getResources", onlyWith = JDK11OrLater.class)
156156
public Enumeration<Target_jdk_internal_loader_Resource_JDK11OrLater> getResourcesJDK11OrLater(final String name,
157157
final boolean check) {
158-
return URLClassPathHelper.findResources(name);
158+
return URLClassPathHelper.nameToResources(name);
159159
}
160160

161161
@Substitute
162162
@TargetElement(name = "getResource", onlyWith = JDK8OrEarlier.class)
163163
public Target_sun_misc_Resource_JDK8OrEarlier getResourceJDK8OrEarlier(String name, boolean check) {
164-
return URLClassPathHelper.urlToResource(name);
164+
return URLClassPathHelper.nameToResource(name);
165165
}
166166

167167
@Substitute
168168
@TargetElement(name = "getResources", onlyWith = JDK8OrEarlier.class)
169169
public Enumeration<Target_sun_misc_Resource_JDK8OrEarlier> getResourcesJDK8OrEarlier(final String name,
170170
final boolean check) {
171-
return URLClassPathHelper.findResources(name);
171+
return URLClassPathHelper.nameToResources(name);
172172
}
173173
}
174174

@@ -213,6 +213,21 @@ private List<URL> findMiscResource(String name) {
213213
private URL findResource(Target_java_lang_module_ModuleReference mref, String name) {
214214
return null;
215215
}
216+
217+
@Substitute
218+
private URL findResourceOnClassPath(String name) {
219+
return Resources.createURL(name);
220+
}
221+
222+
@Substitute
223+
private Enumeration<URL> findResourcesOnClassPath(String name) {
224+
Enumeration<URL> urls = Resources.createURLs(name);
225+
List<URL> resourceURLs = new ArrayList<>();
226+
while (urls.hasMoreElements()) {
227+
resourceURLs.add(urls.nextElement());
228+
}
229+
return Collections.enumeration(resourceURLs);
230+
}
216231
}
217232

218233
/**

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/resources/ByteArrayChannel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636

3737
/**
3838
* <p>
39-
* This class is copy of class {@link jdk.nio.zipfs.ByteArrayChannel }. There are two reasons why we
40-
* cannot substitute this class:
39+
* This class is copy of class jdk.nio.zipfs.ByteArrayChannel. There are two reasons why we cannot
40+
* substitute this class:
4141
* <ul>
4242
* <li>This class doesn't exists on JDK8.</li>
43-
* <li>On JDK11, we need to extends this class, and in case of substitution, it's not possible because
44-
* substituted class is final</li>
43+
* <li>On JDK11, we need to extends this class, and in case of substitution, it's not possible
44+
* because substituted class is final</li>
4545
* </ul>
4646
* </p>
4747
*/

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/resources/NativeImageResourceFileSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@
8989

9090
/**
9191
* <p>
92-
* Some parts of code from this class are copied from {@link jdk.nio.zipfs.ZipFileSystem } with small tweaks. The
93-
* main reason why we cannot reuse this class (without any code copying) is that we cannot extend
94-
* {@link jdk.nio.zipfs.ZipPath } which is central class in Zip file system.
92+
* Some parts of code from this class are copied from jdk.nio.zipfs.ZipFileSystem with small tweaks.
93+
* The main reason why we cannot reuse this class (without any code copying) is that we cannot
94+
* extend jdk.nio.zipfs.ZipPath which is central class in Zip file system.
9595
* </p>
9696
*/
9797
public class NativeImageResourceFileSystem extends FileSystem {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/resources/NativeImageResourcePath.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@
6969

7070
/**
7171
* <p>
72-
* Most of the code from this class is a copy of {@link jdk.nio.zipfs.ZipPath } with small tweaks.
73-
* The main reason why we cannot reuse this class is that this class is final in its original
74-
* implementation.
72+
* Most of the code from this class is a copy of jdk.nio.zipfs.ZipPath with small tweaks. The main
73+
* reason why we cannot reuse this class is that this class is final in its original implementation.
7574
* </p>
7675
*/
7776
public class NativeImageResourcePath implements Path {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/resources/ResourceURLConnection.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.InputStream;
3232
import java.net.URL;
3333
import java.net.URLConnection;
34+
import java.util.List;
3435

3536
import com.oracle.svm.core.jdk.Resources;
3637

@@ -64,13 +65,16 @@ public void connect() {
6465
String resourceName = resolveName(url.getPath());
6566
ResourceStorageEntry entry = Resources.get(resourceName);
6667
if (entry != null) {
67-
if (index < entry.getData().size()) {
68-
data = entry.getData().get(index);
68+
List<byte[]> bytes = entry.getData();
69+
if (index < bytes.size()) {
70+
this.data = bytes.get(index);
6971
} else {
7072
// This will happen only in case that we are creating one URL with the second URL as
7173
// a context.
72-
data = entry.getData().get(0);
74+
this.data = bytes.get(0);
7375
}
76+
} else {
77+
this.data = null;
7478
}
7579
}
7680

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ResourcesFeature.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import java.net.URISyntaxException;
3333
import java.net.URL;
3434
import java.net.URLClassLoader;
35-
import java.nio.file.FileSystems;
36-
import java.nio.file.Paths;
3735
import java.util.ArrayList;
3836
import java.util.Arrays;
3937
import java.util.Collections;
@@ -311,7 +309,7 @@ private static void scanJar(DebugContext debugContext, File root, Pattern[] incl
311309
if (entry.isDirectory()) {
312310
String dirName = entry.getName().substring(0, entry.getName().length() - 1);
313311
if (matches(includePatterns, excludePatterns, dirName)) {
314-
// Directory content is emtpy during JAR scanning.
312+
// Directory content is emtpy in JAR scanning.
315313
registerDirectoryResource(debugContext, dirName, "");
316314
}
317315
} else {
@@ -325,7 +323,7 @@ private static void scanJar(DebugContext debugContext, File root, Pattern[] incl
325323
}
326324

327325
private static boolean matches(Pattern[] includePatterns, Pattern[] excludePatterns, String relativePath) {
328-
String relativePathWithSeparator = Paths.get(FileSystems.getDefault().getSeparator(), relativePath).toString();
326+
String relativePathWithSeparator = "/" + relativePath;
329327
for (Pattern p : excludePatterns) {
330328
if (p.matcher(relativePath).matches() || p.matcher(relativePathWithSeparator).matches()) {
331329
return false;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Args = -H:IncludeResources=resources/resource-test1.txt|resources/resource-test2.txt \
2-
--initialize-at-run-time=com.oracle.svm.test
1+
Args = --initialize-at-run-time=com.oracle.svm.test

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/NativeImageResourceFileSystemProviderTest.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,35 @@
5454
import java.util.Map;
5555
import java.util.Set;
5656

57+
import com.oracle.svm.core.annotate.AutomaticFeature;
58+
import com.oracle.svm.core.configure.ResourcesRegistry;
59+
import org.graalvm.nativeimage.ImageSingletons;
60+
import org.graalvm.nativeimage.hosted.Feature;
61+
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
5762
import org.junit.Assert;
5863
import org.junit.Test;
5964

6065
public class NativeImageResourceFileSystemProviderTest {
6166

62-
private static final String RESOURCE_DIR = Paths.get(FileSystems.getDefault().getSeparator(), "resources").toString();
63-
private static final String RESOURCE_FILE1 = Paths.get(RESOURCE_DIR, "resource-test1.txt").toString();
64-
private static final String RESOURCE_FILE2 = Paths.get(RESOURCE_DIR, "resource-test2.txt").toString();
65-
private static final String NEW_DIRECTORY = Paths.get(RESOURCE_DIR, "tmp").toString();
67+
private static final String RESOURCE_DIR = "/resources";
68+
private static final String RESOURCE_FILE_1 = RESOURCE_DIR + "/resource-test1.txt";
69+
private static final String RESOURCE_FILE_2 = RESOURCE_DIR + "/resource-test2.txt";
70+
private static final String NEW_DIRECTORY = RESOURCE_DIR + "/tmp";
6671

6772
private static final int TIME_SPAN = 1_000_000;
6873

74+
// Register resources.
75+
@AutomaticFeature
76+
private static final class RegisterResourceFeature implements Feature {
77+
@Override
78+
public void beforeAnalysis(BeforeAnalysisAccess access) {
79+
RuntimeClassInitialization.initializeAtBuildTime(NativeImageResourceFileSystemProviderTest.class);
80+
ResourcesRegistry registry = ImageSingletons.lookup(ResourcesRegistry.class);
81+
registry.addResources(RESOURCE_FILE_1);
82+
registry.addResources(RESOURCE_FILE_2);
83+
}
84+
}
85+
6986
private static URL resourceNameToURL(String resourceName) {
7087
URL resource = NativeImageResourceFileSystemProviderTest.class.getResource(resourceName);
7188
Assert.assertNotNull("Resource " + resourceName + " is not found!", resource);
@@ -107,7 +124,7 @@ private static boolean compareTwoURLs(URL url1, URL url2) throws IOException {
107124
}
108125

109126
private static FileSystem createNewFileSystem() {
110-
URI resource = resourceNameToURI(RESOURCE_FILE1);
127+
URI resource = resourceNameToURI(RESOURCE_FILE_1);
111128

112129
Map<String, String> env = new HashMap<>();
113130
env.put("create", "true");
@@ -158,8 +175,8 @@ private static void closeFileSystem(FileSystem fileSystem) {
158175
@Test
159176
public void githubIssues() {
160177
try {
161-
URL url1 = resourceNameToURL(RESOURCE_FILE1);
162-
URL url2 = new URL(url1, RESOURCE_FILE2);
178+
URL url1 = resourceNameToURL(RESOURCE_FILE_1);
179+
URL url2 = new URL(url1, RESOURCE_FILE_2);
163180
Assert.assertNotNull("Second URL is null!", url2);
164181
Assert.assertFalse("Two URLs are same!", compareTwoURLs(url1, url2));
165182
} catch (IOException e) {
@@ -187,7 +204,7 @@ public void readingFileByteChannel() {
187204
FileSystem fileSystem = createNewFileSystem();
188205

189206
Path resourceDirectory = fileSystem.getPath(RESOURCE_DIR);
190-
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE1);
207+
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE_1);
191208

192209
// 2. Reading from file.
193210
try (SeekableByteChannel channel = Files.newByteChannel(resourceDirectory, StandardOpenOption.READ)) {
@@ -230,7 +247,7 @@ public void writingFileByteChannel() {
230247
FileSystem fileSystem = createNewFileSystem();
231248

232249
Path resourceDirectory = fileSystem.getPath(RESOURCE_DIR);
233-
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE1);
250+
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE_1);
234251

235252
// 2. Writing into file.
236253
try {
@@ -282,7 +299,7 @@ public void readingFileFileChannel() {
282299
FileSystem fileSystem = createNewFileSystem();
283300

284301
Path resourceDirectory = fileSystem.getPath(RESOURCE_DIR);
285-
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE1);
302+
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE_1);
286303

287304
// 2. Reading from file.
288305
Set<StandardOpenOption> permissions = Collections.singleton(StandardOpenOption.READ);
@@ -327,7 +344,7 @@ public void writingFileFileChannel() {
327344
FileSystemProvider provider = fileSystem.provider();
328345

329346
Path resourceDirectory = fileSystem.getPath(RESOURCE_DIR);
330-
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE1);
347+
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE_1);
331348

332349
Set<StandardOpenOption> readPermissions = Collections.singleton(StandardOpenOption.READ);
333350
Set<StandardOpenOption> writePermissions = Collections.singleton(StandardOpenOption.WRITE);
@@ -388,8 +405,8 @@ public void fileSystemOperations() {
388405
// 1. Creating new file system.
389406
FileSystem fileSystem = createNewFileSystem();
390407

391-
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE1);
392-
Path resourceFile2 = resourceNameToPath(RESOURCE_FILE2);
408+
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE_1);
409+
Path resourceFile2 = resourceNameToPath(RESOURCE_FILE_2);
393410

394411
// 2. Creating new directory.
395412
Path newDirectory = fileSystem.getPath(NEW_DIRECTORY);
@@ -488,7 +505,7 @@ public void readingFileAttributes() {
488505
FileSystem fileSystem = createNewFileSystem();
489506

490507
Path resourceDirectory = fileSystem.getPath(RESOURCE_DIR);
491-
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE1);
508+
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE_1);
492509

493510
try {
494511
// 2. Reading file attributes.
@@ -540,7 +557,7 @@ public void readingFileAttributes() {
540557
public void writingFileAttributes() {
541558
// 1. Creating new file system.
542559
FileSystem fileSystem = createNewFileSystem();
543-
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE1);
560+
Path resourceFile1 = resourceNameToPath(RESOURCE_FILE_1);
544561

545562
try {
546563
// 2. Writing file attributes.

0 commit comments

Comments
 (0)