Using Scripting Service in open-esb
By narayanaa on Apr 04, 2007
Scripting Service Engine is a service engine which encapsulates JSR 223 and supports all the scripting languages that this JSR supports. It gives the user the flexibility of invoking any of the script that is being supported by the implementation of the specification. The current scripting support project on java.net has support for several scripting languages. This gives the user the flexibility of invoking/ using of the implemented Scripting Engines from the open-esb world. I plan to write this as 2 notes 1 st one on the background of JSR 223 which forms the basis of the Scripting Service Engine and the 2 nd note how to use the scripting service engine.
Brief background on JSR 223:
JSR 223 is a pretty cool interface for the scripting languages to be invoked from Java platform. This JSR talks about interoperability and interaction of the Java objects with other scripting languages. The specification provides for Java Bindings API using which any scripting platform to implement for accessing Java objects and invoke methods on the same. It exposes Script Engine API from Java perspective for interpretation of the scripts from Java and access the result from Java Application Developer's perspective. Java is readily usable for practical purposes through Java-capable scripting languages and/or interactive shells. This will extend Java beyond a mere system language for creating application software and become a readily usable platform, so Java programmers can manipulate file systems and text files, run commands, process and analyze data in various formats, prototype, develop, test, package and deploy Java software products, etc., all on top of Java.
Java application developers can now use languages such as scripting languages or even domain-specific declarative languages. Those languages all have gateways to the underlying operating system or platform, so their capabilities are not limited. For instance, Perl, Python and PHP all have large libraries available that deal with every aspect of general computing and various areas and have several running applications already. This JSR gives them an interface with Java and Java based applications the power of utilizing native code or much more control on the resources which hitherto not available to the Java programmers.
Few keywords of relevance while understanding JSR 223:
Scripting Engine: A software component that executes programs that are written in some scripting language
Interpretor: portion of the script engine which executes the script. This component consists of 2 subcomponents. The implementations of the interpreters differ in persistence and visibility of the state contained in the intermediary code and symbol-table. So these differences effect the programming interfaces and behavior of interpreters. (Implementation of scripting interface by different interpreters gives a uniform API)
Front-end: parses the script and generates the intermediate code. This is the internal representation of the code for the specific scripting type.
Back-end: called executor- stores the values of variables in the script in “symbol tables”.
JSR defines 2 technologies:
Java Language Bindings: This involves the representation of Java objects in the scripting environment. These are akin to JAXB in the context of XML/XSD. Spec does not talk about scripting language interpreter implementation. It supports static / dynamic as well as programmatic bindings.
Dynamic bindings take care of bindings created during execution of scripts using scripting language constructs.
Static bindings are scripting language constructs and are created while creation of scripting engine. These are created by the creator of the Scripting Engine rather than the application programmer. These can be native method calls depending on the scripting engine.
Programmatic bindings are created while using the scripting engine in the application.
Scripting Engine API: Interfaces and classes for utilizing script engines in Java Applications. These allow for discovery of the particular scripting engine using scripting manager, instantiating the scripting engine and allowing for running of script and the Java bindings for the scripting. Different Script Engine implementations have different capabilities for compilation (Compiled Script has the capability of compiling once and running 'n' number of times) and invocation (invocation of a particular method with in the script submitted). This gives the elements of Web Scripting framework. This API has the following important classes/interfaces.
ScriptEngineManager: implements a discovery and instantiation mechanism for
ScriptEngineclasses and also maintains a collection of key/value pairs storing state shared by all engines created by the Manager. This class uses the service provider mechanism to enumerate all the implementations of
ScriptEngineManagerprovides a method to return an array of all these factories as well as utility methods which look up factories on the basis of language name, file extension and mime type. The
Bindingsof key/value pairs, referred to as the "Global Scope" maintained by the manager is available to all instances of
ScriptEnginecreated by the
ScriptEngineManager. The values in the
Bindingsare generally exposed in all scripts.
ScriptEngineFactory: used to describe and instantiate
ScriptEngines.Each class implementingScriptEngine has a corresponding factory that exposes metadata describing the engine class.The
ScriptEngineManageruses the service provider mechanism described in the Jar File Specification to obtain instances of all
ScriptEngineFactoriesavailable in the current ClassLoader.
ScriptEngine: fundamental interface whose methods must be fully functional in every implementation of this specification. These methods provide basic scripting functionality. Applications written to this simple interface are expected to work with minimal modifications in every implementation. It includes methods that execute scripts, and ones that set and get values. The values are key/value pairs of two types. The first type of pairs consists of those whose keys are reserved and defined in this specification or by individual implementations. The values in the pairs with reserved keys have specified meanings. The other type of pairs consists of those that create Java language Bindings, the values are usually represented in scripts by the corresponding keys or by decorated forms of them
We can visualize the JSR 223 from the following figure:
I will continue with the usage of the Scripting Service Engine in open-esb in my next post on this.