Wednesday, December 24, 2008

Scripting in JDK6 (JSR 223) Part 2

Its Lunch Time!

After programmer 2 shown programmer 1 how to deal with JSR 223 or JDK6 scripting ( part 1) programmer 2 went for lunch as he didn’t eat anything since yesterday and left programmer 1 to meet his fate.

Programmer 1 began to apply what he learned from programmer 2 and stuff went so bad, after 30 minutes programmer 2 came back.

Programmer 2: Man, how is everything going? (While he was just about to finish his last bite from the sandwich)

Programmer 1: man the performance sucks, it is really really slow and that won’t be good thing, it is bottleneck now

Programmer 2: ok let me see what did you do. (Swallowed his last bite hardly)

The Crime!:

What programmer 1 wrote was the following:

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("javascript");
for(int i=0;i<100;i++){

engine.eval("function xReturner(){" +" x=x*10; return x;};xReturner();");


As we can see programmer 1 did nothing wrong , he just used what he learned but this code is a disaster, it will make performance issue , in our case we are using JavaScript which is interpreted with each request to the eval method ,this mean that every hit to this method and then calling the eval will do the following : read the file(if we are reading from file) , evaluate and then execute , oh man that so bad if we have a very complicated logic resides in a very big file or even a big function this will cause performance issue, lucky enough that there is something to over come this.

The Solution! :

There is an interface shipped with the script package named Compilable, from its name we can conclude that it might be used to compile our script, and yup that’s right it compiles our script so that we don’t need to evaluate and read it before executing it every time we need to invoke the script, this can be achieved as shown in the following code:

ScriptEngine engine = manager.getEngineByName("javascript");
if(engine instanceof Compilable)


Compilable compiledEng=(Compilable) engine;

CompiledScript script= compiledEng.compile("function xReturner(){" +" x=x*10; return x;};xReturner();");
for(int i=0;i<100;i++){


Object result =script.eval();}



engine.eval("function xReturner(){" +" x=x*10; return x;};xReturner();");


First we check to see if this engine is implementing Compilable interface or no (this is an optional interface so some engines might not implement it but in our case as we are using JS engine shipped with JDK which implements this interface) so if our engine implements this interface then everything will be smooth else we don’t have any other way except the normal eval method.

Ok so we will just check if it implements the Compilable interface so we will invoke the compile method of the engine which will return CompiledScript which we are going to use and invoke our eval method on it and get the result.

By invoking these two scripts on programmers 1 machine we got these results:

Case 1 (no compiling): 931 ms

Case 2(with compiling) 661 ms

*Oh yea the machine that was used is a little bit slow (very slow, in fact used for performance testing for sure :p)

After solving the disaster programmer 1 was very happy by this performance tip.

Programmer 1: oh man thanks a lot for this tip, but I have another thing in mind, does JSR 223 enable me to invoke a method of my choice from a list of methods?

Programmer 2: oh that’s nice questions, let me show you how.

The Invocable!

Yup as you figured out it is the same thing as Compliable an interface that is optionally implemented by the engine you are using.

It enables you to invoke a method by name and also pass parameters to this method (we won’t need any binding here as all the stuff is handled by concrete implementation of this interface).

Before we dig deep in it and see sample code we need to mention that there are two methods that are used for this purpose and they are “invokeMethod”,”invokeFunction” lol it has the same meaning but if you give it another thought you would figure out that the names is correctly used (Function is usually used to address functions in non Object oriented or in other words flat structure and Method are used to address methods in object oriented language) so what are the difference between them ?

The difference between them is as follow:

invokeFunction: used to invoke methods on the top level which means methods that doesn’t reside inside a class (as in Ruby for example)

invokeMethod: used to invoke methods inside a class or module

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");

engine.eval("function getWelcomeMessage(name){return 'hello '+name;};");
Object[] params = {"Wolrd"};
Invocable invEngine=(Invocable)engine;

As we can see in the previous code we just got instance from our engine and cast it to invocable and then call the invokeFunction method which will take the function we want to invoke and also the parameters we want to pass, nothing else we can say about the invokeFunction on the other side invokeMethod as we said it can be applied for JRuby, well as people say nothing is better than a nice simple example ;)

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
Object obj=engine.eval(new FileReader("c:\\JRubySample.rb"));
Invocable invEngine=(Invocable)engine;
Object[] params = {"Wolrd"};



