-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (C) 2009-2023 the original author(s). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fusesource.jansi.internal; | ||
|
||
import static org.fusesource.jansi.AnsiConsole.JANSI_PROVIDERS; | ||
|
||
public final class AnsiConsoleSupportHolder { | ||
|
||
private static final String PROVIDER_NAME; | ||
private static final AnsiConsoleSupport.CLibrary CLIBRARY; | ||
private static final AnsiConsoleSupport.Kernel32 KERNEL32; | ||
private static final Throwable ERR; | ||
|
||
private static AnsiConsoleSupport getDefaultProvider() { | ||
try { | ||
// Call the specialized constructor to check whether the module has native access enabled | ||
// If not, fallback to JNI to avoid the JDK printing warnings in stderr | ||
return (AnsiConsoleSupport) Class.forName("org.fusesource.jansi.internal.ffm.AnsiConsoleSupportImpl") | ||
.getConstructor(boolean.class) | ||
.newInstance(true); | ||
} catch (Throwable ignored) { | ||
} | ||
|
||
return new org.fusesource.jansi.internal.jni.AnsiConsoleSupportImpl(); | ||
} | ||
|
||
private static AnsiConsoleSupport findProvider(String providerList) { | ||
String[] providers = providerList.split(","); | ||
|
||
RuntimeException error = null; | ||
|
||
for (String provider : providers) { | ||
try { | ||
return (AnsiConsoleSupport) | ||
Class.forName("org.fusesource.jansi.internal." + provider + ".AnsiConsoleSupportImpl") | ||
.getConstructor() | ||
.newInstance(); | ||
} catch (Throwable t) { | ||
if (error == null) { | ||
error = new RuntimeException("Unable to create AnsiConsoleSupport provider"); | ||
} | ||
|
||
error.addSuppressed(t); | ||
} | ||
} | ||
|
||
// User does not specify any provider, falling back to the default | ||
if (error == null) { | ||
return getDefaultProvider(); | ||
} | ||
|
||
throw error; | ||
} | ||
|
||
static { | ||
String providerList = System.getProperty(JANSI_PROVIDERS); | ||
|
||
AnsiConsoleSupport ansiConsoleSupport = null; | ||
Throwable err = null; | ||
|
||
try { | ||
if (providerList == null) { | ||
ansiConsoleSupport = getDefaultProvider(); | ||
} else { | ||
ansiConsoleSupport = findProvider(providerList); | ||
} | ||
} catch (Throwable e) { | ||
err = e; | ||
} | ||
|
||
String providerName = null; | ||
AnsiConsoleSupport.CLibrary clib = null; | ||
AnsiConsoleSupport.Kernel32 kernel32 = null; | ||
|
||
if (ansiConsoleSupport != null) { | ||
try { | ||
providerName = ansiConsoleSupport.getProviderName(); | ||
clib = ansiConsoleSupport.getCLibrary(); | ||
kernel32 = OSInfo.isWindows() ? ansiConsoleSupport.getKernel32() : null; | ||
} catch (Throwable e) { | ||
err = e; | ||
} | ||
} | ||
|
||
PROVIDER_NAME = providerName; | ||
CLIBRARY = clib; | ||
KERNEL32 = kernel32; | ||
ERR = err; | ||
} | ||
|
||
public static String getProviderName() { | ||
return PROVIDER_NAME; | ||
} | ||
|
||
public static AnsiConsoleSupport.CLibrary getCLibrary() { | ||
if (CLIBRARY == null) { | ||
throw new RuntimeException("Unable to get the instance of CLibrary", ERR); | ||
} | ||
|
||
return CLIBRARY; | ||
} | ||
|
||
public static AnsiConsoleSupport.Kernel32 getKernel32() { | ||
if (KERNEL32 == null) { | ||
if (OSInfo.isWindows()) { | ||
throw new RuntimeException("Unable to get the instance of Kernel32", ERR); | ||
} else { | ||
throw new UnsupportedOperationException("Not Windows"); | ||
} | ||
} | ||
|
||
return KERNEL32; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,21 +13,30 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fusesource.jansi.ffm; | ||
package org.fusesource.jansi.internal.ffm; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
|
||
import org.fusesource.jansi.AnsiConsoleSupport; | ||
import org.fusesource.jansi.internal.AnsiConsoleSupport; | ||
import org.fusesource.jansi.internal.OSInfo; | ||
import org.fusesource.jansi.io.AnsiProcessor; | ||
|
||
import static org.fusesource.jansi.ffm.Kernel32.*; | ||
import static org.fusesource.jansi.internal.ffm.Kernel32.*; | ||
|
||
public final class AnsiConsoleSupportImpl implements AnsiConsoleSupport { | ||
|
||
public AnsiConsoleSupportImpl() {} | ||
|
||
public AnsiConsoleSupportImpl(boolean checkNativeAccess) { | ||
if (checkNativeAccess && !AnsiConsoleSupportImpl.class.getModule().isNativeAccessEnabled()) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Glavo
Author
Contributor
|
||
throw new UnsupportedOperationException("Native access is not enabled for the current module"); | ||
} | ||
} | ||
|
||
public class AnsiConsoleSupportFfm implements AnsiConsoleSupport { | ||
@Override | ||
public String getProviderName() { | ||
return "ffm"; | ||
|
@@ -88,7 +97,7 @@ public int getLastError() { | |
|
||
@Override | ||
public String getErrorMessage(int errorCode) { | ||
return org.fusesource.jansi.ffm.Kernel32.getErrorMessage(errorCode); | ||
return org.fusesource.jansi.internal.ffm.Kernel32.getErrorMessage(errorCode); | ||
} | ||
|
||
@Override | ||
|
@Glavo I actually have a problem with this line. On the standard macos terminal, it causes the FFM provider to not be selected by default when using the class path and not the module path (I do run
java -jar target/jansi-xxx.jar
). I wonder if this would need an additional check for modules or something similar ... ?