comparison src/luan/host/WebHandler.java @ 1766:8df0b80e715e

fix scheduled tasks
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Jun 2023 14:33:24 -0600
parents 164c6ea53147
children 9157e0d5936e
comparison
equal deleted inserted replaced
1765:1ffe1e06ea55 1766:8df0b80e715e
1 package luan.host; 1 package luan.host;
2 2
3 import java.io.Closeable;
3 import java.io.File; 4 import java.io.File;
4 import java.io.IOException; 5 import java.io.IOException;
6 import java.lang.ref.Reference;
7 import java.lang.ref.WeakReference;
8 import java.lang.reflect.Method;
9 import java.util.Set;
10 import java.util.Collections;
11 import java.util.concurrent.ConcurrentHashMap;
5 import goodjava.logging.Logger; 12 import goodjava.logging.Logger;
6 import goodjava.logging.LoggerFactory; 13 import goodjava.logging.LoggerFactory;
7 import goodjava.io.IoUtils; 14 import goodjava.io.IoUtils;
8 import goodjava.webserver.Handler; 15 import goodjava.webserver.Handler;
9 import goodjava.webserver.Request; 16 import goodjava.webserver.Request;
21 import goodjava.webserver.handlers.RegexHandler; 28 import goodjava.webserver.handlers.RegexHandler;
22 import luan.Luan; 29 import luan.Luan;
23 import luan.LuanException; 30 import luan.LuanException;
24 import luan.LuanTable; 31 import luan.LuanTable;
25 import luan.LuanFunction; 32 import luan.LuanFunction;
33 import luan.LuanJavaFunction;
26 import luan.LuanClosure; 34 import luan.LuanClosure;
27 import luan.LuanRuntimeException; 35 import luan.LuanRuntimeException;
28 import luan.modules.http.LuanHandler; 36 import luan.modules.http.LuanHandler;
29 import luan.modules.http.NotFound; 37 import luan.modules.http.NotFound;
30 import luan.modules.logging.LuanLogger; 38 import luan.modules.logging.LuanLogger;
32 40
33 public class WebHandler implements Handler { 41 public class WebHandler implements Handler {
34 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); 42 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class);
35 private static final long days30 = 1000L*60*60*24*30; 43 private static final long days30 = 1000L*60*60*24*30;
36 44
37 private static final class MyHandler implements Handler { 45 private static final class MyHandler implements Handler, Closeable {
38 private final Handler handler; 46 private final Handler handler;
39 final LuanHandler luanHandler; 47 final LuanHandler luanHandler;
40 48
41 MyHandler(Handler handler,LuanHandler luanHandler) { 49 MyHandler(Handler handler,LuanHandler luanHandler) {
42 this.handler = handler; 50 this.handler = handler;
43 this.luanHandler = luanHandler; 51 this.luanHandler = luanHandler;
44 } 52 }
45 53
46 @Override public Response handle(Request request) { 54 @Override public Response handle(Request request) {
47 return handler.handle(request); 55 return handler.handle(request);
56 }
57
58 @Override public void close() {
59 Object obj = dontGc.remove(this);
60 luanHandler.close();
48 } 61 }
49 } 62 }
50 63
51 private static final DomainHandler.Factory factory = new DomainHandler.Factory() { 64 private static final DomainHandler.Factory factory = new DomainHandler.Factory() {
52 public Handler newHandler(String domain) { 65 public Handler newHandler(String domain) {
74 } catch(LuanException e) { 87 } catch(LuanException e) {
75 throw new LuanRuntimeException(e); 88 throw new LuanRuntimeException(e);
76 } finally { 89 } finally {
77 LuanLogger.endThreadLogging(); 90 LuanLogger.endThreadLogging();
78 } 91 }
92 Fns fns = new Fns();
93 try {
94 LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan");
95 Http.put( luan, "dont_gc", new LuanJavaFunction(dontGcMethod,fns) );
96 } catch(LuanException e) {
97 throw new RuntimeException(e);
98 }
79 security(luan,dirStr); 99 security(luan,dirStr);
80 LuanHandler luanHandler = new LuanHandler(luan,domain); 100 LuanHandler luanHandler = new LuanHandler(luan,domain);
81 101
82 FileHandler fileHandler = new FileHandler(dirStr+"/site/"); 102 FileHandler fileHandler = new FileHandler(dirStr+"/site/");
83 Handler handler = new ListHandler( luanHandler, fileHandler ); 103 Handler handler = new ListHandler( luanHandler, fileHandler );
91 handler = new RegexHandler("^/private/",auth,handler); 111 handler = new RegexHandler("^/private/",auth,handler);
92 handler = new HeadersHandler(handler); 112 handler = new HeadersHandler(handler);
93 handler = new SafeHandler(handler); 113 handler = new SafeHandler(handler);
94 handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir),days30)); 114 handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir),days30));
95 115
96 return new MyHandler(handler,luanHandler); 116 MyHandler myHandler = new MyHandler(handler,luanHandler);
117 fns.set(myHandler);
118 return myHandler;
97 } 119 }
98 }; 120 };
99 121
100 public static LuanTable config; 122 public static LuanTable config;
101 private static final DomainHandler domainHandler = new DomainHandler(factory); 123 private static final DomainHandler domainHandler = new DomainHandler(factory);
181 } 203 }
182 }; 204 };
183 Luan.setSecurity(luan,security); 205 Luan.setSecurity(luan,security);
184 } 206 }
185 207
208
209 private static final Set<MyHandler> dontGc = Collections.newSetFromMap(new ConcurrentHashMap<MyHandler,Boolean>());
210 private static final Method dontGcMethod;
211 static {
212 try {
213 dontGcMethod = WebHandler.Fns.class.getMethod( "dont_gc" );
214 } catch(NoSuchMethodException e) {
215 throw new RuntimeException(e);
216 }
217 }
218 public static final class Fns {
219 private Reference<MyHandler> ref = null;
220 private boolean dont = false;
221
222 private void set(MyHandler myHandler) {
223 if( dont ) {
224 dontGc.add(myHandler);
225 } else {
226 ref = new WeakReference<MyHandler>(myHandler);
227 }
228 }
229
230 public void dont_gc() throws LuanException {
231 logger.info("dont_gc");
232 if( ref == null ) {
233 dont = true;
234 } else {
235 MyHandler mh = ref.get();
236 if( mh == null )
237 throw new LuanException("HTTP handler has been garbage collected");
238 dontGc.add(mh);
239 }
240 }
241 }
242
186 } 243 }