Index: src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java =================================================================== --- src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java (revision 1127017) +++ src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java (working copy) @@ -18,7 +18,6 @@ */ package org.apache.karaf.shell.console.completer; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -26,14 +25,12 @@ import java.util.Set; import org.apache.felix.gogo.commands.basic.AbstractCommand; +import org.apache.felix.gogo.runtime.Closure; import org.apache.felix.gogo.runtime.CommandProxy; import org.apache.felix.gogo.runtime.CommandSessionImpl; import org.apache.felix.service.command.CommandSession; import org.apache.felix.service.command.Function; -import org.apache.karaf.shell.console.CompletableFunction; import org.apache.karaf.shell.console.Completer; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; /** * Like the {@link org.apache.karaf.shell.console.completer.CommandsCompleter} but does not use OSGi but is @@ -64,9 +61,14 @@ if (!names.equals(commands)) { commands.clear(); completers.clear(); + + Set aliases = getAliases(); + completers.add(new StringsCompleter(aliases)); + + // add argument completers for each command for (String command : names) { Function function = (Function) session.get(command); - function = unProxy(function); + function = unProxy(function); if (function instanceof AbstractCommand) { completers.add(new ArgumentCompleter(session, (AbstractCommand) function, command)); } @@ -75,27 +77,31 @@ } } + + private Set getAliases() { + Set vars = (Set) session.get(null); + Set aliases = new HashSet(); + for (String varname : vars) { + Object content = session.get(varname); + if (content instanceof Closure) { + aliases.add(varname); + } + } + return aliases; + } + protected Function unProxy(Function function) { - try { - if (function instanceof CommandProxy) { - Field contextField = function.getClass().getDeclaredField("context"); - Field referenceField = function.getClass().getDeclaredField("reference"); - contextField.setAccessible(true); - referenceField.setAccessible(true); - BundleContext context = (BundleContext) contextField.get(function); - ServiceReference reference = (ServiceReference) referenceField.get(function); - Object target = context.getService(reference); - try { - if (target instanceof Function) { - function = (Function) target; - } - } finally { - context.ungetService(reference); - } - } - } catch (Throwable t) { - } - return function; + if (function instanceof CommandProxy) { + CommandProxy proxy = (CommandProxy) function; + Object target = proxy.getTarget(); + if (target instanceof Function) { + return (Function) target; + } else { + return function; + } + } else { + return function; + } } } Index: src/main/java/org/apache/karaf/shell/console/jline/Console.java =================================================================== --- src/main/java/org/apache/karaf/shell/console/jline/Console.java (revision 1127017) +++ src/main/java/org/apache/karaf/shell/console/jline/Console.java (working copy) @@ -182,57 +182,10 @@ welcome(); setSessionProperties(); String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT); - 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 - } - } - } - } + executeScript(scriptFileName); while (running) { try { - 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 { - command += " " + line; - } - reader.getHistory().replace(command); - try { - new Parser(command).program(); - loop = false; - } catch (Exception e) { - loop = true; - first = false; - } - } + String command = readAndParseCommand(); if (command == null) { break; } @@ -254,35 +207,7 @@ } catch (Throwable t) { - try { - LOGGER.info("Exception caught while executing command", t); - session.put(LAST_EXCEPTION, t); - if (t instanceof CommandException) { - session.getConsole().println(((CommandException) t).getNiceHelp()); - } else 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); - } - if ( getBoolean(PRINT_STACK_TRACES)) { - 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 if (!(t instanceof CommandException) && !(t instanceof CommandNotFoundException)) { - session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString()); - session.getConsole().println("Error executing command: " - + (t.getMessage() != null ? t.getMessage() : t.getClass().getName())); - session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString()); - } - } catch (Exception ignore) { - // ignore - } + logException(t); } } close(); @@ -293,6 +218,94 @@ } } + private void logException(Throwable t) { + try { + LOGGER.info("Exception caught while executing command", t); + session.put(LAST_EXCEPTION, t); + if (t instanceof CommandException) { + session.getConsole().println(((CommandException) t).getNiceHelp()); + } else 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); + } + if ( getBoolean(PRINT_STACK_TRACES)) { + 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 if (!(t instanceof CommandException) && !(t instanceof CommandNotFoundException)) { + session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString()); + session.getConsole().println("Error executing command: " + + (t.getMessage() != null ? t.getMessage() : t.getClass().getName())); + session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString()); + } + } catch (Exception ignore) { + // ignore + } + } + + 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 { + command += " " + line; + } + reader.getHistory().replace(command); + try { + new Parser(command).program(); + loop = false; + } catch (Exception e) { + loop = true; + first = 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 boolean getBoolean(String name) { Object s = session.get(name); if (s == null) {