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

The following examples show how to use org.apache.bsf.BSFException#REASON_OTHER_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: BeanShellBSFEngine.java    From beanshell with Apache License 2.0 6 votes vote down vote up
public void initialize ( BSFManager mgr, String lang, Vector declaredBeans)
throws BSFException
{
    super.initialize( mgr, lang, declaredBeans );

    interpreter = new Interpreter();

    // declare the bsf manager for callbacks, etc.
    try {
        interpreter.set( "bsf", mgr );
    } catch ( EvalError e ) {
        throw new BSFException(BSFException.REASON_OTHER_ERROR, "bsh internal error: "+e, e);
    }

    for(int i=0; i<declaredBeans.size(); i++)
    {
        BSFDeclaredBean bean = (BSFDeclaredBean)declaredBeans.get(i);
        declareBean( bean );
    }
}
 
Example 2
Source File: JaclEngine.java    From commons-bsf with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize the engine.
 */
public void initialize (BSFManager mgr, String lang,
            Vector declaredBeans) throws BSFException {
  super.initialize (mgr, lang, declaredBeans);

  // create interpreter
  interp = new Interp();

  // register the extension that user's can use to get at objects
  // registered by the app
  interp.createCommand ("bsf", new BSFCommand (mgr, this));

  // Make java functions be available to Jacl
      try {
      interp.eval("jaclloadjava");
  } catch (TclException e) {
      throw new BSFException (BSFException.REASON_OTHER_ERROR,
                  "error while loading java package: " +
                  interp.getResult (), e);
  }

  int size = declaredBeans.size ();
  for (int i = 0; i < size; i++) {
    declareBean ((BSFDeclaredBean) declaredBeans.elementAt (i));
  }
}
 
Example 3
Source File: JavaClassEngine.java    From commons-bsf with Apache License 2.0 6 votes vote down vote up
/**
 * call the named method of the given object. If object is an instance
 * of Class, then the call is a static call on that object. If not, its
 * an instance method call or a static call (as per Java) on the given 
 * object.
 */
public Object call (Object object, String method, Object[] args) 
                                                      throws BSFException {
  // determine arg types
  Class[] argTypes = null;
  if (args != null) {
    argTypes = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
  argTypes[i] = (args[i] != null) ? args[i].getClass () : null;
    }
  }

  // now find method with the right signature, call it and return result
  try {
    Method m = MethodUtils.getMethod (object, method, argTypes);
    return m.invoke (object, args);
  } catch (Exception e) {
    // something went wrong while invoking method
    Throwable t = (e instanceof InvocationTargetException) ?
              ((InvocationTargetException)e).getTargetException () :
              null;
    throw new BSFException (BSFException.REASON_OTHER_ERROR,
                "method invocation failed: " + e +
                ((t==null)?"":(" target exception: "+t)), t);
  }
}
 
Example 4
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 5
Source File: BeanShellBSFEngine.java    From beanshell with Apache License 2.0 5 votes vote down vote up
public void declareBean (BSFDeclaredBean bean)
    throws BSFException
{
    try {
        interpreter.set( bean.name, bean.bean);
    } catch ( EvalError e ) {
        throw new BSFException(BSFException.REASON_OTHER_ERROR, "error declaring bean: "+bean.name
        +" : "+e, e);
    }
}
 
Example 6
Source File: BeanShellBSFEngine.java    From beanshell with Apache License 2.0 5 votes vote down vote up
public void undeclareBean (BSFDeclaredBean bean)
    throws BSFException
{
    try {
        interpreter.unset( bean.name );
    } catch ( EvalError e ) {
        throw new BSFException(BSFException.REASON_OTHER_ERROR, "bsh internal error: "+e, e);
    }
}
 
Example 7
Source File: EngineUtils.java    From commons-bsf with Apache License 2.0 5 votes vote down vote up
/**
 * Add a script as a listener to some event coming out of an object. The
 * first two args identify the src of the event and the event set
 * and the rest identify the script which should be run when the event
 * fires. The processing will use the engine's apply() method.
 *
 * @param bean         event source
 * @param eventSetName name of event set from event src to bind to
 * @param filter       filter for events
 * @param engine       BSFEngine which can run this script
 * @param manager      BSFManager of the above engine
 * @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 when the event occurs
 * @param dataFromScriptingEngine
 *                     this contains any object supplied by the scripting engine and gets sent
 *                     back with the supplied script, if the event occurs.
 *                     This could be used e.g. for indicating to the scripting engine which
 *                     scripting engine object/routine/function/procedure
 *                     should be ultimately informed of the event occurrence.
 *
 * @exception BSFException if anything goes wrong while running the script
 */
public static void addEventListenerReturningEventInfos ( Object bean,
                           String eventSetName,
                           String filter,
                           BSFEngine engine,
                           BSFManager manager,
                           String source,
                           int lineNo,
                           int columnNo,
                           Object script,
                           Object dataFromScriptingEngine
                           ) throws BSFException
{
    BSFEventProcessorReturningEventInfos ep =
    new BSFEventProcessorReturningEventInfos (engine,
                                              manager,
                                              filter,
                                              source,
                                              lineNo,
                                              columnNo,
                                              script,
                                              dataFromScriptingEngine
                                              );

    try {
        ReflectionUtils.addEventListener (bean, eventSetName, ep);
    } catch (Exception e) {
        e.printStackTrace ();
        throw new BSFException (BSFException.REASON_OTHER_ERROR,
                                "[EngineUtils.addEventListenerReturningEventInfos()] ouch while adding event listener: "
                                + e, e);
    }
}
 
