|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2023 the original author(s). |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.fusesource.jansi.internal; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +import org.fusesource.jansi.AnsiConsole; |
| 21 | +import org.graalvm.nativeimage.hosted.Feature; |
| 22 | +import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; |
| 23 | +import org.graalvm.nativeimage.hosted.RuntimeSystemProperties; |
| 24 | + |
| 25 | +public class NativeImageFeature implements Feature { |
| 26 | + @Override |
| 27 | + public String getURL() { |
| 28 | + return "https://github.com/fusesource/jansi"; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void duringSetup(DuringSetupAccess access) { |
| 33 | + RuntimeClassInitialization.initializeAtBuildTime(AnsiConsoleSupportHolder.class); |
| 34 | + |
| 35 | + String providers = System.getProperty(AnsiConsole.JANSI_PROVIDERS); |
| 36 | + if (providers != null) { |
| 37 | + try { |
| 38 | + RuntimeSystemProperties.register(AnsiConsole.JANSI_PROVIDERS, providers); |
| 39 | + } catch (Throwable ignored) { |
| 40 | + // GraalVM version < 23.0 |
| 41 | + // No need to worry as we select the provider at build time |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + String provider = Objects.requireNonNull(AnsiConsoleSupportHolder.getProviderName(), "No provider available"); |
| 46 | + if (provider.equals(AnsiConsole.JANSI_PROVIDER_JNI)) { |
| 47 | + String jansiNativeLibraryName = System.mapLibraryName("jansi"); |
| 48 | + if (jansiNativeLibraryName.endsWith(".dylib")) { |
| 49 | + jansiNativeLibraryName = jansiNativeLibraryName.replace(".dylib", ".jnilib"); |
| 50 | + } |
| 51 | + |
| 52 | + String packagePath = JansiLoader.class.getPackage().getName().replace('.', '/'); |
| 53 | + |
| 54 | + try { |
| 55 | + Class<?> moduleClass = Class.forName("java.lang.Module"); |
| 56 | + Class<?> rraClass = Class.forName("org.graalvm.nativeimage.hosted.RuntimeResourceAccess"); |
| 57 | + |
| 58 | + Object module = Class.class.getMethod("getModule").invoke(JansiLoader.class); |
| 59 | + rraClass.getMethod("addResource", moduleClass, String.class) |
| 60 | + .invoke( |
| 61 | + null, |
| 62 | + module, |
| 63 | + String.format( |
| 64 | + "%s/native/%s/%s", |
| 65 | + packagePath, |
| 66 | + OSInfo.getNativeLibFolderPathForCurrentOS(), |
| 67 | + jansiNativeLibraryName)); |
| 68 | + |
| 69 | + } catch (Throwable ignored) { |
| 70 | + // GraalVM version < 22.3 |
| 71 | + // Users need to manually add the JNI library as resources |
| 72 | + } |
| 73 | + } else if (provider.equals(AnsiConsole.JANSI_PROVIDER_FFM)) { |
| 74 | + try { |
| 75 | + // FFM is only available in JDK 21+, so we need to compile it separately |
| 76 | + Class.forName("org.fusesource.jansi.internal.ffm.NativeImageDowncallRegister") |
| 77 | + .getMethod("registerForDowncall") |
| 78 | + .invoke(null); |
| 79 | + } catch (Throwable e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments