You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2013/10/09 20:54:18 UTC
svn commit: r1530761 - in /karaf/trunk:
assemblies/features/framework/src/main/resources/resources/etc/
shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/
shell/commands/src/main/resources/OSGI-INF/blueprint/
shell/console/src/main/java...
Author: jbonofre
Date: Wed Oct 9 18:54:18 2013
New Revision: 1530761
URL: http://svn.apache.org/r1530761
Log:
[KARAF-2496] Add a session configuration to define the completion mode
Added:
karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CompletionAction.java
- copied, changed from r1530425, karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java
karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTracesAction.java
- copied, changed from r1530425, karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java
Removed:
karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java
Modified:
karaf/trunk/assemblies/features/framework/src/main/resources/resources/etc/org.apache.karaf.shell.cfg
karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/SessionProperties.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java
Modified: karaf/trunk/assemblies/features/framework/src/main/resources/resources/etc/org.apache.karaf.shell.cfg
URL: http://svn.apache.org/viewvc/karaf/trunk/assemblies/features/framework/src/main/resources/resources/etc/org.apache.karaf.shell.cfg?rev=1530761&r1=1530760&r2=1530761&view=diff
==============================================================================
--- karaf/trunk/assemblies/features/framework/src/main/resources/resources/etc/org.apache.karaf.shell.cfg (original)
+++ karaf/trunk/assemblies/features/framework/src/main/resources/resources/etc/org.apache.karaf.shell.cfg Wed Oct 9 18:54:18 2013
@@ -59,4 +59,17 @@ hostKey=${karaf.base}/etc/host.key
#
# Specify host key algorithm, defaults to DSA
#
-# algorithm=DSA
\ No newline at end of file
+# algorithm=DSA
+
+#
+# Defines the completion mode on the Karaf shell console. The possible values are:
+# - GLOBAL: it's the same behavior as in previous Karaf releases. The completion displays all commands and all aliases
+# ignoring if you are in a subshell or not.
+# - FIRST: the completion displays all commands and all aliases only when you are not in a subshell. When you are
+# in a subshell, the completion displays only the commands local to the subshell.
+# - SUBSHELL: the completion displays only the subshells on the root level. When you are in a subshell, the completion
+# displays only the commands local to the subshell.
+# This property define the default value when you use the Karaf shell console.
+# You can change the completion mode directly in the shell console, using shell:completion command.
+#
+completionMode=GLOBAL
\ No newline at end of file
Copied: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CompletionAction.java (from r1530425, karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java)
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CompletionAction.java?p2=karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CompletionAction.java&p1=karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java&r1=1530425&r2=1530761&rev=1530761&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java (original)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CompletionAction.java Wed Oct 9 18:54:18 2013
@@ -22,18 +22,21 @@ import org.apache.karaf.shell.console.Ab
import org.apache.karaf.shell.console.SessionProperties;
/**
- * Command for showing the full tree of bundles that have been used to resolve
- * a given bundle.
+ * Command to change the completion mode while using the shell console.
*/
-@Command(scope = "shell", name = "stack-traces-print", description = "Prints the full stack trace in the console when the execution of a command throws an exception.")
-public class PrintStackTraces extends AbstractAction {
+@Command(scope = "shell", name = "completion", description = "Change the completion mode on the current console session.")
+public class CompletionAction extends AbstractAction {
- @Argument(name = "print", description="Print stack traces or not", required = false, multiValued = false)
- boolean print = true;
+ @Argument(index = 0, name = "mode", description = "", required = true, multiValued = false)
+ String mode;
protected Object doExecute() throws Exception {
- System.out.println("Printing of stacktraces set to " + print);
- session.put(SessionProperties.PRINT_STACK_TRACES, Boolean.valueOf(print));
+ if (!mode.equalsIgnoreCase("global") && !mode.equalsIgnoreCase("first") && !mode.equalsIgnoreCase("subshell")) {
+ System.err.println("The completion mode is not correct. The valid modes are: global, first, subshell. See documentation for details.");
+ return null;
+ }
+
+ session.put(SessionProperties.COMPLETION_MODE, mode);
return null;
}
Copied: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTracesAction.java (from r1530425, karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java)
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTracesAction.java?p2=karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTracesAction.java&p1=karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java&r1=1530425&r2=1530761&rev=1530761&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTraces.java (original)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/PrintStackTracesAction.java Wed Oct 9 18:54:18 2013
@@ -26,7 +26,7 @@ import org.apache.karaf.shell.console.Se
* a given bundle.
*/
@Command(scope = "shell", name = "stack-traces-print", description = "Prints the full stack trace in the console when the execution of a command throws an exception.")
-public class PrintStackTraces extends AbstractAction {
+public class PrintStackTracesAction extends AbstractAction {
@Argument(name = "print", description="Print stack traces or not", required = false, multiValued = false)
boolean print = true;
Modified: karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1530761&r1=1530760&r2=1530761&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
+++ karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Wed Oct 9 18:54:18 2013
@@ -30,6 +30,9 @@
<action class="org.apache.karaf.shell.commands.impl.ClearAction"/>
</command>
<command>
+ <action class="org.apache.karaf.shell.commands.impl.CompletionAction"/>
+ </command>
+ <command>
<action class="org.apache.karaf.shell.commands.impl.DateAction"/>
</command>
<command>
@@ -68,7 +71,7 @@
<action class="org.apache.karaf.shell.commands.impl.NewAction"/>
</command>
<command>
- <action class="org.apache.karaf.shell.commands.impl.PrintStackTraces" />
+ <action class="org.apache.karaf.shell.commands.impl.PrintStackTracesAction"/>
</command>
<command>
<action class="org.apache.karaf.shell.commands.impl.LogoutAction"/>
Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/SessionProperties.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/SessionProperties.java?rev=1530761&r1=1530760&r2=1530761&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/SessionProperties.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/SessionProperties.java Wed Oct 9 18:54:18 2013
@@ -23,5 +23,6 @@ public class SessionProperties {
public static final String PRINT_STACK_TRACES = "karaf.printStackTraces";
public static final String LAST_EXCEPTION = "karaf.lastException";
public static final String IGNORE_INTERRUPTS = "karaf.ignoreInterrupts";
+ public static final String COMPLETION_MODE = "karaf.completionMode";
}
Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java?rev=1530761&r1=1530760&r2=1530761&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java Wed Oct 9 18:54:18 2013
@@ -30,6 +30,7 @@ import org.apache.felix.service.command.
import org.apache.felix.service.command.Function;
import org.apache.karaf.shell.console.CommandSessionHolder;
import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.SessionProperties;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
@@ -74,6 +75,9 @@ public class CommandsCompleter implement
// TODO: fix that in gogo instead
// get the current sub-shell
String subshell = (String) session.get("SUBSHELL");
+ String completion = (String) session.get(SessionProperties.COMPLETION_MODE);
+ if (completion == null)
+ completion = "GLOBAL";
Set<String> names = new HashSet<String>((Set<String>) session.get(COMMANDS));
Set<String> filteredNames = new HashSet<String>();
@@ -82,16 +86,20 @@ public class CommandsCompleter implement
filteredNames.add(command);
}
}
-
+
if (!filteredNames.equals(commands)) {
commands.clear();
completers.clear();
-
- if (subshell == null || subshell.length() == 0) {
- // Add aliases if we are not in a subshell
+ if (completion.equalsIgnoreCase("GLOBAL")) {
Set<String> aliases = this.getAliases();
completers.add(new StringsCompleter(aliases));
+ } else {
+ if (subshell == null || subshell.length() == 0) {
+ // add aliases if we are not in a subshell
+ Set<String> aliases = this.getAliases();
+ completers.add(new StringsCompleter(aliases));
+ }
}
// add argument completers for each command
@@ -100,23 +108,35 @@ public class CommandsCompleter implement
function = unProxy(function);
if (function instanceof CommandWithAction) {
- if (command.startsWith(subshell)) {
- if (subshell.length() > 1 && command.length() > subshell.length()) {
- command = command.substring(subshell.length() + 1);
- }
-
- // filter on subshell
- if (command.contains(":")) {
- int index = command.indexOf(':');
- command = command.substring(0, index);
- }
-
- command.trim();
-
+ if (completion.equalsIgnoreCase("GLOBAL") ||
+ (completion.equalsIgnoreCase("FIRST") && (subshell == null || subshell.length() == 0))) {
try {
completers.add(new ArgumentCompleter(session, (CommandWithAction) function, command));
} catch (Throwable t) {
- LOGGER.debug("Unable to create completers for command '" + command + "'", t);
+ LOGGER.debug("Unable to create completers for command '{}'", command, t);
+ }
+ } else {
+ if (command.startsWith(subshell)) {
+
+ if (subshell.length() > 1 && command.length() > subshell.length()) {
+ command = command.substring(subshell.length() + 1);
+ }
+
+ if (completion.equalsIgnoreCase("SUBSHELL")) {
+ // filter on subshell
+ // as the completion mode is set to SUBSHELL, we complete only with the commands local
+ // to the current subshell
+ if (command.contains(":")) {
+ int index = command.indexOf(':');
+ command = command.substring(0, index);
+ }
+ command.trim();
+ }
+ try {
+ completers.add(new ArgumentCompleter(session, (CommandWithAction) function, command));
+ } catch (Throwable t) {
+ LOGGER.debug("Unable to create completers for command '{}'", command, t);
+ }
}
}
}
@@ -136,7 +156,7 @@ public class CommandsCompleter implement
Set<String> aliases = new HashSet<String>();
for (String var : vars) {
Object content = session.get(var);
- if ("org.apache.felix.gogo.runtime.Closure".equals(content.getClass().getName())) {
+ if ("org.apache.felix.gogo.runtime.Closure".equals(content.getClass().getName())) {
aliases.add(var);
}
}
Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java?rev=1530761&r1=1530760&r2=1530761&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/ConsoleImpl.java Wed Oct 9 18:54:18 2013
@@ -51,15 +51,11 @@ import org.apache.karaf.shell.util.Shell
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class ConsoleImpl implements Console
-{
+public class ConsoleImpl implements Console {
public static final String SHELL_INIT_SCRIPT = "karaf.shell.init.script";
public static final String PROMPT = "PROMPT";
public static final String DEFAULT_PROMPT = "\u001B[1m${USER}\u001B[0m@${APPLICATION}(${SUBSHELL})> ";
- public static final String PRINT_STACK_TRACES = "karaf.printStackTraces";
- public static final String LAST_EXCEPTION = "karaf.lastException";
- public static final String IGNORE_INTERRUPTS = "karaf.ignoreInterrupts";
private static final Logger LOGGER = LoggerFactory.getLogger(Console.class);
@@ -79,13 +75,12 @@ public class ConsoleImpl implements Cons
private Thread thread;
public ConsoleImpl(CommandProcessor processor,
- InputStream in,
- PrintStream out,
- PrintStream err,
- Terminal term,
- String encoding,
- Runnable closeCallback)
- {
+ InputStream in,
+ PrintStream out,
+ PrintStream err,
+ Terminal term,
+ String encoding,
+ Runnable closeCallback) {
this.in = in;
this.out = out;
this.err = err;
@@ -95,26 +90,27 @@ public class ConsoleImpl implements Cons
this.session = processor.createSession(this.consoleInput, this.out, this.err);
this.session.put("SCOPE", "shell:bundle:*");
this.session.put("SUBSHELL", "");
+ this.setCompletionMode();
this.closeCallback = closeCallback;
try {
reader = new ConsoleReader(null,
- this.consoleInput,
- this.out,
- this.terminal,
- encoding);
+ this.consoleInput,
+ this.out,
+ this.terminal,
+ encoding);
} catch (IOException e) {
throw new RuntimeException("Error opening console reader", e);
}
- final File file = getHistoryFile();
-
+ 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);
- }
+ 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);
+ }
session.put(".jline.reader", reader);
session.put(".jline.history", reader.getHistory());
Completer completer = createCompleter();
@@ -128,10 +124,11 @@ public class ConsoleImpl implements Cons
/**
* Subclasses can override to use a different history file.
+ *
* @return
*/
protected File getHistoryFile() {
- String defaultHistoryPath = new File(System.getProperty("user.home"), ".karaf/karaf.history").toString();
+ String defaultHistoryPath = new File(System.getProperty("user.home"), ".karaf/karaf.history").toString();
return new File(System.getProperty("karaf.history", defaultHistoryPath));
}
@@ -154,14 +151,12 @@ public class ConsoleImpl implements Cons
running = false;
CommandSessionHolder.unset();
pipe.interrupt();
- if (closedByUser && closeCallback != null)
- {
+ if (closedByUser && closeCallback != null) {
closeCallback.run();
}
}
- public void run()
- {
+ public void run() {
thread = Thread.currentThread();
CommandSessionHolder.setSession(session);
running = true;
@@ -179,45 +174,52 @@ public class ConsoleImpl implements Cons
}
//session.getConsole().println("Executing: " + line);
Object result = session.execute(command);
- if (result != null)
- {
+ if (result != null) {
session.getConsole().println(session.format(result, Converter.INSPECT));
}
- }
- catch (InterruptedIOException e)
- {
+ } catch (InterruptedIOException e) {
//System.err.println("^C");
// TODO: interrupt current thread
- }
- catch (CloseShellException e)
- {
+ } catch (CloseShellException e) {
break;
- }
- catch (Throwable t)
- {
+ } catch (Throwable t) {
ShellUtil.logException(session, t);
}
}
close(true);
}
- 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;
- }
- if (reader.getHistory().size()==0) {
+ private void setCompletionMode() {
+ try {
+ File shellCfg = new File(System.getProperty("karaf.base"), "/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 {}/etc/org.apache.karaf.shell.cfg file. The completion is set to default.", System.getProperty("karaf.base"));
+ }
+ }
+
+ 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;
+ }
+ if (reader.getHistory().size() == 0) {
reader.getHistory().add(command);
} else {
// jline doesn't add blank lines to the history so we don't
@@ -248,8 +250,8 @@ public class ConsoleImpl implements Cons
return command;
}
- private void executeScript(String scriptFileName) {
- if (scriptFileName != null) {
+ private void executeScript(String scriptFileName) {
+ if (scriptFileName != null) {
Reader r = null;
try {
File scriptFile = new File(scriptFileName);
@@ -274,7 +276,7 @@ public class ConsoleImpl implements Cons
}
}
}
- }
+ }
protected void welcome(Properties brandingProps) {
String welcome = brandingProps.getProperty("welcome");
@@ -343,10 +345,8 @@ public class ConsoleImpl implements Cons
thread.interrupt();
}
- private class ConsoleInputStream extends InputStream
- {
- private int read(boolean wait) throws IOException
- {
+ private class ConsoleInputStream extends InputStream {
+ private int read(boolean wait) throws IOException {
if (!running) {
return -1;
}
@@ -372,14 +372,12 @@ public class ConsoleImpl implements Cons
}
@Override
- public int read() throws IOException
- {
+ public int read() throws IOException {
return read(true);
}
@Override
- public int read(byte b[], int off, int len) throws IOException
- {
+ 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) {
@@ -411,47 +409,32 @@ public class ConsoleImpl implements Cons
}
}
- private class Pipe implements Runnable
- {
- public void run()
- {
+ private class Pipe implements Runnable {
+ public void run() {
try {
- while (running)
- {
- try
- {
+ while (running) {
+ try {
int c = in.read();
- if (c == -1)
- {
+ if (c == -1) {
return;
- }
- else if (c == 4 && !ShellUtil.getBoolean(session, SessionProperties.IGNORE_INTERRUPTS))
- {
+ } else if (c == 4 && !ShellUtil.getBoolean(session, SessionProperties.IGNORE_INTERRUPTS)) {
err.println("^D");
return;
- }
- else if (c == 3 && !ShellUtil.getBoolean(session, SessionProperties.IGNORE_INTERRUPTS))
- {
+ } else if (c == 3 && !ShellUtil.getBoolean(session, SessionProperties.IGNORE_INTERRUPTS)) {
err.println("^C");
reader.getCursorBuffer().clear();
interrupt();
}
queue.put(c);
- }
- catch (Throwable t) {
+ } catch (Throwable t) {
return;
}
}
- }
- finally
- {
+ } finally {
eof = true;
- try
- {
+ try {
queue.put(-1);
- }
- catch (InterruptedException e)
- {
+ } catch (InterruptedException e) {
}
}
}
Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java?rev=1530761&r1=1530760&r2=1530761&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java Wed Oct 9 18:54:18 2013
@@ -42,99 +42,70 @@ public class ShellUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ShellUtil.class);
- public static String getBundleName(Bundle bundle)
- {
- if (bundle != null)
- {
+ public static String getBundleName(Bundle bundle) {
+ if (bundle != null) {
String name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME);
return (name == null)
- ? "Bundle " + Long.toString(bundle.getBundleId())
- : name + " (" + Long.toString(bundle.getBundleId()) + ")";
+ ? "Bundle " + Long.toString(bundle.getBundleId())
+ : name + " (" + Long.toString(bundle.getBundleId()) + ")";
}
return "[STALE BUNDLE]";
}
- public static String getUnderlineString(String s)
- {
+ public static String getUnderlineString(String s) {
StringBuilder sb = new StringBuilder(s.length());
- for (int i = 0; i < s.length(); i++)
- {
+ for (int i = 0; i < s.length(); i++) {
sb.append('-');
}
return sb.toString();
}
- public static String getValueString(Object obj)
- {
+ public static String getValueString(Object obj) {
if (obj == null) {
return "null";
- }
- else if (obj.getClass().isArray())
- {
+ } else if (obj.getClass().isArray()) {
Object[] array = (Object[]) obj;
StringBuilder sb = new StringBuilder();
sb.append("[");
- for (int i = 0; i < array.length; i++)
- {
- if (i != 0)
- {
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
sb.append(", ");
}
sb.append(getValueString(array[i]));
}
sb.append("]");
return sb.toString();
- }
- else if (obj instanceof String)
- {
+ } else if (obj instanceof String) {
return (String) obj;
- }
- else if (obj instanceof Boolean)
- {
+ } else if (obj instanceof Boolean) {
return ((Boolean) obj).toString();
- }
- else if (obj instanceof Long)
- {
+ } else if (obj instanceof Long) {
return ((Long) obj).toString();
- }
- else if (obj instanceof Integer)
- {
+ } else if (obj instanceof Integer) {
return ((Integer) obj).toString();
- }
- else if (obj instanceof Short)
- {
+ } else if (obj instanceof Short) {
return ((Short) obj).toString();
- }
- else if (obj instanceof Double)
- {
+ } else if (obj instanceof Double) {
return ((Double) obj).toString();
- }
- else if (obj instanceof Float)
- {
+ } else if (obj instanceof Float) {
return ((Float) obj).toString();
- }
- else if (obj instanceof URL)
- {
- return ((URL)obj).toExternalForm();
- }
- else if (obj instanceof URI)
- {
+ } else if (obj instanceof URL) {
+ return ((URL) obj).toExternalForm();
+ } else if (obj instanceof URI) {
try {
- return ((URI)obj).toURL().toExternalForm();
+ return ((URI) obj).toURL().toExternalForm();
} catch (MalformedURLException e) {
- LOGGER.error("URI could not be transformed to URL",e);
+ LOGGER.error("URI could not be transformed to URL", e);
return obj.toString();
}
- }
- else
- {
+ } else {
return obj.toString();
}
}
/**
* Check if a bundle is a system bundle (start level < 50)
- *
+ *
* @param bundleContext
* @param bundle
* @return true if the bundle has start level minor than 50
@@ -142,13 +113,12 @@ public class ShellUtil {
public static boolean isASystemBundle(BundleContext bundleContext, Bundle bundle) {
int level = bundle.adapt(BundleStartLevel.class).getStartLevel();
int sbsl = 49;
- final String sbslProp = bundleContext.getProperty( "karaf.systemBundlesStartLevel" );
+ final String sbslProp = bundleContext.getProperty("karaf.systemBundlesStartLevel");
if (sbslProp != null) {
try {
- sbsl = Integer.valueOf( sbslProp );
- }
- catch( Exception ignore ) {
- // ignore
+ sbsl = Integer.valueOf(sbslProp);
+ } catch (Exception ignore) {
+ // ignore
}
}
return level <= sbsl;
@@ -156,18 +126,18 @@ public class ShellUtil {
/**
* Ask the user to confirm the access to a system bundle
- *
+ *
* @param bundleId
* @param session
* @return true if the user confirm
* @throws IOException
*/
public static boolean accessToSystemBundleIsAllowed(long bundleId, CommandSession session) throws IOException {
- for (;;) {
+ for (; ; ) {
StringBuffer sb = new StringBuffer();
System.err.print("You are about to access system bundle " + bundleId + ". Do you wish to continue (yes/no): ");
System.err.flush();
- for (;;) {
+ for (; ; ) {
int c = session.getKeyboard().read();
if (c < 0) {
return false;
@@ -196,7 +166,7 @@ public class ShellUtil {
if (is == null) {
return "Unable to load description from " + path;
}
-
+
try {
Reader r = new InputStreamReader(is);
StringWriter sw = new StringWriter();
@@ -231,42 +201,41 @@ public class ShellUtil {
}
public static void logException(CommandSession session, Throwable t) {
- try {
+ try {
boolean isCommandNotFound = "org.apache.felix.gogo.runtime.CommandNotFoundException".equals(t.getClass().getName());
if (isCommandNotFound) {
LOGGER.debug("Unknown command entered", t);
- } else if (t instanceof CommandException ) {
+ } else if (t instanceof CommandException) {
LOGGER.debug("Command exception (Undefined option, ...)", t);
} else {
LOGGER.error("Exception caught while executing command", t);
}
- session.put(SessionProperties.LAST_EXCEPTION, t);
- if (t instanceof CommandException) {
- session.getConsole().println(((CommandException) t).getNiceHelp());
- } else if (isCommandNotFound) {
- String str = Ansi.ansi()
- .fg(Ansi.Color.RED)
- .a("Command not found: ")
- .a(Ansi.Attribute.INTENSITY_BOLD)
- .a(t.getClass().getMethod("getCommand").invoke(t))
- .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
- .fg(Ansi.Color.DEFAULT).toString();
- session.getConsole().println(str);
- }
- if ( getBoolean(session, SessionProperties.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) && !isCommandNotFound) {
- 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
- }
+ session.put(SessionProperties.LAST_EXCEPTION, t);
+ if (t instanceof CommandException) {
+ session.getConsole().println(((CommandException) t).getNiceHelp());
+ } else if (isCommandNotFound) {
+ String str = Ansi.ansi()
+ .fg(Ansi.Color.RED)
+ .a("Command not found: ")
+ .a(Ansi.Attribute.INTENSITY_BOLD)
+ .a(t.getClass().getMethod("getCommand").invoke(t))
+ .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
+ .fg(Ansi.Color.DEFAULT).toString();
+ session.getConsole().println(str);
+ }
+ if (getBoolean(session, SessionProperties.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) && !isCommandNotFound) {
+ 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
+ }
}
}