Mercurial Hosting > luan
comparison src/luan/lib/HttpLib.java @ 140:f4ce03ff6b2f
add per_session for Http
git-svn-id: https://luan-java.googlecode.com/svn/trunk@141 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 13 Jun 2014 15:04:29 +0000 |
parents | 3b384dc5ca91 |
children | 05f8c21160ef |
comparison
equal
deleted
inserted
replaced
139:3b384dc5ca91 | 140:f4ce03ff6b2f |
---|---|
9 import javax.servlet.ServletOutputStream; | 9 import javax.servlet.ServletOutputStream; |
10 import javax.servlet.http.Cookie; | 10 import javax.servlet.http.Cookie; |
11 import javax.servlet.http.HttpServletRequest; | 11 import javax.servlet.http.HttpServletRequest; |
12 import javax.servlet.http.HttpServletResponse; | 12 import javax.servlet.http.HttpServletResponse; |
13 import javax.servlet.http.HttpSession; | 13 import javax.servlet.http.HttpSession; |
14 import luan.Luan; | |
14 import luan.LuanState; | 15 import luan.LuanState; |
15 import luan.LuanFunction; | 16 import luan.LuanFunction; |
16 import luan.LuanElement; | 17 import luan.LuanElement; |
17 import luan.LuanException; | 18 import luan.LuanException; |
18 import luan.LuanTable; | 19 import luan.LuanTable; |
29 @Override public Object call(LuanState luan,Object[] args) { | 30 @Override public Object call(LuanState luan,Object[] args) { |
30 return new LuanTable(); // starts empty | 31 return new LuanTable(); // starts empty |
31 } | 32 } |
32 }; | 33 }; |
33 | 34 |
34 public static void service_per_request(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) | 35 public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) |
35 throws LuanException | 36 throws LuanException |
36 { | 37 { |
37 LuanFunction fn; | 38 LuanFunction fn; |
38 synchronized(luan) { | 39 synchronized(luan) { |
39 fn = getFn(luan,modName); | 40 Object mod = PackageLib.require(luan,modName); |
40 DeepCloner cloner = new DeepCloner(); | 41 if( !(mod instanceof LuanTable) ) |
41 luan = cloner.deepClone(luan); | 42 throw luan.exception( "module '"+modName+"' must return a table" ); |
42 fn = cloner.get(fn); | 43 LuanTable tbl = (LuanTable)mod; |
43 } | 44 if( Luan.toBoolean( tbl.get("per_session") ) ) { |
44 service(luan,request,response,fn); | 45 HttpSession session = request.getSession(); |
45 } | 46 LuanState sessionLuan = (LuanState)session.getValue("luan"); |
46 | 47 if( sessionLuan!=null ) { |
47 public static void service_global(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) | 48 luan = sessionLuan; |
48 throws LuanException | 49 } else { |
49 { | 50 DeepCloner cloner = new DeepCloner(); |
50 LuanFunction fn = getFn(luan,modName); | 51 luan = cloner.deepClone(luan); |
51 service(luan,request,response,fn); | 52 session.putValue("luan",luan); |
52 } | 53 } |
53 | 54 tbl = (LuanTable)PackageLib.require(luan,modName); |
54 public static void service_per_session(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) | 55 fn = (LuanFunction)tbl.get("page"); |
55 throws LuanException | 56 } else { |
56 { | 57 fn = (LuanFunction)tbl.get("page"); |
57 HttpSession session = request.getSession(); | 58 if( fn == null ) |
58 LuanState sessionLuan = (LuanState)session.getValue("luan"); | 59 throw luan.exception( "function 'page' is not defined" ); |
59 if( sessionLuan!=null ) { | 60 DeepCloner cloner = new DeepCloner(); |
60 luan = sessionLuan; | 61 luan = cloner.deepClone(luan); |
61 } else { | 62 fn = cloner.get(fn); |
62 DeepCloner cloner = new DeepCloner(); | 63 } |
63 luan = cloner.deepClone(luan); | 64 } |
64 session.putValue("luan",luan); | 65 |
65 } | |
66 LuanFunction fn = getFn(luan,modName); | |
67 service(luan,request,response,fn); | |
68 } | |
69 | |
70 private static LuanFunction getFn(LuanState luan,String modName) throws LuanException { | |
71 Object mod = PackageLib.require(luan,modName); | |
72 if( !(mod instanceof LuanTable) ) | |
73 throw luan.exception( "module '"+modName+"' must return a table" ); | |
74 LuanTable tbl = (LuanTable)mod; | |
75 LuanFunction fn = (LuanFunction)tbl.get("page"); | |
76 if( fn == null ) | |
77 throw luan.exception( "function 'page' is not defined" ); | |
78 return fn; | |
79 } | |
80 | |
81 private static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,LuanFunction fn) | |
82 throws LuanException | |
83 { | |
84 LuanTable module = (LuanTable)luan.loaded().get(NAME); | 66 LuanTable module = (LuanTable)luan.loaded().get(NAME); |
85 if( module == null ) | 67 if( module == null ) |
86 throw luan.exception( "module 'Http' not defined" ); | 68 throw luan.exception( "module 'Http' not defined" ); |
87 HttpLib lib = new HttpLib(request,response); | 69 HttpLib lib = new HttpLib(request,response); |
88 try { | 70 try { |