Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Oct 9, 2023
2 parents 4097d8a + c490b06 commit b7c0487
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 105 deletions.
22 changes: 11 additions & 11 deletions src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.fusesource.jansi.io.AnsiProcessor;
import org.fusesource.jansi.io.FastBufferedOutputStream;

import static org.fusesource.jansi.internal.AnsiConsoleSupport.CLibrary.STDERR_FILENO;
import static org.fusesource.jansi.internal.AnsiConsoleSupport.CLibrary.STDOUT_FILENO;
import static org.fusesource.jansi.internal.AnsiConsoleSupportHolder.getCLibrary;
import static org.fusesource.jansi.internal.AnsiConsoleSupportHolder.getKernel32;

Expand Down Expand Up @@ -239,10 +241,6 @@ public static int getTerminalWidth() {

static final int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;

static int STDOUT_FILENO = 1;

static int STDERR_FILENO = 2;

static {
if (getBoolean(JANSI_EAGER)) {
initStreams();
Expand All @@ -267,8 +265,6 @@ private static AnsiPrintStream ansiStream(boolean stdout) {
final boolean isatty;
boolean isAtty;
boolean withException;
// Do not use the CLibrary.STDOUT_FILENO to avoid errors in case
// the library can not be loaded on unsupported platforms
final int fd = stdout ? STDOUT_FILENO : STDERR_FILENO;
try {
// If we can detect that stdout is not a tty, then setup
Expand All @@ -280,11 +276,15 @@ private static AnsiPrintStream ansiStream(boolean stdout) {
isAtty = false;
}
withException = false;
} catch (Throwable ignore) {
// These errors happen if the JNI lib is not available for your platform.
// But since we are on ANSI friendly platform, assume the user is on the console.
isAtty = false;
withException = true;
} catch (Throwable e) {
// These errors happen if there are no available providers.
// If the jansi.graceful system property is set to true, fall back to pure emulation.
if (Boolean.parseBoolean(System.getProperty(AnsiConsole.JANSI_GRACEFUL, "true"))) {
isAtty = false;
withException = true;
} else {
throw e;
}
}
isatty = isAtty;

Expand Down
32 changes: 14 additions & 18 deletions src/main/java/org/fusesource/jansi/AnsiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,25 @@ public static void main(String... args) throws IOException {
System.out.println();

System.out.println("jansi.providers= " + System.getProperty(AnsiConsole.JANSI_PROVIDERS, ""));
String provider = AnsiConsoleSupportHolder.getProviderName();

String provider = AnsiConsoleSupportHolder.getProvider() != null
? AnsiConsoleSupportHolder.getProvider().getProviderName()
: "No Provider Available";
System.out.println("Selected provider: " + provider);

if (AnsiConsole.JANSI_PROVIDER_JNI.equals(provider)) {
// info on native library
System.out.println("library.jansi.path= " + System.getProperty("library.jansi.path", ""));
System.out.println("library.jansi.version= " + System.getProperty("library.jansi.version", ""));
boolean loaded = JansiLoader.initialize();
if (loaded) {

try {
JansiLoader.initialize(false);
System.out.println("Jansi native library loaded from " + JansiLoader.getNativeLibraryPath());
if (JansiLoader.getNativeLibrarySourceUrl() != null) {
System.out.println(" which was auto-extracted from " + JansiLoader.getNativeLibrarySourceUrl());
}
} else {
String prev = System.getProperty(AnsiConsole.JANSI_GRACEFUL);
try {
System.setProperty(AnsiConsole.JANSI_GRACEFUL, "false");
JansiLoader.initialize();
} catch (Throwable e) {
e.printStackTrace(System.out);
} finally {
if (prev != null) {
System.setProperty(AnsiConsole.JANSI_GRACEFUL, prev);
} else {
System.clearProperty(AnsiConsole.JANSI_GRACEFUL);
}
}
} catch (Throwable e) {
e.printStackTrace(System.out);
}
}

Expand Down Expand Up @@ -204,7 +196,11 @@ private static String getJansiVersion() {
private static void diagnoseTty(boolean stderr) {
int isatty;
int width;
if (AnsiConsole.IS_WINDOWS) {

if (AnsiConsoleSupportHolder.getProvider() == null) {
isatty = 0;
width = 0;
} else if (AnsiConsole.IS_WINDOWS) {
long console = AnsiConsoleSupportHolder.getKernel32().getStdHandle(!stderr);
isatty = AnsiConsoleSupportHolder.getKernel32().isTty(console);
if ((AnsiConsole.IS_CONEMU || AnsiConsole.IS_CYGWIN || AnsiConsole.IS_MSYSTEM) && isatty == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public final Kernel32 getKernel32() {

return kernel32;
}

@Override
public String toString() {
return getProviderName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public final class AnsiConsoleSupportHolder {

static final AnsiConsoleSupport PROVIDER;

static final Throwable ERR;

private static AnsiConsoleSupport getDefaultProvider() {
Expand Down Expand Up @@ -85,15 +84,12 @@ private static AnsiConsoleSupport findProvider(String providerList) {
ERR = err;
}

public static AnsiConsoleSupport getProvider() {
if (PROVIDER == null) {
throw new RuntimeException("No provider available", ERR);
}
return PROVIDER;
public static boolean isAvailable() {
return PROVIDER != null;
}

public static String getProviderName() {
return getProvider().getProviderName();
public static AnsiConsoleSupport getProvider() {
return PROVIDER;
}

public static AnsiConsoleSupport.CLibrary getCLibrary() {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/fusesource/jansi/internal/CLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CLibrary {
public static final boolean LOADED;

static {
LOADED = JansiLoader.initialize();
LOADED = JansiLoader.initialize(true);
if (LOADED) {
init();
}
Expand Down Expand Up @@ -113,8 +113,9 @@ public static short getTerminalWidth(int fd) {
public static class WinSize {

static {
JansiLoader.initialize();
init();
if (JansiLoader.initialize(true)) {
init();
}
}

private static native void init();
Expand Down Expand Up @@ -143,8 +144,9 @@ public WinSize(short ws_row, short ws_col) {
public static class Termios {

static {
JansiLoader.initialize();
init();
if (JansiLoader.initialize(true)) {
init();
}
}

private static native void init();
Expand Down
84 changes: 40 additions & 44 deletions src/main/java/org/fusesource/jansi/internal/JansiLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Random;
Expand All @@ -60,34 +58,50 @@
*/
public class JansiLoader {

private static boolean loaded = false;
private static String nativeLibraryPath;
private static String nativeLibrarySourceUrl;

private static final class Holder {
static final Throwable ERROR;

static {
Thread cleanup = new Thread(JansiLoader::cleanup, "cleanup");
cleanup.setPriority(Thread.MIN_PRIORITY);
cleanup.setDaemon(true);
cleanup.start();

Throwable error = null;
try {
loadJansiNativeLibrary();
} catch (Exception e) {
error = e;
}

ERROR = error;
}
}

/**
* Loads Jansi native library.
*
* @return True if jansi native library is successfully loaded; false
* otherwise.
*/
public static synchronized boolean initialize() {
// only cleanup before the first extract
if (!loaded) {
Thread cleanup = new Thread(JansiLoader::cleanup, "cleanup");
cleanup.setPriority(Thread.MIN_PRIORITY);
cleanup.setDaemon(true);
cleanup.start();
}
try {
loadJansiNativeLibrary();
} catch (Exception e) {
if (!Boolean.parseBoolean(System.getProperty(AnsiConsole.JANSI_GRACEFUL, "true"))) {
public static boolean initialize() {
return initialize(Boolean.parseBoolean(System.getProperty(AnsiConsole.JANSI_GRACEFUL, "true")));
}

public static boolean initialize(boolean silent) {
if (Holder.ERROR != null) {
if (silent) {
return false;
} else {
throw new RuntimeException(
"Unable to load jansi native library. You may want set the `jansi.graceful` system property to true to be able to use Jansi on your platform",
e);
Holder.ERROR);
}
}
return loaded;
return true;
}

public static String getNativeLibraryPath() {
Expand All @@ -107,16 +121,9 @@ private static File getTempDir() {
* on VM-Exit (bug #80)
*/
static void cleanup() {
String tempFolder = getTempDir().getAbsolutePath();
File dir = new File(tempFolder);

File[] nativeLibFiles = dir.listFiles(new FilenameFilter() {
private final String searchPattern = "jansi-" + getVersion();

public boolean accept(File dir, String name) {
return name.startsWith(searchPattern) && !name.endsWith(".lck");
}
});
String filePrefix = "jansi-" + getVersion();
File[] nativeLibFiles =
getTempDir().listFiles((dir1, name) -> name.startsWith(filePrefix) && !name.endsWith(".lck"));
if (nativeLibFiles != null) {
for (File nativeLibFile : nativeLibFiles) {
File lckFile = new File(nativeLibFile.getAbsolutePath() + ".lck");
Expand Down Expand Up @@ -281,11 +288,7 @@ private static boolean loadNativeLibrary(File libPath) {
* @throws
*/
private static void loadJansiNativeLibrary() throws Exception {
if (loaded) {
return;
}

List<String> triedPaths = new LinkedList<String>();
List<String> triedPaths = new ArrayList<>();

// Try loading library from library.jansi.path library path */
String jansiNativeLibraryPath = System.getProperty("library.jansi.path");
Expand All @@ -301,14 +304,12 @@ private static void loadJansiNativeLibrary() throws Exception {
if (jansiNativeLibraryPath != null) {
String withOs = jansiNativeLibraryPath + "/" + OSInfo.getNativeLibFolderPathForCurrentOS();
if (loadNativeLibrary(new File(withOs, jansiNativeLibraryName))) {
loaded = true;
return;
} else {
triedPaths.add(withOs);
}

if (loadNativeLibrary(new File(jansiNativeLibraryPath, jansiNativeLibraryName))) {
loaded = true;
return;
} else {
triedPaths.add(jansiNativeLibraryPath);
Expand All @@ -326,21 +327,19 @@ private static void loadJansiNativeLibrary() throws Exception {
String tempFolder = getTempDir().getAbsolutePath();
// Try extracting the library from jar
if (extractAndLoadLibraryFile(jansiNativeLibraryPath, jansiNativeLibraryName, tempFolder)) {
loaded = true;
return;
} else {
triedPaths.add(jansiNativeLibraryPath);
}
}

// As a last resort try from java.library.path
// As a last resort, try from java.library.path
String javaLibraryPath = System.getProperty("java.library.path", "");
for (String ldPath : javaLibraryPath.split(File.pathSeparator)) {
if (ldPath.isEmpty()) {
continue;
}
if (loadNativeLibrary(new File(ldPath, jansiNativeLibraryName))) {
loaded = true;
return;
} else {
triedPaths.add(ldPath);
Expand Down Expand Up @@ -376,14 +375,11 @@ public static int getMinorVersion() {
* @return The version of the jansi library.
*/
public static String getVersion() {

URL versionFile = JansiLoader.class.getResource("/org/fusesource/jansi/jansi.properties");

String version = "unknown";
try {
if (versionFile != null) {
try (InputStream stream = JansiLoader.class.getResourceAsStream("/org/fusesource/jansi/jansi.properties")) {
if (stream != null) {
Properties versionData = new Properties();
versionData.load(versionFile.openStream());
versionData.load(stream);
version = versionData.getProperty("version", version);
version = version.trim().replaceAll("[^0-9.]", "");
}
Expand Down
Loading

0 comments on commit b7c0487

Please sign in to comment.