Example 8
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);
    }
}
 
Example 9
Source File: JavaScriptEngine.java    From commons-bsf with Apache License 2.0 4 votes vote down vote up
private void handleError(Throwable t) throws BSFException {
    if (t instanceof WrappedException)
        t = ((WrappedException) t).getWrappedException();

    String message = null;
    Throwable target = t;

    if (t instanceof JavaScriptException) {
        message = t.getLocalizedMessage();

        // Is it an exception wrapped in a JavaScriptException?
        Object value = ((JavaScriptException) t).getValue();
        if (value instanceof Throwable) {
            // likely a wrapped exception from a LiveConnect call.
            // Display its stack trace as a diagnostic
            target = (Throwable) value;
        }
    }
    else if (t instanceof EvaluatorException ||
             t instanceof SecurityException) {
        message = t.getLocalizedMessage();
    }
    else if (t instanceof RuntimeException) {
        message = "Internal Error: " + t.toString();
    }
    else if (t instanceof StackOverflowError) {
        message = "Stack Overflow";
    }

    if (message == null)
        message = t.toString();

    if (t instanceof Error && !(t instanceof StackOverflowError)) {
        // Re-throw Errors because we're supposed to let the JVM see it
        // Don't re-throw StackOverflows, because we know we've
        // corrected the situation by aborting the loop and
        // a long stacktrace would end up on the user's console
        throw (Error) t;
    }
    else {
        throw new BSFException(BSFException.REASON_OTHER_ERROR,
                               "JavaScript Error: " + message,
                               target);
    }
}
 
Example 10
Source File: EngineUtils.java    From commons-bsf with Apache License 2.0 4 votes vote down vote up
/**
 * Add a script as a listener to some event coming out of an object. The
 * first two args identify the src of the event and the event set
 * and the rest identify the script which should be run when the event
 * fires.
 *
 * @param bean         event source
 * @param eventSetName name of event set from event src to bind to
 * @param filter       filter for events
 * @param engine       BSFEngine which can run this script
 * @param manager      BSFManager of the above engine
 * @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 when the event occurs
 *
 * @exception BSFException if anything goes wrong while running the script
 */
public static void addEventListener (Object bean, String eventSetName,
                                     String filter, BSFEngine engine,
                                     BSFManager manager, String source,
                                     int lineNo, int columnNo,
                                     Object script) throws BSFException {
    BSFEventProcessor ep = new BSFEventProcessor (engine, manager, filter,
                                                  source, lineNo, columnNo,
                                                  script);

    try {
        ReflectionUtils.addEventListener (bean, eventSetName, ep);
    } catch (Exception e) {
        e.printStackTrace ();
        throw new BSFException (BSFException.REASON_OTHER_ERROR,
                                "[EngineUtils.addEventListener()] ouch while adding event listener: "
                                + e, e);
    }
}
 
Example 11
Source File: EngineUtils.java    From commons-bsf with Apache License 2.0 4 votes vote down vote up
/**
 * Loads a class using the following sequence of class loaders:
 * <ul>
 * <li>  Thread's context class loader,
 * <li>  settable class loader stored with BSFManager,
 * <li>  BSFManager's defining class loader,
 * <li>  BSF customized class loader (from the BSFManager's temporary directory).
 *
 * @param mgr  BSFManager who's classLoader and tempDir props are
 *        consulted
 * @param name name of the class to load
 *
 * @return the loaded class
 *
 * @exception BSFException if something goes wrong.
 */
public static Class loadClass (BSFManager mgr, String name)
    throws BSFException {

    ClassLoader mgrCL = null;

    try {
        // TCCL may not be set, adapt logic!
        ClassLoader cl=Thread.currentThread().getContextClassLoader();
        if (cl!=null)
        {
            try {   // try the Thread's context loader first
                    return Thread.currentThread().getContextClassLoader().loadClass(name);
            }
            catch (ClassNotFoundException e01) {
            }
        }

        try {   // try the class loader of the supplied BSFManager ("mgr")
            mgrCL = mgr.getClassLoader ();
            if (mgrCL != null) {
                return mgrCL.loadClass(name);
            }
        }
        catch (ClassNotFoundException e02) {
                // o.k., now try the defined class loader
        }

            // try the class loader stored with the BSF manager
        if (mgrCL != bsfManagerDefinedCL) {
            return bsfManagerDefinedCL.loadClass(name);
        }

    } catch (ClassNotFoundException e) {
        // try to load it from the temp dir using my own class loader
        try {
            if (bsfCL == null)
                bsfCL = new BSFClassLoader ();
            bsfCL.setTempDir (mgr.getTempDir ());
            return bsfCL.loadClass (name);
        } catch (ClassNotFoundException e2) {
            throw new BSFException (BSFException.REASON_OTHER_ERROR,
                    "[EngineUtils.loadClass()] unable to load class '" + name + "':" + e, e);
        }
    }

    throw new BSFException (BSFException.REASON_OTHER_ERROR,
            "[EngineUtils.loadClass()] unable to load class '" + name + "'");
}