You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ov...@apache.org on 2001/12/17 08:02:37 UTC
cvs commit: xml-cocoon2/scratchpad/schecoon/src/org/apache/cocoon/scheme/servlet REPLGenericServlet.java
ovidiu 01/12/16 23:02:37
Modified: scratchpad/schecoon/src/org/apache/cocoon/scheme/servlet
REPLGenericServlet.java
Log:
Use a pool of interpreters to handle multi-threading. This is based on
the new SISC model of one interpreter per thread.
Revision Changes Path
1.3 +67 -50 xml-cocoon2/scratchpad/schecoon/src/org/apache/cocoon/scheme/servlet/REPLGenericServlet.java
Index: REPLGenericServlet.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/scratchpad/schecoon/src/org/apache/cocoon/scheme/servlet/REPLGenericServlet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- REPLGenericServlet.java 2001/12/12 17:34:00 1.2
+++ REPLGenericServlet.java 2001/12/17 07:02:37 1.3
@@ -8,6 +8,8 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
+import java.lang.NumberFormatException;
+import java.util.Stack;
import java.util.zip.GZIPInputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
@@ -15,12 +17,17 @@
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
+import sisc.AppContext;
import sisc.ContinuationException;
+import sisc.DynamicEnv;
import sisc.Interpreter;
+import sisc.data.Expression;
import sisc.data.InputPort;
import sisc.data.OutputPort;
import sisc.data.Symbol;
import sisc.data.Value;
+import sisc.exprs.AppExp;
+import sisc.exprs.FreeReferenceExp;
import sisc.modules.J2S;
/**
@@ -32,66 +39,76 @@
*/
public class REPLGenericServlet extends HttpServlet
{
- protected Interpreter interpreter;
+ static final String appCtxAttrName = "sisc scheme interpreter context";
protected ServletContext servletContext;
protected String initExpression;
protected String destroyExpression;
- protected String mainExpression;
+ protected Symbol mainFunction;
+ protected Stack interPool;
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
servletContext = config.getServletContext();
+ interPool = new Stack();
+ Interpreter interp;
+
synchronized (servletContext) {
- String attributeName = "sisc scheme interpreter";
+ AppContext ctx = (AppContext)servletContext.getAttribute(appCtxAttrName);
+
+ if (ctx == null) {
+ ctx = new AppContext();
+ servletContext.setAttribute(appCtxAttrName, ctx);
+
+ interp = getInterpreter();
+ // Read the heap file
+ String realPath = servletContext.getRealPath("/");
+ String heapFileName = realPath + config.getInitParameter("heap");
+ System.out.println("loading heap " + heapFileName);
+ File heapFile = new File(heapFileName);
+
+ try {
+ FileInputStream fis = new FileInputStream(heapFileName);
+ BufferedInputStream bis
+ = new BufferedInputStream(fis, (int)heapFile.length());
+ GZIPInputStream gzis = new GZIPInputStream(bis);
+ DataInputStream dis
+ = new DataInputStream(new BufferedInputStream(gzis));
+ ctx.loadEnv(interp, dis);
+
+ } catch (IOException ex) {
+ System.err.println("Error loading heap:" + ex);
+ ex.printStackTrace();
+ throw new ServletException(ex);
+ }
- interpreter = (Interpreter)servletContext.getAttribute(attributeName);
- if (interpreter == null) {
- interpreter = new Interpreter(System.in, System.out);
- servletContext.setAttribute(attributeName, interpreter);
+ ctx.setEvaluator("eval");
+ }
+ else {
+ interp = getInterpreter();
}
}
- interpreter.setEvaluator("eval");
- loadHeap(config.getInitParameter("sisc-heap"));
- loadHeap(config.getInitParameter("schecoon-heap"));
-
initExpression = config.getInitParameter("init-expression");
destroyExpression = config.getInitParameter("destroy-expression");
- mainExpression = config.getInitParameter("main-expression");
+ String main = config.getInitParameter("main-function");
+ if (main != null)
+ mainFunction = Symbol.get(main);
+// = new FreeReferenceExp(Symbol.get(main), -1, interp.ctx.toplevel_env);
// Evaluate the init expression, discard the returned value and
// any exception thrown
try {
if (initExpression != null && !initExpression.equals(""))
- interpreter.eval(initExpression);
+ interp.eval(initExpression);
}
catch (Exception ex) {
System.out.println("Exception evaluating the init expression: " + ex);
}
- }
-
- protected void loadHeap(String relativePath)
- {
- String realPath = servletContext.getRealPath("/");
- String heapFileName = realPath + relativePath;
- System.out.println("loading heap " + heapFileName);
- File heapFile = new File(heapFileName);
- try {
- FileInputStream fis = new FileInputStream(heapFileName);
- BufferedInputStream bis
- = new BufferedInputStream(fis, (int)heapFile.length());
- GZIPInputStream gzis = new GZIPInputStream(bis);
- DataInputStream dis = new DataInputStream(new BufferedInputStream(gzis));
- interpreter.loadEnv(dis);
-
- } catch (IOException e) {
- System.err.println("Error loading heap!");
- e.printStackTrace();
- }
+ releaseInterpreter(interp);
}
public void destroy()
@@ -100,31 +117,31 @@
// any exception thrown
try {
if (destroyExpression != null && !destroyExpression.equals(""))
- interpreter.eval(destroyExpression);
+ ((Interpreter)interPool.pop()).eval(destroyExpression);
}
catch (Exception ex) {
System.out.println("Exception evaluating the destroy expression: " + ex);
}
}
- public Value eval(String expression,
- ServletRequest request,
- ServletResponse response)
+ public Interpreter getInterpreter()
{
- try {
- interpreter.define(Symbol.get("*http-request*"),
- new J2S.JavaObject(request),
- sisc.Util.TOPLEVEL);
- interpreter.define(Symbol.get("*http-response*"),
- new J2S.JavaObject(response),
- sisc.Util.TOPLEVEL);
- Value value = interpreter.eval(expression);
- return value;
+ synchronized(interPool) {
+ if (!interPool.empty())
+ return (Interpreter)interPool.pop();
+
+ // Create a new interpreter and return it
+ AppContext ctx = (AppContext)servletContext.getAttribute(appCtxAttrName);
+ DynamicEnv environment = new DynamicEnv(System.in, System.out);
+ Interpreter interp = new Interpreter(ctx, environment);
+ return interp;
}
- catch (Exception ex) {
- System.out.println("evaluation error: " + ex);
- ex.printStackTrace();
- return null;
+ }
+
+ public void releaseInterpreter(Interpreter interp)
+ {
+ synchronized(interPool) {
+ interPool.push(interp);
}
}
}
----------------------------------------------------------------------
In case of troubles, e-mail: webmaster@xml.apache.org
To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org