Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/DomainHandler.java @ 1417:c7f9dd062eda
lucene cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 18 Oct 2019 19:38:08 -0600 |
parents | 546edc2df02c |
children | 42c07ecb0ddc |
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; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1401
diff
changeset
|
11 import goodjava.logging.Logger; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1401
diff
changeset
|
12 import goodjava.logging.LoggerFactory; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1401
diff
changeset
|
13 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1401
diff
changeset
|
14 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1401
diff
changeset
|
15 import goodjava.webserver.Response; |
1315 | 16 |
17 | |
18 public final class DomainHandler implements Handler { | |
19 private static final Logger logger = LoggerFactory.getLogger(DomainHandler.class); | |
20 | |
21 public interface Factory { | |
22 public Handler newHandler(String domain); | |
23 } | |
24 | |
25 private static void close(Handler handler) { | |
26 if( handler instanceof Closeable ) { | |
27 try { | |
28 ((Closeable)handler).close(); | |
29 } catch(IOException e) { | |
30 logger.error(handler.toString(),e); | |
31 } | |
32 } | |
33 } | |
34 | |
1401 | 35 private final Map<String,Reference<Handler>> map = new HashMap<String,Reference<Handler>>(); |
1315 | 36 |
37 private final Factory factory; | |
38 | |
39 public DomainHandler(Factory factory) { | |
40 this.factory = factory; | |
41 } | |
42 | |
43 public Response handle(Request request) { | |
44 String host = (String)request.headers.get("host"); | |
1331 | 45 if( host == null ) |
46 return null; | |
1315 | 47 int i = host.indexOf(':'); |
48 String domain = i == -1 ? host : host.substring(0,i); | |
49 Handler handler = getHandler(domain); | |
50 return handler==null ? null : handler.handle(request); | |
51 } | |
52 | |
53 public Handler getHandler(String domain) { | |
1401 | 54 domain = domain.toLowerCase(); |
55 synchronized(map) { | |
56 Reference<Handler> ref = map.get(domain); | |
57 Handler handler = ref==null ? null : ref.get(); | |
58 if( handler == null ) { | |
59 //if(ref!=null) logger.info("gc "+domain); | |
60 handler = factory.newHandler(domain); | |
61 if( handler == null ) | |
62 return null; | |
63 map.put(domain,new SoftReference<Handler>(handler)); | |
64 } | |
65 return handler; | |
66 } | |
1315 | 67 } |
68 | |
69 public void removeHandler(String domain) { | |
1405 | 70 //logger.info("removeHandler "+domain); |
1315 | 71 domain = domain.toLowerCase(); |
72 synchronized(map) { | |
1401 | 73 Reference<Handler> ref = map.remove(domain); |
74 Handler handler = ref==null ? null : ref.get(); | |
75 if( handler != null ) { | |
76 close(handler); | |
1315 | 77 } |
78 } | |
79 } | |
80 | |
81 } |