comparison src/luan/modules/http/LuanHandler.java @ 1256:c147e2e877e3

allow subclassing of HttpServicer
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 19 Sep 2018 20:15:16 -0600
parents 0b75337bb91a
children e38f5869e9df
comparison
equal deleted inserted replaced
1255:4f571a974132 1256:c147e2e877e3
12 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
13 import luan.webserver.Request; 13 import luan.webserver.Request;
14 import luan.webserver.Response; 14 import luan.webserver.Response;
15 import luan.webserver.Server; 15 import luan.webserver.Server;
16 import luan.webserver.Handler; 16 import luan.webserver.Handler;
17 import luan.webserver.Status;
18 import luan.webserver.ResponseOutputStream; 17 import luan.webserver.ResponseOutputStream;
19 import luan.Luan; 18 import luan.Luan;
20 import luan.LuanState; 19 import luan.LuanState;
21 import luan.LuanTable; 20 import luan.LuanTable;
22 import luan.LuanFunction; 21 import luan.LuanFunction;
27 26
28 27
29 public class LuanHandler implements Handler, Closeable { 28 public class LuanHandler implements Handler, Closeable {
30 private final LuanState luanInit; 29 private final LuanState luanInit;
31 private final Logger logger; 30 private final Logger logger;
31 private final HttpServicer httpServicer;
32 private final ReadWriteLock lock = new ReentrantReadWriteLock(); 32 private final ReadWriteLock lock = new ReentrantReadWriteLock();
33 private LuanState luan; 33 private LuanState luan;
34 34
35 private static final Method resetLuanMethod; 35 private static final Method resetLuanMethod;
36 static { 36 static {
39 } catch(NoSuchMethodException e) { 39 } catch(NoSuchMethodException e) {
40 throw new RuntimeException(e); 40 throw new RuntimeException(e);
41 } 41 }
42 } 42 }
43 43
44 public LuanHandler(LuanState luanInit,String loggerRoot) { 44 public LuanHandler(LuanState luanInit,String loggerRoot,HttpServicer httpServicer) {
45 this.luanInit = luanInit; 45 this.luanInit = luanInit;
46 if( loggerRoot==null ) 46 if( loggerRoot==null )
47 loggerRoot = ""; 47 loggerRoot = "";
48 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName()); 48 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
49 try { 49 try {
50 LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan"); 50 LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan");
51 Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) ); 51 Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
52 } catch(LuanException e) { 52 } catch(LuanException e) {
53 throw new RuntimeException(e); 53 throw new RuntimeException(e);
54 } 54 }
55 this.httpServicer = httpServicer!=null ? httpServicer : new HttpServicer();
55 setLuan(); 56 setLuan();
56 luanInit.onClose(this); 57 luanInit.onClose(this);
57 } 58 }
58 59
59 private void setLuan() { 60 private void setLuan() {
78 Thread thread = Thread.currentThread(); 79 Thread thread = Thread.currentThread();
79 String oldName = thread.getName(); 80 String oldName = thread.getName();
80 thread.setName(request.headers.get("host")+request.path); 81 thread.setName(request.headers.get("host")+request.path);
81 lock.readLock().lock(); 82 lock.readLock().lock();
82 try { 83 try {
83 Response response = HttpServicer.service(luan,request,modName); 84 return httpServicer.service(luan,request,modName);
84 return response;
85 } catch(LuanException e) {
86 //e.printStackTrace();
87 String err = e.getLuanStackTraceString();
88 logger.error(err+"\n"+request.rawHead.trim()+"\n");
89 String msg = "Internel Server Error\n\n" + err;
90 return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg );
91 } finally { 85 } finally {
92 lock.readLock().unlock(); 86 lock.readLock().unlock();
93 thread.setName(oldName); 87 thread.setName(oldName);
94 } 88 }
95 } 89 }