Skip to content

Commit

Permalink
8333103: Re-examine the console provider loading
Browse files Browse the repository at this point in the history
Reviewed-by: redestad, jpai
  • Loading branch information
naotoj committed Jun 3, 2024
1 parent 4de6207 commit 9686e80
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/java.base/share/classes/java/io/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ private static UnsupportedOperationException newUnsupportedOperationException()

CHARSET = cs;

cons = instantiateConsole(istty);
cons = instantiateConsole();

// Set up JavaIOAccess in SharedSecrets
SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
Expand All @@ -647,32 +647,37 @@ public Console console() {
}

@SuppressWarnings("removal")
private static Console instantiateConsole(boolean istty) {
private static Console instantiateConsole() {
Console c;

try {
/*
* The JdkConsole provider used for Console instantiation can be specified
* with the system property "jdk.console", whose value designates the module
* name of the implementation, and which defaults to the value of
* {@code JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME}. If no
* providers are available, or instantiation failed, java.base built-in
* {@code JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME}. If multiple
* provider implementations exist in that module, the first one found is used.
* If no providers are available, or instantiation failed, java.base built-in
* Console implementation is used.
*/
PrivilegedAction<Console> pa = () -> {
var consModName = System.getProperty("jdk.console",
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);
return ServiceLoader.load(ModuleLayer.boot(), JdkConsoleProvider.class).stream()
.map(ServiceLoader.Provider::get)
.filter(jcp -> consModName.equals(jcp.getClass().getModule().getName()))
.map(jcp -> jcp.console(istty, CHARSET))
.filter(Objects::nonNull)
.findAny()
.map(jc -> (Console) new ProxyingConsole(jc))
.orElse(null);
};
c = AccessController.doPrivileged(pa);
} catch (ServiceConfigurationError ignore) {
c = AccessController.doPrivileged(new PrivilegedAction<Console>() {
public Console run() {
var consModName = System.getProperty("jdk.console",
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);

for (var jcp : ServiceLoader.load(ModuleLayer.boot(), JdkConsoleProvider.class)) {
if (consModName.equals(jcp.getClass().getModule().getName())) {
var jc = jcp.console(istty, CHARSET);
if (jc != null) {
return new ProxyingConsole(jc);
}
break;
}
}
return null;
}
});
} catch (ServiceConfigurationError _) {
c = null;
}

Expand Down

1 comment on commit 9686e80

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.