Skip to content

Commit 50c216d

Browse files
committed
Add native-image API method RuntimeSystemProperties.register
1 parent 266073d commit 50c216d

File tree

7 files changed

+128
-4
lines changed

7 files changed

+128
-4
lines changed

sdk/src/org.graalvm.nativeimage/snapshot.sigtest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,10 @@ meth public static void registerLambdaCapturingClass(java.lang.Class<?>)
10651065
meth public static void registerWithTargetConstructorClass(java.lang.Class<?>,java.lang.Class<?>)
10661066
supr java.lang.Object
10671067

1068+
CLSS public final org.graalvm.nativeimage.hosted.RuntimeSystemProperties
1069+
meth public static void register(java.lang.String,java.lang.String)
1070+
supr java.lang.Object
1071+
10681072
CLSS public abstract interface org.graalvm.nativeimage.impl.InternalPlatform
10691073
innr public abstract interface static NATIVE_ONLY
10701074
innr public abstract interface static PLATFORM_JNI
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.hosted;
42+
43+
import java.util.Objects;
44+
45+
import org.graalvm.nativeimage.ImageSingletons;
46+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
47+
48+
public final class RuntimeSystemProperties {
49+
50+
/**
51+
* Define a system property setting for image runtime. This ensures a given key has the given
52+
* value at runtime even if no system property is set via command line of the image execution.
53+
*
54+
* @since 23.0
55+
*/
56+
public static void register(String key, String value) {
57+
Objects.requireNonNull(key);
58+
Objects.requireNonNull(value);
59+
ImageSingletons.lookup(RuntimeSystemPropertiesSupport.class).initializeProperty(key, value);
60+
}
61+
62+
private RuntimeSystemProperties() {
63+
}
64+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.impl;
42+
43+
public interface RuntimeSystemPropertiesSupport {
44+
void initializeProperty(String key, String value);
45+
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.graalvm.nativeimage.c.type.CCharPointer;
3131
import org.graalvm.nativeimage.c.type.CTypeConversion;
3232
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
33+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
3334
import org.graalvm.word.UnsignedWord;
3435
import org.graalvm.word.WordFactory;
3536

@@ -130,6 +131,8 @@ protected String osVersionValue() {
130131
class DarwinSystemPropertiesFeature implements InternalFeature {
131132
@Override
132133
public void duringSetup(DuringSetupAccess access) {
133-
ImageSingletons.add(SystemPropertiesSupport.class, new DarwinSystemPropertiesSupport());
134+
DarwinSystemPropertiesSupport systemPropertiesSupport = new DarwinSystemPropertiesSupport();
135+
ImageSingletons.add(SystemPropertiesSupport.class, systemPropertiesSupport);
136+
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, systemPropertiesSupport);
134137
}
135138
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxSystemPropertiesSupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.graalvm.nativeimage.c.type.CCharPointer;
2929
import org.graalvm.nativeimage.c.type.CTypeConversion;
3030
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
31+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
3132

3233
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
3334
import com.oracle.svm.core.feature.InternalFeature;
@@ -92,6 +93,8 @@ class LinuxSystemPropertiesFeature implements InternalFeature {
9293

9394
@Override
9495
public void duringSetup(DuringSetupAccess access) {
95-
ImageSingletons.add(SystemPropertiesSupport.class, new LinuxSystemPropertiesSupport());
96+
LinuxSystemPropertiesSupport systemPropertiesSupport = new LinuxSystemPropertiesSupport();
97+
ImageSingletons.add(SystemPropertiesSupport.class, systemPropertiesSupport);
98+
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, systemPropertiesSupport);
9699
}
97100
}

substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/WindowsSystemPropertiesSupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.graalvm.nativeimage.c.type.CTypeConversion;
3838
import org.graalvm.nativeimage.c.type.VoidPointer;
3939
import org.graalvm.nativeimage.c.type.WordPointer;
40+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
4041
import org.graalvm.word.UnsignedWord;
4142
import org.graalvm.word.WordFactory;
4243

@@ -390,6 +391,8 @@ public Pair<String, String> getOsNameAndVersion() {
390391
class WindowsSystemPropertiesFeature implements InternalFeature {
391392
@Override
392393
public void duringSetup(DuringSetupAccess access) {
393-
ImageSingletons.add(SystemPropertiesSupport.class, new WindowsSystemPropertiesSupport());
394+
WindowsSystemPropertiesSupport systemPropertiesSupport = new WindowsSystemPropertiesSupport();
395+
ImageSingletons.add(SystemPropertiesSupport.class, systemPropertiesSupport);
396+
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, systemPropertiesSupport);
394397
}
395398
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.graalvm.nativeimage.ImageSingletons;
3636
import org.graalvm.nativeimage.Platform;
3737
import org.graalvm.nativeimage.Platforms;
38+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
3839

3940
import com.oracle.svm.core.VM;
4041
import com.oracle.svm.core.config.ConfigurationValues;
@@ -49,7 +50,7 @@
4950
* the current working directory is quite expensive. We initialize such a property either when it is
5051
* explicitly accessed, or when all properties are accessed.
5152
*/
52-
public abstract class SystemPropertiesSupport {
53+
public abstract class SystemPropertiesSupport implements RuntimeSystemPropertiesSupport {
5354

5455
/** System properties that are taken from the VM hosting the image generator. */
5556
private static final String[] HOSTED_PROPERTIES = {
@@ -193,6 +194,7 @@ public void setProperties(Properties props) {
193194
* Initializes a property at startup from external input (e.g., command line arguments). This
194195
* must only be called while the runtime is single threaded.
195196
*/
197+
@Override
196198
public void initializeProperty(String key, String value) {
197199
savedProperties.put(key, value);
198200
properties.setProperty(key, value);

0 commit comments

Comments
 (0)