beanshell
Script Engine to execute BeanShell
Index > Books > Declarative Development Guide > Reference Library > Accessor Listing > beanshell

Rate this page:
Really useful
Satisfactory
Not helpful
Confusing
Incorrect
Unsure
Extra comments:


Module

urn:org:ten60:netkernel:ext:script

Definition

Active URI Base

active:beanshell

Format

<instr>
  <type>beanshell</type>
  <operator>myscript.bs</operator>
  <myargument>mydoc.xml</myargument>
  <target>var:result</target>
</instr>

Syntax

ArgumentRulesDescription
typeMandatory beanshell
operatorMandatory script to be executed
arg1Optional An argument to pass to the script
argNOptional An argument to pass to the script
targetOptional the result of the request

This accessor provides script execution services based on beanshell.

The operator specifies a script resource. This resource may either be a plain/text script document or an XML document with the script specified as the text of the root element.

When using XML it is strongly advisable to enclose your script inside a CDATA section, for example:

<script><![CDATA[ ...your script .. ]]></script>

The beanscript requires that a main() method be defined which will be invoked when the script is invoked (beanscript usually allows code to executed outside the scope of a method). This restriction is placed on the beanscript to improve performance as it allows a pre-execution parsing stage to be performed as a one off. For small scripts the performance gain is approximately five-fold.

Script Context

Each script execution is provided with a NetKernel context object which allows the script to be a fully featured NetKernel accessor. The context is an instance of the INKFConvenienceHelper class. The context has three purposes, to allow introspection of the request which initiated the script, to allow sub-requests to be initiated, and to specify a response. For more details see the NKF Reference Guide.

A script must always return a response. Since it cannot be assumed that a scripting language will return an object that is useable by the underlying script accessor, a script must create and issue its response via the context.

INKFResponse response=context.createResponseFrom(...);
context.issueResponse(response);

Parameters

Parameters can be passed to a script by specifying abritrarily named arguments. These are accessible in the scripting context using the this:param URI address space convention used in the DPML runtime.

For example if we had

active:beanshell+operator@myscript.bs+myargument@data.xml

The myargument parameter can be obtained inside the script using the context and the argument URI "this:param:myargument".

context.source("this:param:myargument")

Libraries

Code reuse is generally good and creating libraries of reusable code is good way to get reuse in beanshell. Beanshell has access to all the java classes in the context of its execution but this code is all compiled not scripted. One way to load libraries of scripted code from the context of execution, this may be either classes or method or even just global variables is to use the eval() method:

import com.ten60.netkernel.urii.aspect.IAspectString;
myLibraryURI = "libs/library1.bsh";
eval( context.sourceAspect( myLibraryURI, IAspectString.class ).getString() );
	
Place this code outside the main method and it will be evaluated at script compile time.

Example

Here's an example of a beanshell script for performing an XSLT, logging the result and returning the result as the response:

import org.ten60.netkernel.layer1.nkf.*;
import org.ten60.netkernel.layer1.representation.IURRepresentation;

void main()
{
    //Create and issue XSLT request
    INKFRequest sr=context.createSubRequest();
    sr.setURI("active:xslt");
    sr.addArgument("operand","data1.xml");
    sr.addArgument("operator","style1.xsl");
    IURRepresentation rep=context.issueSubRequest(sr);
    
    //Log result
    sr=context.createSubRequest();
    sr.setURI("active:log");
    sr.addArgument("operand",rep);
    context.issueSubRequest(sr);

    //Create and issue response
    INKFResponse resp=context.createResponseFrom(rep);
    context.setResponse(resp);	
}
	
© 2003-2005, 1060 Research Limited. 1060 registered trademark, NetKernel trademark of 1060 Research Limited.