chuck       2003/03/05 04:13:24

  Modified:    src/bsf-2.3/bsf/src/org/apache/bsf BSFEngine.java
                        BSFManager.java
               src/bsf-2.3/bsf/src/org/apache/bsf/engines/jython
                        JythonEngine.java
               src/bsf-2.3/bsf/src/org/apache/bsf/util BSFEngineImpl.java
  Log:
  APR extension by Victor Orlikowski to handle use of interactive language
  shells with BSF
  
  Revision  Changes    Path
  1.3       +19 -1     jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/BSFEngine.java
  
  Index: BSFEngine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/BSFEngine.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -d -b -u -r1.2 -r1.3
  --- BSFEngine.java    7 Nov 2002 01:49:10 -0000       1.2
  +++ BSFEngine.java    5 Mar 2003 12:13:24 -0000       1.3
  @@ -264,6 +264,24 @@
         */
        public void exec(String source, int lineNo, int columnNo, Object script)
                throws BSFException;
  +    /**
  +     * This is used by an application to execute some script, as though
  +     * one were interacting with the language in an interactive session. 
  +     * The expression may be string or some other type, depending on the
  +     * language. Returns nothing but if something goes wrong it excepts (of
  +     * course).
  +     *
  +     * @param source   (context info) the source of this expression
  +     *                 (e.g., filename)
  +     * @param lineNo   (context info) the line number in source for expr
  +     * @param columnNo (context info) the column number in source for expr
  +     * @param script   the script to execute
  +     *
  +     * @exception BSFException if anything goes wrong while exec'ing a
  +     *            BSFException is thrown. The reason indicates the problem.
  +     */
  +    public void iexec(String source, int lineNo, int columnNo, Object script)
  +        throws BSFException;
                
        /**
         * This method is used to initialize the engine right after construction.
  
  
  
  1.4       +37 -0     jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/BSFManager.java
  
  Index: BSFManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/BSFManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -d -b -u -r1.3 -r1.4
  --- BSFManager.java   11 Nov 2002 23:54:42 -0000      1.3
  +++ BSFManager.java   5 Mar 2003 12:13:24 -0000       1.4
  @@ -490,6 +490,43 @@
       }
   
       /**
  +     * Execute the given script of the given language, attempting to
  +     * emulate an interactive session w/ the language.
  +     *
  +     * @param lang     language identifier
  +     * @param source   (context info) the source of this expression 
  +     *                 (e.g., filename)
  +     * @param lineNo   (context info) the line number in source for expr
  +     * @param columnNo (context info) the column number in source for expr
  +     * @param script   the script to execute
  +     *
  +     * @exception BSFException if anything goes wrong while running the script
  +     */
  +    public void iexec(String lang,
  +                     String source,
  +                     int lineNo,
  +                     int columnNo,
  +                     Object script)
  +        throws BSFException {
  +        final BSFEngine e = loadScriptingEngine(lang);
  +        final String sourcef = source;
  +        final int lineNof = lineNo, columnNof = columnNo;
  +        final Object scriptf = script;
  +
  +        try {
  +            AccessController.doPrivileged(new PrivilegedExceptionAction() {
  +                    public Object run() throws Exception {
  +                        e.iexec(sourcef, lineNof, columnNof, scriptf);
  +                        return null;
  +                    }
  +                });
  +        }
  +        catch (PrivilegedActionException prive) {
  +            throw (BSFException) prive.getException();
  +        }
  +    }
  +
  +    /**
        * Get classLoader
        */
       public ClassLoader getClassLoader() {
  
  
  
  1.4       +79 -3     
jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/engines/jython/JythonEngine.java
  
  Index: JythonEngine.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/engines/jython/JythonEngine.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -d -b -u -r1.3 -r1.4
  --- JythonEngine.java 15 Jan 2003 08:12:20 -0000      1.3
  +++ JythonEngine.java 5 Mar 2003 12:13:24 -0000       1.4
  @@ -74,7 +74,7 @@
    */
   
   public class JythonEngine extends BSFEngineImpl {
  -  PythonInterpreter interp;
  +  BSFPythonInterpreter interp;
   
     /**
      * call the named method of the given object.
  @@ -101,12 +101,50 @@
        }
        return null;
     }
  +
     /**
      * Declare a bean
      */
     public void declareBean (BSFDeclaredBean bean) throws BSFException {
        interp.set (bean.name, bean.bean);
     }
  +
  +  /**
  +   * Evaluate an anonymous function (differs from eval() in that apply() 
  +   * handles multiple lines).
  +   */
  +  public Object apply (String source, int lineNo, int columnNo, 
  +                       Object funcBody, Vector paramNames,
  +                       Vector arguments) throws BSFException {
  +      try {
  +          /* We wrapper the original script in a function definition, and
  +           * evaluate the function. A hack, no question, but it allows
  +           * apply() to pretend to work on Jython.
  +           */
  +          StringBuffer script = new StringBuffer(funcBody.toString());
  +          int index = 0;
  +          script.insert(0, "def bsf_temp_fn():\n");
  +         
  +          while (index < script.length()) {
  +              if (script.charAt(index) == '\n') {
  +                  script.insert(index+1, '\t');
  +              }
  +              index++;
  +          }
  +          
  +          interp.exec (script.toString ());
  +          
  +          Object result = interp.eval ("bsf_temp_fn()");
  +          
  +          if (result != null && result instanceof PyJavaInstance)
  +              result = ((PyJavaInstance)result).__tojava__(Object.class);
  +          return result;
  +      } catch (PyException e) {
  +          throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
  +                                  "exception from Jython: " + e, e);
  +      }
  +  }
  +
     /**
      * Evaluate an expression.
      */
  @@ -122,6 +160,7 @@
                              "exception from Jython: " + e, e);
        }
     }
  +
     /**
      * Execute a script. 
      */
  @@ -134,6 +173,25 @@
                              "exception from Jython: " + e, e);
        }
     }
  +
  +  /**
  +   * Execute script code, emulating console interaction.
  +   */
  +  public void iexec (String source, int lineNo, int columnNo,
  +                     Object script) throws BSFException {
  +      try {
  +          if (interp.buffer.length() > 0)
  +              interp.buffer.append("\n");
  +          interp.buffer.append(script);
  +          if (!(interp.runsource(interp.buffer.toString())))
  +              interp.resetbuffer();
  +      } catch (PyException e) {
  +          interp.resetbuffer();
  +          throw new BSFException(BSFException.REASON_EXECUTION_ERROR, 
  +                                 "exception from Jython: " + e, e);
  +      }
  +  }
  +
     /**
      * Initialize the engine.
      */
  @@ -142,11 +200,12 @@
        super.initialize (mgr, lang, declaredBeans);
   
        // create an interpreter
  -     interp = new PythonInterpreter ();
  +     interp = new BSFPythonInterpreter ();
   
        // register the mgr with object name "bsf"
        interp.set ("bsf", new BSFFunctions (mgr, this));
   
  +    // Declare all declared beans to the interpreter
        int size = declaredBeans.size ();
        for (int i = 0; i < size; i++) {
          declareBean ((BSFDeclaredBean) declaredBeans.elementAt (i));
  @@ -159,6 +218,7 @@
     public void undeclareBean (BSFDeclaredBean bean) throws BSFException {
        interp.set (bean.name, null);
     }
  +
     public Object unwrap(PyObject result) {
        if (result != null) {
           Object ret = result.__tojava__(Object.class);
  @@ -166,5 +226,21 @@
                  return ret;
        }
        return result;
  +  }
  +  
  +  private class BSFPythonInterpreter extends InteractiveInterpreter {
  +
  +      public BSFPythonInterpreter() {
  +          super();
  +      }
  +
  +      // Override runcode so as not to print the stack dump
  +      public void runcode(PyObject code) {
  +          try {
  +              this.exec(code);
  +          } catch (PyException exc) {
  +              throw exc;
  +          }
  +      }
     }
   }
  
  
  
  1.3       +8 -0      
jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/util/BSFEngineImpl.java
  
  Index: BSFEngineImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-bsf/src/bsf-2.3/bsf/src/org/apache/bsf/util/BSFEngineImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -d -b -u -r1.2 -r1.3
  --- BSFEngineImpl.java        7 Nov 2002 01:49:12 -0000       1.2
  +++ BSFEngineImpl.java        5 Mar 2003 12:13:24 -0000       1.3
  @@ -225,6 +225,14 @@
       }
   
       /**
  +     * Default impl of interactive execution - calls eval and ignores the result.
  +     */
  +    public void iexec(String source, int lineNo, int columnNo, Object script)
  +        throws BSFException {
  +        eval(source, lineNo, columnNo, script);
  +    }
  +
  +    /**
        * Get the debug manager in the constructor, not in initialize.
        * First, this is ok since the debug manager is not BSFManager
        * dependent. Second, the debug manager needs to be known
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to