Skip to content

Commit

Permalink
Improve resource support. Implemenation of resource in-memory file sy…
Browse files Browse the repository at this point in the history
…stem.
  • Loading branch information
jovanstevanovic committed Jun 10, 2021
1 parent 2f811ae commit 6931388
Show file tree
Hide file tree
Showing 31 changed files with 4,549 additions and 285 deletions.
1 change: 1 addition & 0 deletions substratevm/mx.substratevm/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
"jdk.internal.module",
"jdk.internal.misc",
"jdk.internal.logger",
"jdk.internal.loader",
"sun.util.resources",
"sun.text.spi",
"jdk.internal.perf",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2021, 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.svm.core.jdk;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class JDKVersionSpecificResourceBuilder {

public static Object buildResource(String name, URL url, URLConnection urlConnection) {
return new jdk.internal.loader.Resource() {

@Override
public String getName() {
return name;
}

@Override
public URL getURL() {
return url;
}

@Override
public URL getCodeSourceURL() {
// We are deleting resource URL class path during native image build,
// so in runtime we don't have this information.
return null;
}

@Override
public InputStream getInputStream() throws IOException {
return urlConnection.getInputStream();
}

@Override
public int getContentLength() throws IOException {
return urlConnection.getContentLength();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2021, 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.svm.core.jdk;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class JDKVersionSpecificResourceBuilder {

public static Object buildResource(String name, URL url, URLConnection urlConnection) {
return new sun.misc.Resource() {

@Override
public String getName() {
return name;
}

@Override
public URL getURL() {
return url;
}

@Override
public URL getCodeSourceURL() {
return null;
}

@Override
public InputStream getInputStream() throws IOException {
return urlConnection.getInputStream();
}

@Override
public int getContentLength() throws IOException {
return urlConnection.getContentLength();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystemProvider
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

//Checkstyle: allow reflection

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
Expand Down Expand Up @@ -81,7 +80,6 @@
import com.oracle.svm.core.jdk.JDK16OrLater;
import com.oracle.svm.core.jdk.JDK8OrEarlier;
import com.oracle.svm.core.jdk.Package_jdk_internal_reflect;
import com.oracle.svm.core.jdk.Resources;
import com.oracle.svm.core.jdk.Target_java_lang_Module;
import com.oracle.svm.core.jdk.Target_jdk_internal_reflect_Reflection;
import com.oracle.svm.core.meta.SharedType;
Expand Down Expand Up @@ -720,34 +718,18 @@ public Enum<?>[] getEnumConstantsShared() {
return (Enum<?>[]) enumConstantsReference;
}

@Substitute
private InputStream getResourceAsStream(String resourceName) {
final String path = resolveName(getName(), resourceName);
List<byte[]> arr = Resources.get(path);
return arr == null ? null : new ByteArrayInputStream(arr.get(0));
}
@KeepOriginal
public native URL getResource(String resourceName);

@Substitute
private URL getResource(String resourceName) {
final String path = resolveName(getName(), resourceName);
List<byte[]> arr = Resources.get(path);
return arr == null ? null : Resources.createURL(path, arr.get(0));
}
@KeepOriginal
public native InputStream getResourceAsStream(String resourceName);

private String resolveName(String baseName, String resourceName) {
if (resourceName == null) {
return resourceName;
}
if (resourceName.startsWith("/")) {
return resourceName.substring(1);
}
int index = baseName.lastIndexOf('.');
if (index != -1) {
return baseName.substring(0, index).replace('.', '/') + "/" + resourceName;
} else {
return resourceName;
}
}
@KeepOriginal
private native String resolveName(String resourceName);

@KeepOriginal
@TargetElement(name = "isOpenToCaller", onlyWith = JDK11OrLater.class)
private native boolean isOpenToCaller(String resourceName, Class<?> caller);

@KeepOriginal
private native ClassLoader getClassLoader();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021, 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.svm.core.jdk;

import java.net.URL;
import java.net.URLConnection;

public class JDKVersionSpecificResourceBuilder {

@SuppressWarnings("unused")
public static Object buildResource(String name, URL url, URLConnection urlConnection) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
import static com.oracle.svm.core.snippets.KnownIntrinsics.readHub;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -726,28 +724,6 @@ private static boolean hasClassPath() {
return true;
}

@SuppressWarnings("unused")
@Substitute
private static URL findResource(String mn, String name) {
return ClassLoader.getSystemClassLoader().getResource(name);
}

@SuppressWarnings("unused")
@Substitute
private static InputStream findResourceAsStream(String mn, String name) {
return ClassLoader.getSystemClassLoader().getResourceAsStream(name);
}

@Substitute
private static URL findResource(String name) {
return ClassLoader.getSystemClassLoader().getResource(name);
}

@Substitute
private static Enumeration<URL> findResources(String name) throws IOException {
return ClassLoader.getSystemClassLoader().getResources(name);
}

/**
* All ClassLoaderValue are reset at run time for now. See also
* {@link Target_java_lang_ClassLoader#classLoaderValueMap} for resetting of individual class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

// Checkstyle: allow reflection

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -58,6 +54,7 @@
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.c.CGlobalData;
import com.oracle.svm.core.c.CGlobalDataFactory;
import com.oracle.svm.core.jdk.resources.ResourceURLConnection;
import com.oracle.svm.core.option.OptionUtils;
import com.oracle.svm.core.option.SubstrateOptionsParser;
import com.oracle.svm.core.util.VMError;
Expand Down Expand Up @@ -240,31 +237,8 @@ static URLStreamHandler getURLStreamHandler(String protocol) throws MalformedURL
static URLStreamHandler createResourcesURLStreamHandler() {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
return new URLConnection(url) {
private InputStream in;

@Override
public void connect() throws IOException {
if (connected) {
return;
}
connected = true;
// remove "resource:" from url to get the resource name
String resName = url.toString().substring(1 + JavaNetSubstitutions.RESOURCE_PROTOCOL.length());
final List<byte[]> bytes = Resources.get(resName);
if (bytes == null || bytes.size() < 1) {
throw new FileNotFoundException(url.toString());
}
in = new ByteArrayInputStream(bytes.get(0));
}

@Override
public InputStream getInputStream() throws IOException {
connect();
return in;
}
};
protected URLConnection openConnection(URL url) {
return new ResourceURLConnection(url);
}
};
}
Expand Down
Loading

0 comments on commit 6931388

Please sign in to comment.