annotate src/luan/modules/http/LuanHandler.java @ 1264:d41997776788

fix onClose issues
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 25 Sep 2018 17:03:57 -0600
parents 382c444a6c77
children 3f4644246e39
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1171
794ddcfbee20 remove http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1170
diff changeset
1 package luan.modules.http;
175
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
1231
0b75337bb91a better closing
Franklin Schmidt <fschmidt@gmail.com>
parents: 1223
diff changeset
3 import java.io.Closeable;
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
4 import java.io.Writer;
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
5 import java.io.PrintWriter;
175
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
6 import java.io.IOException;
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
7 import java.lang.ref.Reference;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
8 import java.lang.ref.WeakReference;
786
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
9 import java.lang.reflect.Method;
1004
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
10 import java.net.BindException;
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
11 import java.util.List;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
12 import java.util.ArrayList;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
13 import java.util.concurrent.locks.ReadWriteLock;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
14 import java.util.concurrent.locks.ReentrantReadWriteLock;
280
2164b4479661 log web exceptions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
15 import org.slf4j.Logger;
2164b4479661 log web exceptions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
16 import org.slf4j.LoggerFactory;
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
17 import luan.webserver.Request;
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
18 import luan.webserver.Response;
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
19 import luan.webserver.Server;
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
20 import luan.webserver.Handler;
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
21 import luan.webserver.ResponseOutputStream;
786
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
22 import luan.Luan;
175
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
23 import luan.LuanState;
743
2c41f2aec92f improve Rpc and implement rpc call for local webserver
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
24 import luan.LuanTable;
2c41f2aec92f improve Rpc and implement rpc call for local webserver
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
25 import luan.LuanFunction;
786
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
26 import luan.LuanJavaFunction;
781
fbbdd369a13a rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents: 777
diff changeset
27 import luan.LuanCloner;
175
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
28 import luan.LuanException;
743
2c41f2aec92f improve Rpc and implement rpc call for local webserver
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
29 import luan.modules.PackageLuan;
1263
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
30 import luan.modules.BasicLuan;
175
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
31
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
32
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
33 public final class LuanHandler implements Handler {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
34
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
35 private class Instance implements LuanState.OnClose {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
36 private final List<Reference<Closeable>> onClose;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
37 private final LuanState luan;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
38 private final ReadWriteLock lock = new ReentrantReadWriteLock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
39
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
40 Instance() {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
41 onClose = new ArrayList<Reference<Closeable>>();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
42 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
43 luan = (LuanState)cloner.clone(luanInit);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
44 luan.onClose = this;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
45 try {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
46 PackageLuan.load(luan,"site:/init.luan");
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
47 } catch(LuanException e) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
48 String err = e.getLuanStackTraceString();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
49 logger.error(err);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
50 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
51 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
52
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
53 Response handle(Request request,String modName) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
54 Thread thread = Thread.currentThread();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
55 String oldName = thread.getName();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
56 thread.setName(request.headers.get("host")+request.path);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
57 lock.readLock().lock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
58 try {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
59 return HttpServicer.service(luan,request,modName);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
60 } finally {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
61 lock.readLock().unlock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
62 thread.setName(oldName);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
63 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
64 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
65
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
66 Object call_rpc(String fnName,Object... args) throws LuanException {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
67 lock.readLock().lock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
68 try {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
69 LuanFunction fn;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
70 LuanState luan = this.luan;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
71 synchronized(luan) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
72 PackageLuan.enableLoad(luan,"luan:Rpc.luan");
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
73 LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan");
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
74 LuanTable fns = (LuanTable)rpc.get(luan,"functions");
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
75 fn = (LuanFunction)fns.get(luan,fnName);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
76 if( fn == null )
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
77 throw new LuanException( "function not found: " + fnName );
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
78 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
79 luan = (LuanState)cloner.clone(luan);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
80 fn = (LuanFunction)cloner.get(fn);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
81 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
82 return fn.call(luan,args);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
83 } finally {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
84 lock.readLock().unlock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
85 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
86 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
87
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
88 public Object runLuan(String sourceText,String sourceName) throws LuanException {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
89 lock.readLock().lock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
90 try {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
91 LuanFunction fn = Luan.load(sourceText,sourceName);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
92 synchronized(luan) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
93 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
94 LuanState luan = (LuanState)cloner.clone(this.luan);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
95 return fn.call(luan);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
96 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
97 } finally {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
98 lock.readLock().unlock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
99 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
100 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
101
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
102 public void onClose(Closeable c) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
103 synchronized(onClose) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
104 onClose.add(new WeakReference<Closeable>(c));
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
105 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
106 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
107
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
108 public void close() {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
109 synchronized(onClose) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
110 for( Reference<Closeable> ref : onClose ) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
111 Closeable c = ref.get();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
112 if( c != null ) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
113 try {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
114 c.close();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
115 } catch(IOException e) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
116 logger.error(c.toString(),e);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
117 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
118 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
119 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
120 onClose.clear();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
121 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
122 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
123
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
124 Instance cloneInstance() {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
125 synchronized(luan) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
126 return new Instance(this);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
127 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
128 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
129
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
130 private Instance(Instance instance) {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
131 onClose = instance.onClose;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
132 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
133 luan = (LuanState)cloner.clone(instance.luan);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
134 luan.onClose = this;
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
135 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
136 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
137
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
138 private final LuanState luanInit;
627
a98812908fbc add loggerRoot param to LuanHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 531
diff changeset
139 private final Logger logger;
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
140 private volatile Instance instance;
786
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
141
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
142 private static final Method resetLuanMethod;
1263
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
143 private static final Method evalInRootMethod;
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
144 static {
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
145 try {
1263
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
146 resetLuanMethod = LuanHandler.class.getMethod( "reset_luan" );
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
147 evalInRootMethod = LuanHandler.class.getMethod( "eval_in_root", String.class );
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
148 } catch(NoSuchMethodException e) {
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
149 throw new RuntimeException(e);
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
150 }
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
151 }
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
152
1263
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
153 public LuanHandler(LuanState luanInit,String loggerRoot) {
1231
0b75337bb91a better closing
Franklin Schmidt <fschmidt@gmail.com>
parents: 1223
diff changeset
154 this.luanInit = luanInit;
627
a98812908fbc add loggerRoot param to LuanHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 531
diff changeset
155 if( loggerRoot==null )
a98812908fbc add loggerRoot param to LuanHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 531
diff changeset
156 loggerRoot = "";
a98812908fbc add loggerRoot param to LuanHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 531
diff changeset
157 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
158 try {
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
159 LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan");
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
160 Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
1263
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
161 Http.rawPut( "eval_in_root", new LuanJavaFunction(evalInRootMethod,this) );
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
162 } catch(LuanException e) {
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
163 throw new RuntimeException(e);
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
164 }
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
165 instance = new Instance();
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
166 }
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
167 /*
1257
e38f5869e9df don't reset in send_redirect and other improvements
Franklin Schmidt <fschmidt@gmail.com>
parents: 1256
diff changeset
168 public LuanState getLuan() {
e38f5869e9df don't reset in send_redirect and other improvements
Franklin Schmidt <fschmidt@gmail.com>
parents: 1256
diff changeset
169 return luan;
e38f5869e9df don't reset in send_redirect and other improvements
Franklin Schmidt <fschmidt@gmail.com>
parents: 1256
diff changeset
170 }
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
171 */
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1136
diff changeset
172 @Override public Response handle(Request request) {
1167
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
173 if( request.path.endsWith("/") )
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
174 return null;
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
175 String modName = "site:" + request.path +".luan";
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
176 return handle(request,modName);
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
177 }
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
178
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
179 Response handle(Request request,String modName) {
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
180 return instance.handle(request,modName);
175
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
181 }
786
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
182
1231
0b75337bb91a better closing
Franklin Schmidt <fschmidt@gmail.com>
parents: 1223
diff changeset
183 public void close() {
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
184 instance.close();
517
8dcf9e12446b add Luan.on_luan_close()
Franklin Schmidt <fschmidt@gmail.com>
parents: 494
diff changeset
185 }
1185
94cf2576a922 implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents: 1171
diff changeset
186
743
2c41f2aec92f improve Rpc and implement rpc call for local webserver
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
187 public Object call_rpc(String fnName,Object... args) throws LuanException {
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
188 return instance.call_rpc(fnName,args);
743
2c41f2aec92f improve Rpc and implement rpc call for local webserver
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
189 }
2c41f2aec92f improve Rpc and implement rpc call for local webserver
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
190
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
191 public void reset_luan() {
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
192 new Thread() {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
193 public void run() {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
194 instance.lock.writeLock().lock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
195 try {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
196 instance.close();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
197 instance = new Instance();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
198 } finally {
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
199 instance.lock.writeLock().unlock();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
200 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
201 }
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
202 }.start();
1186
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
203 }
ef8cd42e23d5 add back reset_luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1185
diff changeset
204
786
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
205 public Object runLuan(String sourceText,String sourceName) throws LuanException {
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
206 return instance.runLuan(sourceText,sourceName);
786
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
207 }
fe63c508a177 add Http.reset_luan()
Franklin Schmidt <fschmidt@gmail.com>
parents: 785
diff changeset
208
1263
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
209 public void eval_in_root(String text) throws LuanException {
1264
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
210 Instance newInstance = this.instance.cloneInstance();
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
211 BasicLuan.load(text,"<eval_in_root>",null).call(newInstance.luan);
d41997776788 fix onClose issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
212 this.instance = newInstance;
1263
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
213 }
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
214
1004
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
215 public static void start(Server server) throws Exception {
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
216 try {
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
217 server.start();
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
218 } catch(BindException e) {
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
219 throw new LuanException(e.toString());
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
220 }
3fa54d9d19cd better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1000
diff changeset
221 }
1077
ca968ef1747a better handling of BindException
Franklin Schmidt <fschmidt@gmail.com>
parents: 800
diff changeset
222
175
bdbd4740121f finish web server
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
223 }