X

Sundararajan's Weblog

  • Java
    August 29, 2006

Multiple language programming in JDK

Guest Author

Phobos is a
lightweight, scripting-friendly, web application environment
running on the Java platform. Primary language used in Phobos project
is JavaScript - but it is possible to use any JSR-223
compliant language.

Sometimes you may even want to use more than
one language in the same application. For example, there is a
calculator
sample application
in Phobos. This application
has a simple HTML GUI for a four-function calculator. The add, subtract, multiply and divide
operations of this application are implemented as JavaScript
number arithmetic - which is same as Java's double precision arithmetic. For example,
Let us assume that you want to extend this application to do Java BigDecimal
arithmetic instead of double arithmetic. Ofcourse, you can call
Java API for BigDecimal arithmetic from JavaScript. But, you can
implement this feature very easily using the JEP script engine!

I did the following experiment after expanding and installing the
Phobos sample calculator.war
under Apache Tomcat [I used Tomcat 5.5].

I copied the following jar files to webapp's lib directory
WEB-INF/lib:

  1. jep-engine.jar [available from http://scripting.dev.java.net project]
    This is the JSR-223 script engine for JEP.
  2. jep-2.4.0.jar
  3. ext-1.1.0.jar
  4. Jama-1.0.2.jar

The last three jar files can be downloaded from http://www.singularsys.com/jep/.
If CVS checked out complete sources from scripting.dev.java.net, these
jar files are under scripting/engines/jep/lib directory.


Then, I made the following changes to WEB-INF/application/startup.js.
Note that this script executes when the calculator application is
started for the first time. In this script, I've created a script
engine for JEP and stored it in application object.

// application startup script

// creata a script engine manager
application.smanager = new Packages.javax.script.ScriptEngineManager();
// create a Java Math Expression Parser (JEP) script engine
application.jepEngine = application.smanager.getEngineByName("jep");
/\* Set the JEP mode to be "bigreal" - with this mode JEP engine
\* arithmetic is done using java.math.BigDecimal objects
\* rather than double values.
\*/
application.jepEngine.eval("mode(\\"bigreal\\")");


After that, I made the following changes to WEB-INF/application/controller/calculator.js:
Mainly, the calculator's compute function is modified
as follows:

POST: function() {
var value = request.getParameter("value");
var operand = request.getParameter("operand");
var operator = request.getParameter("operator")
/\*
\* Who needs the switch statement? :-)
\*/
value = ({ add: function(x,y) { return application.jepEngine.eval(x + "+" +y); },
subtract: function(x,y) { return application.jepEngine.eval(x + "-" + y); },
multiply: function(x,y) { return application.jepEngine.eval(x + "\*" + y); },
divide: function(x,y) { return y == 0 ? 0 : application.jepEngine.eval(x + "/" + y); },
}[operator])(value, operand);
[... more code deleted for brevity ...]

Instead of converting the request parameters as JavaScript
numbers (like the original sample did), I create and evaluate JEP expressions
in add, subtract, multiply and divide methods. After making the above
changes, I started Tomcat and visited http://localhost:8080/calculator.
Now, with the same calculator interface, the user can do BigDecimal
arithmetic! What's more - we can easily change JEP "mode" to - say vector
arithmetic.

If we make the following change to startup.js,


application.jepEngine.eval("mode(\\"vector\\")");

then the calculator supports vector/matrix/tensor arithmetic! For
example, we can enter [3232.343243, 4324] and [3432, 3434.353454] as input values
and choose "add" option to do vector addition.

Another example of mixing languages: you may use
JavaScript Templates
script engine to generate dynamic HTML
content using TrimPath
JavaScript Templates

With JSR-223 API, it is eary to call between scripts written in
different scripting languages - provided there are JSR-223 script
engines for the languages involved. It is very easy call
eval.
If you want more closer co-operation between scripts - like calling a function implemented in another scripting language or implementing a Java interface in script (which could be called from other scripting language or from Java), then there is Invocable
interface.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.