Skip to content

Commit

Permalink
Change the behavior to fail gracefuly in case the system is not suppo…
Browse files Browse the repository at this point in the history
…rted, fixes #204
  • Loading branch information
gnodet committed Oct 11, 2021
1 parent cb843bf commit 98c430a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public class AnsiConsole {
* reset when the streams are uninstalled.
*/
public static final String JANSI_NORESET = "jansi.noreset";
/**
* If the <code>jansi.graceful</code> system property is set to false, any exception that occurs
* during the initialization will cause the library to report this exception and fail. The default
* behavior is to behave gracefully and fall back to pure emulation on posix systems.
*/
public static final String JANSI_GRACEFUL = "jansi.graceful";

/**
* @deprecated this field will be made private in a future release, use {@link #sysOut()} instead
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/org/fusesource/jansi/AnsiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,26 @@ public static void main(String... args) throws IOException {
// 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", ""));
JansiLoader.initialize();
System.out.println("Jansi native library loaded from " + JansiLoader.getNativeLibraryPath());
if (JansiLoader.getNativeLibrarySourceUrl() != null) {
System.out.println(" which was auto-extracted from " + JansiLoader.getNativeLibrarySourceUrl());
boolean loaded = JansiLoader.initialize();
if (loaded) {
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);
}
}
}

System.out.println();
Expand All @@ -75,6 +91,7 @@ public static void main(String... args) throws IOException {

System.out.println();

System.out.println(AnsiConsole.JANSI_GRACEFUL + "= " + System.getProperty(AnsiConsole.JANSI_GRACEFUL, ""));
System.out.println(AnsiConsole.JANSI_MODE + "= " + System.getProperty(AnsiConsole.JANSI_MODE, ""));
System.out.println(AnsiConsole.JANSI_OUT_MODE + "= " + System.getProperty(AnsiConsole.JANSI_OUT_MODE, ""));
System.out.println(AnsiConsole.JANSI_ERR_MODE + "= " + System.getProperty(AnsiConsole.JANSI_ERR_MODE, ""));
Expand Down Expand Up @@ -171,7 +188,7 @@ private static String getJansiVersion() {

private static void diagnoseTty(boolean stderr) {
int fd = stderr ? CLibrary.STDERR_FILENO : CLibrary.STDOUT_FILENO;
int isatty = CLibrary.isatty(fd);
int isatty = CLibrary.LOADED ? CLibrary.isatty(fd) : 0;

System.out.println("isatty(STD" + (stderr ? "ERR" : "OUT") + "_FILENO): " + isatty + ", System."
+ (stderr ? "err" : "out") + " " + ((isatty == 0) ? "is *NOT*" : "is") + " a terminal");
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/org/fusesource/jansi/internal/CLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@
@SuppressWarnings("unused")
public class CLibrary {

//
// Initialization
//

public static final boolean LOADED;

static {
JansiLoader.initialize();
init();
LOADED = JansiLoader.initialize();
if (LOADED) {
init();
}
}

private static native void init();

//
// Constants
//

public static int STDOUT_FILENO = 1;

public static int STDERR_FILENO = 2;
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/org/fusesource/jansi/internal/JansiLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.Properties;
import java.util.Random;

import org.fusesource.jansi.AnsiConsole;

/**
* Set the system properties, org.jansi.lib.path, org.jansi.lib.name,
* appropriately so that jansi can find *.dll, *.jnilib and
Expand All @@ -41,7 +43,7 @@
*/
public class JansiLoader {

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

Expand All @@ -53,15 +55,15 @@ public class JansiLoader {
*/
public static synchronized boolean initialize() {
// only cleanup before the first extract
if (!extracted) {
if (!loaded) {
cleanup();
}
try {
loadJansiNativeLibrary();
} catch (Exception e) {
throw new RuntimeException("Unable to load jansi native library", e);
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);
}
return extracted;
return loaded;
}

public static String getNativeLibraryPath() {
Expand Down Expand Up @@ -263,7 +265,7 @@ private static boolean loadNativeLibrary(File libPath) {
* @throws
*/
private static void loadJansiNativeLibrary() throws Exception {
if (extracted) {
if (loaded) {
return;
}

Expand All @@ -283,14 +285,14 @@ private static void loadJansiNativeLibrary() throws Exception {
if (jansiNativeLibraryPath != null) {
String withOs = jansiNativeLibraryPath + "/" + OSInfo.getNativeLibFolderPathForCurrentOS();
if (loadNativeLibrary(new File(withOs, jansiNativeLibraryName))) {
extracted = true;
loaded = true;
return;
} else {
triedPaths.add(withOs);
}

if (loadNativeLibrary(new File(jansiNativeLibraryPath, jansiNativeLibraryName))) {
extracted = true;
loaded = true;
return;
} else {
triedPaths.add(jansiNativeLibraryPath);
Expand All @@ -308,7 +310,7 @@ private static void loadJansiNativeLibrary() throws Exception {
String tempFolder = getTempDir().getAbsolutePath();
// Try extracting the library from jar
if (extractAndLoadLibraryFile(jansiNativeLibraryPath, jansiNativeLibraryName, tempFolder)) {
extracted = true;
loaded = true;
return;
} else {
triedPaths.add(jansiNativeLibraryPath);
Expand All @@ -322,16 +324,18 @@ private static void loadJansiNativeLibrary() throws Exception {
continue;
}
if (loadNativeLibrary(new File(ldPath, jansiNativeLibraryName))) {
extracted = true;
loaded = true;
return;
} else {
triedPaths.add(ldPath);
}
}

extracted = false;
throw new Exception(String.format("No native library found for os.name=%s, os.arch=%s, paths=[%s]",
OSInfo.getOSName(), OSInfo.getArchName(), join(triedPaths, File.pathSeparator)));
loaded = false;
if (!Boolean.parseBoolean(System.getProperty(AnsiConsole.JANSI_GRACEFUL, "true"))) {
throw new Exception(String.format("No native library found for os.name=%s, os.arch=%s, paths=[%s]",
OSInfo.getOSName(), OSInfo.getArchName(), join(triedPaths, File.pathSeparator)));
}
}

private static boolean hasResource(String path) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/fusesource/jansi/internal/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
public class Kernel32 {

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

private static native void init();
Expand Down

0 comments on commit 98c430a

Please sign in to comment.