Skip to content

Commit

Permalink
Add native-image API method RuntimeSystemProperties.register
Browse files Browse the repository at this point in the history
  • Loading branch information
olpaw committed Dec 2, 2022
1 parent 266073d commit 524c223
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 8 deletions.
4 changes: 4 additions & 0 deletions sdk/src/org.graalvm.nativeimage/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,10 @@ meth public static void registerLambdaCapturingClass(java.lang.Class<?>)
meth public static void registerWithTargetConstructorClass(java.lang.Class<?>,java.lang.Class<?>)
supr java.lang.Object

CLSS public final org.graalvm.nativeimage.hosted.RuntimeSystemProperties
meth public static void register(java.lang.String,java.lang.String)
supr java.lang.Object

CLSS public abstract interface org.graalvm.nativeimage.impl.InternalPlatform
innr public abstract interface static NATIVE_ONLY
innr public abstract interface static PLATFORM_JNI
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.nativeimage.hosted;

import java.util.Objects;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;

public class RuntimeSystemProperties {

/**
* Define a system property setting for image runtime. This ensures a given key has the given
* value at runtime even if no system property is set via command line of the image execution.
*
* @since 23.0
*/
public static void register(String key, String value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
ImageSingletons.lookup(RuntimeSystemPropertiesSupport.class).initializeProperty(key, value);
}

private RuntimeSystemProperties() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.nativeimage.impl;

public interface RuntimeSystemPropertiesSupport {
void initializeProperty(String key, String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

Expand Down Expand Up @@ -130,6 +131,8 @@ protected String osVersionValue() {
class DarwinSystemPropertiesFeature implements InternalFeature {
@Override
public void duringSetup(DuringSetupAccess access) {
ImageSingletons.add(SystemPropertiesSupport.class, new DarwinSystemPropertiesSupport());
DarwinSystemPropertiesSupport systemPropertiesSupport = new DarwinSystemPropertiesSupport();
ImageSingletons.add(SystemPropertiesSupport.class, systemPropertiesSupport);
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, systemPropertiesSupport);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;

import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
import com.oracle.svm.core.feature.InternalFeature;
Expand Down Expand Up @@ -92,6 +93,8 @@ class LinuxSystemPropertiesFeature implements InternalFeature {

@Override
public void duringSetup(DuringSetupAccess access) {
ImageSingletons.add(SystemPropertiesSupport.class, new LinuxSystemPropertiesSupport());
LinuxSystemPropertiesSupport systemPropertiesSupport = new LinuxSystemPropertiesSupport();
ImageSingletons.add(SystemPropertiesSupport.class, systemPropertiesSupport);
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, systemPropertiesSupport);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.VoidPointer;
import org.graalvm.nativeimage.c.type.WordPointer;
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

Expand Down Expand Up @@ -390,6 +391,8 @@ public Pair<String, String> getOsNameAndVersion() {
class WindowsSystemPropertiesFeature implements InternalFeature {
@Override
public void duringSetup(DuringSetupAccess access) {
ImageSingletons.add(SystemPropertiesSupport.class, new WindowsSystemPropertiesSupport());
WindowsSystemPropertiesSupport systemPropertiesSupport = new WindowsSystemPropertiesSupport();
ImageSingletons.add(SystemPropertiesSupport.class, systemPropertiesSupport);
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, systemPropertiesSupport);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;

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

/** System properties that are taken from the VM hosting the image generator. */
private static final String[] HOSTED_PROPERTIES = {
Expand Down Expand Up @@ -193,6 +194,7 @@ public void setProperties(Properties props) {
* Initializes a property at startup from external input (e.g., command line arguments). This
* must only be called while the runtime is single threaded.
*/
@Override
public void initializeProperty(String key, String value) {
savedProperties.put(key, value);
properties.setProperty(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

import java.util.Arrays;

import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.jdk.SystemPropertiesSupport;
import org.graalvm.nativeimage.hosted.RuntimeSystemProperties;

public final class RuntimePropertyParser {

Expand Down Expand Up @@ -69,7 +67,7 @@ private static boolean parseProperty(String property) {
String key = property.substring(0, splitIndex);
String value = property.substring(splitIndex + 1);

ImageSingletons.lookup(SystemPropertiesSupport.class).initializeProperty(key, value);
RuntimeSystemProperties.register(key, value);

return true;
}
Expand Down

0 comments on commit 524c223

Please sign in to comment.