As we can see above the code, it is the same as we did before but here we changed our scripting engine to be JRuby and not JavaScript as we used to do but here we read from a file named JRubySample which has the following contents:

# Class Scripting sample
class SampleClass

def show(message)
puts "hello "+message

def show(message)
puts "hello "+message +" outside"
sample =

return sample;

we can see that we have two methods with the name show, one is a top level (flat structure) and the other reside in a class so as we mentioned before the invoke function will work on top level functions so after invoking the invokeFunction we will get the value “hello world outside” on the other side we said that invokeMethod works on methods that resides in a class.

How can we decide which class we want to invoke its method?

This is determined in the first parameter of the invokeMethod as we can see in our JRuby sample we are returning an object from the class SampleClass and then this object is returned by the eval method and then we pass it to the invokeMethod which will take this object, look for show method in it and then invoke it so we will get this message “hello world”.

Nice, but is that the only thing Invocable interface can do? Nope it can do more than this.

Its Creation Time!

The title sounds strange (all titles in fact), well it is not that strange when you know what is it all about.

The other nice feature about Invocable interface is : with the usage of getInterface method you can create interface implementation from the methods in the script you are evaluating ,this mean that getInterface method returns an implementation of a specific java interface and the methods in this interface is implemented by the script being evaluated(the script has functions that have the same signature as the one in the java interface), so when I get the object returned by getInterface and invoke a method on it , it will just run the logic that were written in the script method, in the code below we can see that happens :

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");

engine.eval("function getAccountName(){var name='Account 1'; return name;};");
Invocable invEngine=(Invocable)engine;
AccountOperations operation=invEngine.getInterface(AccountOperations.class);

As we can see in the previous code sample we are going to evaluate the script which has one method named getAccountName (returns a string that represents the account name)
The new thing here is that we are using the getInterface method which would return an interface implementation to us, as we can see it takes the Interface that we want to get implementation for (it will return an implementation of that interface) after that we will call getAccountName method (the only method inside this simple interface) on the instance returned by the getInterface method and that’s it!

One last thing:

Ok I feel guilty as I didn’t mention something which is:” we can use java objects in JavaScript engine that is shipped with JDK6”

Oh yea you are talking about binding right? nope we are not talking about the bindings here I mean it literally, what I mean is that I can use java objects inside the script I am writing lets take a look at the following example :

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine egine = manager.getEngineByExtension("js");egine.eval("importPackage(javax.swing);var optionPane =JOptionPane.showMessageDialog(null, 'Hello!');");

As we can see we used this time getEngineByExtension instead of getEngineByName (both will do the same thing , no reason why I changed it here but as we said before I can use getEngineByExtension if we are parsing scripts for example so we don’t know what script we have now so we will just get engine instance based on its extension ) after that we actually imported java objects and used it in our script engine and when we call the eval method we will get a message box with Hello message

Back To Our Programmers:

Programmer 1: oh man that’s awesome thanks a lot. I owe you one, I want to do anything for you, tell me what can i do?
Programmer 2: it is ok, no need for that
Programmer 1: no no I insist
Programmer 2: well if you are, invite me on dinner.
Programmer 1: ok consider it done.

And in the restaurant programmer 1 forgot that he doesn’t have enough money and programmer 2 had to pay for it.

Despite this sad ending for programmer 2, programmer 1 were able to finish the task on time, thanks to JSR 223 which enabled him to do so and lets not forget programmer 2 as well


JSR 223 is really nice feature shipped with JDK 6

*We saw that JSR 223 enables us to pass data from java to JavaScript and vice versa.

*Also enable us to invoke scripts not only that but also enables us to return implementation of a specific interface when the script we are invoking have methods as the interface we want to get implementation for.

