comparison src/luan/host/WebHandler.java @ 1231:0b75337bb91a

better closing
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 05 Apr 2018 16:38:33 -0600
parents 034f2a0b3915
children 857eb648d4e5
comparison
equal deleted inserted replaced
1230:034f2a0b3915 1231:0b75337bb91a
31 public class WebHandler implements Handler { 31 public class WebHandler implements Handler {
32 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); 32 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class);
33 33
34 private static class LuanRef { 34 private static class LuanRef {
35 private final LuanState luan; 35 private final LuanState luan;
36 36 private final Handler handler;
37 private LuanRef(LuanState luan) { 37 private final LuanHandler luanHandler;
38
39 private LuanRef(LuanState luan,Handler handler,LuanHandler luanHandler) {
38 this.luan = luan; 40 this.luan = luan;
41 this.handler = handler;
42 this.luanHandler = luanHandler;
39 } 43 }
40 } 44 }
41 45
42 private static final ReferenceQueue<LuanRef> queue = new ReferenceQueue<LuanRef>(); 46 private static final ReferenceQueue<LuanRef> queue = new ReferenceQueue<LuanRef>();
43 47
44 private static class MyReference extends SoftReference<LuanRef> { 48 private static class MyReference extends SoftReference<LuanRef> {
45 private final LuanState luan; 49 private LuanState luan;
46 50
47 private MyReference(LuanRef lr) { 51 private MyReference(LuanRef lr) {
48 super(lr,queue); 52 super(lr,queue);
49 this.luan = lr.luan; 53 this.luan = lr.luan;
50 } 54 }
55 MyReference ref = (MyReference)queue.poll(); 59 MyReference ref = (MyReference)queue.poll();
56 if( ref == null ) 60 if( ref == null )
57 return; 61 return;
58 logger.info("sweep"); 62 logger.info("sweep");
59 ref.luan.close(); 63 ref.luan.close();
64 ref.luan = null;
60 } 65 }
61 } 66 }
62 67
63 68
64 public static String allowJavaFileName = "allow_java"; // change for security 69 public static String allowJavaFileName = "allow_java"; // change for security
65 private static final Map<String,MyReference> siteMap = new HashMap<String,MyReference>(); 70 private static final Map<String,MyReference> siteMap = new HashMap<String,MyReference>();
66 private static String sitesDir = null; 71 private static String sitesDir = null;
67
68 private static final String HANDLER = "WebHandler.handler";
69 private static final String LUAN_HANDLER = "WebHandler.luanHandler";
70 72
71 public static boolean isServing() { 73 public static boolean isServing() {
72 return sitesDir != null; 74 return sitesDir != null;
73 } 75 }
74 76
86 String domain = i == -1 ? host : host.substring(0,i); 88 String domain = i == -1 ? host : host.substring(0,i);
87 // System.out.println("handle "+domain); 89 // System.out.println("handle "+domain);
88 LuanRef lr = getSite(domain); 90 LuanRef lr = getSite(domain);
89 if( lr == null ) 91 if( lr == null )
90 return null; 92 return null;
91 LuanState luan = lr.luan; 93 return lr.handler.handle(request);
92 Handler handler = (Handler)luan.registry().get(HANDLER);
93 return handler.handle(request);
94 } 94 }
95 95
96 public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException { 96 public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException {
97 LuanRef lr = getSite(domain); 97 LuanRef lr = getSite(domain);
98 LuanState luan = lr.luan; 98 return lr.luanHandler.runLuan(sourceText,sourceName);
99 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER);
100 return luanHandler.runLuan(sourceText,sourceName);
101 } 99 }
102 100
103 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { 101 public static Object callSite(String domain,String fnName,Object... args) throws LuanException {
104 LuanRef lr = getSite(domain); 102 LuanRef lr = getSite(domain);
105 LuanState luan = lr.luan; 103 return lr.luanHandler.call_rpc(fnName,args);
106 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER);
107 return luanHandler.call_rpc(fnName,args);
108 } 104 }
109 105
110 private static LuanRef getSite(String domain) { 106 private static LuanRef getSite(String domain) {
111 synchronized(siteMap) { 107 synchronized(siteMap) {
112 Reference<LuanRef> ref = siteMap.get(domain); 108 Reference<LuanRef> ref = siteMap.get(domain);
117 throw new NullPointerException("sitesDir"); 113 throw new NullPointerException("sitesDir");
118 File dir = new File(sitesDir,domain); 114 File dir = new File(sitesDir,domain);
119 if( !dir.exists() /* && !recover(dir) */ ) 115 if( !dir.exists() /* && !recover(dir) */ )
120 return null; 116 return null;
121 sweep(); 117 sweep();
122 LuanState luan = newSite(dir.toString(),domain); 118 lr = newSite(dir.toString(),domain);
123 lr = new LuanRef(luan);
124 siteMap.put(domain,new MyReference(lr)); 119 siteMap.put(domain,new MyReference(lr));
125 } 120 }
126 return lr; 121 return lr;
127 } 122 }
128 } 123 }
163 IoLuan.setSecurity( luan, ioSecurity(dir) ); 158 IoLuan.setSecurity( luan, ioSecurity(dir) );
164 } 159 }
165 return init; 160 return init;
166 } 161 }
167 162
168 private static LuanState newSite(String dir,String domain) { 163 private static LuanRef newSite(String dir,String domain) {
169 LuanState luan = new LuanState(); 164 LuanState luan = new LuanState();
170 LuanTable init = initLuan(luan,dir,domain); 165 LuanTable init = initLuan(luan,dir,domain);
171 166
172 String loggerRoot = (String)init.rawGet("logger_root"); 167 String loggerRoot = (String)init.rawGet("logger_root");
173 LuanHandler luanHandler = new LuanHandler(luan,loggerRoot); 168 LuanHandler luanHandler = new LuanHandler(luan,loggerRoot);
178 handler = new ListHandler( handler, notFoundHandler ); 173 handler = new ListHandler( handler, notFoundHandler );
179 174
180 String logDir = dir + "/site/private/local/logs/web"; 175 String logDir = dir + "/site/private/local/logs/web";
181 new File(logDir).mkdirs(); 176 new File(logDir).mkdirs();
182 177
183 luan.registry().put(HANDLER,handler); 178 return new LuanRef(luan,handler,luanHandler);
184 luan.registry().put(LUAN_HANDLER,luanHandler);
185
186 return luan;
187 } 179 }
188 180
189 public static void removeHandler(String domain) throws Exception { 181 public static void removeHandler(String domain) throws Exception {
190 synchronized(siteMap) { 182 synchronized(siteMap) {
191 Reference<LuanRef> ref = siteMap.remove(domain); 183 Reference<LuanRef> ref = siteMap.remove(domain);
192 LuanRef lr = ref==null ? null : ref.get(); 184 LuanRef lr = ref==null ? null : ref.get();
193 if( lr != null ) { 185 if( lr != null ) {
194 LuanState luan = lr.luan; 186 lr.luan.close();
195 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER);
196 luanHandler.close();
197 } 187 }
198 } 188 }
199 } 189 }
200 190
201 public static void loadHandler(String domain) { 191 public static void loadHandler(String domain) {