Mercurial Hosting > luan
comparison src/luan/tools/WebShell.java @ 49:8ede219cd111
add WebShell
git-svn-id: https://luan-java.googlecode.com/svn/trunk@50 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 28 Dec 2012 19:35:04 +0000 |
parents | |
children | 272b6f1d7dac |
comparison
equal
deleted
inserted
replaced
48:64ecb7a3aad7 | 49:8ede219cd111 |
---|---|
1 package luan.tools; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.PrintStream; | |
5 import java.io.Writer; | |
6 import java.io.PrintWriter; | |
7 import java.io.ByteArrayOutputStream; | |
8 import java.io.OutputStreamWriter; | |
9 import javax.servlet.ServletException; | |
10 import javax.servlet.http.HttpServlet; | |
11 import javax.servlet.http.HttpServletRequest; | |
12 import javax.servlet.http.HttpServletResponse; | |
13 import javax.servlet.http.HttpSession; | |
14 import org.slf4j.Logger; | |
15 import org.slf4j.LoggerFactory; | |
16 import luan.LuanFunction; | |
17 import luan.LuanState; | |
18 import luan.LuanException; | |
19 import luan.interp.LuanCompiler; | |
20 import luan.lib.BasicLib; | |
21 import luan.lib.JavaLib; | |
22 import luan.lib.StringLib; | |
23 import luan.lib.TableLib; | |
24 import luan.lib.HtmlLib; | |
25 | |
26 | |
27 public class WebShell extends HttpServlet { | |
28 private static final Logger logger = LoggerFactory.getLogger(WebShell.class); | |
29 | |
30 protected LuanState newLuanState() { | |
31 LuanState luan = LuanCompiler.newLuanState(); | |
32 BasicLib.register(luan); | |
33 JavaLib.register(luan); | |
34 StringLib.register(luan); | |
35 TableLib.register(luan); | |
36 HtmlLib.register(luan); | |
37 return luan; | |
38 } | |
39 | |
40 protected Object[] eval(LuanState luan,String cmd) throws LuanException { | |
41 LuanFunction fn = BasicLib.load(luan,cmd,"WebShell"); | |
42 return luan.call(fn,null,null); | |
43 } | |
44 | |
45 protected void service(HttpServletRequest request,HttpServletResponse response) | |
46 throws ServletException, IOException | |
47 { | |
48 PrintWriter out = response.getWriter(); | |
49 HttpSession session = request.getSession(); | |
50 | |
51 ByteArrayOutputStream history = (ByteArrayOutputStream)session.getValue("history"); | |
52 if( history==null ) { | |
53 history = new ByteArrayOutputStream(); | |
54 session.putValue("history",history); | |
55 } | |
56 | |
57 if( request.getParameter("clear") != null ) { | |
58 history.reset(); | |
59 } else { | |
60 String cmd = request.getParameter("cmd"); | |
61 if( cmd != null ) { | |
62 Writer writer = new OutputStreamWriter(history); | |
63 writer.write( "% " + HtmlLib.encode(cmd) + "\r\n" ); | |
64 try { | |
65 LuanState luan = (LuanState)session.getValue("luan"); | |
66 if( luan==null ) { | |
67 luan = newLuanState(); | |
68 session.putValue("luan",luan); | |
69 } | |
70 luan.out = new PrintStream(history); | |
71 luan.global().put("request",request); | |
72 luan.global().put("response",response); | |
73 Object[] result = eval(luan,cmd); | |
74 if( result.length > 0 ) { | |
75 for( int i=0; i<result.length; i++ ) { | |
76 if( i > 0 ) | |
77 writer.write(" "); | |
78 writer.write(HtmlLib.encode(luan.toString(null,result[i]))); | |
79 } | |
80 writer.write("\r\n"); | |
81 } | |
82 } catch(LuanException e) { | |
83 logger.info("",e); | |
84 writer.write( HtmlLib.encode(e.toString()) ); | |
85 } | |
86 writer.flush(); | |
87 } | |
88 } | |
89 | |
90 out.println( "<html>" ); | |
91 out.println( "<title>Luan Shell</title>" ); | |
92 out.println( "<body>" ); | |
93 out.println( "<p>This is a command shell. Enter commands below." ); | |
94 out.println( "<pre>" + history + "</pre>" ); | |
95 out.println( "<form name='theForm' method='post'>" ); | |
96 out.println( "% <input name='cmd' size=60>" ); | |
97 out.println( "<input type=submit value=run>" ); | |
98 out.println( "<input type=submit name=clear value=clear>" ); | |
99 out.println( "</form>" ); | |
100 | |
101 out.println( "<script>document.theForm.cmd.focus();</script>" ); | |
102 | |
103 out.println( "<p>" ); | |
104 out.println( "</body>" ); | |
105 out.println( "</html>" ); | |
106 } | |
107 } |