Class BSHMethod

java.lang.Object
org.jpos.transaction.participant.BSHMethod

public class BSHMethod extends Object
This is a utility class that makes it a bit easier to work with beanshell scripts. Check out the execute methods.
  • Constructor Summary

    Constructors
    Constructor
    Description
    BSHMethod(String bshData, boolean source)
    Creates a BSHMethod.
  • Method Summary

    Modifier and Type
    Method
    Description
    static BSHMethod
    createBshMethod(org.jdom2.Element e)
    Creates a BSHMethod from a JDom Element.
    execute(Map arguments, String resultName)
    Sets the given arguments in the Interpreter, evaluates the script, and returns the object stored in the variable named resultName.
    execute(Map arguments, Collection returnNames)
    Sets the given arguments in the Interpreter, evaluates the script, and returns a map of named result variables.
    protected bsh.Interpreter
    initInterpreter(Map arguments)
    Initialises a BeanShell Interpreter with the given arguments and evaluates the script.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BSHMethod

      public BSHMethod(String bshData, boolean source)
      Creates a BSHMethod.
      Parameters:
      bshData - - May either be the file to source or the script itself to evaluate.
      source - - If true indicates that the bshData passed is a file to source. Otherwise the string itself is evaluated.
  • Method Details

    • createBshMethod

      public static BSHMethod createBshMethod(org.jdom2.Element e) throws IOException
      Creates a BSHMethod from a JDom Element. The element is not requiered to have a specific name. If the given element defines an attribute named 'file' the file specified as its value will be sourced by the created BSHMethod. If the 'file' attribute is specified, a 'cache' attribute may be specified as well which can take the values true|false and indicates wether to load the script to memory or to read from the file for every script evaluation. If the 'file' attibute is not specified then the text contained by the element is set to be evaluated by the new BSHMethod.
        Example 1 : 
                <prepare>
                        import org.jpos.iso.*;
                      import org.jpos.transaction.*;
      
                              msg = context.get("txnRequest");
                              BaseChannel.getChannel("loop-channel").send(msg);
                              result=TransactionConstants.PREPARED | TransactionConstants.READONLY;
                      </prepare>
      
        Example 2 :
                <routing file='cfg\files\routing1.bsh' cache='false'/>
        
      Creates a BSHMethod from the given XML element configuration.
      Parameters:
      e - the XML element configuration (may be null)
      Returns:
      a configured BSHMethod, or null if element is null
      Throws:
      IOException - if the script file cannot be read
    • execute

      public Object execute(Map arguments, String resultName) throws bsh.EvalError, IOException
      Sets the given arguments in the Interpreter, evaluates the script, and returns the object stored in the variable named resultName.
      Parameters:
      arguments - map of variable name to value pairs (all keys must be Strings)
      resultName - the variable name whose value to return
      Returns:
      the value of resultName after script evaluation
      Throws:
      bsh.EvalError - on BeanShell evaluation error
      IOException - if the script cannot be read
    • execute

      public Map execute(Map arguments, Collection returnNames) throws bsh.EvalError, IOException
      Sets the given arguments in the Interpreter, evaluates the script, and returns a map of named result variables.
      Parameters:
      arguments - map of variable name to value pairs (all keys must be Strings)
      returnNames - collection of variable names to extract from the interpreter
      Returns:
      map of variable name to value pairs after script evaluation
      Throws:
      bsh.EvalError - on BeanShell evaluation error
      IOException - if the script cannot be read
    • initInterpreter

      protected bsh.Interpreter initInterpreter(Map arguments) throws bsh.EvalError, IOException
      Initialises a BeanShell Interpreter with the given arguments and evaluates the script.
      Parameters:
      arguments - map of variable name to value pairs to set in the interpreter
      Returns:
      a configured Interpreter after script evaluation
      Throws:
      bsh.EvalError - on script evaluation error
      IOException - if the script cannot be read
    • toString

      public String toString()
      Overrides:
      toString in class Object