comparison 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
comparison
equal deleted inserted replaced
774:3e30cf310e56 775:1a68fc55a80c
1 package luan.modules.http;
2
3 import java.io.IOException;
4 import javax.servlet.ServletException;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.eclipse.jetty.server.Request;
11 import org.eclipse.jetty.server.handler.AbstractHandler;
12 import luan.LuanState;
13 import luan.LuanTable;
14 import luan.LuanFunction;
15 import luan.DeepCloner;
16 import luan.LuanException;
17 import luan.modules.PackageLuan;
18
19
20 public class LuanHandler extends AbstractHandler {
21 private final LuanState luan;
22 private final Logger logger;
23 private String welcomeFile = "index.html";
24
25 public LuanHandler(LuanState luan,String loggerRoot) {
26 this.luan = luan;
27 if( loggerRoot==null )
28 loggerRoot = "";
29 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
30 }
31
32 @Override public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
33 throws IOException
34 {
35 if( target.endsWith("/") )
36 target += welcomeFile;
37 try {
38 if( !HttpServicer.service(luan,request,response,"site:"+target+".luan") )
39 return;
40 } catch(LuanException e) {
41 String err = e.getFullMessage();
42 logger.error(err);
43 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,err);
44 }
45 baseRequest.setHandled(true);
46 }
47
48 public void setWelcomeFile(String welcomeFile) {
49 this.welcomeFile = welcomeFile;
50 }
51
52 @Override protected void doStop() throws Exception {
53 synchronized(luan) {
54 luan.close();
55 }
56 //System.out.println("qqqqqqqqqqqqqqqqqqqq doStop "+this);
57 super.doStop();
58 }
59 /*
60 @Override public void destroy() {
61 System.out.println("qqqqqqqqqqqqqqqqqqqq destroy "+this);
62 super.destroy();
63 }
64 */
65
66 public Object call_rpc(String fnName,Object... args) throws LuanException {
67 return callRpc(luan,fnName,args);
68 }
69
70 public static Object callRpc(LuanState luan,String fnName,Object... args) throws LuanException {
71 synchronized(luan) {
72 DeepCloner cloner = new DeepCloner();
73 luan = (LuanState)cloner.deepClone(luan);
74 }
75 LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan");
76 LuanTable fns = (LuanTable)rpc.get(luan,"functions");
77 LuanFunction fn = (LuanFunction)fns.get(luan,fnName);
78 if( fn == null )
79 throw new LuanException( "function not found: " + fnName );
80 return fn.call(luan,args);
81 }
82
83 }