Mercurial Hosting > luan
changeset 1231:0b75337bb91a
better closing
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 05 Apr 2018 16:38:33 -0600 |
parents | 034f2a0b3915 |
children | 857eb648d4e5 |
files | src/luan/host/WebHandler.java src/luan/modules/http/LuanHandler.java |
diffstat | 2 files changed, 20 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/host/WebHandler.java Wed Apr 04 14:54:29 2018 -0600 +++ b/src/luan/host/WebHandler.java Thu Apr 05 16:38:33 2018 -0600 @@ -33,16 +33,20 @@ private static class LuanRef { private final LuanState luan; + private final Handler handler; + private final LuanHandler luanHandler; - private LuanRef(LuanState luan) { + private LuanRef(LuanState luan,Handler handler,LuanHandler luanHandler) { this.luan = luan; + this.handler = handler; + this.luanHandler = luanHandler; } } private static final ReferenceQueue<LuanRef> queue = new ReferenceQueue<LuanRef>(); private static class MyReference extends SoftReference<LuanRef> { - private final LuanState luan; + private LuanState luan; private MyReference(LuanRef lr) { super(lr,queue); @@ -57,6 +61,7 @@ return; logger.info("sweep"); ref.luan.close(); + ref.luan = null; } } @@ -65,9 +70,6 @@ private static final Map<String,MyReference> siteMap = new HashMap<String,MyReference>(); private static String sitesDir = null; - private static final String HANDLER = "WebHandler.handler"; - private static final String LUAN_HANDLER = "WebHandler.luanHandler"; - public static boolean isServing() { return sitesDir != null; } @@ -88,23 +90,17 @@ LuanRef lr = getSite(domain); if( lr == null ) return null; - LuanState luan = lr.luan; - Handler handler = (Handler)luan.registry().get(HANDLER); - return handler.handle(request); + return lr.handler.handle(request); } public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException { LuanRef lr = getSite(domain); - LuanState luan = lr.luan; - LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); - return luanHandler.runLuan(sourceText,sourceName); + return lr.luanHandler.runLuan(sourceText,sourceName); } public static Object callSite(String domain,String fnName,Object... args) throws LuanException { LuanRef lr = getSite(domain); - LuanState luan = lr.luan; - LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); - return luanHandler.call_rpc(fnName,args); + return lr.luanHandler.call_rpc(fnName,args); } private static LuanRef getSite(String domain) { @@ -119,8 +115,7 @@ if( !dir.exists() /* && !recover(dir) */ ) return null; sweep(); - LuanState luan = newSite(dir.toString(),domain); - lr = new LuanRef(luan); + lr = newSite(dir.toString(),domain); siteMap.put(domain,new MyReference(lr)); } return lr; @@ -165,7 +160,7 @@ return init; } - private static LuanState newSite(String dir,String domain) { + private static LuanRef newSite(String dir,String domain) { LuanState luan = new LuanState(); LuanTable init = initLuan(luan,dir,domain); @@ -180,10 +175,7 @@ String logDir = dir + "/site/private/local/logs/web"; new File(logDir).mkdirs(); - luan.registry().put(HANDLER,handler); - luan.registry().put(LUAN_HANDLER,luanHandler); - - return luan; + return new LuanRef(luan,handler,luanHandler); } public static void removeHandler(String domain) throws Exception { @@ -191,9 +183,7 @@ Reference<LuanRef> ref = siteMap.remove(domain); LuanRef lr = ref==null ? null : ref.get(); if( lr != null ) { - LuanState luan = lr.luan; - LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); - luanHandler.close(); + lr.luan.close(); } } }
--- a/src/luan/modules/http/LuanHandler.java Wed Apr 04 14:54:29 2018 -0600 +++ b/src/luan/modules/http/LuanHandler.java Thu Apr 05 16:38:33 2018 -0600 @@ -1,5 +1,6 @@ package luan.modules.http; +import java.io.Closeable; import java.io.Writer; import java.io.PrintWriter; import java.io.IOException; @@ -25,7 +26,7 @@ import luan.modules.PackageLuan; -public class LuanHandler implements Handler { +public class LuanHandler implements Handler, Closeable { private final LuanState luanInit; private final Logger logger; private final ReadWriteLock lock = new ReentrantReadWriteLock(); @@ -40,8 +41,8 @@ } } - public LuanHandler(LuanState luan,String loggerRoot) { - this.luanInit = luan; + public LuanHandler(LuanState luanInit,String loggerRoot) { + this.luanInit = luanInit; if( loggerRoot==null ) loggerRoot = ""; logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName()); @@ -52,6 +53,7 @@ throw new RuntimeException(e); } setLuan(); + luanInit.onClose(this); } private void setLuan() { @@ -92,7 +94,7 @@ } } - public void close() throws IOException { + public void close() { synchronized(luan) { luan.close(); }