Mercurial Hosting > luan
comparison src/luan/host/WebHandler.java @ 1202:d3a3ca116e42
gc site instances
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 05 Mar 2018 19:30:51 -0700 |
parents | 83c8a5a47f70 |
children | 7e6d132904fd |
comparison
equal
deleted
inserted
replaced
1201:7f4a12fb7716 | 1202:d3a3ca116e42 |
---|---|
1 package luan.host; | 1 package luan.host; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.lang.ref.Reference; | |
6 import java.lang.ref.SoftReference; | |
7 //import java.lang.ref.WeakReference; | |
5 import java.util.Map; | 8 import java.util.Map; |
6 import java.util.HashMap; | 9 import java.util.HashMap; |
7 import org.slf4j.Logger; | 10 import org.slf4j.Logger; |
8 import org.slf4j.LoggerFactory; | 11 import org.slf4j.LoggerFactory; |
9 import luan.webserver.Handler; | 12 import luan.webserver.Handler; |
25 | 28 |
26 | 29 |
27 public class WebHandler implements Handler { | 30 public class WebHandler implements Handler { |
28 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); | 31 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); |
29 | 32 |
30 private static class Site { | 33 public static String allowJavaFileName = "allow_java"; // change for security |
31 final Handler handler; | 34 private static final Map<String,Reference<LuanState>> siteMap = new HashMap<String,Reference<LuanState>>(); |
32 final LuanHandler luanHandler; | 35 private static String sitesDir = null; |
33 | 36 |
34 Site(Handler handler,LuanHandler luanHandler) { | 37 private static final String HANDLER = "WebHandler.handler"; |
35 this.handler = handler; | 38 private static final String LUAN_HANDLER = "WebHandler.luanHandler"; |
36 this.luanHandler = luanHandler; | |
37 } | |
38 } | |
39 | |
40 public static String allowJavaFileName = "allow_java"; // change for security | |
41 private static final Map<String,Site> siteMap = new HashMap<String,Site>(); | |
42 private static String sitesDir = null; | |
43 | 39 |
44 public static boolean isServing() { | 40 public static boolean isServing() { |
45 return sitesDir != null; | 41 return sitesDir != null; |
46 } | 42 } |
47 | 43 |
56 public Response handle(Request request) { | 52 public Response handle(Request request) { |
57 String host = (String)request.headers.get("host"); | 53 String host = (String)request.headers.get("host"); |
58 int i = host.indexOf(':'); | 54 int i = host.indexOf(':'); |
59 String domain = i == -1 ? host : host.substring(0,i); | 55 String domain = i == -1 ? host : host.substring(0,i); |
60 // System.out.println("handle "+domain); | 56 // System.out.println("handle "+domain); |
61 Site site = getSite(domain); | 57 LuanState luan = getSite(domain); |
62 return site==null ? null : site.handler.handle(request); | 58 if( luan == null ) |
59 return null; | |
60 Handler handler = (Handler)luan.registry().get(HANDLER); | |
61 return handler.handle(request); | |
63 } | 62 } |
64 | 63 |
65 public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException { | 64 public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException { |
66 return getSite(domain).luanHandler.runLuan(sourceText,sourceName); | 65 LuanState luan = getSite(domain); |
66 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); | |
67 return luanHandler.runLuan(sourceText,sourceName); | |
67 } | 68 } |
68 | 69 |
69 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { | 70 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { |
70 return getSite(domain).luanHandler.call_rpc(fnName,args); | 71 LuanState luan = getSite(domain); |
72 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); | |
73 return luanHandler.call_rpc(fnName,args); | |
71 } | 74 } |
72 | 75 |
73 private static Site getSite(String domain) { | 76 private static LuanState getSite(String domain) { |
74 synchronized(siteMap) { | 77 synchronized(siteMap) { |
75 Site site = siteMap.get(domain); | 78 Reference<LuanState> ref = siteMap.get(domain); |
76 if( site == null ) { | 79 LuanState luan = ref==null ? null : ref.get(); |
80 if( luan == null ) { | |
81 if(ref!=null) logger.info("gc "+domain); | |
77 if( sitesDir==null ) | 82 if( sitesDir==null ) |
78 throw new NullPointerException("sitesDir"); | 83 throw new NullPointerException("sitesDir"); |
79 File dir = new File(sitesDir,domain); | 84 File dir = new File(sitesDir,domain); |
80 if( !dir.exists() /* && !recover(dir) */ ) | 85 if( !dir.exists() /* && !recover(dir) */ ) |
81 return null; | 86 return null; |
82 site = newSite(dir.toString(),domain); | 87 luan = newSite(dir.toString(),domain); |
83 siteMap.put(domain,site); | 88 siteMap.put(domain,new SoftReference<LuanState>(luan)); |
84 } | 89 } |
85 return site; | 90 return luan; |
86 } | 91 } |
87 } | 92 } |
88 /* | 93 /* |
89 private static boolean recover(File dir) { | 94 private static boolean recover(File dir) { |
90 File backups = new File(dir.getParentFile().getParentFile(),"backups"); | 95 File backups = new File(dir.getParentFile().getParentFile(),"backups"); |
122 IoLuan.setSecurity( luan, ioSecurity(dir) ); | 127 IoLuan.setSecurity( luan, ioSecurity(dir) ); |
123 } | 128 } |
124 return init; | 129 return init; |
125 } | 130 } |
126 | 131 |
127 private static Site newSite(String dir,String domain) { | 132 private static LuanState newSite(String dir,String domain) { |
128 LuanState luan = new LuanState(); | 133 LuanState luan = new LuanState(); |
129 LuanTable init = initLuan(luan,dir,domain); | 134 LuanTable init = initLuan(luan,dir,domain); |
130 | 135 |
131 String loggerRoot = (String)init.rawGet("logger_root"); | 136 String loggerRoot = (String)init.rawGet("logger_root"); |
132 LuanHandler luanHandler = new LuanHandler(luan,loggerRoot); | 137 LuanHandler luanHandler = new LuanHandler(luan,loggerRoot); |
134 | 139 |
135 Handler handler = luanHandler; | 140 Handler handler = luanHandler; |
136 handler = new IndexHandler(handler); | 141 handler = new IndexHandler(handler); |
137 handler = new ListHandler( handler, notFoundHandler ); | 142 handler = new ListHandler( handler, notFoundHandler ); |
138 | 143 |
139 String logDir = dir+"/site/private/local/logs/web"; | 144 String logDir = dir + "/site/private/local/logs/web"; |
140 new File(logDir).mkdirs(); | 145 new File(logDir).mkdirs(); |
141 | 146 |
142 return new Site(handler,luanHandler); | 147 luan.registry().put(HANDLER,handler); |
148 luan.registry().put(LUAN_HANDLER,luanHandler); | |
149 | |
150 return luan; | |
143 } | 151 } |
144 | 152 |
145 public static void removeHandler(String domain) throws Exception { | 153 public static void removeHandler(String domain) throws Exception { |
146 synchronized(siteMap) { | 154 synchronized(siteMap) { |
147 Site site = siteMap.remove(domain); | 155 Reference<LuanState> ref = siteMap.remove(domain); |
148 if( site != null ) { | 156 LuanState luan = ref==null ? null : ref.get(); |
149 site.luanHandler.close(); | 157 if( luan != null ) { |
158 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); | |
159 luanHandler.close(); | |
150 } | 160 } |
151 } | 161 } |
152 } | 162 } |
153 | 163 |
154 public static void loadHandler(String domain) { | 164 public static void loadHandler(String domain) { |