Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LOGMGR-354] Avoid expensive JLine setup on JDK 23+ #491

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Base64;
import java.util.Objects;
import java.util.logging.ErrorManager;
Expand Down Expand Up @@ -60,15 +62,8 @@ public enum Target {
CONSOLE,
}

private static final PrintWriter console;

private final ErrorManager localErrorManager = new HandlerErrorManager(this);

static {
final Console con = System.console();
console = con == null ? null : con.writer();
}

/**
* Construct a new instance.
*/
Expand All @@ -82,7 +77,7 @@ public ConsoleHandler() {
* @param formatter the formatter to use
*/
public ConsoleHandler(final Formatter formatter) {
this(console == null ? Target.SYSTEM_OUT : Target.CONSOLE, formatter);
this(defaultTarget(), formatter);
}

/**
Expand Down Expand Up @@ -111,7 +106,7 @@ public ConsoleHandler(final Target target, final Formatter formatter) {
setOutputStream(wrap(err));
break;
case CONSOLE:
setWriter(wrap(console));
setWriter(wrap(ConsoleHolder.console));
break;
default:
throw new IllegalArgumentException();
Expand All @@ -124,7 +119,7 @@ public ConsoleHandler(final Target target, final Formatter formatter) {
* @param target the target to write to, or {@code null} to clear the target
*/
public void setTarget(Target target) {
final Target t = (target == null ? console == null ? Target.SYSTEM_OUT : Target.CONSOLE : target);
final Target t = (target == null ? defaultTarget() : target);
switch (t) {
case SYSTEM_OUT:
setOutputStream(wrap(out));
Expand All @@ -133,7 +128,7 @@ public void setTarget(Target target) {
setOutputStream(wrap(err));
break;
case CONSOLE:
setWriter(wrap(console));
setWriter(wrap(ConsoleHolder.console));
break;
default:
throw new IllegalArgumentException();
Expand Down Expand Up @@ -260,7 +255,7 @@ public void setOutputStream(final OutputStream outputStream) {
* @return {@code true} if there is a console, {@code false} otherwise
*/
public static boolean hasConsole() {
return console != null;
return ConsoleHolder.console != null;
}

/**
Expand Down Expand Up @@ -297,4 +292,28 @@ public static boolean isGraphicsSupportPassivelyDetected() {
|| term.equalsIgnoreCase("wezterm")
|| term.equalsIgnoreCase("konsole")) || termProgram != null && termProgram.equalsIgnoreCase("wezterm");
}

private static Target defaultTarget() {
return ConsoleHolder.console == null ? Target.SYSTEM_OUT : Target.CONSOLE;
}

private static final class ConsoleHolder {
private static final PrintWriter console;

static {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
// prevent jline from being used if we are the first console user
String res = System.getProperty("jdk.console");
if (res == null) {
System.setProperty("jdk.console", "java.base");
}
return null;
});
Console cons = System.console();
console = cons == null ? null : cons.writer();
}

private ConsoleHolder() {
}
}
}