Skip to content

Commit d34f64e

Browse files
Add getExecutableURL method to ProcessPropertiesSupport
Signed-off-by: Lazar Mitrović <[email protected]>
1 parent 8410f47 commit d34f64e

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/ProcessPropertiesSupport.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@
4040
*/
4141
package org.graalvm.nativeimage.impl;
4242

43+
import java.net.URL;
4344
import java.nio.file.Path;
4445

4546
import org.graalvm.nativeimage.c.function.CEntryPointLiteral;
4647

4748
public interface ProcessPropertiesSupport {
48-
default String getExecutableName() {
49-
return "java";
49+
String getExecutableName();
50+
51+
default URL getExecutableURL() {
52+
return null;
5053
}
5154

5255
long getProcessID();

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/BaseProcessPropertiesSupport.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,7 +27,23 @@
2727

2828
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
2929

30+
import java.io.File;
31+
import java.net.MalformedURLException;
32+
import java.net.URL;
33+
3034
public abstract class BaseProcessPropertiesSupport implements ProcessPropertiesSupport {
35+
36+
@Override
37+
public URL getExecutableURL() {
38+
try {
39+
return new File(getExecutableName()).toURI().toURL();
40+
} catch (MalformedURLException ex) {
41+
// This should not really happen; the file is cannonicalized, absolute, so it should
42+
// always have file:// URL.
43+
return null;
44+
}
45+
}
46+
3147
@Override
3248
public int getArgumentVectorBlockSize() {
3349
return JavaMainWrapper.getCRuntimeArgumentBlockLength();

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,6 @@
2626

2727
//Checkstyle: allow reflection
2828

29-
import java.io.File;
3029
import java.io.InputStream;
3130
import java.io.Serializable;
3231
import java.lang.annotation.Annotation;
@@ -43,7 +42,6 @@
4342
import java.lang.reflect.Modifier;
4443
import java.lang.reflect.Type;
4544
import java.lang.reflect.TypeVariable;
46-
import java.net.MalformedURLException;
4745
import java.net.URL;
4846
import java.security.CodeSource;
4947
import java.security.ProtectionDomain;
@@ -58,10 +56,11 @@
5856
import org.graalvm.compiler.core.common.NumUtil;
5957
import org.graalvm.compiler.core.common.SuppressFBWarnings;
6058
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
59+
import org.graalvm.nativeimage.ImageSingletons;
6160
import org.graalvm.nativeimage.Platform;
6261
import org.graalvm.nativeimage.Platforms;
63-
import org.graalvm.nativeimage.ProcessProperties;
6462
import org.graalvm.nativeimage.c.function.CFunctionPointer;
63+
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
6564
import org.graalvm.util.DirectAnnotationAccess;
6665

6766
import com.oracle.svm.core.RuntimeAssertionsSupport;
@@ -336,16 +335,12 @@ public void setModule(Object module) {
336335
java.security.Permissions perms = new java.security.Permissions();
337336
perms.add(SecurityConstants.ALL_PERMISSION);
338337
CodeSource cs;
339-
try {
340-
// Try to use executable image's name as code source for the class.
341-
// The file location can be used by Java code to determine its location on disk, similar
342-
// to argv[0].
343-
cs = new CodeSource(new File(ProcessProperties.getExecutableName()).toURI().toURL(), (Certificate[]) null);
344-
} catch (MalformedURLException ex) {
345-
// This should not really happen; the file is cannonicalized, absolute, so it should
346-
// always have file:// URL.
347-
cs = null;
348-
}
338+
339+
// Try to use executable image's name as code source for the class.
340+
// The file location can be used by Java code to determine its location on disk, similar
341+
// to argv[0].
342+
cs = new CodeSource(ImageSingletons.lookup(ProcessPropertiesSupport.class).getExecutableURL(), (Certificate[]) null);
343+
349344
return new java.security.ProtectionDomain(cs, perms);
350345
});
351346

0 commit comments

Comments
 (0)