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