# Bean Scripting Framework

version 3.0.3

| The Bean Scripting Framework is an attempt to create an API to allow calling scripting languages from Java. It hasn’t been updated for long and abandoned in favor of the standard JSR-223 API.

The BSF engine for Groovy is implemented by the `org.codehaus.groovy.bsf.GroovyEngine` class. However, that fact is normally hidden away by the BSF APIs. You just treat Groovy like any of the other scripting languages via the BSF API.

| Since Groovy has its own native support for integration with Java, you only need to worry about BSF if you also want to also be able to call other languages, e.g. JRuby or if you want to remain very loosely coupled from your scripting language.

## 1. Getting started

Provided you have Groovy and BSF jars in your classpath, you can use the following Java code to run a sample Groovy script:

    String myScript = "println('Hello World')\n  return [1, 2, 3]";
    BSFManager manager = new BSFManager();
    List answer = (List) manager.eval("groovy", "myScript.groovy", 0, 0, myScript);
    assertEquals(3, answer.size());

## 2. Passing in variables

BSF lets you pass beans between Java and your scripting language. You can register/unregister beans which makes them known to BSF. You can then use BSF methods to lookup beans as required. Alternatively, you can declare/undeclare beans. This will register them but also make them available for use directly in your scripting language. This second approach is the normal approach used with Groovy. Here is an example:

    BSFManager manager = new BSFManager();
    manager.declareBean("xyz", 4, Integer.class);
    Object answer = manager.eval("groovy", "test.groovy", 0, 0, "xyz + 1");
    assertEquals(5, answer);

## 3. Other calling options

The previous examples used the eval method. BSF makes multiple methods available for your use (see the BSF documentation for more details). One of the other available methods is apply. It allows you to define an anonymous function in your scripting language and apply that function to arguments. Groovy supports this function using closures. Here is an example:

    BSFManager manager = new BSFManager();
    Vector<String> ignoreParamNames = null;
    Vector<Integer> args = new Vector<>();
    args.add(2);
    args.add(5);
    args.add(1);
    Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0,
            "def summer = { a, b, c -> a * 100 + b * 10 + c }", ignoreParamNames, args);
    assertEquals(251, actual.intValue());

## 4. Access to the scripting engine

Although you don’t normally need it, BSF does provide a hook that lets you get directly to the scripting engine. One of the functions which the engine can perform is to invoke a single method call on an object. Here is an example:

    BSFManager manager = new BSFManager();
    BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
    manager.declareBean("myvar", "hello", String.class);
    Object myvar = manager.lookupBean("myvar");
    String result = (String) bsfEngine.call(myvar, "reverse", new Object[0]);
    assertEquals("olleh", result);