changeset 1186:ef8cd42e23d5

add back reset_luan
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 21 Feb 2018 21:50:20 -0700
parents 94cf2576a922
children 83c8a5a47f70
files src/luan/modules/http/LuanHandler.java
diffstat 1 files changed, 41 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
diff -r 94cf2576a922 -r ef8cd42e23d5 src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Wed Feb 21 21:22:16 2018 -0700
+++ b/src/luan/modules/http/LuanHandler.java	Wed Feb 21 21:50:20 2018 -0700
@@ -26,17 +26,37 @@
 
 
 public class LuanHandler implements Handler {
+	private final LuanState luanInit;
 	private final Logger logger;
 	private final ReadWriteLock lock = new ReentrantReadWriteLock();
-	private final LuanState luan;
+	private LuanState luan;
 
-	public LuanHandler(LuanState luanInit,String loggerRoot) {
+	private static final Method resetLuanMethod;
+	static {
+		try {
+			resetLuanMethod = LuanHandler.class.getMethod("reset_luan");
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	public LuanHandler(LuanState luan,String loggerRoot) {
+		this.luanInit = luan;
 		if( loggerRoot==null )
 			loggerRoot = "";
 		logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
+		try {
+			LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan");
+			Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
+		} catch(LuanException e) {
+			throw new RuntimeException(e);
+		}
+		setLuan();
+	}
 
+	private void setLuan() {
 		LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
-		this.luan = (LuanState)cloner.clone(luanInit);
+		luan = (LuanState)cloner.clone(luanInit);
 		try {
 			PackageLuan.load(luan,"site:/init.luan");
 		} catch(LuanException e) {
@@ -100,6 +120,24 @@
 		}
 	}
 
+	public void reset_luan() {
+		new Thread() {
+			public void run() {
+				lock.writeLock().lock();
+				try {
+					synchronized(luan) {
+						luan.close();
+						setLuan();
+					}
+				} catch(IOException e) {
+					logger.error("reset_luan failed",e);
+				} finally {
+					lock.writeLock().unlock();
+				}
+			}
+		}.start();
+	}
+
 	public Object runLuan(String sourceText,String sourceName) throws LuanException {
 		LuanFunction fn = Luan.load(sourceText,sourceName);
 		synchronized(luan) {