annotate src/goodjava/webserver/handlers/DomainHandler.java @ 1766:8df0b80e715e

fix scheduled tasks
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Jun 2023 14:33:24 -0600
parents de5a81327d9c
children ad53577872cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
1 package goodjava.webserver.handlers;
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.Closeable;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.IOException;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Map;
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
6 import java.util.Set;
1601
de5a81327d9c fix deadlock
Franklin Schmidt <fschmidt@gmail.com>
parents: 1498
diff changeset
7 import java.util.Collections;
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
8 import java.util.Timer;
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
9 import java.util.TimerTask;
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
10 import java.util.concurrent.ConcurrentHashMap;
1498
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents: 1443
diff changeset
11 import goodjava.util.SoftCacheMap;
1766
8df0b80e715e fix scheduled tasks
Franklin Schmidt <fschmidt@gmail.com>
parents: 1601
diff changeset
12 import goodjava.util.WeakCacheMap;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
13 import goodjava.logging.Logger;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
14 import goodjava.logging.LoggerFactory;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
15 import goodjava.webserver.Handler;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
16 import goodjava.webserver.Request;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
17 import goodjava.webserver.Response;
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public final class DomainHandler implements Handler {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 private static final Logger logger = LoggerFactory.getLogger(DomainHandler.class);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 public interface Factory {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public Handler newHandler(String domain);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
27 private static class MyTask extends TimerTask {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
28 private final Set<Handler> dontGc;
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
29
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
30 MyTask(Set<Handler> dontGc) {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
31 this.dontGc = dontGc;
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
32 }
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
33
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
34 public void run() {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
35 dontGc.clear();
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
36 logger.info("dontGc.clear()");
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
40 private static final long HOUR = 1000L*60*60;
1601
de5a81327d9c fix deadlock
Franklin Schmidt <fschmidt@gmail.com>
parents: 1498
diff changeset
41 private final Map<String,Handler> map = Collections.synchronizedMap(new SoftCacheMap<String,Handler>());
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
42 private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet();
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
43 private final Timer timer = new Timer();
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 private final Factory factory;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 public DomainHandler(Factory factory) {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 this.factory = factory;
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
49 timer.schedule(new MyTask(dontGc),HOUR,HOUR);
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
50 }
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
51
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
52 protected void finalize() throws Throwable {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
53 timer.cancel();
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 public Response handle(Request request) {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 String host = (String)request.headers.get("host");
1331
02fe660e7748 minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1315
diff changeset
58 if( host == null )
02fe660e7748 minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1315
diff changeset
59 return null;
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 int i = host.indexOf(':');
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 String domain = i == -1 ? host : host.substring(0,i);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 Handler handler = getHandler(domain);
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
63 if( handler==null )
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
64 return null;
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
65 dontGc.add(handler);
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
66 return handler.handle(request);
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 public Handler getHandler(String domain) {
1601
de5a81327d9c fix deadlock
Franklin Schmidt <fschmidt@gmail.com>
parents: 1498
diff changeset
70 domain = domain.toLowerCase().intern();
de5a81327d9c fix deadlock
Franklin Schmidt <fschmidt@gmail.com>
parents: 1498
diff changeset
71 synchronized(domain) {
1498
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents: 1443
diff changeset
72 Handler handler = map.get(domain);
1401
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
73 if( handler == null ) {
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
74 //if(ref!=null) logger.info("gc "+domain);
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
75 handler = factory.newHandler(domain);
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
76 if( handler == null )
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
77 return null;
1498
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents: 1443
diff changeset
78 map.put(domain,handler);
1401
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
79 }
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
80 return handler;
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
81 }
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 public void removeHandler(String domain) {
1405
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
85 //logger.info("removeHandler "+domain);
1601
de5a81327d9c fix deadlock
Franklin Schmidt <fschmidt@gmail.com>
parents: 1498
diff changeset
86 domain = domain.toLowerCase().intern();
de5a81327d9c fix deadlock
Franklin Schmidt <fschmidt@gmail.com>
parents: 1498
diff changeset
87 synchronized(domain) {
1498
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents: 1443
diff changeset
88 Handler handler = map.remove(domain);
1401
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
89 if( handler != null ) {
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
90 close(handler);
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94
1443
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
95 private static void close(Handler handler) {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
96 if( handler instanceof Closeable ) {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
97 try {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
98 ((Closeable)handler).close();
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
99 } catch(IOException e) {
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
100 logger.error(handler.toString(),e);
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
101 }
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
102 }
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
103 }
42c07ecb0ddc DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1405
diff changeset
104
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 }