Mercurial Hosting > luan
diff src/luan/tools/WebServlet.java @ 76:97b03fc807ad
add HttpLib and WebServlet
git-svn-id: https://luan-java.googlecode.com/svn/trunk@77 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 15 Feb 2013 04:52:16 +0000 |
parents | |
children | 4bf3d0c0b6b9 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/tools/WebServlet.java Fri Feb 15 04:52:16 2013 +0000 @@ -0,0 +1,66 @@ +package luan.tools; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import luan.LuanState; +import luan.LuanException; +import luan.LuanRuntimeException; +import luan.LuanFunction; +import luan.LuanElement; +import luan.interp.LuanCompiler; +import luan.lib.HttpLib; +import luan.lib.BasicLib; +import luan.lib.PackageLib; +import luan.lib.MathLib; +import luan.lib.StringLib; +import luan.lib.TableLib; +import luan.lib.HtmlLib; + + +public class WebServlet extends HttpServlet { + + public static final String HTTP_SERVER = "http_server"; + + protected void loadLibs(LuanState luan) throws LuanException { + luan.load(BasicLib.LOADER,BasicLib.NAME); + luan.load(PackageLib.LOADER,PackageLib.NAME); + luan.load(MathLib.LOADER,MathLib.NAME); + luan.load(StringLib.LOADER,StringLib.NAME); + luan.load(TableLib.LOADER,TableLib.NAME); + luan.load(HtmlLib.LOADER,HtmlLib.NAME); + } + + protected void loadLuan(LuanState luan) throws LuanException { + PackageLib.require(luan,HTTP_SERVER); + Object fn = luan.global.get(HttpLib.FN_NAME); + if( !(fn instanceof LuanFunction) ) + throw new LuanException( luan, LuanElement.JAVA, "function '"+HttpLib.FN_NAME+"' not defined" ); + } + + protected LuanState newLuanState() throws LuanException { + LuanState luan = LuanCompiler.newLuanState(); + loadLibs(luan); + loadLuan(luan); + return luan; + } + + protected LuanState getLuanState() throws LuanException { + return newLuanState(); + } + + protected void service(HttpServletRequest request,HttpServletResponse response) + throws ServletException, IOException + { + try { + LuanState luan = getLuanState(); + HttpLib.service(luan,request,response); + } catch(LuanException e) { + throw new LuanRuntimeException(e); + } + } + +}