Skip to content

Commit

Permalink
Refactor Graal native-image resource: handling
Browse files Browse the repository at this point in the history
Detect the resource: scheme by checking the scheme of a well-known
resource instead of looking at a system property.

Rename NativeImagePathResource* to GraalIssue5720PathResource* and make
these classes package-private.

Signed-off-by: Christian Kohlschütter <[email protected]>
  • Loading branch information
kohlschuetter committed Jan 10, 2023
1 parent c74ad3c commit 8d0d327
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

/**
* GraalVM Native-Image {@link Path} Resource.
*
* @see <a href="https://github.com/oracle/graal/issues/5720">Graal issue 5720</a>
*/
public class NativeImagePathResource extends PathResource
final class GraalIssue5720PathResource extends PathResource
{
private static final String URI_BAD_RESOURCE_PREFIX = "file:///resources!";

NativeImagePathResource(Path path, URI uri, boolean bypassAllowedSchemeCheck)
GraalIssue5720PathResource(Path path, URI uri, boolean bypassAllowedSchemeCheck)
{
super(path, correctResourceURI(uri), (bypassAllowedSchemeCheck || isResourceScheme(uri)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand All @@ -24,14 +25,22 @@

/**
* GraalVM Native-Image {@link PathResourceFactory}.
*
* @see <a href="https://github.com/oracle/graal/issues/5720">Graal issue 5720</a>
*/
public class NativeImagePathResourceFactory extends PathResourceFactory
final class GraalIssue5720PathResourceFactory extends PathResourceFactory
{
static final boolean ENABLE_GRAALVM_RESOURCE_SCHEME = (System.getProperty("org.graalvm.nativeimage.kind") != null);
static final boolean ENABLE_NATIVE_IMAGE_RESOURCE_SCHEME;

public NativeImagePathResourceFactory()
static
{
if (ENABLE_GRAALVM_RESOURCE_SCHEME)
URL url = GraalIssue5720PathResourceFactory.class.getResource("/org/eclipse/jetty/version/build.properties");
ENABLE_NATIVE_IMAGE_RESOURCE_SCHEME = (url != null && "resource".equals(url.getProtocol()));
}

public GraalIssue5720PathResourceFactory()
{
if (ENABLE_NATIVE_IMAGE_RESOURCE_SCHEME)
{
initNativeImageResourceFileSystem();
}
Expand Down Expand Up @@ -60,12 +69,12 @@ private void initNativeImageResourceFileSystem()
@Override
public Resource newResource(URI uri)
{
uri = NativeImagePathResource.correctResourceURI(uri.normalize());
uri = GraalIssue5720PathResource.correctResourceURI(uri.normalize());
Path path = Path.of(uri);

if (!Files.exists(path))
return null;

return new NativeImagePathResource(path, uri, false);
return new GraalIssue5720PathResource(path, uri, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class ResourceFactoryInternals
RESOURCE_FACTORIES.put("file", pathResourceFactory);
RESOURCE_FACTORIES.put("jrt", pathResourceFactory);

if (NativeImagePathResourceFactory.ENABLE_GRAALVM_RESOURCE_SCHEME)
RESOURCE_FACTORIES.put("resource", new NativeImagePathResourceFactory());
if (GraalIssue5720PathResourceFactory.ENABLE_NATIVE_IMAGE_RESOURCE_SCHEME)
RESOURCE_FACTORIES.put("resource", new GraalIssue5720PathResourceFactory());
}

static ResourceFactory ROOT = new CompositeResourceFactory()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"resources":{
"includes":[
{
"pattern":"\\Qorg/eclipse/jetty/version/build.properties\\E"
}
]},
"bundles":[]
}

0 comments on commit 8d0d327

Please sign in to comment.