From ba5757cbfe4630e795f85fb7f4dc5ff5d684e8c7 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 28 Oct 2009 00:15:55 +0100 Subject: [PATCH] Fixed the bsvc console implementation. --- samples/bsh/src/dolda/bsvc/ShellPage.java | 90 +++++++++---------------------- 1 file changed, 26 insertions(+), 64 deletions(-) diff --git a/samples/bsh/src/dolda/bsvc/ShellPage.java b/samples/bsh/src/dolda/bsvc/ShellPage.java index e7412ad..c26998e 100644 --- a/samples/bsh/src/dolda/bsvc/ShellPage.java +++ b/samples/bsh/src/dolda/bsvc/ShellPage.java @@ -6,47 +6,9 @@ import java.io.*; import bsh.Interpreter; public class ShellPage extends SimpleWriter { - private RConsole cons = new RConsole(); + private Console cons = new Console(); private Interpreter ip = new Interpreter(cons); - private static class RConsole implements bsh.ConsoleInterface { - public Console back; - Reader in = new StringReader(""); - - public void error(Object msg) { - if(back != null) - back.error(msg); - } - - public void print(Object msg) { - if(back != null) - back.print(msg); - } - - public void println(Object msg) { - if(back != null) - back.println(msg); - } - - public PrintStream getOut() { - if(back == null) - return(null); - return(back.getOut()); - } - - public PrintStream getErr() { - if(back == null) - return(null); - return(back.getErr()); - } - - public Reader getIn() { - if(back == null) - return(null); - return(in); - } - } - private static class Console implements bsh.ConsoleInterface { ByteArrayOutputStream obuf = new ByteArrayOutputStream(); ByteArrayOutputStream ebuf = new ByteArrayOutputStream(); @@ -85,6 +47,11 @@ public class ShellPage extends SimpleWriter { public Reader getIn() { return(in); } + + public void reset() { + obuf.reset(); + ebuf.reset(); + } } public void respond(Request req, PrintWriter out) { @@ -101,37 +68,32 @@ public class ShellPage extends SimpleWriter { out.println(""); out.println("

Shell

"); if((req.method() == "POST") && (cmd != null)) { - Console cons = new Console(); - synchronized(ip) { - this.cons.back = cons; + String eo, ee; + synchronized(cons) { + cons.reset(); + Object resp; try { - Object resp; - try { - ip.set("req", req); - resp = ip.eval(cmd); - out.println("
");
-			out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
-			out.println("
"); - } catch(bsh.EvalError exc) { - out.println("

Evaluation error

"); + ip.set("req", req); + resp = ip.eval(cmd); + out.println("
");
+		    out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
+		    out.println("
"); + } catch(bsh.EvalError exc) { + out.println("

Evaluation error

"); + out.println("
");
+		    out.print(exc.toString());
+		    out.println("
"); + if(exc instanceof bsh.TargetError) { + bsh.TargetError te = (bsh.TargetError)exc; + out.println("

Target error

"); out.println("
");
-			out.print(exc.toString());
+			te.getTarget().printStackTrace(out);
 			out.println("
"); - if(exc instanceof bsh.TargetError) { - bsh.TargetError te = (bsh.TargetError)exc; - out.println("

Target error

"); - out.println("
");
-			    te.getTarget().printStackTrace(out);
-			    out.println("
"); - } } - } finally { - this.cons.back = null; } + eo = new String(cons.obuf.toByteArray(), Misc.utf8); + ee = new String(cons.ebuf.toByteArray(), Misc.utf8); } - String eo = new String(cons.obuf.toByteArray(), Misc.utf8); - String ee = new String(cons.ebuf.toByteArray(), Misc.utf8); - cons = null; if(eo.length() > 0) { out.println("

Output

"); out.println("
");
-- 
2.11.0