Provide Karaf Session object inside OSGiConsole (#4635)

The Karaf `Session` enables ConsoleCommandExtensions to prompt for user input.

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/4680/head
jimtng 2025-03-23 09:26:59 +10:00 committed by GitHub
parent baf79c5f27
commit 0cf3e2963a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 99 additions and 55 deletions

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2010-2025 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.io.console.karaf;
import java.io.IOException;
import java.io.PrintStream;
import org.apache.karaf.shell.api.console.Session;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.jline.reader.LineReader;
import org.openhab.core.io.console.Console;
/**
*
* @author Markus Rathgeb - Initial contribution
*/
@NonNullByDefault
public class OSGiConsole implements Console {
private final String scope;
private final PrintStream out;
private final Session session;
public OSGiConsole(final String scope, PrintStream out, Session session) {
this.scope = scope;
this.out = out;
this.session = session;
}
@Override
public void printf(String format, Object... args) {
out.printf(format, args);
}
@Override
public void print(final String s) {
out.print(s);
}
@Override
public void println(final String s) {
out.println(s);
}
@Override
public void printUsage(final String s) {
out.println(String.format("Usage: %s:%s", scope, s));
}
@Override
public String readLine(String prompt, final @Nullable Character mask) throws IOException {
// Prevent readLine() from logging a warning
// see:
// https://github.com/apache/karaf/blob/ad427cd12543dc78e095bbaa4608d7ca3d5ea4d8/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java#L549
// https://github.com/jline/jline3/blob/ee4886bf24f40288a4044f9b4b74917b58103e49/reader/src/main/java/org/jline/reader/LineReaderBuilder.java#L90
String previousSetting = System.setProperty(LineReader.PROP_SUPPORT_PARSEDLINE, "true");
try {
return session.readLine(prompt, mask);
} finally {
if (previousSetting != null) {
System.setProperty(LineReader.PROP_SUPPORT_PARSEDLINE, previousSetting);
} else {
System.clearProperty(LineReader.PROP_SUPPORT_PARSEDLINE);
}
}
}
public Session getSession() {
return session;
}
}

View File

@ -27,6 +27,7 @@ import org.apache.karaf.shell.api.console.Session;
import org.openhab.core.io.console.Console;
import org.openhab.core.io.console.ConsoleInterpreter;
import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
import org.openhab.core.io.console.karaf.OSGiConsole;
/**
* This class wraps OH ConsoleCommandExtensions to commands for Apache Karaf
@ -68,7 +69,7 @@ public class CommandWrapper implements Command, Action {
public Object execute(Session session, List<Object> argList) throws Exception {
String[] args = argList.stream().map(Object::toString).toArray(String[]::new);
PrintStream out = Process.Utils.current().out();
final Console console = new OSGiConsole(getScope(), out);
final Console console = new OSGiConsole(getScope(), out, session);
if (args.length == 1 && "--help".equals(args[0])) {
for (final String usage : command.getUsages()) {

View File

@ -1,54 +0,0 @@
/*
* Copyright (c) 2010-2025 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.io.console.karaf.internal;
import java.io.PrintStream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.io.console.Console;
/**
*
* @author Markus Rathgeb - Initial contribution
*/
@NonNullByDefault
public class OSGiConsole implements Console {
private final String scope;
private final PrintStream out;
public OSGiConsole(final String scope, PrintStream out) {
this.scope = scope;
this.out = out;
}
@Override
public void printf(String format, Object... args) {
out.printf(format, args);
}
@Override
public void print(final String s) {
out.print(s);
}
@Override
public void println(final String s) {
out.println(s);
}
@Override
public void printUsage(final String s) {
out.println(String.format("Usage: %s:%s", scope, s));
}
}

View File

@ -12,7 +12,10 @@
*/
package org.openhab.core.io.console;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* This interface must be implemented by consoles which want to use the {@link ConsoleInterpreter}.
@ -38,4 +41,16 @@ public interface Console {
* @param s the main usage string (console independent)
*/
void printUsage(String s);
/**
* Reads a line from the console. The prompt is displayed before the line is read.
*
* @param prompt the prompt to display
* @param mask the character to use for masking input (e.g. '*'), or null if no masking is required
* @return the line read from the console
* @throws IOException if an I/O error occurs
*/
default String readLine(String prompt, final @Nullable Character mask) throws IOException {
throw new UnsupportedOperationException("readLine not supported");
}
}