*it also enable us to use java objects in the script as in the JavaScript engine “rhino“ shipped with the JDK 6

Read more!

Saturday, December 13, 2008

Scripting in JDK6 (JSR 223) Part 1


For sure most of us (mm guess so) have heard about the Scripting provided in Java 6 (Mustang) or JSR 223, 1st time I saw that (just saw the title) I thought that the Java guys will enable us to compile and run JavaScript scripts and that’s the end, well nope that wasn’t what Scripting in java 6 about but actually it is about enabling scripting languages to access the java platform and get the advantages of using java and the java programmers to use such scripting languages engines and get advantages from that (as we will see ).after I got it I was thinking : oh man if the team who implemented the Scripting engine were living in the dark age and they came up with such idea they will be accused of The practice of witchcraft but thanks God we are open-minded now and they will live and the good thing is that they will continue to practice witchcraft :D
So for now java guys will stay alive and continue to do their voodoo stuff and we will be looking at this nice scripting engine.

A sad story (life before Scripting engine):

Programmer 1 is reading documents of an approved change request (while listening to Bon Jovi’s Have a nice Day) which was as follow:

“We need to change our Java Scripts in the client side and move it to server side and also our JRuby scripts to be converted into java classes and we need this by tomorrow”

Programmer 1:” Man, I’m going to have a bad day, how will I do this (develop, test and deploy) in one day this is insane, And that task is assigned to only me :S I don’t know JRuby and I am not that good at JavaScript, well better to move on and get some tutorials”

And in order to stick to the deadline programmer 1 had to get some advanced JavaScript, JRuby tutorials and didn’t have his lunch break L and he had to work till late hours in the next day morning in order to meet the deadline but as expected from programmer 1(that’s why they hired him) he completed his job on time and didn’t complain but he experienced a very bad day. TA DA end of story and it is one sad story, now lets rewind the tape and see how will the story end if programmer 1 had someone else to help him and to show him what is JSR 223.

A happy story (life with Scripting engine):

Same thing as previous (programmer 1 is reading CR document) and he got shocked

Programmer 1: man I’m gonna have a bad day, how will I do this (develop, test and deploy) in one day, this is insane.

A voice came from nowhere and said: what is going on? (That was programmer 2)

Programmer 1: man, check this change request we don’t have such time, we will have to work so hard to finish it in this time

Programmer 2: let me see… mmm well we could make a workaround and give them a build that everything is converted into java classes in no time.

Programmer 1: ok tell me more.

Programmer 2: we will give them what they want, which is all scripts are called and manipulated by java classes and called from server side (in JavaScript case for example) and by using the script engine we wont need to rewrite JRuby script or JavaScript in java we wont even need to know the logic or get any JRuby or JavaScript developer we will just call use the same logic and return the results and TA DA everything is going fine

Programmer 1: huh, seems to me that something hit you on the head, ok how we are going to do this?

Programmer 2: by using the scripting engine shipped with java 6

Programmer 1: huh !

Programmer 2: ok seems to me that you don’t understand a word, I will show you, just give me a paper

The Details:

What we need to know that the main class that we will need to interact with is ScriptEngineManager which will give us access to ScriptEngine which is the engine to any scripting language we want (in our case JavaScript and JRuby) by default JavaScript engine is shipped with JDK6 (Mozilla Rhino) but if we want other script engine we should download its engine implementation (there are some engines for, JRuby, PHP, Python, Groovy,…) ok lets see some JavaScript in action

In Action:

