Java Code Examples for org.apache.bsf.BSFException#REASON_EXECUTION_ERROR

The following examples show how to use org.apache.bsf.BSFException#REASON_EXECUTION_ERROR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CachingGroovyEngine.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate an expression.
 */
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
    try {
        Class scriptClass = evalScripts.get(script);
        if (scriptClass == null) {
            scriptClass = loader.parseClass(script.toString(), source);
            evalScripts.put(script, scriptClass);
        } else {
            LOG.fine("eval() - Using cached script...");
        }
        //can't cache the script because the context may be different.
        //but don't bother loading parsing the class again
        Script s = InvokerHelper.createScript(scriptClass, context);
        return s.run();
    } catch (Exception e) {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
    }
}
 
Example 2
Source File: CachingGroovyEngine.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Execute a script.
 */
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException {
    try {
        //          shell.run(script.toString(), source, EMPTY_ARGS);

        Class scriptClass = execScripts.get(script);
        if (scriptClass == null) {
            scriptClass = loader.parseClass(script.toString(), source);
            execScripts.put(script, scriptClass);
        } else {
            LOG.fine("exec() - Using cached version of class...");
        }
        InvokerHelper.invokeMethod(scriptClass, "main", EMPTY_ARGS);
    } catch (Exception e) {
        LOG.log(Level.WARNING, "BSF trace", e);
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
    }
}
 
Example 3
Source File: JaclEngine.java    From commons-bsf with Apache License 2.0 6 votes vote down vote up
/**
 * This is used by an application to evaluate a string containing
 * some expression.
 */
public Object eval (String source, int lineNo, int columnNo, 
            Object oscript) throws BSFException {
  String script = oscript.toString ();
  try {
    interp.eval (script);
    TclObject result = interp.getResult();
    Object internalRep = result.getInternalRep();

    // if the object has a corresponding Java type, unwrap it
    if (internalRep instanceof ReflectObject)
      return ReflectObject.get(interp,result);
    if (internalRep instanceof TclString)
      return result.toString();
    if (internalRep instanceof TclDouble)
      return new Double(TclDouble.get(interp,result));
    if (internalRep instanceof TclInteger)
      return new Integer(TclInteger.get(interp,result));

    return result;
  } catch (TclException e) { 
    throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
                "error while eval'ing Jacl expression: " + 
                interp.getResult (), e);
  }
}
 
Example 4
Source File: JythonEngine.java    From commons-bsf with Apache License 2.0 6 votes vote down vote up
/**
 * Execute script code, emulating console interaction.
 */
public void iexec (String source, int lineNo, int columnNo,
                   Object script) throws BSFException {
    String scriptStr = byteify(script.toString());
    importPackage(scriptStr);
    int newline = scriptStr.indexOf("\n");

    if (newline > -1)
        scriptStr = scriptStr.substring(0, newline);

    try {
        if (interp.buffer.length() > 0)
            interp.buffer.append("\n");
        interp.buffer.append(scriptStr);
        if (!(interp.runsource(interp.buffer.toString())))
            interp.resetbuffer();
    } catch (PyException e) {
        interp.resetbuffer();
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR, 
                               "exception from Jython:\n" + e, e);
    }
}
 
Example 5
Source File: JEXLEngine.java    From commons-bsf with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluates the expression as a JEXL Script.
 *
 * @param fileName The file name, if it is available.
 * @param lineNo The line number, if it is available.
 * @param colNo The column number, if it is available.
 * @param expr The expression to be evaluated.
 *
 * @throws BSFException For any exception that occurs while
 *                      evaluating the expression.
 */
public Object eval(String fileName, int lineNo, int colNo, Object expr)
        throws BSFException {
    if (expr == null) {
        return null;
    }
    try {
        Script jExpr = null;
        if (expr instanceof File) {
            jExpr = ScriptFactory.createScript((File) expr);
        } else if (expr instanceof URL) {
            jExpr = ScriptFactory.createScript((URL) expr);
        } else {
            jExpr = ScriptFactory.createScript((String) expr);
        }
        return jExpr.execute(jc);
    } catch (Exception e) {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
            "Exception from Commons JEXL:\n" + e.getMessage(), e);
    }
}
 
Example 6
Source File: JEXLEngine.java    From commons-bsf with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the script as a JEXL {@link Script}.
 *
 * @param fileName The file name, if it is available.
 * @param lineNo The line number, if it is available.
 * @param colNo The column number, if it is available.
 * @param script The script to be executed.
 *
 * @throws BSFException For any exception that occurs while
 *                      executing the script.
 */
public void exec(String fileName, int lineNo, int colNo, Object script)
        throws BSFException {
    if (script == null) {
        return;
    }
    try {
        Script jExpr = null;
        if (script instanceof File) {
            jExpr = ScriptFactory.createScript((File) script);
        } else if (script instanceof URL) {
            jExpr = ScriptFactory.createScript((URL) script);
        } else {
            jExpr = ScriptFactory.createScript((String) script);
        }
        jExpr.execute(jc);
    } catch (Exception e) {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
            "Exception from Commons JEXL:\n" + e.getMessage(), e);
    }
}
 
Example 7
Source File: BeanShellBSFEngine.java    From beanshell with Apache License 2.0 5 votes vote down vote up
public Object eval (
    String source, int lineNo, int columnNo, Object expr)
    throws BSFException
{
    if ( ! (expr instanceof String) )
        throw new BSFException("BeanShell expression must be a string");

    try {
        return interpreter.eval( ((String)expr) );
    } catch ( InterpreterError e )
    {
        throw new BSFException(BSFException.REASON_UNKNOWN_LANGUAGE,
            "BeanShell interpreter internal error: "+e
            + sourceInfo(source,lineNo,columnNo), e);
    } catch ( TargetError e2 )
    {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
            "The application script threw an exception: "
            + e2.getTarget()
            + sourceInfo(source,lineNo,columnNo), e2);
    } catch ( EvalError e3 )
    {
        throw new BSFException(BSFException.REASON_OTHER_ERROR,
            "BeanShell script error: "+e3
            + sourceInfo(source,lineNo,columnNo), e3);
    }
}
 
