changeset 521:8a217fe5b4f3

cleaner LuanState.onClose()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 27 May 2015 03:12:28 -0600
parents fcb9b4c8e972
children a7ea7ff0a726
files core/src/luan/LuanState.java core/src/luan/modules/BasicLuan.java core/src/luan/modules/Luan.luan http/ext/slf4j-api-1.6.4.jar http/src/luan/modules/http/LuanHandler.java lucene/src/luan/modules/lucene/Lucene.luan lucene/src/luan/modules/lucene/LuceneIndex.java scripts/build-luan.sh
diffstat 8 files changed, 41 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanState.java	Wed May 27 01:30:25 2015 -0600
+++ b/core/src/luan/LuanState.java	Wed May 27 03:12:28 2015 -0600
@@ -1,5 +1,7 @@
 package luan;
 
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Map;
@@ -13,7 +15,7 @@
 	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
 
 	private Map registry;
-	private final List<LuanFunction> onClose = new ArrayList<LuanFunction>();
+	private final List<Reference<Runnable>> onClose = new ArrayList<Reference<Runnable>>();
 
 	protected LuanState() {
 		registry = new HashMap();
@@ -33,13 +35,15 @@
 		return registry;
 	}
 
-	public void onClose(LuanFunction fn) {
-		onClose.add(fn);
+	public void onClose(Runnable fn) {
+		onClose.add(new WeakReference<Runnable>(fn));
 	}
 
-	public void close() throws LuanException {
-		for( LuanFunction fn : onClose ) {
-			call(fn);
+	public void close() {
+		for( Reference<Runnable> ref : onClose ) {
+			Runnable r = ref.get();
+			if( r != null )
+				r.run();
 		}
 		onClose.clear();
 	}
--- a/core/src/luan/modules/BasicLuan.java	Wed May 27 01:30:25 2015 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Wed May 27 03:12:28 2015 -0600
@@ -225,8 +225,4 @@
 		}
 	}
 
-	public static void on_luan_close(LuanState luan,LuanFunction fn) {
-		luan.onClose(fn);
-	}
-
 }
--- a/core/src/luan/modules/Luan.luan	Wed May 27 01:30:25 2015 -0600
+++ b/core/src/luan/modules/Luan.luan	Wed May 27 03:12:28 2015 -0600
@@ -15,7 +15,6 @@
 M.load = BasicLuan.load
 M.load_file = BasicLuan.load_file
 M.new_error = BasicLuan.new_error
-M.on_luan_close = BasicLuan.on_luan_close
 M.pairs = BasicLuan.pairs
 M.pcall = BasicLuan.pcall
 M.range = BasicLuan.range
Binary file http/ext/slf4j-api-1.6.4.jar has changed
--- a/http/src/luan/modules/http/LuanHandler.java	Wed May 27 01:30:25 2015 -0600
+++ b/http/src/luan/modules/http/LuanHandler.java	Wed May 27 03:12:28 2015 -0600
@@ -43,7 +43,9 @@
 	}
 
 	@Override protected void doStop() throws Exception {
-		luan.close();
+		synchronized(luan) {
+			luan.close();
+		}
 //System.out.println("qqqqqqqqqqqqqqqqqqqq doStop "+this);
 		super.doStop();
 	}
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Wed May 27 01:30:25 2015 -0600
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Wed May 27 03:12:28 2015 -0600
@@ -28,8 +28,6 @@
 	index.map_field_name = java_index.map_field_name
 	index.close = java_index.close
 
-	Luan.on_luan_close(index.close)
-
 	function index.save_document(doc)
 		index.Writer( function(writer)
 			writer.save_document(doc)
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed May 27 01:30:25 2015 -0600
+++ b/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed May 27 03:12:28 2015 -0600
@@ -31,9 +31,13 @@
 import luan.LuanFunction;
 import luan.LuanJavaFunction;
 import luan.LuanException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 public final class LuceneIndex {
+	private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class);
+
 	private static final String FLD_NEXT_ID = "nextId";
 
 	final Lock writeLock = new ReentrantLock();
@@ -43,6 +47,7 @@
 	private DirectoryReader reader;
 	private LuceneSearcher searcher;
 	public final FieldTable fields = new FieldTable();
+	private boolean isClosed = false;
 
 	public LuceneIndex(LuanState luan,String indexDirStr) throws LuanException, IOException {
 		File indexDir = new File(indexDirStr);
@@ -56,6 +61,13 @@
 		writer = new IndexWriter(dir,conf);
 		writer.commit();  // commit index creation
 		reader = DirectoryReader.open(dir);
+		luan.onClose(new Runnable(){public void run() {
+			try {
+				close();
+			} catch(IOException e) {
+				logger.error("",e);
+			}
+		}});
 		searcher = new LuceneSearcher(this,reader);
 		initId(luan);
 	}
@@ -187,8 +199,19 @@
 	}
 
 	public void close() throws IOException {
-		writer.close();
-		reader.close();
+		if( !isClosed ) {
+			writer.close();
+			reader.close();
+			isClosed = true;
+		}
+	}
+
+	protected void finalize() throws Throwable {
+		if( !isClosed ) {
+			logger.error("not closed");
+			close();
+		}
+		super.finalize();
 	}
 
 
--- a/scripts/build-luan.sh	Wed May 27 01:30:25 2015 -0600
+++ b/scripts/build-luan.sh	Wed May 27 03:12:28 2015 -0600
@@ -3,6 +3,7 @@
 cd `dirname $0`/..
 LUAN_HOME=`pwd`
 LUAN_BUILD=$LUAN_HOME/build
+SLF4J=$LUAN_HOME/logging/ext/slf4j-api-1.6.4.jar
 
 VERSION=`scripts/version.sh`
 
@@ -23,7 +24,7 @@
 
 cd $LUAN_HOME
 SRC=http/src
-CLASSPATH=$LUAN_HOME/core/src:$LUAN_HOME/$SRC
+CLASSPATH=$LUAN_HOME/core/src:$LUAN_HOME/$SRC:$SLF4J
 for i in $LUAN_HOME/http/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
 javac -classpath $CLASSPATH `find $SRC -name *.java`
 cd $SRC
@@ -50,7 +51,7 @@
 
 cd $LUAN_HOME
 SRC=lucene/src
-CLASSPATH=$LUAN_HOME/core/src:$LUAN_HOME/$SRC
+CLASSPATH=$LUAN_HOME/core/src:$LUAN_HOME/$SRC:$SLF4J
 for i in $LUAN_HOME/lucene/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
 javac -classpath $CLASSPATH `find $SRC -name *.java`
 cd $SRC