annotate src/goodjava/webserver/handlers/DomainHandler.java @ 1474:13cbce740e1e

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