comparison src/goodjava/webserver/handlers/DomainHandler.java @ 1443:42c07ecb0ddc

DomainHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 28 Jan 2020 01:16:31 -0700
parents 546edc2df02c
children 1b809d2fdf03
comparison
equal deleted inserted replaced
1442:bbc41b305935 1443:42c07ecb0ddc
6 //import java.lang.ref.WeakReference; 6 //import java.lang.ref.WeakReference;
7 import java.lang.ref.SoftReference; 7 import java.lang.ref.SoftReference;
8 import java.lang.ref.ReferenceQueue; 8 import java.lang.ref.ReferenceQueue;
9 import java.util.Map; 9 import java.util.Map;
10 import java.util.HashMap; 10 import java.util.HashMap;
11 import java.util.Set;
12 import java.util.Timer;
13 import java.util.TimerTask;
14 import java.util.concurrent.ConcurrentHashMap;
11 import goodjava.logging.Logger; 15 import goodjava.logging.Logger;
12 import goodjava.logging.LoggerFactory; 16 import goodjava.logging.LoggerFactory;
13 import goodjava.webserver.Handler; 17 import goodjava.webserver.Handler;
14 import goodjava.webserver.Request; 18 import goodjava.webserver.Request;
15 import goodjava.webserver.Response; 19 import goodjava.webserver.Response;
20 24
21 public interface Factory { 25 public interface Factory {
22 public Handler newHandler(String domain); 26 public Handler newHandler(String domain);
23 } 27 }
24 28
25 private static void close(Handler handler) { 29 private static class MyTask extends TimerTask {
26 if( handler instanceof Closeable ) { 30 private final Set<Handler> dontGc;
27 try { 31
28 ((Closeable)handler).close(); 32 MyTask(Set<Handler> dontGc) {
29 } catch(IOException e) { 33 this.dontGc = dontGc;
30 logger.error(handler.toString(),e); 34 }
31 } 35
36 public void run() {
37 dontGc.clear();
38 logger.info("dontGc.clear()");
32 } 39 }
33 } 40 }
34 41
42 private static final long HOUR = 1000L*60*60;
35 private final Map<String,Reference<Handler>> map = new HashMap<String,Reference<Handler>>(); 43 private final Map<String,Reference<Handler>> map = new HashMap<String,Reference<Handler>>();
44 private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet();
45 private final Timer timer = new Timer();
36 46
37 private final Factory factory; 47 private final Factory factory;
38 48
39 public DomainHandler(Factory factory) { 49 public DomainHandler(Factory factory) {
40 this.factory = factory; 50 this.factory = factory;
51 timer.schedule(new MyTask(dontGc),HOUR,HOUR);
52 }
53
54 protected void finalize() throws Throwable {
55 timer.cancel();
41 } 56 }
42 57
43 public Response handle(Request request) { 58 public Response handle(Request request) {
44 String host = (String)request.headers.get("host"); 59 String host = (String)request.headers.get("host");
45 if( host == null ) 60 if( host == null )
46 return null; 61 return null;
47 int i = host.indexOf(':'); 62 int i = host.indexOf(':');
48 String domain = i == -1 ? host : host.substring(0,i); 63 String domain = i == -1 ? host : host.substring(0,i);
49 Handler handler = getHandler(domain); 64 Handler handler = getHandler(domain);
50 return handler==null ? null : handler.handle(request); 65 if( handler==null )
66 return null;
67 dontGc.add(handler);
68 return handler.handle(request);
51 } 69 }
52 70
53 public Handler getHandler(String domain) { 71 public Handler getHandler(String domain) {
54 domain = domain.toLowerCase(); 72 domain = domain.toLowerCase();
55 synchronized(map) { 73 synchronized(map) {
76 close(handler); 94 close(handler);
77 } 95 }
78 } 96 }
79 } 97 }
80 98
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
81 } 109 }