Mercurial Hosting > luan
diff src/luan/modules/http/LuanHandler.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | http/src/luan/modules/http/LuanHandler.java@3f461f85243d |
children | 1460d297e960 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/LuanHandler.java Fri Aug 26 14:36:40 2016 -0600 @@ -0,0 +1,83 @@ +package luan.modules.http; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; +import luan.LuanState; +import luan.LuanTable; +import luan.LuanFunction; +import luan.DeepCloner; +import luan.LuanException; +import luan.modules.PackageLuan; + + +public class LuanHandler extends AbstractHandler { + private final LuanState luan; + private final Logger logger; + private String welcomeFile = "index.html"; + + public LuanHandler(LuanState luan,String loggerRoot) { + this.luan = luan; + if( loggerRoot==null ) + loggerRoot = ""; + logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName()); + } + + @Override public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) + throws IOException + { + if( target.endsWith("/") ) + target += welcomeFile; + try { + if( !HttpServicer.service(luan,request,response,"site:"+target+".luan") ) + return; + } catch(LuanException e) { + String err = e.getFullMessage(); + logger.error(err); + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,err); + } + baseRequest.setHandled(true); + } + + public void setWelcomeFile(String welcomeFile) { + this.welcomeFile = welcomeFile; + } + + @Override protected void doStop() throws Exception { + synchronized(luan) { + luan.close(); + } +//System.out.println("qqqqqqqqqqqqqqqqqqqq doStop "+this); + super.doStop(); + } +/* + @Override public void destroy() { +System.out.println("qqqqqqqqqqqqqqqqqqqq destroy "+this); + super.destroy(); + } +*/ + + public Object call_rpc(String fnName,Object... args) throws LuanException { + return callRpc(luan,fnName,args); + } + + public static Object callRpc(LuanState luan,String fnName,Object... args) throws LuanException { + synchronized(luan) { + DeepCloner cloner = new DeepCloner(); + luan = (LuanState)cloner.deepClone(luan); + } + LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan"); + LuanTable fns = (LuanTable)rpc.get(luan,"functions"); + LuanFunction fn = (LuanFunction)fns.get(luan,fnName); + if( fn == null ) + throw new LuanException( "function not found: " + fnName ); + return fn.call(luan,args); + } + +}