comparison src/luan/modules/http/LuanHandler.java @ 1231:0b75337bb91a

better closing
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 05 Apr 2018 16:38:33 -0600
parents c7adcdf79bf2
children c147e2e877e3
comparison
equal deleted inserted replaced
1230:034f2a0b3915 1231:0b75337bb91a
1 package luan.modules.http; 1 package luan.modules.http;
2 2
3 import java.io.Closeable;
3 import java.io.Writer; 4 import java.io.Writer;
4 import java.io.PrintWriter; 5 import java.io.PrintWriter;
5 import java.io.IOException; 6 import java.io.IOException;
6 import java.lang.reflect.Method; 7 import java.lang.reflect.Method;
7 import java.net.BindException; 8 import java.net.BindException;
23 import luan.LuanCloner; 24 import luan.LuanCloner;
24 import luan.LuanException; 25 import luan.LuanException;
25 import luan.modules.PackageLuan; 26 import luan.modules.PackageLuan;
26 27
27 28
28 public class LuanHandler implements Handler { 29 public class LuanHandler implements Handler, Closeable {
29 private final LuanState luanInit; 30 private final LuanState luanInit;
30 private final Logger logger; 31 private final Logger logger;
31 private final ReadWriteLock lock = new ReentrantReadWriteLock(); 32 private final ReadWriteLock lock = new ReentrantReadWriteLock();
32 private LuanState luan; 33 private LuanState luan;
33 34
38 } catch(NoSuchMethodException e) { 39 } catch(NoSuchMethodException e) {
39 throw new RuntimeException(e); 40 throw new RuntimeException(e);
40 } 41 }
41 } 42 }
42 43
43 public LuanHandler(LuanState luan,String loggerRoot) { 44 public LuanHandler(LuanState luanInit,String loggerRoot) {
44 this.luanInit = luan; 45 this.luanInit = luanInit;
45 if( loggerRoot==null ) 46 if( loggerRoot==null )
46 loggerRoot = ""; 47 loggerRoot = "";
47 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName()); 48 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
48 try { 49 try {
49 LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan"); 50 LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan");
50 Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) ); 51 Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
51 } catch(LuanException e) { 52 } catch(LuanException e) {
52 throw new RuntimeException(e); 53 throw new RuntimeException(e);
53 } 54 }
54 setLuan(); 55 setLuan();
56 luanInit.onClose(this);
55 } 57 }
56 58
57 private void setLuan() { 59 private void setLuan() {
58 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 60 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
59 luan = (LuanState)cloner.clone(luanInit); 61 luan = (LuanState)cloner.clone(luanInit);
90 lock.readLock().unlock(); 92 lock.readLock().unlock();
91 thread.setName(oldName); 93 thread.setName(oldName);
92 } 94 }
93 } 95 }
94 96
95 public void close() throws IOException { 97 public void close() {
96 synchronized(luan) { 98 synchronized(luan) {
97 luan.close(); 99 luan.close();
98 } 100 }
99 } 101 }
100 102