annotate src/goodjava/webserver/handlers/DomainHandler.java @ 1703:3a61451f8130

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