From eb81f42eef6ff400b79410db867f3c2bff687e11 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 26 Oct 2009 01:56:48 +0100 Subject: [PATCH] Added a sample bsh service. --- samples/bsh/.gitignore | 3 + samples/bsh/build.xml | 43 ++++++++++++ samples/bsh/etc/jsvc.properties | 4 ++ samples/bsh/lib/README | 1 + samples/bsh/src/dolda/bsvc/Main.java | 13 ++++ samples/bsh/src/dolda/bsvc/ShellPage.java | 112 ++++++++++++++++++++++++++++++ samples/bsh/static/base.css | 7 ++ 7 files changed, 183 insertions(+) create mode 100644 samples/bsh/.gitignore create mode 100644 samples/bsh/build.xml create mode 100644 samples/bsh/etc/jsvc.properties create mode 100644 samples/bsh/lib/README create mode 100644 samples/bsh/src/dolda/bsvc/Main.java create mode 100644 samples/bsh/src/dolda/bsvc/ShellPage.java create mode 100644 samples/bsh/static/base.css diff --git a/samples/bsh/.gitignore b/samples/bsh/.gitignore new file mode 100644 index 0000000..ac55ebd --- /dev/null +++ b/samples/bsh/.gitignore @@ -0,0 +1,3 @@ +/build +/lib/jsvc.jar +/lib/bsh.jar diff --git a/samples/bsh/build.xml b/samples/bsh/build.xml new file mode 100644 index 0000000..ad2e8c3 --- /dev/null +++ b/samples/bsh/build.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/bsh/etc/jsvc.properties b/samples/bsh/etc/jsvc.properties new file mode 100644 index 0000000..4f46acf --- /dev/null +++ b/samples/bsh/etc/jsvc.properties @@ -0,0 +1,4 @@ +jsvc.j2ee.appname = Online BSH +jsvc.bootstrap = dolda.bsvc.Main +jsvc.timelimit = 30000 +jsvc.forcelimit = true diff --git a/samples/bsh/lib/README b/samples/bsh/lib/README new file mode 100644 index 0000000..6a2df68 --- /dev/null +++ b/samples/bsh/lib/README @@ -0,0 +1 @@ +You have to add a compiled version of jsvc.jar and bsh.jar here manually. diff --git a/samples/bsh/src/dolda/bsvc/Main.java b/samples/bsh/src/dolda/bsvc/Main.java new file mode 100644 index 0000000..a1687af --- /dev/null +++ b/samples/bsh/src/dolda/bsvc/Main.java @@ -0,0 +1,13 @@ +package dolda.bsvc; + +import dolda.jsvc.*; +import dolda.jsvc.util.*; + +public class Main { + public static Responder responder() { + Multiplexer root = new Multiplexer(); + root.file("sh", new ShellPage()); + root.file("css", new StaticContent(Main.class, "static/base.css", false, "text/css")); + return(Misc.stdroot(root)); + } +} diff --git a/samples/bsh/src/dolda/bsvc/ShellPage.java b/samples/bsh/src/dolda/bsvc/ShellPage.java new file mode 100644 index 0000000..29d0d3d --- /dev/null +++ b/samples/bsh/src/dolda/bsvc/ShellPage.java @@ -0,0 +1,112 @@ +package dolda.bsvc; + +import dolda.jsvc.*; +import dolda.jsvc.util.*; +import java.io.*; +import bsh.Interpreter; + +public class ShellPage extends SimpleWriter { + private static class Console implements bsh.ConsoleInterface { + ByteArrayOutputStream obuf = new ByteArrayOutputStream(); + ByteArrayOutputStream ebuf = new ByteArrayOutputStream(); + Reader in = new StringReader(""); + PrintStream out; + PrintStream err; + { + try { + out = new PrintStream(obuf, false, "UTF-8"); + err = new PrintStream(ebuf, false, "UTF-8"); + } catch(UnsupportedEncodingException e) { + throw(new Error(e)); + } + } + + public void error(Object msg) { + getErr().println(msg); + } + + public void print(Object o) { + getOut().print(o); + } + + public void println(Object o) { + getOut().println(o); + } + + public PrintStream getOut() { + return(out); + } + + public PrintStream getErr() { + return(err); + } + + public Reader getIn() { + return(in); + } + } + + public void respond(Request req, PrintWriter out) { + MultiMap params = req.params(); + String cmd = params.get("cmd"); + + out.println(""); + out.println(""); + out.println(""); + out.println(""); + out.println("Shell"); + out.println(""); + out.println(""); + out.println(""); + out.println("

Shell

"); + if((req.method() == "POST") && (cmd != null)) { + Console cons = new Console(); + Interpreter ip = new Interpreter(cons); + 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

"); + 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("
");
+		    te.getTarget().printStackTrace(out);
+		    out.println("
"); + } + } + 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("
");
+		out.println(Misc.htmlq(eo));
+		out.println("
"); + } + if(ee.length() > 0) { + out.println("

Errors

"); + out.println("
");
+		out.println(Misc.htmlq(ee));
+		out.println("
"); + } + } + out.println("
"); + out.println(""); + out.println(""); + out.println(""); + out.println("
"); + out.println(""); + out.println(""); + } +} diff --git a/samples/bsh/static/base.css b/samples/bsh/static/base.css new file mode 100644 index 0000000..060a727 --- /dev/null +++ b/samples/bsh/static/base.css @@ -0,0 +1,7 @@ +body { + font-family: sans-serif; +} + +img { + border-width: 0px; +} -- 2.11.0