changeset 1329:5a39b006acd1

make LuanDomainHandler flexible
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 10 Feb 2019 02:01:49 -0700
parents 55b8210b4ec0
children f41919741100
files src/luan/modules/http/LuanDomainHandler.java
diffstat 1 files changed, 20 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
diff -r 55b8210b4ec0 -r 5a39b006acd1 src/luan/modules/http/LuanDomainHandler.java
--- a/src/luan/modules/http/LuanDomainHandler.java	Sat Feb 09 21:49:42 2019 -0700
+++ b/src/luan/modules/http/LuanDomainHandler.java	Sun Feb 10 02:01:49 2019 -0700
@@ -1,6 +1,5 @@
 package luan.modules.http;
 
-import org.apache.log4j.MDC;
 import luan.webserver.Request;
 import luan.webserver.Response;
 import luan.webserver.Handler;
@@ -12,39 +11,35 @@
 import luan.modules.logging.LuanLogger;
 
 
-public final class LuanDomainHandler implements Handler {
+public class LuanDomainHandler implements Handler, DomainHandler.Factory {
 
 	private final LuanState luanInit;
-
-	private final DomainHandler.Factory factory = new DomainHandler.Factory() {
-		public Handler newHandler(String domain) {
-			LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
-			LuanState luan = (LuanState)cloner.clone(luanInit);
-			try {
-				LuanLogger.newLoggerRepository(luan);
-				LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan");
-				Http.put( "domain", domain );
-			} catch(LuanException e) {
-				throw new RuntimeException(e);
-			}
-			return new LuanHandler(luan);
-		}
-	};
-
-	private final DomainHandler domainHandler = new DomainHandler(factory);
+	private final DomainHandler domainHandler = new DomainHandler(this);
 
 	public LuanDomainHandler(LuanState luanInit) {
 		LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
 		this.luanInit = (LuanState)cloner.clone(luanInit);
 	}
 
-	@Override public Response handle(Request request) {
-		String host = (String)request.headers.get("host");
-		MDC.put("host",host);
+	@Override public Handler newHandler(String domain) {
+		LuanState luan = newLuan(domain);
+		return new LuanHandler(luan);
+	}
+
+	protected LuanState newLuan(String domain) {
+		LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
+		LuanState luan = (LuanState)cloner.clone(luanInit);
 		try {
-			return domainHandler.handle(request);
-		} finally {
-			MDC.remove("host");
+			LuanLogger.newLoggerRepository(luan);
+			LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan");
+			Http.put( "domain", domain );
+		} catch(LuanException e) {
+			throw new RuntimeException(e);
 		}
+		return luan;
+	}
+
+	@Override public Response handle(Request request) {
+		return domainHandler.handle(request);
 	}
 }