FELIX-1694: [karaf][shell] Do not print stack traces on exceptions, patch provided by Alin Dreghiciu
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@821877 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
index be4baac..858e5bc 100644
--- a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
@@ -24,6 +24,7 @@
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Properties;
+import java.util.concurrent.Callable;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.concurrent.ArrayBlockingQueue;
@@ -32,7 +33,6 @@
import jline.ConsoleReader;
import jline.Terminal;
import jline.UnsupportedTerminal;
-import jline.WindowsTerminal;
import jline.AnsiWindowsTerminal;
import org.apache.felix.karaf.shell.console.Completer;
import org.osgi.service.command.CommandProcessor;
@@ -44,6 +44,7 @@
public static final String PROMPT = "PROMPT";
public static final String DEFAULT_PROMPT = "\u001B[1m${USER}\u001B[0m@${APPLICATION}> ";
+ public static final String PRINT_STACK_TRACES = "karaf.printStackTraces";
private CommandSession session;
private ConsoleReader reader;
@@ -57,6 +58,7 @@
private InputStream in;
private PrintStream out;
private PrintStream err;
+ private Callable<Boolean> printStackTraces;
public Console(CommandProcessor processor,
InputStream in,
@@ -64,7 +66,8 @@
PrintStream err,
Terminal term,
Completer completer,
- Runnable closeCallback) throws Exception
+ Runnable closeCallback,
+ Callable<Boolean> printStackTraces ) throws Exception
{
this.in = in;
this.out = out;
@@ -75,6 +78,7 @@
this.session = processor.createSession(this.consoleInput, this.out, this.err);
this.session.put("SCOPE", "shell:osgi:*");
this.closeCallback = closeCallback;
+ this.printStackTraces = printStackTraces;
reader = new ConsoleReader(this.consoleInput,
new PrintWriter(this.out),
@@ -125,8 +129,16 @@
}
catch (Throwable t)
{
- t.printStackTrace(session.getConsole());
- //System.err.println("Exception: " + t.getMessage());
+ try {
+ if ( printStackTraces.call()) {
+ t.printStackTrace(session.getConsole());
+ }
+ else {
+ session.getConsole().println(t.getMessage());
+ }
+ } catch (Exception ignore) {
+ // ignore
+ }
}
}
//System.err.println("Exiting console...");
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java
index 75085a8..756fb12 100644
--- a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java
@@ -23,6 +23,7 @@
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
+import java.util.concurrent.Callable;
import org.apache.felix.karaf.shell.console.Completer;
import org.apache.felix.karaf.shell.console.completer.AggregateCompleter;
@@ -81,13 +82,19 @@
}
}
};
+ final Callable<Boolean> printStackTraces = new Callable<Boolean>() {
+ public Boolean call() {
+ return Boolean.valueOf(bundleContext.getProperty(Console.PRINT_STACK_TRACES));
+ }
+ };
this.console = new Console(commandProcessor,
in,
wrap(out),
wrap(err),
terminal,
new AggregateCompleter(completers),
- callback);
+ callback,
+ printStackTraces);
CommandSession session = console.getSession();
session.put("USER", "karaf");
session.put("APPLICATION", System.getProperty("karaf.name", "root"));
diff --git a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellFactoryImpl.java b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellFactoryImpl.java
index f38cc1b..0be73f3 100644
--- a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellFactoryImpl.java
+++ b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellFactoryImpl.java
@@ -26,6 +26,7 @@
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.Callable;
import org.apache.felix.karaf.shell.console.Completer;
import org.apache.felix.karaf.shell.console.completer.AggregateCompleter;
@@ -86,6 +87,11 @@
public void start(final Environment env) throws IOException {
try {
+ final Callable<Boolean> printStackTraces = new Callable<Boolean>() {
+ public Boolean call() {
+ return Boolean.valueOf(System.getProperty(Console.PRINT_STACK_TRACES));
+ }
+ };
Console console = new Console(commandProcessor,
in,
new PrintStream(out),
@@ -96,7 +102,8 @@
public void run() {
destroy();
}
- });
+ },
+ printStackTraces);
CommandSession session = console.getSession();
session.put("APPLICATION", System.getProperty("karaf.name", "root"));
for (Map.Entry<String,String> e : env.getEnv().entrySet()) {
diff --git a/karaf/webconsole/gogo/src/main/java/org/apache/felix/karaf/webconsole/gogo/GogoPlugin.java b/karaf/webconsole/gogo/src/main/java/org/apache/felix/karaf/webconsole/gogo/GogoPlugin.java
index cadca38..20ffa9f 100644
--- a/karaf/webconsole/gogo/src/main/java/org/apache/felix/karaf/webconsole/gogo/GogoPlugin.java
+++ b/karaf/webconsole/gogo/src/main/java/org/apache/felix/karaf/webconsole/gogo/GogoPlugin.java
@@ -31,6 +31,7 @@
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
import java.io.InputStream;
+import java.util.concurrent.Callable;
import java.util.zip.GZIPOutputStream;
import java.util.List;
import java.net.URL;
@@ -198,13 +199,20 @@
out = new PipedInputStream();
PrintStream pipedOut = new PrintStream(new PipedOutputStream(out), true);
+ final Callable<Boolean> printStackTraces = new Callable<Boolean>() {
+ public Boolean call() {
+ return Boolean.valueOf(bundleContext.getProperty(Console.PRINT_STACK_TRACES));
+ }
+ };
+
console = new Console(commandProcessor,
new PipedInputStream(in),
pipedOut,
pipedOut,
new WebTerminal(TERM_WIDTH, TERM_HEIGHT),
new AggregateCompleter(completers),
- null);
+ null,
+ printStackTraces);
CommandSession session = console.getSession();
session.put("APPLICATION", System.getProperty("karaf.name", "root"));
session.put("USER", "karaf");