Example 8
Source File: GroovyEngine.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate an expression.
 */
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
    try {
        source = convertToValidJavaClassname(source);
        return getEvalShell().evaluate(script.toString(), source);
    } catch (Exception e) {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
    }
}
 
Example 9
Source File: GroovyEngine.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Execute a script.
 */
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException {
    try {
        // use evaluate to pass in the BSF variables
        source = convertToValidJavaClassname(source);
        getEvalShell().evaluate(script.toString(), source);
    } catch (Exception e) {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
    }
}
 
Example 10
Source File: JythonEngine.java    From commons-bsf with Apache License 2.0 5 votes vote down vote up
/**
 * call the named method of the given object.
 */
public Object call (Object object, String method, Object[] args) 
    throws BSFException {
    try {
        PyObject[] pyargs = Py.EmptyObjects;

        if (args != null) {
            pyargs = new PyObject[args.length];
            for (int i = 0; i < pyargs.length; i++)
                pyargs[i] = Py.java2py(args[i]);
        }

        if (object != null) {
            PyObject o = Py.java2py(object);
            return unwrap(o.invoke(method, pyargs));
        }

        PyObject m = interp.get(method);

        if (m == null)
            m = interp.eval(method);
        if (m != null) {
            return unwrap(m.__call__(pyargs));
        }

        return null;
    } catch (PyException e) {
        throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
                                "exception from Jython:\n" + e, e);
    }
}
 
Example 11
Source File: JythonEngine.java    From commons-bsf with Apache License 2.0 5 votes vote down vote up
/**
 * 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(byteify(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++;
        }

        String scriptStr = script.toString ();
        importPackage(scriptStr);
        interp.exec (scriptStr);
        
        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:\n" + e, e);
    }
}
 
Example 12
Source File: JythonEngine.java    From commons-bsf with Apache License 2.0 5 votes vote down vote up
/**
  * Evaluate an expression.
  */
 public Object eval (String source, int lineNo, int columnNo, 
	      Object script) throws BSFException {
try {
  String scriptStr = byteify(script.toString ());
  importPackage(scriptStr);
  Object result = interp.eval (scriptStr);
  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:\n" + e, e);
}
 }
 
Example 13
Source File: JythonEngine.java    From commons-bsf with Apache License 2.0 5 votes vote down vote up
/**
  * Execute a script. 
  */
 public void exec (String source, int lineNo, int columnNo,
	    Object script) throws BSFException {
try {
  String scriptStr = byteify(script.toString());
  importPackage(scriptStr);
  interp.exec (scriptStr);
} catch (PyException e) {
  throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
		      "exception from Jython:\n" + e, e);
}
 }
 
Example 14
Source File: JEXLEngine.java    From commons-bsf with Apache License 2.0 5 votes vote down vote up
/**
 * Uses reflection to make the call.
 *
 * @param object The object to make the call on.
 * @param name The call to make.
 * @param args The arguments to pass.
 *
 * @return The result of the call.
 *
 * @throws BSFException For any exception that occurs while making
 *                      the call.
 */
public Object call(Object object, String name, Object[] args)
        throws BSFException {
    try {
        Class[] types = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
            types[i] = args[i].getClass();
        }
        Method m = object.getClass().getMethod(name, types);
        return m.invoke(object, args);
    } catch (Exception e) {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
            "Exception from JEXLEngine:\n" + e.getMessage(), e);
    }
}
 
Example 15
Source File: BeanShellBSFEngine.java    From beanshell with Apache License 2.0 4 votes vote down vote up
/**
    This is an implementation of the BSF apply() method.
    It exectutes the funcBody text in an "anonymous" method call with
    arguments.
*/
/*
    Note: the apply() method may be supported directly in BeanShell in an
    upcoming release and would not require special support here.
*/
public Object apply (
    String source, int lineNo, int columnNo, Object funcBody,
    Vector namesVec, Vector argsVec )
   throws BSFException
{
    if ( namesVec.size() != argsVec.size() )
        throw new BSFException("number of params/names mismatch");
    if ( !(funcBody instanceof String) )
        throw new BSFException("apply: functino body must be a string");

    String [] names = new String [ namesVec.size() ];
    namesVec.copyInto(names);
    Object [] args = new Object [ argsVec.size() ];
    argsVec.copyInto(args);

    try
    {
        if ( !installedApplyMethod )
        {
            interpreter.eval( bsfApplyMethod );
            installedApplyMethod = true;
        }

        bsh.This global = (bsh.This)interpreter.get("global");
        Object value = global.invokeMethod(
            "_bsfApply", new Object [] { names, args, (String)funcBody } );
        return Primitive.unwrap( value );

    } catch ( InterpreterError e )
    {
        throw new BSFException(BSFException.REASON_UNKNOWN_LANGUAGE,
            "BeanShell interpreter internal error: "+e
            + sourceInfo(source,lineNo,columnNo), e);
    } catch ( TargetError e2 )
    {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
            "The application script threw an exception: "
            + e2.getTarget()
            + sourceInfo(source,lineNo,columnNo), e2);
    } catch ( EvalError e3 )
    {
        throw new BSFException(BSFException.REASON_OTHER_ERROR,
            "BeanShell script error: "+e3
            + sourceInfo(source,lineNo,columnNo), e3);
    }
}