ScriptingScripting
Home > Books > Getting Started > Quick Introduction > Code First > Scripting

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


Code First - Scripting

In NetKernel and in resource oriented computing, information is primary and languages are secondary. You have seen a Hello World! program written in Java. Now we will show how to use scripting languages to implement accessors and services.

A scripting language runtime in NetKernel is a service and is implemented as an accessor. For example, the Beanshell language is implemented by the service whose URI is active:beanshell.

Beanshell

A Beanshell version of the Hello World! program can be run at the address http://localhost:8080/tutorial/helloworld-beanshell and is shown below

import org.ten60.netkernel.layer1.representation.StringAspect;

main()
{ aspect   = context.sourceAspect("ffcpl:/src/helloworld.txt",StringAspect.class);
  message  = aspect.getString();
  message  = message.concat(" from Beanshell");
  literal  = new StringAspect(message);
  response = context.createResponseFrom(literal);
  response.setMimeType("text/plain");
  response.setCacheable();
}

The mapping for the logical address ffcpl:/helloworld-beanshell is directed to another logical address:

<rewrite>
  <match>ffcpl:/helloworld-beanshell</match>
  <to>active:beanshell+operator@ffcpl:/src/helloworld.bsh</to>
</rewrite>

This target address, called an Active URI, causes the Beanshell runtime service to be located (in an imported module) and is passed the resource ffcpl:/src/helloworld.bsh (which is our scripted program) as the operator parameter.

If you make a change to the message in the Beanshell program NetKernel does not have to re-commission the module as no changes have been made to any Java classes and hence the classloader does not need to be reloaded. The language runtime will, however, dynamically compile the script resource into Java byte codes. This allows you to quickly write script code that runs with high performance.

JavaScript

JavaScript is a supported scripting language in NetKernel. The JavaScript version of the Hello World! program can be run at the address http://localhost:8080/tutorial/helloworld-javascript and follows:

importClass(Packages.org.ten60.netkernel.layer1.representation.StringAspect);

aspect   = context.sourceAspect("ffcpl:/src/helloworld.txt",StringAspect);
message  = aspect.getString();
message  = message.concat(" from JavaScript");
aspect   = new StringAspect(message);
response = context.createResponseFrom(aspect);
response.setMimeType("text/plain");
response.setCacheable();

and is mapped to a logical address:

<rewrite>
  <match>ffcpl:/helloworld-javascript</match>
  <to>active:javascript+operator@ffcpl:/src/helloworld.js</to>
</rewrite>

Other Languages

NetKernel can support any language that runs in the Java Runtime environment (JRE). Even domain specific languages such as XML processing languages are uniformly treated in the same way. You can even develop your own custom application or domain specific language if it makes sense for your application. NetKernel 3.3 supports Beanshell, Ruby, Groovy, Python, JavaScript, Java and the XML family of languages.

An example of using XML for the Hello World! program is included in the tutorial module. It can be run at the address http://localhost:8080/tutorial/helloworld-xml Read the documentation in the module for more information about this version.

© 2003-2007, 1060 Research Limited. 1060 registered trademark, NetKernel trademark of 1060 Research Limited.