The new javax.script API....
A few days ago the JCP cranked out a public review of JSR-223, which has so far been received with a few blog notes and relatively little discussion. This is not surprising, given that if you you read the JCP description for this group it appears to have something to do with getting PHP pages into WAR files. (Useful, not very exciting). But I'm going to let you in on a little secret. There are really two APIs buried in this specification and
one of them is actually quite interesting. Java is about to get a standardized API for working with scripting languages and it's not just about web applications any more.
While the original focus of JSR-223 was indeed to develop a standard API for deploying scripted pages in web applications, over the past year the expert group has actually spent most of its time on the lower level, prerequisite API for exposing scripting language engines to the Java platform. The new javax.script package provides a standardized script engine API, similar to
the IBM/Apache BSF (Bean Scripting Framework), but takes the integration much further, offering: a more powerful discovery mechanism, fine grained namespace and script environment management, powerful application integration, and other advanced features.
Since much of the specification is aimed at scripting engine developers, some of these user APIs may not be immediately obvious. I'll just run down some of the important points here and leave the details for an upcoming article.
The Java Scripting API includes:
- Standardized packaging and deployment of scripting language engines for use
with Java Applications and Webapps. The JAR services mechanism allows you to simply drop in a JAR file to add support for a new language.
- An API for discovery and instantiation of scripting language engines
utilizing metadata, including: common language names, file extensions, and
- A simple API for evaluation of scripts, optionally supporting script
compilation and method invocation for both procedural and object oriented
- A fine grained, pluggable namespace and script context API, allowing
complete control over the evaluation environment of scripts and management of
engines. Create your own namespaces of bound values with well defined scoping
- Tight application integration via Map based namespaces and proxy
Java interface binding to scripts. Expose parts of your host application to
scripts via a Map interface that binds directly to the namespace of a script.
Expose methods of a procedural script via a real Java proxy interface.
- A standardized classification of script engine threading models, allowing
developers to optimize engine usage if desired. Languages are categorized
into one of three levels of concurrency support: Java language semantics,
"thread isolated" concurrency, and purely stateless concurrent interpreters.
- A deliberately limited, but useful API for language independent script
code generation. This allows clients to generate basic "output" and method
call statements in a language neutral way, enabling simple macro generation
and scripted action recording in any target language.
- A "non-normative" description of Java bindings which can be implemented by
languages that to work with Java methods and objects. This includes a
reference implementation intended to clarify and ease the implementation of
finer points of Java language semantics such as overloaded method resolution.
The reference implementation is available from the JSR-223 home page:
Interestingly, despite numerous references to the
BeanShell Java scripting language in the
specification, the development of a BeanShell engine by the spec lead, and the participation of
the primary BeanShell developer (me) in the spec development, the Sun download
does not include a BeanShell engine implementation or examples. (The politics of Groovy continue to amaze me.) But not to fear, the next release of BeanShell will include an engine implementation as part of its own distribution.
Check it out!