Mercurial Hosting > luan
comparison src/luan/modules/http/LuanHandler.java @ 786:fe63c508a177
add Http.reset_luan()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 02 Sep 2016 17:43:29 -0600 |
parents | d69d3c51c44e |
children | ca81307adf7c |
comparison
equal
deleted
inserted
replaced
785:d69d3c51c44e | 786:fe63c508a177 |
---|---|
1 package luan.modules.http; | 1 package luan.modules.http; |
2 | 2 |
3 import java.io.IOException; | 3 import java.io.IOException; |
4 import java.lang.reflect.Method; | |
5 import java.util.concurrent.locks.ReadWriteLock; | |
6 import java.util.concurrent.locks.ReentrantReadWriteLock; | |
4 import javax.servlet.ServletException; | 7 import javax.servlet.ServletException; |
5 import javax.servlet.http.HttpServlet; | 8 import javax.servlet.http.HttpServlet; |
6 import javax.servlet.http.HttpServletRequest; | 9 import javax.servlet.http.HttpServletRequest; |
7 import javax.servlet.http.HttpServletResponse; | 10 import javax.servlet.http.HttpServletResponse; |
8 import org.slf4j.Logger; | 11 import org.slf4j.Logger; |
9 import org.slf4j.LoggerFactory; | 12 import org.slf4j.LoggerFactory; |
10 import org.eclipse.jetty.server.Request; | 13 import org.eclipse.jetty.server.Request; |
11 import org.eclipse.jetty.server.handler.AbstractHandler; | 14 import org.eclipse.jetty.server.handler.AbstractHandler; |
15 import luan.Luan; | |
12 import luan.LuanState; | 16 import luan.LuanState; |
13 import luan.LuanTable; | 17 import luan.LuanTable; |
14 import luan.LuanFunction; | 18 import luan.LuanFunction; |
19 import luan.LuanJavaFunction; | |
15 import luan.LuanCloner; | 20 import luan.LuanCloner; |
16 import luan.LuanException; | 21 import luan.LuanException; |
17 import luan.modules.PackageLuan; | 22 import luan.modules.PackageLuan; |
18 | 23 |
19 | 24 |
20 public class LuanHandler extends AbstractHandler { | 25 public class LuanHandler extends AbstractHandler { |
21 private final LuanState luan; | 26 private final LuanState luanInit; |
22 private final Logger logger; | 27 private final Logger logger; |
23 private String welcomeFile = "index.html"; | 28 private String welcomeFile = "index.html"; |
29 private final ReadWriteLock lock = new ReentrantReadWriteLock(); | |
30 private LuanState luan; | |
31 | |
32 private static final Method resetLuanMethod; | |
33 static { | |
34 try { | |
35 resetLuanMethod = LuanHandler.class.getMethod("reset_luan"); | |
36 } catch(NoSuchMethodException e) { | |
37 throw new RuntimeException(e); | |
38 } | |
39 } | |
24 | 40 |
25 public LuanHandler(LuanState luan,String loggerRoot) { | 41 public LuanHandler(LuanState luan,String loggerRoot) { |
26 this.luan = luan; | 42 this.luanInit = luan; |
27 if( loggerRoot==null ) | 43 if( loggerRoot==null ) |
28 loggerRoot = ""; | 44 loggerRoot = ""; |
29 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName()); | 45 logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName()); |
46 try { | |
47 LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan"); | |
48 Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) ); | |
49 } catch(LuanException e) { | |
50 throw new RuntimeException(e); | |
51 } | |
30 } | 52 } |
31 | 53 |
32 @Override public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) | 54 @Override public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) |
33 throws IOException | 55 throws IOException |
34 { | 56 { |
35 if( target.endsWith("/") ) | 57 if( target.endsWith("/") ) |
36 target += welcomeFile; | 58 target += welcomeFile; |
59 lock.readLock().lock(); | |
37 try { | 60 try { |
38 if( !HttpServicer.service(luan,request,response,"site:"+target+".luan") ) | 61 if( !HttpServicer.service(luan,request,response,"site:"+target+".luan") ) |
39 return; | 62 return; |
40 } catch(LuanException e) { | 63 } catch(LuanException e) { |
41 //e.printStackTrace(); | 64 //e.printStackTrace(); |
42 String err = e.getFullMessage(); | 65 String err = e.getFullMessage(); |
43 logger.error(err); | 66 logger.error(err); |
44 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,err); | 67 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,err); |
68 } finally { | |
69 lock.readLock().unlock(); | |
45 } | 70 } |
46 baseRequest.setHandled(true); | 71 baseRequest.setHandled(true); |
47 } | 72 } |
48 | 73 |
49 public void setWelcomeFile(String welcomeFile) { | 74 public void setWelcomeFile(String welcomeFile) { |
50 this.welcomeFile = welcomeFile; | 75 this.welcomeFile = welcomeFile; |
76 } | |
77 | |
78 @Override protected void doStart() throws Exception { | |
79 // Thread.dumpStack(); | |
80 //System.out.println("qqqqqqqqqqqqqqqqqqqq doStart "+this); | |
81 setLuan(); | |
82 super.doStart(); | |
51 } | 83 } |
52 | 84 |
53 @Override protected void doStop() throws Exception { | 85 @Override protected void doStop() throws Exception { |
54 synchronized(luan) { | 86 synchronized(luan) { |
55 luan.close(); | 87 luan.close(); |
63 super.destroy(); | 95 super.destroy(); |
64 } | 96 } |
65 */ | 97 */ |
66 | 98 |
67 public Object call_rpc(String fnName,Object... args) throws LuanException { | 99 public Object call_rpc(String fnName,Object... args) throws LuanException { |
68 return callRpc(luan,fnName,args); | 100 LuanState luan = this.luan; |
69 } | |
70 | |
71 public static Object callRpc(LuanState luan,String fnName,Object... args) throws LuanException { | |
72 synchronized(luan) { | 101 synchronized(luan) { |
73 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | 102 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); |
74 luan = (LuanState)cloner.clone(luan); | 103 luan = (LuanState)cloner.clone(luan); |
75 } | 104 } |
76 LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan"); | 105 LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan"); |
79 if( fn == null ) | 108 if( fn == null ) |
80 throw new LuanException( "function not found: " + fnName ); | 109 throw new LuanException( "function not found: " + fnName ); |
81 return fn.call(luan,args); | 110 return fn.call(luan,args); |
82 } | 111 } |
83 | 112 |
113 public void reset_luan() { | |
114 new Thread() { | |
115 public void run() { | |
116 lock.writeLock().lock(); | |
117 try { | |
118 synchronized(luan) { | |
119 luan.close(); | |
120 setLuan(); | |
121 } | |
122 } catch(IOException e) { | |
123 logger.error("reset_luan failed",e); | |
124 } finally { | |
125 lock.writeLock().unlock(); | |
126 } | |
127 } | |
128 }.start(); | |
129 } | |
130 | |
131 private void setLuan() { | |
132 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | |
133 luan = (LuanState)cloner.clone(luanInit); | |
134 try { | |
135 PackageLuan.load(luan,"site:/init.luan"); | |
136 } catch(LuanException e) { | |
137 String err = e.getFullMessage(); | |
138 logger.error(err); | |
139 } | |
140 } | |
141 | |
142 public Object runLuan(String sourceText,String sourceName) throws LuanException { | |
143 LuanFunction fn = Luan.load(sourceText,sourceName); | |
144 synchronized(luan) { | |
145 return fn.call(luan); | |
146 } | |
147 } | |
148 | |
84 } | 149 } |