Skip to content

Commit 6931388

Browse files
Improve resource support. Implemenation of resource in-memory file system.
1 parent 2f811ae commit 6931388

31 files changed

+4549
-285
lines changed

Diff for: substratevm/mx.substratevm/suite.py

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
"jdk.internal.module",
207207
"jdk.internal.misc",
208208
"jdk.internal.logger",
209+
"jdk.internal.loader",
209210
"sun.util.resources",
210211
"sun.text.spi",
211212
"jdk.internal.perf",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.core.jdk;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.net.URL;
31+
import java.net.URLConnection;
32+
33+
public class JDKVersionSpecificResourceBuilder {
34+
35+
public static Object buildResource(String name, URL url, URLConnection urlConnection) {
36+
return new jdk.internal.loader.Resource() {
37+
38+
@Override
39+
public String getName() {
40+
return name;
41+
}
42+
43+
@Override
44+
public URL getURL() {
45+
return url;
46+
}
47+
48+
@Override
49+
public URL getCodeSourceURL() {
50+
// We are deleting resource URL class path during native image build,
51+
// so in runtime we don't have this information.
52+
return null;
53+
}
54+
55+
@Override
56+
public InputStream getInputStream() throws IOException {
57+
return urlConnection.getInputStream();
58+
}
59+
60+
@Override
61+
public int getContentLength() throws IOException {
62+
return urlConnection.getContentLength();
63+
}
64+
};
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.core.jdk;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.net.URL;
31+
import java.net.URLConnection;
32+
33+
public class JDKVersionSpecificResourceBuilder {
34+
35+
public static Object buildResource(String name, URL url, URLConnection urlConnection) {
36+
return new sun.misc.Resource() {
37+
38+
@Override
39+
public String getName() {
40+
return name;
41+
}
42+
43+
@Override
44+
public URL getURL() {
45+
return url;
46+
}
47+
48+
@Override
49+
public URL getCodeSourceURL() {
50+
return null;
51+
}
52+
53+
@Override
54+
public InputStream getInputStream() throws IOException {
55+
return urlConnection.getInputStream();
56+
}
57+
58+
@Override
59+
public int getContentLength() throws IOException {
60+
return urlConnection.getContentLength();
61+
}
62+
};
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystemProvider

Diff for: substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

+10-28
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
//Checkstyle: allow reflection
2828

29-
import java.io.ByteArrayInputStream;
3029
import java.io.File;
3130
import java.io.InputStream;
3231
import java.io.Serializable;
@@ -81,7 +80,6 @@
8180
import com.oracle.svm.core.jdk.JDK16OrLater;
8281
import com.oracle.svm.core.jdk.JDK8OrEarlier;
8382
import com.oracle.svm.core.jdk.Package_jdk_internal_reflect;
84-
import com.oracle.svm.core.jdk.Resources;
8583
import com.oracle.svm.core.jdk.Target_java_lang_Module;
8684
import com.oracle.svm.core.jdk.Target_jdk_internal_reflect_Reflection;
8785
import com.oracle.svm.core.meta.SharedType;
@@ -720,34 +718,18 @@ public Enum<?>[] getEnumConstantsShared() {
720718
return (Enum<?>[]) enumConstantsReference;
721719
}
722720

723-
@Substitute
724-
private InputStream getResourceAsStream(String resourceName) {
725-
final String path = resolveName(getName(), resourceName);
726-
List<byte[]> arr = Resources.get(path);
727-
return arr == null ? null : new ByteArrayInputStream(arr.get(0));
728-
}
721+
@KeepOriginal
722+
public native URL getResource(String resourceName);
729723

730-
@Substitute
731-
private URL getResource(String resourceName) {
732-
final String path = resolveName(getName(), resourceName);
733-
List<byte[]> arr = Resources.get(path);
734-
return arr == null ? null : Resources.createURL(path, arr.get(0));
735-
}
724+
@KeepOriginal
725+
public native InputStream getResourceAsStream(String resourceName);
736726

737-
private String resolveName(String baseName, String resourceName) {
738-
if (resourceName == null) {
739-
return resourceName;
740-
}
741-
if (resourceName.startsWith("/")) {
742-
return resourceName.substring(1);
743-
}
744-
int index = baseName.lastIndexOf('.');
745-
if (index != -1) {
746-
return baseName.substring(0, index).replace('.', '/') + "/" + resourceName;
747-
} else {
748-
return resourceName;
749-
}
750-
}
727+
@KeepOriginal
728+
private native String resolveName(String resourceName);
729+
730+
@KeepOriginal
731+
@TargetElement(name = "isOpenToCaller", onlyWith = JDK11OrLater.class)
732+
private native boolean isOpenToCaller(String resourceName, Class<?> caller);
751733

752734
@KeepOriginal
753735
private native ClassLoader getClassLoader();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.core.jdk;
27+
28+
import java.net.URL;
29+
import java.net.URLConnection;
30+
31+
public class JDKVersionSpecificResourceBuilder {
32+
33+
@SuppressWarnings("unused")
34+
public static Object buildResource(String name, URL url, URLConnection urlConnection) {
35+
return null;
36+
}
37+
}

Diff for: substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangSubstitutions.java

-24
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
import static com.oracle.svm.core.snippets.KnownIntrinsics.readHub;
3030

3131
import java.io.File;
32-
import java.io.IOException;
3332
import java.io.InputStream;
3433
import java.io.PrintStream;
3534
import java.net.URL;
36-
import java.util.Enumeration;
3735
import java.util.Map;
3836
import java.util.Properties;
3937
import java.util.concurrent.ConcurrentHashMap;
@@ -726,28 +724,6 @@ private static boolean hasClassPath() {
726724
return true;
727725
}
728726

729-
@SuppressWarnings("unused")
730-
@Substitute
731-
private static URL findResource(String mn, String name) {
732-
return ClassLoader.getSystemClassLoader().getResource(name);
733-
}
734-
735-
@SuppressWarnings("unused")
736-
@Substitute
737-
private static InputStream findResourceAsStream(String mn, String name) {
738-
return ClassLoader.getSystemClassLoader().getResourceAsStream(name);
739-
}
740-
741-
@Substitute
742-
private static URL findResource(String name) {
743-
return ClassLoader.getSystemClassLoader().getResource(name);
744-
}
745-
746-
@Substitute
747-
private static Enumeration<URL> findResources(String name) throws IOException {
748-
return ClassLoader.getSystemClassLoader().getResources(name);
749-
}
750-
751727
/**
752728
* All ClassLoaderValue are reset at run time for now. See also
753729
* {@link Target_java_lang_ClassLoader#classLoaderValueMap} for resetting of individual class

Diff for: substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaNetSubstitutions.java

+3-29
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626

2727
// Checkstyle: allow reflection
2828

29-
import java.io.ByteArrayInputStream;
30-
import java.io.FileNotFoundException;
31-
import java.io.IOException;
32-
import java.io.InputStream;
3329
import java.net.MalformedURLException;
3430
import java.net.URL;
3531
import java.net.URLConnection;
@@ -58,6 +54,7 @@
5854
import com.oracle.svm.core.annotate.TargetClass;
5955
import com.oracle.svm.core.c.CGlobalData;
6056
import com.oracle.svm.core.c.CGlobalDataFactory;
57+
import com.oracle.svm.core.jdk.resources.ResourceURLConnection;
6158
import com.oracle.svm.core.option.OptionUtils;
6259
import com.oracle.svm.core.option.SubstrateOptionsParser;
6360
import com.oracle.svm.core.util.VMError;
@@ -240,31 +237,8 @@ static URLStreamHandler getURLStreamHandler(String protocol) throws MalformedURL
240237
static URLStreamHandler createResourcesURLStreamHandler() {
241238
return new URLStreamHandler() {
242239
@Override
243-
protected URLConnection openConnection(URL url) throws IOException {
244-
return new URLConnection(url) {
245-
private InputStream in;
246-
247-
@Override
248-
public void connect() throws IOException {
249-
if (connected) {
250-
return;
251-
}
252-
connected = true;
253-
// remove "resource:" from url to get the resource name
254-
String resName = url.toString().substring(1 + JavaNetSubstitutions.RESOURCE_PROTOCOL.length());
255-
final List<byte[]> bytes = Resources.get(resName);
256-
if (bytes == null || bytes.size() < 1) {
257-
throw new FileNotFoundException(url.toString());
258-
}
259-
in = new ByteArrayInputStream(bytes.get(0));
260-
}
261-
262-
@Override
263-
public InputStream getInputStream() throws IOException {
264-
connect();
265-
return in;
266-
}
267-
};
240+
protected URLConnection openConnection(URL url) {
241+
return new ResourceURLConnection(url);
268242
}
269243
};
270244
}

0 commit comments

Comments
 (0)