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