import bsh.Interpreter;
public class ShellPage extends SimpleWriter {
+ private Console cons = new Console();
+ private Interpreter ip = new Interpreter(cons);
+
private static class Console implements bsh.ConsoleInterface {
ByteArrayOutputStream obuf = new ByteArrayOutputStream();
ByteArrayOutputStream ebuf = new ByteArrayOutputStream();
public Reader getIn() {
return(in);
}
+
+ public void reset() {
+ obuf.reset();
+ ebuf.reset();
+ }
}
public void respond(Request req, PrintWriter out) {
out.println("<body>");
out.println("<h1>Shell</h1>");
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("<pre>");
- out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
- out.println("</pre>");
- } catch(bsh.EvalError exc) {
- out.println("<h2>Evaluation error</h2>");
- out.println("<pre>");
- out.print(exc.toString());
- out.println("</pre>");
- if(exc instanceof bsh.TargetError) {
- bsh.TargetError te = (bsh.TargetError)exc;
- out.println("<h3>Target error</h3>");
+ String eo, ee;
+ synchronized(cons) {
+ cons.reset();
+ Object resp;
+ try {
+ ip.set("req", req);
+ resp = ip.eval(cmd);
+ out.println("<pre>");
+ out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
+ out.println("</pre>");
+ } catch(bsh.EvalError exc) {
+ out.println("<h2>Evaluation error</h2>");
out.println("<pre>");
- te.getTarget().printStackTrace(out);
+ out.print(exc.toString());
out.println("</pre>");
+ if(exc instanceof bsh.TargetError) {
+ bsh.TargetError te = (bsh.TargetError)exc;
+ out.println("<h3>Target error</h3>");
+ out.println("<pre>");
+ te.getTarget().printStackTrace(out);
+ out.println("</pre>");
+ }
}
+ 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("<h2>Output</h2>");
out.println("<pre>");