Mercurial Hosting > luan
comparison src/luan/modules/http/LuanHandler.java @ 1400:221eedb0f54e
fix inner class gc bug
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 13 Sep 2019 05:05:51 -0600 |
parents | 0dc9837c16be |
children | ef1620aa99cb |
comparison
equal
deleted
inserted
replaced
1399:38a1c1b4279a | 1400:221eedb0f54e |
---|---|
11 import java.util.List; | 11 import java.util.List; |
12 import java.util.ArrayList; | 12 import java.util.ArrayList; |
13 import java.util.concurrent.locks.ReadWriteLock; | 13 import java.util.concurrent.locks.ReadWriteLock; |
14 import java.util.concurrent.locks.ReentrantReadWriteLock; | 14 import java.util.concurrent.locks.ReentrantReadWriteLock; |
15 import luan.lib.logging.Logger; | 15 import luan.lib.logging.Logger; |
16 import luan.lib.logging.LoggerFactory; | |
16 import luan.lib.webserver.Request; | 17 import luan.lib.webserver.Request; |
17 import luan.lib.webserver.Response; | 18 import luan.lib.webserver.Response; |
18 import luan.lib.webserver.Status; | 19 import luan.lib.webserver.Status; |
19 import luan.lib.webserver.Server; | 20 import luan.lib.webserver.Server; |
20 import luan.lib.webserver.Handler; | 21 import luan.lib.webserver.Handler; |
29 import luan.modules.BasicLuan; | 30 import luan.modules.BasicLuan; |
30 import luan.modules.logging.LuanLogger; | 31 import luan.modules.logging.LuanLogger; |
31 | 32 |
32 | 33 |
33 public final class LuanHandler implements Handler, Luan.OnClose { | 34 public final class LuanHandler implements Handler, Luan.OnClose { |
35 private static final Logger sysLogger = LoggerFactory.getLogger(LuanHandler.class); | |
36 | |
34 private final Luan luanInit; | 37 private final Luan luanInit; |
35 private final Logger logger; | 38 private final String domain; |
39 private final Logger luanLogger; | |
36 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); | 40 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); |
37 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>(); | 41 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>(); |
38 private volatile Luan currentLuan; | 42 private volatile Luan currentLuan; |
39 private volatile boolean isDisabled = false; | 43 private volatile boolean isDisabled = false; |
40 | 44 |
49 } catch(NoSuchMethodException e) { | 53 } catch(NoSuchMethodException e) { |
50 throw new RuntimeException(e); | 54 throw new RuntimeException(e); |
51 } | 55 } |
52 } | 56 } |
53 | 57 |
54 public LuanHandler(Luan luanInit) { | 58 public LuanHandler(Luan luanInit,String domain) { |
55 this.luanInit = luanInit; | 59 this.luanInit = luanInit; |
56 this.logger = LuanLogger.getLogger(luanInit,LuanHandler.class.getName()); | 60 this.domain = domain; |
61 this.luanLogger = luanInit.getLogger(LuanHandler.class); | |
57 try { | 62 try { |
58 LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan"); | 63 LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan"); |
59 if( Http.get("reset_luan") == null ) | 64 if( Http.get("reset_luan") == null ) |
60 Http.put( "reset_luan", new LuanJavaFunction(luanInit,resetLuanMethod,this) ); | 65 Http.put( "reset_luan", new LuanJavaFunction(luanInit,resetLuanMethod,this) ); |
61 Http.put( "eval_in_root", new LuanJavaFunction(luanInit,evalInRootMethod,this) ); | 66 Http.put( "eval_in_root", new LuanJavaFunction(luanInit,evalInRootMethod,this) ); |
62 Http.put( "disable_luan", new LuanJavaFunction(luanInit,disableLuanMethod,this) ); | 67 Http.put( "disable_luan", new LuanJavaFunction(luanInit,disableLuanMethod,this) ); |
63 } catch(LuanException e) { | 68 } catch(LuanException e) { |
64 throw new RuntimeException(e); | 69 throw new RuntimeException(e); |
65 } | 70 } |
66 currentLuan = newLuan(); | 71 currentLuan = newLuan(); |
72 sysLogger.info("new "+domain); | |
73 } | |
74 | |
75 protected void finalize() throws Throwable { | |
76 sysLogger.info("gc "+domain); | |
67 } | 77 } |
68 | 78 |
69 private Luan newLuan() { | 79 private Luan newLuan() { |
70 Luan luan; | 80 Luan luan; |
71 synchronized(luanInit) { | 81 synchronized(luanInit) { |
76 try { | 86 try { |
77 PackageLuan.load(luan,"site:/init.luan"); | 87 PackageLuan.load(luan,"site:/init.luan"); |
78 } catch(LuanException e) { | 88 } catch(LuanException e) { |
79 //e.printStackTrace(); | 89 //e.printStackTrace(); |
80 String err = e.getLuanStackTraceString(); | 90 String err = e.getLuanStackTraceString(); |
81 logger.error(err); | 91 luanLogger.error(err); |
82 } | 92 } |
83 return luan; | 93 return luan; |
84 } | 94 } |
85 | 95 |
86 /* | 96 /* |
105 rwLock.readLock().lock(); | 115 rwLock.readLock().lock(); |
106 try { | 116 try { |
107 return service(request,notFound); | 117 return service(request,notFound); |
108 } catch(LuanException e) { | 118 } catch(LuanException e) { |
109 String err = e.getLuanStackTraceString(); | 119 String err = e.getLuanStackTraceString(); |
110 logger.error(err+"\n"+request.rawHead.trim()+"\n"); | 120 luanLogger.error(err+"\n"+request.rawHead.trim()+"\n"); |
111 String msg = "Internel Server Error\n\n" + err; | 121 String msg = "Internel Server Error\n\n" + err; |
112 return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg ); | 122 return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg ); |
113 } finally { | 123 } finally { |
114 rwLock.readLock().unlock(); | 124 rwLock.readLock().unlock(); |
115 thread.setName(oldName); | 125 thread.setName(oldName); |
128 Closeable c = ref.get(); | 138 Closeable c = ref.get(); |
129 if( c != null ) { | 139 if( c != null ) { |
130 try { | 140 try { |
131 c.close(); | 141 c.close(); |
132 } catch(IOException e) { | 142 } catch(IOException e) { |
133 logger.error(c.toString(),e); | 143 luanLogger.error(c.toString(),e); |
134 } | 144 } |
135 } | 145 } |
136 } | 146 } |
137 onClose.clear(); | 147 onClose.clear(); |
138 } | 148 } |