changeset 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 c2ee8a717b73
files src/luan/LuanState.java src/luan/lib/HttpLib.java src/luan/tools/web_shell.luan
diffstat 3 files changed, 35 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanState.java	Fri Jun 13 12:26:44 2014 +0000
+++ b/src/luan/LuanState.java	Fri Jun 13 15:04:29 2014 +0000
@@ -160,12 +160,15 @@
 		}
 	}
 
-	public final Object eval(String cmd) throws LuanException {
-		LuanFunction fn = BasicLib.load(this,cmd,"eval",true,true);
-		return call(fn);
+	public final Object eval(String cmd) {
+		try {
+			LuanFunction fn = BasicLib.load(this,cmd,"eval",true,true);
+			return call(fn);
+		} catch(LuanException e) {
+			throw new RuntimeException(e);
+		}
 	}
 
-
 	public final LuanTable getMetatable(Object obj) {
 		if( obj instanceof LuanTable ) {
 			LuanTable table = (LuanTable)obj;
--- a/src/luan/lib/HttpLib.java	Fri Jun 13 12:26:44 2014 +0000
+++ b/src/luan/lib/HttpLib.java	Fri Jun 13 15:04:29 2014 +0000
@@ -11,6 +11,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import luan.Luan;
 import luan.LuanState;
 import luan.LuanFunction;
 import luan.LuanElement;
@@ -31,56 +32,37 @@
 		}
 	};
 
-	public static void service_per_request(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
+	public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
 		throws LuanException
 	{
 		LuanFunction fn;
 		synchronized(luan) {
-			fn = getFn(luan,modName);
-			DeepCloner cloner = new DeepCloner();
-			luan = cloner.deepClone(luan);
-			fn = cloner.get(fn);
+			Object mod = PackageLib.require(luan,modName);
+			if( !(mod instanceof LuanTable) )
+				throw luan.exception( "module '"+modName+"' must return a table" );
+			LuanTable tbl = (LuanTable)mod;
+			if( Luan.toBoolean( tbl.get("per_session") ) ) {
+				HttpSession session = request.getSession();
+				LuanState sessionLuan  = (LuanState)session.getValue("luan");
+				if( sessionLuan!=null ) {
+					luan = sessionLuan;
+				} else {
+					DeepCloner cloner = new DeepCloner();
+					luan = cloner.deepClone(luan);
+					session.putValue("luan",luan);
+				}
+				tbl = (LuanTable)PackageLib.require(luan,modName);
+				fn = (LuanFunction)tbl.get("page");
+			} else {
+				fn = (LuanFunction)tbl.get("page");
+				if( fn == null )
+					throw luan.exception( "function 'page' is not defined" );
+				DeepCloner cloner = new DeepCloner();
+				luan = cloner.deepClone(luan);
+				fn = cloner.get(fn);
+			}
 		}
-		service(luan,request,response,fn);
-	}
-
-	public static void service_global(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
-		throws LuanException
-	{
-		LuanFunction fn = getFn(luan,modName);
-		service(luan,request,response,fn);
-	}
 
-	public static void service_per_session(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
-		throws LuanException
-	{
-		HttpSession session = request.getSession();
-		LuanState sessionLuan  = (LuanState)session.getValue("luan");
-		if( sessionLuan!=null ) {
-			luan = sessionLuan;
-		} else {
-			DeepCloner cloner = new DeepCloner();
-			luan = cloner.deepClone(luan);
-			session.putValue("luan",luan);
-		}
-		LuanFunction fn = getFn(luan,modName);
-		service(luan,request,response,fn);
-	}
-
-	private static LuanFunction getFn(LuanState luan,String modName) throws LuanException {
-		Object mod = PackageLib.require(luan,modName);
-		if( !(mod instanceof LuanTable) )
-			throw luan.exception( "module '"+modName+"' must return a table" );
-		LuanTable tbl = (LuanTable)mod;
-		LuanFunction fn = (LuanFunction)tbl.get("page");
-		if( fn == null )
-			throw luan.exception( "function 'page' is not defined" );
-		return fn;
-	}
-
-	private static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,LuanFunction fn)
-		throws LuanException
-	{
 		LuanTable module = (LuanTable)luan.loaded().get(NAME);
 		if( module == null )
 			throw luan.exception( "module 'Http' not defined" );
--- a/src/luan/tools/web_shell.luan	Fri Jun 13 12:26:44 2014 +0000
+++ b/src/luan/tools/web_shell.luan	Fri Jun 13 15:04:29 2014 +0000
@@ -1,5 +1,7 @@
 import "Http"
 
+per_session = true
+
 local history = {}
 
 Io.stdout = {}