Mercurial Hosting > luan
comparison http/src/luan/modules/http/HttpServicer.java @ 626:53a50c70c5e2
add Luan.assert_function() and Http.run_later()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 17 Jan 2016 06:19:46 -0700 |
parents | 60c549d43988 |
children | 859c0dedc8b6 |
comparison
equal
deleted
inserted
replaced
625:a3c1e11fb6aa | 626:53a50c70c5e2 |
---|---|
9 import java.util.List; | 9 import java.util.List; |
10 import java.util.ArrayList; | 10 import java.util.ArrayList; |
11 import java.util.Arrays; | 11 import java.util.Arrays; |
12 import java.util.Iterator; | 12 import java.util.Iterator; |
13 import java.util.Enumeration; | 13 import java.util.Enumeration; |
14 import java.util.concurrent.Executor; | |
15 import java.util.concurrent.Executors; | |
14 import javax.servlet.ServletOutputStream; | 16 import javax.servlet.ServletOutputStream; |
15 import javax.servlet.ServletException; | 17 import javax.servlet.ServletException; |
16 import javax.servlet.http.Cookie; | 18 import javax.servlet.http.Cookie; |
17 import javax.servlet.http.HttpServletRequest; | 19 import javax.servlet.http.HttpServletRequest; |
18 import javax.servlet.http.HttpServletResponse; | 20 import javax.servlet.http.HttpServletResponse; |
155 LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response"); | 157 LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response"); |
156 newResponseFn.call( luan, new Object[]{responseTbl} ); | 158 newResponseFn.call( luan, new Object[]{responseTbl} ); |
157 module.rawPut("response",responseTbl); | 159 module.rawPut("response",responseTbl); |
158 | 160 |
159 fn.call(luan); | 161 fn.call(luan); |
162 handle_run_later(luan); | |
160 return true; | 163 return true; |
161 } | 164 } |
162 | 165 |
163 public static void setResponse(LuanTable responseTbl,HttpServletResponse response) throws LuanException { | 166 public static void setResponse(LuanTable responseTbl,HttpServletResponse response) throws LuanException { |
164 int status = Luan.asInteger(responseTbl.rawGet("status")); | 167 int status = Luan.asInteger(responseTbl.rawGet("status")); |
254 delCookie.setDomain(domain); | 257 delCookie.setDomain(domain); |
255 response.addCookie(delCookie); | 258 response.addCookie(delCookie); |
256 } | 259 } |
257 } | 260 } |
258 | 261 |
262 | |
263 | |
264 private static String RUN_LATER_KEY = "Http.run_later"; | |
265 private static final Executor exec = Executors.newSingleThreadExecutor(); | |
266 | |
267 public static void run_later(final LuanState luan,final LuanFunction fn,final Object... args) { | |
268 List list = (List)luan.registry().get(RUN_LATER_KEY); | |
269 if( list == null ) { | |
270 list = new ArrayList(); | |
271 luan.registry().put(RUN_LATER_KEY,list); | |
272 } | |
273 list.add( | |
274 new Runnable(){public void run() { | |
275 try { | |
276 fn.call(luan,args); | |
277 } catch(LuanException e) { | |
278 e.printStackTrace(); | |
279 } | |
280 }} | |
281 ); | |
282 } | |
283 | |
284 private static void handle_run_later(LuanState luan) { | |
285 List list = (List)luan.registry().get(RUN_LATER_KEY); | |
286 if( list==null ) | |
287 return; | |
288 for( Object obj : list ) { | |
289 exec.execute((Runnable)obj); | |
290 } | |
291 } | |
259 } | 292 } |