You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2016/06/14 13:23:20 UTC
[5/6] karaf git commit: [KARAF-4570] Upgrade to JLine 3
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/LessAction.java
----------------------------------------------------------------------
diff --git a/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/LessAction.java b/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/LessAction.java
index af7bca7..9b34b81 100644
--- a/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/LessAction.java
+++ b/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/LessAction.java
@@ -16,28 +16,11 @@
*/
package org.apache.karaf.shell.commands.impl;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FilterInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.InterruptedIOException;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.Stack;
-import java.util.TreeMap;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.regex.Pattern;
-import jline.console.KeyMap;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
@@ -45,23 +28,15 @@ import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.api.console.Session;
-import org.apache.karaf.shell.api.console.Signal;
-import org.apache.karaf.shell.api.console.SignalListener;
-import org.apache.karaf.shell.api.console.Terminal;
-import org.apache.karaf.shell.support.ansi.AnsiSplitter;
-import org.jledit.jline.NonBlockingInputStream;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.jline.builtins.Less;
+import org.jline.builtins.Source;
+import org.jline.builtins.Source.StdInSource;
+import org.jline.builtins.Source.URLSource;
+import org.jline.terminal.Terminal;
@Command(scope = "shell", name = "less", description = "File pager.")
@Service
-public class LessAction implements Action, SignalListener {
-
- private static final int ESCAPE = 27;
- public static final int ESCAPE_TIMEOUT = 100;
- public static final int READ_EXPIRED = -2;
-
- private final Logger log = LoggerFactory.getLogger(getClass());
+public class LessAction implements Action {
@Option(name = "-e", aliases = "--quit-at-eof")
boolean quitAtSecondEof;
@@ -91,805 +66,38 @@ public class LessAction implements Action, SignalListener {
int tabs = 4;
@Argument(multiValued = true)
- List<File> files;
-
- @Reference(optional = true)
- Terminal terminal;
+ List<String> files;
@Reference
Session session;
- BufferedReader reader;
-
- NonBlockingInputStream consoleInput;
- Reader consoleReader;
-
- KeyMap keys;
-
- int firstLineInMemory = 0;
- List<String> lines = new ArrayList<>();
-
- int firstLineToDisplay = 0;
- int firstColumnToDisplay = 0;
- int offsetInLine = 0;
-
- String message;
- final StringBuilder buffer = new StringBuilder();
- final StringBuilder opBuffer = new StringBuilder();
- final Stack<Character> pushBackChar = new Stack<>();
- Thread displayThread;
- final AtomicBoolean redraw = new AtomicBoolean();
-
- final Map<String, Operation> options = new TreeMap<>();
-
- int window;
- int halfWindow;
-
- int nbEof;
-
- String pattern;
-
@Override
public Object execute() throws Exception {
- InputStream in;
- if (files != null && !files.isEmpty()) {
- message = files.get(0).toString();
- in = new FileInputStream(files.get(0));
- } else {
- in = System.in;
- }
- reader = new BufferedReader(new InputStreamReader(new InterruptibleInputStream(in)));
- try {
- if (terminal == null || !isTty(System.out)) {
- String line;
- while ((line = reader.readLine()) != null) {
- System.out.println(line);
- checkInterrupted();
- }
- return null;
+ Terminal terminal = (Terminal) session.get(".jline.terminal");
+ Less less = new Less(terminal);
+ less.quitAtFirstEof = quitAtFirstEof;
+ less.quitAtSecondEof = quitAtSecondEof;
+ less.quiet = quiet;
+ less.veryQuiet = veryQuiet;
+ less.chopLongLines = chopLongLines;
+ less.ignoreCaseAlways = ignoreCaseAlways;
+ less.ignoreCaseCond = ignoreCaseCond;
+ less.tabs = tabs;
+ less.printLineNumbers = printLineNumbers;
+ List<Source> sources = new ArrayList<>();
+ if (files.isEmpty()) {
+ files.add("-");
+ }
+ Path pwd = Paths.get(System.getProperty("karaf.home"));
+ for (String arg : files) {
+ if ("-".equals(arg)) {
+ sources.add(new StdInSource());
} else {
- boolean echo = terminal.isEchoEnabled();
- terminal.setEchoEnabled(false);
- terminal.addSignalListener(this, Signal.WINCH);
- try {
- window = terminal.getHeight() - 1;
- halfWindow = window / 2;
- keys = new KeyMap("less", false);
- bindKeys(keys);
- consoleInput = new NonBlockingInputStream(session.getKeyboard(), true);
- consoleReader = new InputStreamReader(consoleInput);
-
- // Use alternate buffer
- System.out.print("\u001B[?1049h");
- System.out.flush();
-
- displayThread = new Thread() {
- @Override
- public void run() {
- redrawLoop();
- }
- };
- displayThread.start();
- redraw();
- checkInterrupted();
-
- options.put("-e", Operation.OPT_QUIT_AT_SECOND_EOF);
- options.put("--quit-at-eof", Operation.OPT_QUIT_AT_SECOND_EOF);
- options.put("-E", Operation.OPT_QUIT_AT_FIRST_EOF);
- options.put("-QUIT-AT-EOF", Operation.OPT_QUIT_AT_FIRST_EOF);
- options.put("-N", Operation.OPT_PRINT_LINES);
- options.put("--LINE-NUMBERS", Operation.OPT_PRINT_LINES);
- options.put("-q", Operation.OPT_QUIET);
- options.put("--quiet", Operation.OPT_QUIET);
- options.put("--silent", Operation.OPT_QUIET);
- options.put("-Q", Operation.OPT_VERY_QUIET);
- options.put("--QUIET", Operation.OPT_VERY_QUIET);
- options.put("--SILENT", Operation.OPT_VERY_QUIET);
- options.put("-S", Operation.OPT_CHOP_LONG_LINES);
- options.put("--chop-long-lines", Operation.OPT_CHOP_LONG_LINES);
- options.put("-i", Operation.OPT_IGNORE_CASE_COND);
- options.put("--ignore-case", Operation.OPT_IGNORE_CASE_COND);
- options.put("-I", Operation.OPT_IGNORE_CASE_ALWAYS);
- options.put("--IGNORE-CASE", Operation.OPT_IGNORE_CASE_ALWAYS);
-
- Operation op;
- do {
- checkInterrupted();
-
- op = null;
- //
- // Option edition
- //
- if (buffer.length() > 0 && buffer.charAt(0) == '-') {
- int c = consoleReader.read();
- message = null;
- if (buffer.length() == 1) {
- buffer.append((char) c);
- if (c != '-') {
- op = options.get(buffer.toString());
- if (op == null) {
- message = "There is no " + printable(buffer.toString()) + " option";
- buffer.setLength(0);
- }
- }
- } else if (c == '\r') {
- op = options.get(buffer.toString());
- if (op == null) {
- message = "There is no " + printable(buffer.toString()) + " option";
- buffer.setLength(0);
- }
- } else {
- buffer.append((char) c);
- Map<String, Operation> matching = new HashMap<>();
- for (Map.Entry<String, Operation> entry : options.entrySet()) {
- if (entry.getKey().startsWith(buffer.toString())) {
- matching.put(entry.getKey(), entry.getValue());
- }
- }
- switch (matching.size()) {
- case 0:
- buffer.setLength(0);
- break;
- case 1:
- buffer.setLength(0);
- buffer.append(matching.keySet().iterator().next());
- break;
- }
- }
-
- }
- //
- // Pattern edition
- //
- else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) == '?')) {
- int c = consoleReader.read();
- message = null;
- if (c == '\r') {
- pattern = buffer.toString().substring(1);
- if (buffer.charAt(0) == '/') {
- moveToNextMatch();
- } else {
- moveToPreviousMatch();
- }
- buffer.setLength(0);
- } else {
- buffer.append((char) c);
- }
- }
- //
- // Command reading
- //
- else {
- Object obj = readOperation();
- message = null;
- if (obj instanceof Character) {
- char c = (char) obj;
- // Enter option mode or pattern edit mode
- if (c == '-' || c == '/' || c == '?') {
- buffer.setLength(0);
- }
- buffer.append((char) obj);
- } else if (obj instanceof Operation) {
- op = (Operation) obj;
- }
- }
- if (op != null) {
- switch (op) {
- case FORWARD_ONE_LINE:
- moveForward(getStrictPositiveNumberInBuffer(1));
- break;
- case BACKWARD_ONE_LINE:
- moveBackward(getStrictPositiveNumberInBuffer(1));
- break;
- case FORWARD_ONE_WINDOW_OR_LINES:
- moveForward(getStrictPositiveNumberInBuffer(window));
- break;
- case FORWARD_ONE_WINDOW_AND_SET:
- window = getStrictPositiveNumberInBuffer(window);
- moveForward(window);
- break;
- case FORWARD_ONE_WINDOW_NO_STOP:
- moveForward(window);
- // TODO: handle no stop
- break;
- case FORWARD_HALF_WINDOW_AND_SET:
- halfWindow = getStrictPositiveNumberInBuffer(halfWindow);
- moveForward(halfWindow);
- break;
- case BACKWARD_ONE_WINDOW_AND_SET:
- window = getStrictPositiveNumberInBuffer(window);
- moveBackward(window);
- break;
- case BACKWARD_ONE_WINDOW_OR_LINES:
- moveBackward(getStrictPositiveNumberInBuffer(window));
- break;
- case BACKWARD_HALF_WINDOW_AND_SET:
- halfWindow = getStrictPositiveNumberInBuffer(halfWindow);
- moveBackward(halfWindow);
- break;
- case GO_TO_FIRST_LINE_OR_N:
- // TODO: handle number
- firstLineToDisplay = firstLineInMemory;
- offsetInLine = 0;
- break;
- case GO_TO_LAST_LINE_OR_N:
- // TODO: handle number
- moveForward(Integer.MAX_VALUE);
- break;
- case LEFT_ONE_HALF_SCREEN:
- firstColumnToDisplay = Math.max(0, firstColumnToDisplay - terminal.getWidth() / 2);
- break;
- case RIGHT_ONE_HALF_SCREEN:
- firstColumnToDisplay += terminal.getWidth() / 2;
- break;
- case REPEAT_SEARCH_BACKWARD:
- case REPEAT_SEARCH_BACKWARD_SPAN_FILES:
- moveToPreviousMatch();
- break;
- case REPEAT_SEARCH_FORWARD:
- case REPEAT_SEARCH_FORWARD_SPAN_FILES:
- moveToNextMatch();
- break;
- case UNDO_SEARCH:
- pattern = null;
- break;
- case OPT_PRINT_LINES:
- buffer.setLength(0);
- printLineNumbers = !printLineNumbers;
- message = printLineNumbers ? "Constantly display line numbers" : "Don't use line numbers";
- break;
- case OPT_QUIET:
- buffer.setLength(0);
- quiet = !quiet;
- veryQuiet = false;
- message = quiet ? "Ring the bell for errors but not at eof/bof" : "Ring the bell for errors AND at eof/bof";
- break;
- case OPT_VERY_QUIET:
- buffer.setLength(0);
- veryQuiet = !veryQuiet;
- quiet = false;
- message = veryQuiet ? "Never ring the bell" : "Ring the bell for errors AND at eof/bof";
- break;
- case OPT_CHOP_LONG_LINES:
- buffer.setLength(0);
- offsetInLine = 0;
- chopLongLines = !chopLongLines;
- message = chopLongLines ? "Chop long lines" : "Fold long lines";
- break;
- case OPT_IGNORE_CASE_COND:
- ignoreCaseCond = !ignoreCaseCond;
- ignoreCaseAlways = false;
- message = ignoreCaseCond ? "Ignore case in searches" : "Case is significant in searches";
- break;
- case OPT_IGNORE_CASE_ALWAYS:
- ignoreCaseAlways = !ignoreCaseAlways;
- ignoreCaseCond = false;
- message = ignoreCaseAlways ? "Ignore case in searches and in patterns" : "Case is significant in searches";
- break;
- }
- buffer.setLength(0);
- }
- redraw();
- if (quitAtFirstEof && nbEof > 0 || quitAtSecondEof && nbEof > 1) {
- op = Operation.EXIT;
- }
- } while (op != Operation.EXIT);
- } catch (InterruptedException ie) {
- log.debug("Interrupted by user");
- } finally {
- terminal.setEchoEnabled(echo);
- terminal.removeSignalListener(this);
- consoleInput.shutdown();
- displayThread.interrupt();
- displayThread.join();
- // Use main buffer
- System.out.print("\u001B[?1049l");
- // Clear line
- System.out.println();
- System.out.flush();
- }
+ sources.add(new URLSource(pwd.resolve(arg).toUri().toURL(), arg));
}
- } finally {
- reader.close();
}
+ less.run(sources);
return null;
}
- private void moveToNextMatch() throws IOException {
- Pattern compiled = getPattern();
- if (compiled != null) {
- for (int lineNumber = firstLineToDisplay + 1; ; lineNumber++) {
- String line = getLine(lineNumber);
- if (line == null) {
- break;
- } else if (compiled.matcher(line).find()) {
- firstLineToDisplay = lineNumber;
- offsetInLine = 0;
- return;
- }
- }
- }
- message = "Pattern not found";
- }
-
- private void moveToPreviousMatch() throws IOException {
- Pattern compiled = getPattern();
- if (compiled != null) {
- for (int lineNumber = firstLineToDisplay - 1; lineNumber >= firstLineInMemory; lineNumber--) {
- String line = getLine(lineNumber);
- if (line == null) {
- break;
- } else if (compiled.matcher(line).find()) {
- firstLineToDisplay = lineNumber;
- offsetInLine = 0;
- return;
- }
- }
- }
- message = "Pattern not found";
- }
-
- private String printable(String s) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < s.length(); i++) {
- char c = s.charAt(i);
- if (c == ESCAPE) {
- sb.append("ESC");
- } else if (c < 32) {
- sb.append('^').append((char) (c + '@'));
- } else if (c < 128) {
- sb.append(c);
- } else {
- sb.append('\\').append(String.format("03o"));
- }
- }
- return sb.toString();
- }
-
- void moveForward(int lines) throws IOException {
- int width = terminal.getWidth() - (printLineNumbers ? 8 : 0);
- int height = terminal.getHeight();
- while (--lines >= 0) {
-
- int lastLineToDisplay = firstLineToDisplay;
- if (firstColumnToDisplay > 0 || chopLongLines) {
- lastLineToDisplay += height - 1;
- } else {
- int off = offsetInLine;
- for (int l = 0; l < height - 1; l++) {
- String line = getLine(lastLineToDisplay);
- if (ansiLength(line) > off + width) {
- off += width;
- } else {
- off = 0;
- lastLineToDisplay++;
- }
- }
- }
- if (getLine(lastLineToDisplay) == null) {
- eof();
- return;
- }
-
- String line = getLine(firstLineToDisplay);
- if (ansiLength(line) > width + offsetInLine) {
- offsetInLine += width;
- } else {
- offsetInLine = 0;
- firstLineToDisplay++;
- }
- }
- }
-
- void moveBackward(int lines) throws IOException {
- int width = terminal.getWidth() - (printLineNumbers ? 8 : 0);
- while (--lines >= 0) {
- if (offsetInLine > 0) {
- offsetInLine = Math.max(0, offsetInLine - width);
- } else if (firstLineInMemory < firstLineToDisplay) {
- firstLineToDisplay--;
- String line = getLine(firstLineToDisplay);
- int length = ansiLength(line);
- offsetInLine = length - length % width;
- } else {
- bof();
- return;
- }
- }
- }
-
- private void eof() {
- nbEof++;
- message = "(END)";
- if (!quiet && !veryQuiet && !quitAtFirstEof && !quitAtSecondEof) {
- System.out.print((char) 0x07);
- System.out.flush();
- }
- }
-
- private void bof() {
- if (!quiet && !veryQuiet) {
- System.out.print((char) 0x07);
- System.out.flush();
- }
- }
-
- int getStrictPositiveNumberInBuffer(int def) {
- try {
- int n = Integer.parseInt(buffer.toString());
- return (n > 0) ? n : def;
- } catch (NumberFormatException e) {
- return def;
- } finally {
- buffer.setLength(0);
- }
- }
-
- void redraw() {
- synchronized (redraw) {
- redraw.set(true);
- redraw.notifyAll();
- }
- }
-
- void redrawLoop() {
- synchronized (redraw) {
- for (; ; ) {
- try {
- if (redraw.compareAndSet(true, false)) {
- display();
- } else {
- redraw.wait();
- }
- } catch (Exception e) {
- return;
- }
- }
- }
- }
-
- void display() throws IOException {
- System.out.println();
- int width = terminal.getWidth() - (printLineNumbers ? 8 : 0);
- int height = terminal.getHeight();
- int inputLine = firstLineToDisplay;
- String curLine = null;
- Pattern compiled = getPattern();
- for (int terminalLine = 0; terminalLine < height - 1; terminalLine++) {
- if (curLine == null) {
- curLine = getLine(inputLine++);
- if (curLine == null) {
- curLine = "";
- }
- if (compiled != null) {
- curLine = compiled.matcher(curLine).replaceAll("\033[7m$1\033[27m");
- }
- }
- String toDisplay;
- if (firstColumnToDisplay > 0 || chopLongLines) {
- int off = firstColumnToDisplay;
- if (terminalLine == 0 && offsetInLine > 0) {
- off = Math.max(offsetInLine, off);
- }
- toDisplay = ansiSubstring(curLine, off, off + width);
- curLine = null;
- } else {
- if (terminalLine == 0 && offsetInLine > 0) {
- curLine = ansiSubstring(curLine, offsetInLine, Integer.MAX_VALUE);
- }
- toDisplay = ansiSubstring(curLine, 0, width);
- curLine = ansiSubstring(curLine, width, Integer.MAX_VALUE);
- if (curLine.isEmpty()) {
- curLine = null;
- }
- }
- if (printLineNumbers) {
- System.out.print(String.format("%7d ", inputLine));
- }
- System.out.println(toDisplay);
- }
- System.out.flush();
- if (message != null) {
- System.out.print("\033[7m" + message + " \033[0m");
- } else if (buffer.length() > 0) {
- System.out.print(" " + buffer);
- } else if (opBuffer.length() > 0) {
- System.out.print(" " + printable(opBuffer.toString()));
- } else {
- System.out.print(":");
- }
- System.out.flush();
- }
-
- private Pattern getPattern() {
- Pattern compiled = null;
- if (pattern != null) {
- boolean insensitive = ignoreCaseAlways || ignoreCaseCond && pattern.toLowerCase().equals(pattern);
- compiled = Pattern.compile("(" + pattern + ")", insensitive ? Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE : 0);
- }
- return compiled;
- }
-
- private int ansiLength(String curLine) throws IOException {
- return AnsiSplitter.length(curLine, tabs);
- }
-
- private String ansiSubstring(String curLine, int begin, int end) throws IOException {
- return AnsiSplitter.substring(curLine, begin, end, tabs);
- }
-
- String getLine(int line) throws IOException {
- while (line <= lines.size()) {
- String str = reader.readLine();
- if (str != null) {
- lines.add(str);
- } else {
- break;
- }
- }
- if (line < lines.size()) {
- return lines.get(line);
- }
- return null;
- }
-
- @Override
- public void signal(Signal signal) {
- // Ugly hack to force the jline unix terminal to retrieve the width/height of the terminal
- // because results are cached for 1 second.
- try {
- Field field = terminal.getClass().getDeclaredField("terminal");
- field.setAccessible(true);
- Object jlineTerminal = field.get(terminal);
- field = jlineTerminal.getClass().getSuperclass().getDeclaredField("settings");
- field.setAccessible(true);
- Object settings = field.get(jlineTerminal);
- field = settings.getClass().getDeclaredField("configLastFetched");
- field.setAccessible(true);
- field.setLong(settings, 0L);
- } catch (Throwable t) {
- // Ignore
- }
- redraw();
- }
-
- protected boolean isTty(OutputStream out) {
- try {
- Method mth = out.getClass().getDeclaredMethod("getCurrent");
- mth.setAccessible(true);
- Object current = mth.invoke(out);
- return current == session.getConsole();
- } catch (Throwable t) {
- return false;
- }
- }
-
- /**
- * This is for long running commands to be interrupted by ctrl-c.
- *
- * @throws InterruptedException If the command is interrupted.
- */
- public static void checkInterrupted() throws InterruptedException {
- Thread.yield();
- if (Thread.currentThread().isInterrupted()) {
- throw new InterruptedException();
- }
- }
-
-
- protected Object readOperation() throws IOException {
- int c = pushBackChar.isEmpty() ? consoleReader.read() : pushBackChar.pop();
- if (c == -1) {
- return null;
- }
- opBuffer.append((char) c);
-
- Object o = keys.getBound(opBuffer);
- if (o == jline.console.Operation.DO_LOWERCASE_VERSION) {
- opBuffer.setLength(opBuffer.length() - 1);
- opBuffer.append(Character.toLowerCase((char) c));
- o = keys.getBound(opBuffer);
- }
-
- if (o instanceof KeyMap) {
- if (c == ESCAPE
- && pushBackChar.isEmpty()
- && consoleInput.isNonBlockingEnabled()
- && consoleInput.peek(ESCAPE_TIMEOUT) == READ_EXPIRED) {
- o = ((KeyMap) o).getAnotherKey();
- if (o == null || o instanceof KeyMap) {
- return null;
- }
- opBuffer.setLength(0);
- } else {
- return null;
- }
- }
-
- while (o == null && opBuffer.length() > 0) {
- c = opBuffer.charAt(opBuffer.length() - 1);
- opBuffer.setLength(opBuffer.length() - 1);
- Object o2 = keys.getBound(opBuffer);
- if (o2 instanceof KeyMap) {
- o = ((KeyMap) o2).getAnotherKey();
- if (o != null) {
- pushBackChar.push((char) c);
- }
- }
- }
-
- if (o != null) {
- opBuffer.setLength(0);
- pushBackChar.clear();
- }
- return o;
- }
-
-
- private void bindKeys(KeyMap map) {
- // Arrow keys bindings
- map.bind("\033[0A", Operation.BACKWARD_ONE_LINE);
- map.bind("\033[0B", Operation.LEFT_ONE_HALF_SCREEN);
- map.bind("\033[0C", Operation.RIGHT_ONE_HALF_SCREEN);
- map.bind("\033[0D", Operation.FORWARD_ONE_LINE);
-
- map.bind("\340\110", Operation.BACKWARD_ONE_LINE);
- map.bind("\340\113", Operation.LEFT_ONE_HALF_SCREEN);
- map.bind("\340\115", Operation.RIGHT_ONE_HALF_SCREEN);
- map.bind("\340\120", Operation.FORWARD_ONE_LINE);
- map.bind("\000\110", Operation.BACKWARD_ONE_LINE);
- map.bind("\000\113", Operation.LEFT_ONE_HALF_SCREEN);
- map.bind("\000\115", Operation.RIGHT_ONE_HALF_SCREEN);
- map.bind("\000\120", Operation.FORWARD_ONE_LINE);
-
- map.bind("\033[A", Operation.BACKWARD_ONE_LINE);
- map.bind("\033[B", Operation.FORWARD_ONE_LINE);
- map.bind("\033[C", Operation.RIGHT_ONE_HALF_SCREEN);
- map.bind("\033[D", Operation.LEFT_ONE_HALF_SCREEN);
-
- map.bind("\033[OA", Operation.BACKWARD_ONE_LINE);
- map.bind("\033[OB", Operation.FORWARD_ONE_LINE);
- map.bind("\033[OC", Operation.RIGHT_ONE_HALF_SCREEN);
- map.bind("\033[OD", Operation.LEFT_ONE_HALF_SCREEN);
-
- map.bind("\0340H", Operation.BACKWARD_ONE_LINE);
- map.bind("\0340P", Operation.FORWARD_ONE_LINE);
- map.bind("\0340M", Operation.RIGHT_ONE_HALF_SCREEN);
- map.bind("\0340K", Operation.LEFT_ONE_HALF_SCREEN);
-
- map.bind("h", Operation.HELP);
- map.bind("H", Operation.HELP);
-
- map.bind("q", Operation.EXIT);
- map.bind(":q", Operation.EXIT);
- map.bind("Q", Operation.EXIT);
- map.bind(":Q", Operation.EXIT);
- map.bind("ZZ", Operation.EXIT);
-
- map.bind("e", Operation.FORWARD_ONE_LINE);
- map.bind(ctrl('E'), Operation.FORWARD_ONE_LINE);
- map.bind("j", Operation.FORWARD_ONE_LINE);
- map.bind(ctrl('N'), Operation.FORWARD_ONE_LINE);
- map.bind("\r", Operation.FORWARD_ONE_LINE);
-
- map.bind("y", Operation.BACKWARD_ONE_LINE);
- map.bind(ctrl('Y'), Operation.BACKWARD_ONE_LINE);
- map.bind("k", Operation.BACKWARD_ONE_LINE);
- map.bind(ctrl('K'), Operation.BACKWARD_ONE_LINE);
- map.bind(ctrl('P'), Operation.BACKWARD_ONE_LINE);
-
- map.bind("f", Operation.FORWARD_ONE_WINDOW_OR_LINES);
- map.bind(ctrl('F'), Operation.FORWARD_ONE_WINDOW_OR_LINES);
- map.bind(ctrl('V'), Operation.FORWARD_ONE_WINDOW_OR_LINES);
- map.bind(" ", Operation.FORWARD_ONE_WINDOW_OR_LINES);
-
- map.bind("b", Operation.BACKWARD_ONE_WINDOW_OR_LINES);
- map.bind(ctrl('B'), Operation.BACKWARD_ONE_WINDOW_OR_LINES);
- map.bind("\033v", Operation.BACKWARD_ONE_WINDOW_OR_LINES);
-
- map.bind("z", Operation.FORWARD_ONE_WINDOW_AND_SET);
-
- map.bind("w", Operation.BACKWARD_ONE_WINDOW_AND_SET);
-
- map.bind("\033 ", Operation.FORWARD_ONE_WINDOW_NO_STOP);
-
- map.bind("d", Operation.FORWARD_HALF_WINDOW_AND_SET);
- map.bind(ctrl('D'), Operation.FORWARD_HALF_WINDOW_AND_SET);
-
- map.bind("u", Operation.BACKWARD_HALF_WINDOW_AND_SET);
- map.bind(ctrl('U'), Operation.BACKWARD_HALF_WINDOW_AND_SET);
-
- map.bind("\033)", Operation.RIGHT_ONE_HALF_SCREEN);
-
- map.bind("\033(", Operation.LEFT_ONE_HALF_SCREEN);
-
- map.bind("F", Operation.FORWARD_FOREVER);
-
- map.bind("n", Operation.REPEAT_SEARCH_FORWARD);
- map.bind("N", Operation.REPEAT_SEARCH_BACKWARD);
- map.bind("\033n", Operation.REPEAT_SEARCH_FORWARD_SPAN_FILES);
- map.bind("\033N", Operation.REPEAT_SEARCH_BACKWARD_SPAN_FILES);
- map.bind("\033u", Operation.UNDO_SEARCH);
-
- map.bind("g", Operation.GO_TO_FIRST_LINE_OR_N);
- map.bind("<", Operation.GO_TO_FIRST_LINE_OR_N);
- map.bind("\033<", Operation.GO_TO_FIRST_LINE_OR_N);
-
- map.bind("G", Operation.GO_TO_LAST_LINE_OR_N);
- map.bind(">", Operation.GO_TO_LAST_LINE_OR_N);
- map.bind("\033>", Operation.GO_TO_LAST_LINE_OR_N);
-
- for (char c : "-/0123456789?".toCharArray()) {
- map.bind("" + c, c);
- }
- }
-
- String ctrl(char c) {
- return "" + ((char) (c & 0x1f));
- }
-
- static enum Operation {
-
- // General
- HELP,
- EXIT,
-
- // Moving
- FORWARD_ONE_LINE,
- BACKWARD_ONE_LINE,
- FORWARD_ONE_WINDOW_OR_LINES,
- BACKWARD_ONE_WINDOW_OR_LINES,
- FORWARD_ONE_WINDOW_AND_SET,
- BACKWARD_ONE_WINDOW_AND_SET,
- FORWARD_ONE_WINDOW_NO_STOP,
- FORWARD_HALF_WINDOW_AND_SET,
- BACKWARD_HALF_WINDOW_AND_SET,
- LEFT_ONE_HALF_SCREEN,
- RIGHT_ONE_HALF_SCREEN,
- FORWARD_FOREVER,
- REPAINT,
- REPAINT_AND_DISCARD,
-
- // Searching
- REPEAT_SEARCH_FORWARD,
- REPEAT_SEARCH_BACKWARD,
- REPEAT_SEARCH_FORWARD_SPAN_FILES,
- REPEAT_SEARCH_BACKWARD_SPAN_FILES,
- UNDO_SEARCH,
-
- // Jumping
- GO_TO_FIRST_LINE_OR_N,
- GO_TO_LAST_LINE_OR_N,
- GO_TO_PERCENT_OR_N,
- GO_TO_NEXT_TAG,
- GO_TO_PREVIOUS_TAG,
- FIND_CLOSE_BRACKET,
- FIND_OPEN_BRACKET,
-
- // Options
- OPT_PRINT_LINES,
- OPT_CHOP_LONG_LINES,
- OPT_QUIT_AT_FIRST_EOF,
- OPT_QUIT_AT_SECOND_EOF,
- OPT_QUIET,
- OPT_VERY_QUIET,
- OPT_IGNORE_CASE_COND,
- OPT_IGNORE_CASE_ALWAYS,
-
- }
-
- static class InterruptibleInputStream extends FilterInputStream {
- InterruptibleInputStream(InputStream in) {
- super(in);
- }
-
- @Override
- public int read(byte[] b, int off, int len) throws IOException {
- if (Thread.currentThread().isInterrupted()) {
- throw new InterruptedIOException();
- }
- return super.read(b, off, len);
- }
- }
-
}
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/pom.xml
----------------------------------------------------------------------
diff --git a/shell/console/pom.xml b/shell/console/pom.xml
index 01d79ce..498a619 100644
--- a/shell/console/pom.xml
+++ b/shell/console/pom.xml
@@ -48,6 +48,10 @@
<artifactId>jline</artifactId>
</dependency>
<dependency>
+ <groupId>org.fusesource.jansi</groupId>
+ <artifactId>jansi</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
@@ -145,7 +149,7 @@
org.apache.karaf.shell.core
</Fragment-Host>
<Import-Package>
- !org.apache.felix.gogo.runtime.*,
+ !org.apache.felix.gogo.*,
!org.apache.karaf.shell.inject.impl,
org.osgi.service.event;resolution:=optional,
org.apache.karaf.branding;resolution:=optional,
@@ -171,9 +175,9 @@
org.apache.karaf.shell.inject;version=${project.version};-noimport:=true,
org.apache.karaf.shell.util;version=${project.version};-noimport:=true,
org.apache.karaf.shell.util;version=2.3.0;-noimport:=true,
- org.apache.felix.gogo*;version=${felix.gogo.version};-noimport:=true,
- org.apache.felix.service.command;version=${felix.gogo.version};-noimport:=true,
- org.apache.felix.service.threadio;version=${felix.gogo.version};status=provisional;mandatory:=status;-noimport:=true
+ org.apache.felix.gogo*;version=${felix.gogo.runtime.version};-noimport:=true,
+ org.apache.felix.service.command;version=${felix.gogo.runtime.version};-noimport:=true,
+ org.apache.felix.service.threadio;version=${felix.gogo.runtime.version};status=provisional;mandatory:=status;-noimport:=true
</Export-Package>
<Private-Package>
org.apache.karaf.shell.commands.ansi,
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java b/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java
index 0ea9ff2..7a23f4b 100644
--- a/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java
+++ b/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java
@@ -18,14 +18,11 @@
*/
package org.apache.karaf.shell.commands.ansi;
-import org.fusesource.jansi.Ansi;
-import org.fusesource.jansi.Ansi.Color;
-
@Deprecated
public class SimpleAnsi {
- public static String COLOR_RED = Ansi.ansi().fg(Color.RED).toString();
- public static String COLOR_DEFAULT = Ansi.ansi().fg(Color.DEFAULT).toString();
-
- public static String INTENSITY_BOLD = Ansi.ansi().bold().toString();
- public static String INTENSITY_NORMAL = Ansi.ansi().boldOff().toString();
+ public static String COLOR_RED = "\u001b[31m";
+ public static String COLOR_DEFAULT = "\u001b[39m";
+
+ public static String INTENSITY_BOLD = "\u001b[1m";
+ public static String INTENSITY_NORMAL = "\u001b[22m";
}
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/src/main/java/org/apache/karaf/shell/console/factory/ConsoleFactory.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/factory/ConsoleFactory.java b/shell/console/src/main/java/org/apache/karaf/shell/console/factory/ConsoleFactory.java
deleted file mode 100644
index 6c27434..0000000
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/factory/ConsoleFactory.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.karaf.shell.console.factory;
-
-import java.io.InputStream;
-import java.io.PrintStream;
-
-import jline.Terminal;
-
-import org.apache.karaf.shell.console.Console;
-
-@Deprecated
-public interface ConsoleFactory {
- Console create(InputStream in, PrintStream out, PrintStream err, final Terminal term, String encoding, Runnable closeCallback);
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/src/main/java/org/apache/karaf/shell/console/impl/Main.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/Main.java b/shell/console/src/main/java/org/apache/karaf/shell/console/impl/Main.java
deleted file mode 100644
index eea5af8..0000000
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/Main.java
+++ /dev/null
@@ -1,348 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.karaf.shell.console.impl;
-
-import java.io.*;
-import java.lang.reflect.Method;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-
-import jline.Terminal;
-import org.apache.karaf.shell.commands.Action;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.commands.CommandException;
-import org.apache.karaf.shell.commands.basic.AbstractCommand;
-import org.apache.felix.gogo.runtime.CommandNotFoundException;
-import org.apache.felix.gogo.runtime.CommandProcessorImpl;
-import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
-import org.apache.felix.service.command.CommandSession;
-import org.apache.felix.service.command.Function;
-import org.apache.felix.service.threadio.ThreadIO;
-import org.apache.karaf.shell.console.NameScoping;
-import org.apache.karaf.shell.console.impl.jline.ConsoleImpl;
-import org.apache.karaf.shell.console.impl.jline.TerminalFactory;
-import org.fusesource.jansi.Ansi;
-import org.fusesource.jansi.AnsiConsole;
-
-public class Main {
-
- private String application = System.getProperty("karaf.name", "root");
- private String user = "karaf";
-
- public static void main(String args[]) throws Exception {
- Main main = new Main();
- main.run(args);
- }
-
- /**
- * Use this method when the shell is being executed as a top level shell.
- *
- * @param args The console main arguments.
- * @throws Exception In case of console running failure.
- */
- public void run(String args[]) throws Exception {
-
- ThreadIOImpl threadio = new ThreadIOImpl();
- threadio.start();
-
- CommandProcessorImpl commandProcessor = new CommandProcessorImpl(threadio);
-
- InputStream in = unwrap(System.in);
- PrintStream out = wrap(unwrap(System.out));
- PrintStream err = wrap(unwrap(System.err));
- run(commandProcessor, threadio, args, in, out, err);
-
- // TODO: do we need to stop the threadio that was started?
- // threadio.stop();
- }
-
- /**
- * Use this method when the shell is being executed as a command
- * of another shell.
- *
- * @param parent The parent command session.
- * @param threadIO The thread IO to use to execute the console.
- * @param args The console arguments.
- * @throws Exception In case of console running failure.
- */
- public void run(CommandSession parent, ThreadIO threadIO, String args[]) throws Exception {
-
- // TODO: find out what the down side of not using a real ThreadIO implementation is.
- CommandProcessorImpl commandProcessor = new CommandProcessorImpl(new ThreadIO() {
- public void setStreams(InputStream in, PrintStream out, PrintStream err) {
- }
-
- public void close() {
- }
- });
-
- InputStream in = parent.getKeyboard();
- PrintStream out = parent.getConsole();
- PrintStream err = parent.getConsole();
- run(commandProcessor, threadIO, args, in, out, err);
- }
-
- private void run(CommandProcessorImpl commandProcessor, ThreadIO threadIO, String[] args, InputStream in, PrintStream out, PrintStream err) throws Exception {
- StringBuilder sb = new StringBuilder();
- String classpath = null;
- boolean batch = false;
- String file = null;
-
- for (int i = 0; i < args.length; i++) {
- String arg = args[i];
- if (arg.startsWith("--classpath=")) {
- classpath = arg.substring("--classpath=".length());
- } else if (arg.startsWith("-c=")) {
- classpath = arg.substring("-c=".length());
- } else if (arg.equals("--classpath") || arg.equals("-c")) {
- classpath = args[++i];
- } else if (arg.equals("-b") || arg.equals("--batch")) {
- batch = true;
- } else if (arg.startsWith("--file=")) {
- file = arg.substring("--file=".length());
- } else if (arg.startsWith("-f=")) {
- file = arg.substring("-f=".length());
- } else if (arg.equals("--file") || arg.equals("-f")) {
- file = args[++i];
- } else {
- sb.append(arg);
- sb.append(' ');
- }
- }
-
- if (file != null) {
- Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
- try {
- sb.setLength(0);
- for (int c = reader.read(); c >= 0; c = reader.read()) {
- sb.append((char) c);
- }
- } finally {
- reader.close();
- }
- } else if (batch) {
- Reader reader = new BufferedReader(new InputStreamReader(System.in));
- sb.setLength(0);
- for (int c = reader.read(); c >= 0; reader.read()) {
- sb.append((char) c);
- }
- }
-
- ClassLoader cl = Main.class.getClassLoader();
- if (classpath != null) {
- List<URL> urls = getFiles(new File(classpath));
- cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), cl);
- }
-
- discoverCommands(commandProcessor, cl);
-
- run(commandProcessor, threadIO, sb.toString(), in, out, err);
- }
-
- private void run(final CommandProcessorImpl commandProcessor, ThreadIO threadIO, String command, final InputStream in, final PrintStream out, final PrintStream err) throws Exception {
-
- if (command.length() > 0) {
-
- // Shell is directly executing a sub/command, we don't setup a terminal and console
- // in this case, this avoids us reading from stdin un-necessarily.
- CommandSession session = commandProcessor.createSession(in, out, err);
- session.put("USER", user);
- session.put("APPLICATION", application);
- session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
-
- try {
- session.execute(command);
- } catch (Throwable t) {
- if (t instanceof CommandNotFoundException) {
- String str = Ansi.ansi()
- .fg(Ansi.Color.RED)
- .a("Command not found: ")
- .a(Ansi.Attribute.INTENSITY_BOLD)
- .a(((CommandNotFoundException) t).getCommand())
- .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
- .fg(Ansi.Color.DEFAULT).toString();
- session.getConsole().println(str);
- } else if (t instanceof CommandException) {
- session.getConsole().println(((CommandException) t).getNiceHelp());
- } else {
- session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString());
- t.printStackTrace(session.getConsole());
- session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString());
- }
- }
- } else {
- // We are going into full blown interactive shell mode.
-
- final TerminalFactory terminalFactory = new TerminalFactory();
- final Terminal terminal = terminalFactory.getTerminal();
- ConsoleImpl console = createConsole(commandProcessor, threadIO, in, out, err, terminal);
- CommandSession session = console.getSession();
- session.put("USER", user);
- session.put("APPLICATION", application);
- session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
- session.put("#LINES", new Function() {
- public Object execute(CommandSession session, List<Object> arguments) throws Exception {
- return Integer.toString(terminal.getHeight());
- }
- });
- session.put("#COLUMNS", new Function() {
- public Object execute(CommandSession session, List<Object> arguments) throws Exception {
- return Integer.toString(terminal.getWidth());
- }
- });
- session.put(".jline.terminal", terminal);
-
- console.run();
-
- terminalFactory.destroy();
- }
-
- }
-
- /**
- * Allow sub classes of main to change the ConsoleImpl implementation used.
- *
- * @param commandProcessor The command processor to use.
- * @param threadIO The thread IO to use for the console.
- * @param in The input stream.
- * @param out The output stream.
- * @param err The error stream.
- * @param terminal The terminal to use.
- * @return The created console implementation.
- * @throws Exception If the console creation fails.
- */
- protected ConsoleImpl createConsole(CommandProcessorImpl commandProcessor, ThreadIO threadIO, InputStream in, PrintStream out, PrintStream err, Terminal terminal) throws Exception {
- return new ConsoleImpl(commandProcessor, threadIO, in, out, err, terminal, null, null, null, false);
- }
-
- /**
- * Sub classes can override so that their registered commands do not conflict with the default shell
- * implementation.
- *
- * @return The location of the commands resource.
- */
- public String getDiscoveryResource() {
- return "META-INF/services/org/apache/karaf/shell/commands";
- }
-
- private void discoverCommands(CommandProcessorImpl commandProcessor, ClassLoader cl) throws IOException, ClassNotFoundException {
- Enumeration<URL> urls = cl.getResources(getDiscoveryResource());
- while (urls.hasMoreElements()) {
- URL url = urls.nextElement();
- BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()));
- String line = r.readLine();
- while (line != null) {
- line = line.trim();
- if (line.length() > 0 && line.charAt(0) != '#') {
- final Class<Action> actionClass = (Class<Action>) cl.loadClass(line);
- Command cmd = actionClass.getAnnotation(Command.class);
- Function function = new AbstractCommand() {
- @Override
- public Action createNewAction() {
- try {
- return ((Class<? extends Action>) actionClass).newInstance();
- } catch (InstantiationException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
- };
- addCommand(cmd, function, commandProcessor);
- }
- line = r.readLine();
- }
- r.close();
- }
- }
-
- protected void addCommand(Command cmd, Function function, CommandProcessorImpl commandProcessor) {
- try {
- commandProcessor.addCommand(cmd.scope(), function, cmd.name());
- } catch (Exception e) {
- }
- }
-
-
- public String getApplication() {
- return application;
- }
-
- public void setApplication(String application) {
- this.application = application;
- }
-
- public String getUser() {
- return user;
- }
-
- public void setUser(String user) {
- this.user = user;
- }
-
- /**
- * <p>Returns whether or not we are in multi-scope mode.</p>
- *
- * <p>The default mode is multi-scoped where we prefix commands by their scope. If we are in single
- * scoped mode then we don't use scope prefixes when registering or tab completing commands.</p>
- *
- * @return True if the command is multi-scoped, false else.
- */
- public boolean isMultiScopeMode() {
- return true;
- }
-
- private static PrintStream wrap(PrintStream stream) {
- OutputStream o = AnsiConsole.wrapOutputStream(stream);
- if (o instanceof PrintStream) {
- return ((PrintStream) o);
- } else {
- return new PrintStream(o);
- }
- }
-
- private static <T> T unwrap(T stream) {
- try {
- Method mth = stream.getClass().getMethod("getRoot");
- return (T) mth.invoke(stream);
- } catch (Throwable t) {
- return stream;
- }
- }
-
- private static List<URL> getFiles(File base) throws MalformedURLException {
- List<URL> urls = new ArrayList<URL>();
- getFiles(base, urls);
- return urls;
- }
-
- private static void getFiles(File base, List<URL> urls) throws MalformedURLException {
- for (File f : base.listFiles()) {
- if (f.isDirectory()) {
- getFiles(f, urls);
- } else if (f.getName().endsWith(".jar")) {
- urls.add(f.toURI().toURL());
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/Branding.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/Branding.java b/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/Branding.java
deleted file mode 100644
index 55148d6..0000000
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/Branding.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.karaf.shell.console.impl.jline;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import jline.Terminal;
-
-public final class Branding {
-
- static final Logger LOGGER = LoggerFactory.getLogger(Branding.class);
-
- private Branding() { }
-
- public static Properties loadBrandingProperties() {
- Properties props = new Properties();
- loadProps(props, "org/apache/karaf/shell/console/branding.properties");
- loadProps(props, "org/apache/karaf/branding/branding.properties");
- return props;
- }
-
- public static Properties loadBrandingProperties(Terminal terminal) {
- Properties props = new Properties();
- if (terminal != null && terminal.getClass().getName().endsWith("SshTerminal")) {
- //it's a ssh client, so load branding separately
- loadProps(props, "org/apache/karaf/shell/console/branding-ssh.properties");
- } else {
- loadProps(props, "org/apache/karaf/shell/console/branding.properties");
- }
-
- loadProps(props, "org/apache/karaf/branding/branding.properties");
-
- // load branding from etc/branding.properties
- File etcBranding = new File(System.getProperty("karaf.etc"), "branding.properties");
- if (etcBranding.exists()) {
- FileInputStream etcBrandingIs = null;
- try {
- etcBrandingIs = new FileInputStream(etcBranding);
- } catch (FileNotFoundException e) {
- LOGGER.trace("Could not load branding.", e);
- }
- loadProps(props, etcBrandingIs);
- }
- return props;
- }
-
- protected static void loadProps(Properties props, String resource) {
- InputStream is = Branding.class.getClassLoader().getResourceAsStream(resource);
- loadProps(props, is);
- }
-
- protected static void loadProps(Properties props, InputStream is) {
- try {
- if (is != null) {
- props.load(is);
- }
- } catch (IOException e) {
- // ignore
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- // Ignore
- }
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/CompleterAsCompletor.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/CompleterAsCompletor.java b/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/CompleterAsCompletor.java
deleted file mode 100644
index 9c49746..0000000
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/CompleterAsCompletor.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.karaf.shell.console.impl.jline;
-
-import java.util.List;
-
-public class CompleterAsCompletor implements jline.console.completer.Completer {
-
- private final org.apache.karaf.shell.console.Completer completer;
-
- public CompleterAsCompletor(org.apache.karaf.shell.console.Completer completer) {
- this.completer = completer;
- }
-
- public int complete(String buffer, int cursor, List candidates) {
- return completer.complete(buffer, cursor, candidates);
- }
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleFactoryService.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleFactoryService.java b/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleFactoryService.java
deleted file mode 100644
index 02661ea..0000000
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleFactoryService.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.karaf.shell.console.impl.jline;
-
-import java.io.InputStream;
-import java.io.PrintStream;
-import java.lang.management.ManagementFactory;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-
-import jline.Terminal;
-import org.apache.felix.service.command.CommandProcessor;
-import org.apache.felix.service.command.CommandSession;
-import org.apache.felix.service.command.Function;
-import org.apache.felix.service.threadio.ThreadIO;
-import org.apache.karaf.shell.console.Console;
-import org.apache.karaf.shell.console.factory.ConsoleFactory;
-import org.apache.karaf.shell.util.ShellUtil;
-import org.apache.karaf.util.jaas.JaasHelper;
-import org.osgi.framework.BundleContext;
-
-public class ConsoleFactoryService implements ConsoleFactory {
-
- private static final Class<?>[] SECURITY_BUGFIX = {
- JaasHelper.class,
- JaasHelper.OsgiSubjectDomainCombiner.class,
- JaasHelper.DelegatingProtectionDomain.class,
- };
-
- private final BundleContext bundleContext;
-
- private CommandProcessor processor;
-
- private ThreadIO threadIO;
-
- public ConsoleFactoryService(BundleContext bc, CommandProcessor processor, ThreadIO threadIO) {
- bundleContext = bc;
- this.processor = processor;
- this.threadIO = threadIO;
- }
-
- @Override
- public Console create(InputStream in, PrintStream out, PrintStream err, final Terminal terminal,
- String encoding, Runnable closeCallback) {
- ConsoleImpl console = new ConsoleImpl(processor, threadIO, in, out, err, terminal, encoding, closeCallback, bundleContext, true);
- CommandSession session = console.getSession();
-
- session.put("USER", ShellUtil.getCurrentUserName());
- session.put("APPLICATION", System.getProperty("karaf.name", "root"));
- session.put("#LINES", new Function() {
- public Object execute(CommandSession session, List<Object> arguments) throws Exception {
- return Integer.toString(terminal.getHeight());
- }
- });
- session.put("#COLUMNS", new Function() {
- public Object execute(CommandSession session, List<Object> arguments) throws Exception {
- return Integer.toString(terminal.getWidth());
- }
- });
- session.put(".jline.terminal", terminal);
- addSystemProperties(session);
- session.put("pid", getPid());
- return console;
- }
-
- private String getPid() {
- String name = ManagementFactory.getRuntimeMXBean().getName();
- String[] parts = name.split("@");
- return parts[0];
- }
-
- private void addSystemProperties(CommandSession session) {
- Properties sysProps = System.getProperties();
- Iterator<Object> it = sysProps.keySet().iterator();
- while (it.hasNext()) {
- String key = (String) it.next();
- session.put(key, System.getProperty(key));
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/b4ae5245/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java b/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java
deleted file mode 100644
index 78d7479..0000000
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java
+++ /dev/null
@@ -1,599 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.karaf.shell.console.impl.jline;
-
-import java.io.CharArrayWriter;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.InterruptedIOException;
-import java.io.PrintStream;
-import java.io.Reader;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.BlockingQueue;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import jline.Terminal;
-import jline.UnsupportedTerminal;
-import jline.console.ConsoleReader;
-import jline.console.history.MemoryHistory;
-import jline.console.history.PersistentHistory;
-import org.apache.felix.service.command.CommandProcessor;
-import org.apache.felix.service.command.CommandSession;
-import org.apache.felix.service.command.Converter;
-import org.apache.felix.service.threadio.ThreadIO;
-import org.apache.karaf.shell.console.CloseShellException;
-import org.apache.karaf.shell.console.CommandSessionHolder;
-import org.apache.karaf.shell.console.Completer;
-import org.apache.karaf.shell.console.Console;
-import org.apache.karaf.shell.console.SessionProperties;
-import org.apache.karaf.shell.console.completer.CommandsCompleter;
-import org.apache.karaf.shell.security.impl.SecuredCommandProcessorImpl;
-import org.apache.karaf.shell.util.ShellUtil;
-import org.osgi.framework.BundleContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class ConsoleImpl implements Console {
-
- public static final String SHELL_INIT_SCRIPT = "karaf.shell.init.script";
- public static final String SHELL_HISTORY_MAXSIZE = "karaf.shell.history.maxSize";
- public static final String PROMPT = "PROMPT";
- public static final String DEFAULT_PROMPT = "\u001B[1m${USER}\u001B[0m@${APPLICATION}(${SUBSHELL})> ";
-
- private static final Logger LOGGER = LoggerFactory.getLogger(Console.class);
-
- protected CommandSession session;
- protected ThreadIO threadIO;
- private ConsoleReader reader;
- private BlockingQueue<Integer> queue;
- private boolean interrupt;
- private Thread pipe;
- volatile private boolean running;
- volatile private boolean eof;
- private Runnable closeCallback;
- private Terminal terminal;
- private InputStream consoleInput;
- private InputStream in;
- private PrintStream out;
- private PrintStream err;
- private boolean secured;
- private Thread thread;
- private final BundleContext bundleContext;
- private CommandsCompleter completer;
-
- public ConsoleImpl(CommandProcessor processor,
- ThreadIO threadIO,
- InputStream in,
- PrintStream out,
- PrintStream err,
- Terminal term,
- String encoding,
- Runnable closeCallback,
- BundleContext bc,
- boolean secured) {
- this.threadIO = threadIO;
- this.in = in;
- this.out = out;
- this.err = err;
- this.secured = secured;
- this.queue = new ArrayBlockingQueue<Integer>(1024);
- this.terminal = term == null ? new UnsupportedTerminal() : term;
- this.consoleInput = new ConsoleInputStream();
- if (secured) {
- this.session = new DelegateSession();
- } else {
- this.session = processor.createSession(consoleInput, out, err);
- }
- this.session.put("SCOPE", "shell:bundle:*");
- this.session.put("SUBSHELL", "");
- this.setCompletionMode();
- this.closeCallback = closeCallback;
- this.bundleContext = bc;
-
- try {
- reader = new ConsoleReader(null,
- this.consoleInput,
- this.out,
- this.terminal,
- encoding);
- } catch (IOException e) {
- throw new RuntimeException("Error opening console reader", e);
- }
-
- final File file = getHistoryFile();
-
- try {
- file.getParentFile().mkdirs();
- reader.setHistory(new KarafFileHistory(file));
- } catch (Exception e) {
- LOGGER.error("Can not read history from file " + file + ". Using in memory history", e);
- }
-
- if (reader != null && reader.getHistory() instanceof MemoryHistory) {
- String maxSizeStr = System.getProperty(SHELL_HISTORY_MAXSIZE);
- if (maxSizeStr != null) {
- ((MemoryHistory)reader.getHistory()).setMaxSize(Integer.parseInt(maxSizeStr));
- }
- }
-
- session.put(".jline.reader", reader);
- session.put(".jline.history", reader.getHistory());
- completer = createCompleter();
- if (completer != null) {
- reader.addCompleter(new CompleterAsCompletor(completer));
- }
- pipe = new Thread(new Pipe());
- pipe.setName("gogo shell pipe thread");
- pipe.setDaemon(true);
- }
-
- /**
- * Subclasses can override to use a different history file.
- *
- * @return The history file.
- */
- protected File getHistoryFile() {
- String defaultHistoryPath = new File(System.getProperty("user.home"), ".karaf/karaf.history").toString();
- return new File(System.getProperty("karaf.history", defaultHistoryPath));
- }
-
- public boolean isSecured() {
- return secured;
- }
-
- public CommandSession getSession() {
- return session;
- }
-
- public void close(boolean closedByUser) {
- if (!running) {
- return;
- }
- if (reader.getHistory() instanceof PersistentHistory) {
- try {
- ((PersistentHistory) reader.getHistory()).flush();
- } catch (IOException e) {
- // ignore
- }
- }
- running = false;
- CommandSessionHolder.unset();
- pipe.interrupt();
- completer.dispose();
- if (closedByUser && closeCallback != null) {
- closeCallback.run();
- }
- }
-
- public void run() {
- try {
- threadIO.setStreams(consoleInput, out, err);
- SecuredCommandProcessorImpl secCP = null;
- if (secured) {
- secCP = createSecuredCommandProcessor();
- }
- thread = Thread.currentThread();
- CommandSessionHolder.setSession(session);
- running = true;
- pipe.start();
- Properties brandingProps = Branding.loadBrandingProperties(terminal);
- welcome(brandingProps);
- setSessionProperties(brandingProps);
- String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
- executeScript(scriptFileName);
- while (running) {
- try {
- String command = readAndParseCommand();
- if (command == null) {
- break;
- }
- //session.getConsole().println("Executing: " + line);
- Object result = session.execute(command);
- if (result != null) {
- session.getConsole().println(session.format(result, Converter.INSPECT));
- }
- } catch (InterruptedIOException e) {
- //System.err.println("^C");
- // TODO: interrupt current thread
- } catch (InterruptedException e) {
- //interrupt current thread
- } catch (CloseShellException e) {
- break;
- } catch (Throwable t) {
- ShellUtil.logException(session, t);
- }
- }
- if (secured) {
- try {
- secCP.close();
- } catch (Throwable t) {
- // Ignore
- }
- }
- close(true);
- } finally {
- try {
- threadIO.close();
- } catch (Throwable t) {
- // Ignore
- }
- }
- }
-
- SecuredCommandProcessorImpl createSecuredCommandProcessor() {
- if (!(session instanceof DelegateSession)) {
- throw new IllegalStateException("Should be an Delegate Session here, about to set the delegate");
- }
- DelegateSession is = (DelegateSession) session;
-
- // make it active
- SecuredCommandProcessorImpl secCP = new SecuredCommandProcessorImpl(bundleContext);
- CommandSession s = secCP.createSession(consoleInput, out, err);
-
- // before the session is activated attributes may have been set on it. Pass these on to the real session now
- is.setDelegate(s);
- return secCP;
- }
-
- private void setCompletionMode() {
- try {
- File shellCfg = new File(System.getProperty("karaf.etc"), "/org.apache.karaf.shell.cfg");
- Properties properties = new Properties();
- properties.load(new FileInputStream(shellCfg));
- if (properties.get("completionMode") != null) {
- this.session.put(SessionProperties.COMPLETION_MODE, properties.get("completionMode"));
- } else {
- LOGGER.debug("completionMode property is not defined in etc/org.apache.karaf.shell.cfg file. Using default completion mode.");
- }
- } catch (Exception e) {
- LOGGER.warn("Can't read {}/org.apache.karaf.shell.cfg file. The completion is set to default.", System.getProperty("karaf.etc"));
- }
- }
-
- private String readAndParseCommand() throws IOException {
- String command = null;
- boolean loop = true;
- boolean first = true;
- while (loop) {
- checkInterrupt();
- String line = reader.readLine(first ? getPrompt() : "> ");
- if (line == null) {
- break;
- }
- if (command == null) {
- command = line;
- } else {
- if (command.charAt(command.length() - 1) == '\\') {
- command = command.substring(0, command.length() - 1) + line;
- } else {
- command += "\n" + line;
- }
- }
- if (reader.getHistory().size() == 0) {
- reader.getHistory().add(command);
- } else {
- // jline doesn't add blank lines to the history so we don't
- // need to replace the command in jline's console history with
- // an indented one
- if (command.length() > 0 && !" ".equals(command)) {
- reader.getHistory().replace(command);
- }
- }
- if (command.length() > 0 && command.charAt(command.length() - 1) == '\\') {
- loop = true;
- first = false;
- } else {
- try {
- Class<?> cl = CommandSession.class.getClassLoader().loadClass("org.apache.felix.gogo.runtime.Parser");
- Object parser = cl.getConstructor(CharSequence.class).newInstance(command);
- cl.getMethod("program").invoke(parser);
- loop = false;
- } catch (Exception e) {
- loop = true;
- first = false;
- } catch (Throwable t) {
- // Reflection problem ? just quit
- loop = false;
- }
- }
- }
- return command;
- }
-
- private void executeScript(String scriptFileName) {
- if (scriptFileName != null) {
- Reader r = null;
- try {
- File scriptFile = new File(scriptFileName);
- r = new InputStreamReader(new FileInputStream(scriptFile));
- CharArrayWriter w = new CharArrayWriter();
- int n;
- char[] buf = new char[8192];
- while ((n = r.read(buf)) > 0) {
- w.write(buf, 0, n);
- }
- session.execute(new String(w.toCharArray()));
- } catch (Exception e) {
- LOGGER.debug("Error in initialization script", e);
- System.err.println("Error in initialization script: " + e.getMessage());
- } finally {
- if (r != null) {
- try {
- r.close();
- } catch (IOException e) {
- // Ignore
- }
- }
- }
- }
- }
-
- protected void welcome(Properties brandingProps) {
- String welcome = brandingProps.getProperty("welcome");
- if (welcome != null && welcome.length() > 0) {
- session.getConsole().println(welcome);
- }
- }
-
- protected void setSessionProperties(Properties brandingProps) {
- for (Map.Entry<Object, Object> entry : brandingProps.entrySet()) {
- String key = (String) entry.getKey();
- if (key.startsWith("session.")) {
- session.put(key.substring("session.".length()), entry.getValue());
- }
- }
- }
-
- protected CommandsCompleter createCompleter() {
- return new CommandsCompleter(session);
- }
-
- protected String getPrompt() {
- try {
- String prompt;
- try {
- Object p = session.get(PROMPT);
- if (p != null) {
- prompt = p.toString();
- } else {
- Properties properties = Branding.loadBrandingProperties(terminal);
- if (properties.getProperty("prompt") != null) {
- prompt = properties.getProperty("prompt");
- // we put the PROMPT in ConsoleSession to avoid to read
- // the properties file each time.
- session.put(PROMPT, prompt);
- } else {
- prompt = DEFAULT_PROMPT;
- }
- }
- } catch (Throwable t) {
- prompt = DEFAULT_PROMPT;
- }
- Matcher matcher = Pattern.compile("\\$\\{([^}]+)\\}").matcher(prompt);
- while (matcher.find()) {
- Object rep = session.get(matcher.group(1));
- if (rep != null) {
- prompt = prompt.replace(matcher.group(0), rep.toString());
- matcher.reset(prompt);
- }
- }
- return prompt;
- } catch (Throwable t) {
- return "$ ";
- }
- }
-
- private void checkInterrupt() throws IOException {
- if (Thread.interrupted() || interrupt) {
- interrupt = false;
- throw new InterruptedIOException("Keyboard interruption");
- }
- }
-
- private void interrupt() {
- interrupt = true;
- thread.interrupt();
- }
-
- private class ConsoleInputStream extends InputStream {
- private int read(boolean wait) throws IOException {
- if (!running) {
- return -1;
- }
- checkInterrupt();
- if (eof && queue.isEmpty()) {
- return -1;
- }
- Integer i;
- if (wait) {
- try {
- i = queue.take();
- } catch (InterruptedException e) {
- throw new InterruptedIOException();
- }
- checkInterrupt();
- } else {
- i = queue.poll();
- }
- if (i == null) {
- return -1;
- }
- return i;
- }
-
- @Override
- public int read() throws IOException {
- return read(true);
- }
-
- @Override
- public int read(byte b[], int off, int len) throws IOException {
- if (b == null) {
- throw new NullPointerException();
- } else if (off < 0 || len < 0 || len > b.length - off) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return 0;
- }
-
- int nb = 1;
- int i = read(true);
- if (i < 0) {
- return -1;
- }
- b[off++] = (byte) i;
- while (nb < len) {
- i = read(false);
- if (i < 0) {
- return nb;
- }
- b[off++] = (byte) i;
- nb++;
- }
- return nb;
- }
-
- @Override
- public int available() throws IOException {
- return queue.size();
- }
- }
-
- private class Pipe implements Runnable {
- public void run() {
- try {
- while (running) {
- try {
- int c = in.read();
- if (c == -1) {
- return;
- } else if (c == 4 && !ShellUtil.getBoolean(session, SessionProperties.IGNORE_INTERRUPTS)) {
- err.println("^D");
- interrupt();
- return;
- } else if (c == 3 && !ShellUtil.getBoolean(session, SessionProperties.IGNORE_INTERRUPTS)) {
- err.println("^C");
- reader.getCursorBuffer().clear();
- interrupt();
- }
- queue.put(c);
- } catch (Throwable t) {
- return;
- }
- }
- } finally {
- eof = true;
- try {
- queue.put(-1);
- } catch (InterruptedException e) {
- }
- }
- }
- }
-
- static class DelegateSession implements CommandSession {
- final Map<String, Object> attrs = new HashMap<String, Object>();
- volatile CommandSession delegate;
-
- @Override
- public Object execute(CharSequence commandline) throws Exception {
- if (delegate != null)
- return delegate.execute(commandline);
-
- throw new UnsupportedOperationException();
- }
-
- void setDelegate(CommandSession s) {
- synchronized (this) {
- for (Map.Entry<String, Object> entry : attrs.entrySet()) {
- s.put(entry.getKey(), entry.getValue());
- }
- }
- delegate = s;
- }
-
- @Override
- public void close() {
- if (delegate != null)
- delegate.close();
- }
-
- @Override
- public InputStream getKeyboard() {
- if (delegate != null)
- return delegate.getKeyboard();
-
- throw new UnsupportedOperationException();
- }
-
- @Override
- public PrintStream getConsole() {
- if (delegate != null)
- return delegate.getConsole();
-
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Object get(String name) {
- if (delegate != null)
- return delegate.get(name);
-
- return attrs.get(name);
- }
-
- // you can put attributes on this session before it's delegate is set...
- @Override
- public void put(String name, Object value) {
- if (delegate != null) {
- delegate.put(name, value);
- return;
- }
-
- // there is no delegate yet, so we'll keep the attributes locally
- synchronized (this) {
- attrs.put(name, value);
- }
- }
-
- @Override
- public CharSequence format(Object target, int level) {
- if (delegate != null)
- return delegate.format(target, level);
-
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Object convert(Class<?> type, Object instance) {
- if (delegate != null)
- return delegate.convert(type, instance);
-
- throw new UnsupportedOperationException();
- }
- }
-
-}