Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/DomainHandler.java @ 1828:09e90d94b7b5
add DomainFilter
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 15 Sep 2024 19:51:16 -0600 |
parents | 9d9683e76496 |
children |
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 | 2 |
3 import java.io.Closeable; | |
4 import java.io.IOException; | |
5 import java.util.Map; | |
1443 | 6 import java.util.Set; |
1601 | 7 import java.util.Collections; |
1443 | 8 import java.util.Timer; |
9 import java.util.TimerTask; | |
10 import java.util.concurrent.ConcurrentHashMap; | |
1498 | 11 import goodjava.util.SoftCacheMap; |
1766 | 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 | 18 |
19 | |
20 public final class DomainHandler implements Handler { | |
21 private static final Logger logger = LoggerFactory.getLogger(DomainHandler.class); | |
22 | |
23 public interface Factory { | |
24 public Handler newHandler(String domain); | |
25 } | |
26 | |
1443 | 27 private static class MyTask extends TimerTask { |
28 private final Set<Handler> dontGc; | |
29 | |
30 MyTask(Set<Handler> dontGc) { | |
31 this.dontGc = dontGc; | |
32 } | |
33 | |
34 public void run() { | |
35 dontGc.clear(); | |
36 logger.info("dontGc.clear()"); | |
1315 | 37 } |
38 } | |
39 | |
1443 | 40 private static final long HOUR = 1000L*60*60; |
1601 | 41 private final Map<String,Handler> map = Collections.synchronizedMap(new SoftCacheMap<String,Handler>()); |
1443 | 42 private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet(); |
43 private final Timer timer = new Timer(); | |
1315 | 44 |
45 private final Factory factory; | |
46 | |
47 public DomainHandler(Factory factory) { | |
48 this.factory = factory; | |
1443 | 49 timer.schedule(new MyTask(dontGc),HOUR,HOUR); |
50 } | |
51 | |
52 protected void finalize() throws Throwable { | |
53 timer.cancel(); | |
1315 | 54 } |
55 | |
56 public Response handle(Request request) { | |
57 String host = (String)request.headers.get("host"); | |
1331 | 58 if( host == null ) |
59 return null; | |
1315 | 60 int i = host.indexOf(':'); |
61 String domain = i == -1 ? host : host.substring(0,i); | |
62 Handler handler = getHandler(domain); | |
1443 | 63 if( handler==null ) |
64 return null; | |
65 dontGc.add(handler); | |
66 return handler.handle(request); | |
1315 | 67 } |
68 | |
69 public Handler getHandler(String domain) { | |
1601 | 70 domain = domain.toLowerCase().intern(); |
71 synchronized(domain) { | |
1498 | 72 Handler handler = map.get(domain); |
1401 | 73 if( handler == null ) { |
74 //if(ref!=null) logger.info("gc "+domain); | |
75 handler = factory.newHandler(domain); | |
76 if( handler == null ) | |
77 return null; | |
1498 | 78 map.put(domain,handler); |
1401 | 79 } |
80 return handler; | |
81 } | |
1315 | 82 } |
83 | |
84 public void removeHandler(String domain) { | |
1405 | 85 //logger.info("removeHandler "+domain); |
1601 | 86 domain = domain.toLowerCase().intern(); |
87 synchronized(domain) { | |
1498 | 88 Handler handler = map.remove(domain); |
1401 | 89 if( handler != null ) { |
90 close(handler); | |
1769 | 91 dontGc.remove(handler); |
1315 | 92 } |
93 } | |
94 } | |
95 | |
1443 | 96 private static void close(Handler handler) { |
97 if( handler instanceof Closeable ) { | |
98 try { | |
99 ((Closeable)handler).close(); | |
100 } catch(IOException e) { | |
101 logger.error(handler.toString(),e); | |
102 } | |
103 } | |
104 } | |
105 | |
1315 | 106 } |