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 2002/03/13 20:10:35 UTC

cvs commit: xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/components/flow JavaScriptInterpreter.java

ovidiu      02/03/13 11:10:35

  Added:       src/scratchpad/schecoon/src/org/apache/cocoon/components/flow
                        JavaScriptInterpreter.java
  Log:
  Support for JavaScript with continuations as a flow language.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/components/flow/JavaScriptInterpreter.java
  
  Index: JavaScriptInterpreter.java
  ===================================================================
  package org.apache.cocoon.components.flow;
  
  import java.io.Reader;
  import java.util.HashMap;
  import java.util.Map;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.logger.AbstractLoggable;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import org.apache.cocoon.components.treeprocessor.InvokeContext;
  import org.apache.cocoon.environment.Environment;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.environment.Response;
  import org.apache.cocoon.environment.http.HttpEnvironment;
  import org.mozilla.javascript.Scriptable;
  
  public class JavaScriptInterpreter extends AbstractLoggable
    implements Interpreter, Initializable, Composable, ThreadSafe
  {
    Scriptable scope;
    Environment environment;
    InvokeContext invokeContext;
    ComponentManager manager;
  
    public void initialize()
      throws Exception
    {
      System.out.println("entering initialize: " + this);
      
      org.mozilla.javascript.Context context
        = org.mozilla.javascript.Context.enter();
      context.setCompileFunctionsWithDynamicScope(true);
  
      try {
        scope = context.initStandardObjects(null);
      } catch (Exception e) {
        context.exit();
        throw e;
      }
  
      System.out.println("exiting initialize: this " + this
                         + ", scope = " + scope);
    }
  
    public void compose(ComponentManager manager)
    {
      this.manager = manager;
    }
  
    public void readScript(Reader in)
      throws Exception
    {
    }
  
    public void callFunction(String funName, Parameters params,
                             Environment env, InvokeContext ctx)
      throws Exception
    {
      // Remember the environment and the context for later, when the
      // script will call the pipeline processing function.
      this.environment = env;
      this.invokeContext = ctx;
      
      org.mozilla.javascript.Context context
        = org.mozilla.javascript.Context.enter();
      context.setCompileFunctionsWithDynamicScope(true);
      Scriptable threadScope = context.newObject(scope);
  
      try {
        threadScope.setPrototype(scope);
        // We want 'threadScope' to be a new top-level scope, so set its
        // parent scope to null. This means that any variables created
        // by assignments will be properties of "threadScope".
        threadScope.setParentScope(null);
  
        // Put in the thread scope the environment, the request, the
        // response, and the interpreter objects.
        Map objectModel = environment.getObjectModel();
        Request request
          = (Request)objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
        Response response
          = (Response)objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
  
        threadScope.put("environment", threadScope, environment);
        threadScope.put("request", threadScope, request);
        threadScope.put("response", threadScope, response);
        threadScope.put("interpreter", threadScope, this);
  
        String funCall = funName + "();";
  
        context.evaluateString(threadScope, funCall,
                               "org.apache.cocoon.flow.JavaScriptInterpreter",
                               1, null);
      }
      finally {
        // Remove the environment, request, response and interpreter
        // objects from the JavaScript thread scope so they can be
        // garbage collected.
        threadScope.delete("environment");
        threadScope.delete("request");
        threadScope.delete("response");
        threadScope.delete("interpreter");
  
        // At the end of the function processing, reset the environment
        // and invoking context.
        this.environment = null;
        this.invokeContext = null;
  
        context.exit();
      }
    }
  
    public void processPipeline(String name, HashMap pipelineArgs, Object bizData)
      throws Exception
    {
    }
  }
  
  
  
  

----------------------------------------------------------------------
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