try {

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
engine.eval("var out='hi';var date=new Date();");

catch (ScriptException scrpEx)

By analyzing the previous code we can see:

Line 3 we make an instance from the ScriptEngineManager which is the entry point or in other words we use it to gain access to our script engine like line 4 shows , we note here that we got the scriptEngine by specifying the name of the engine ,we can also get our engine by specifying MimeType or Extension and that’s by calling getEngineByMimeType or getEngineByExtension instead of calling getEngineByName ,this can be useful if we want to get a scriptEgnine instance based on a file extension or something like that.

ScriptEngine instance we got is an instance of JavaScript engine which will apply all JavaScript rules to the script written or evaluated on this engine.

After getting the script engine in line 5 we just invoked a method called eval on this engine, this method evaluates the script giving for this method here as we can see we have supplied the script as a normal string we can supply it from a file for example but for the sake of simplicity we will just invoke the eval method on a string.

After invoking the eval method on the previous script, the rules for JavaScript will be applied while parsing this script, if anything went wrong an exception will be thrown and the type of the exception that will be thrown is ScriptException as line 7 shows.

When Stuff Goes Wrong:

What if we changed the parsed line demonstrated in previous code to be something that JavaScript wont recognize, what will show up? A parsing exception will be thrown indicating that this is not correct, something like the exception below (after changing var to vear)

javax.script.ScriptException: missing ; before statement (#1) in at line number 1

As we can see the exception will give you the line number that caused this exception with the column number and also the file name (in our case we are not using any file so we got Unknown Source)


Great now that we have known the basics of the scripting what else can I do with that? It doesn’t give me enough control yet, for example I can’t pass or get variable from java to JavaScript and vice versa, who said so? JSR 223 enables use to pass inputs and receive outputs, in the previous sample we’ve just shown how to acquire a ScriptEngine which is our entry to our selected script language but as we all know that this won’t be enough , we want to deal with the script itself in other words we need to pass variables to the script and get some output from that script to be manipulated by java. Fortunately JSR223 enables us to do this in an easy smooth way; the listing below shows us how we can get the return value from JS:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
System.out.println(engine.eval("var date=new Date(); date.getMonth();"));

The previous code just gets engine instance and after that it evaluates the expression and here eval will evaluate the expression against JS rules and then invoke this script which gives us back the value of the month but we are just printing the value, what if we wanted to take this value and use it somewhere else?

That’s easy we will just need to modify the last line to be like this:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Double month=(Double)engine.eval("var date=new Date(); date.getMonth();");

Cross Worlds (the direct way):

And now we can do whatever we want on the resulted month value.

Ok now we have seen that we can get back the date from the script by evaluating it but what if I wanted to get a specific variable value? Or assigning a specific value to a variable?

What we didn’t mention is the eval method has more than overload (the one we talked about is either pass a string or read the script form a file)

There is something called ScriptContext which enables us to define the scope of Biding Objects (we will see what does that mean shortly).

Bindings, this is what we have been looking for, bindings enables you to pass java objects to the script and also get script variable to java world.

The ScriptContext can either be GLOBAL_SCOPE or ENGINE_SCOPE which means that every object that we are going to bind in case of GLOBAL_SCOPE will be shared for all engine script and ENGINE_SCOPE will mean that it will be only visible for this engine.

Let’s look for some code now:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
Object a = bindings.get("a");
Object b = bindings.get("b");
System.out.println("a = " + a);
System.out.println("b = " + b);
Object result = engine.eval("c = a + b;");
System.out.println("a + b = " + result);

As we can see in the previous code we got the bindings of the engine and we’ve set its scope to be engine scope (only this engine).

After that we are calling bindings put method (which will allow us to pass java object to the script) bind the variable name “a” a value of 1 and variable “b” a value of 5so when evaluating the script, a will have value of 1 and b will have value of 5.

After that we are just getting the value of “a” and “b” back to the java world (we are just printing it but we could do anything with it and this is how we can get variable value from the script) and evaluating the script which will get us a result of 6

Cross Worlds (the indirect way):

We could have achieved the same functionality by using the indirect way as below:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
engine.put("a", 1);
engine.put("b", 5);
Object result = engine.eval("c=a + b;");
System.out.println("a + b = " + result);
System.out.println("a + b = " + engine.get("c"));

We just called put method on the engine which indirectly does the bindings for us same as the previous code and the get method also will get us the variable named “c” to our java world.


For now we have seen how to make a simple application and call a script, evaluate it and get its return and even pass any input to what ever we want in part two we will look into more interesting stuff, so stay tuned ;)

Read more!