Monday 30 December 2013

Tech Talk...JavaScript execution from within JVM

This tutorial describes how to execute JavaScript from within java using the Rhino provided JavaScript execution Engine.

About Rhino:

What is Rhino JavaScript Engine
Maven Repository for Rhino Jar

How to invoke a JavaScript from within java using Rhino:


public class JavaScriptInvoker {
    String myfn = ""
            + "function sayHello(inputStr, outputStr){"
            + " outputStr =  \"Hello\"+inputStr;"
            + "}";
    Function function;
    Context ctx;
    ScriptableObject scriptable;

   public void initJs(){
        ctx = Context.enter();
        ctx.setOptimizationLevel(9);
        scriptable = ctx.initStandardObjects();
        function = ctx.compileFunction(scriptable, myfn, "sayHello", 0, null);
   }

   public void invokeJs(){
      String outputStr;
      Object[] args = {"Neha", outputStr};
      function.call(ctx, scriptable, function, args);
      System.out.println(outputStr);
   }

    public static void main(String[] args) throws Exception{
       JavaScriptInvoker jsInvoker = new JavaScriptInvoker();
       jsInvoker.initJs();
       jsInvoker.invokeJs();
    }
}


Understanding different parts of the program:

1. Initializing the JavaScript engine.
The Rhino JavaScript engine needs to be initialized before it can be used to invoke a java script. This initialization involves initializing the internal objects used by the JavaScript Engine (being done using the line  scriptable = ctx.initStandardObjects(); ) .This single line would be sufficient for you in most of the cases( unless you need to do some sophisticated initializations on your own.).
Also, an optimization level can be set on the context of the engine, which ensures how performant the invocation of JavaScript from within java will be (see the line:ctx.setOptimizationLevel(9);) Optimization levels are integers, 9 being the highest.
Finally, the javascript which has to be invoked, needs to be initialized. This is being done in the line:
 function = ctx.compileFunction(_scriptable, myfn, "sayHello", 0, null);

2. Invoking the JavaScript from within java at runtime.
Once the javascript engine is initialized, it can be invoked at runtime multiple times to invoke the function compiled above, that is happening in the code :
 function.call(ctx, scriptable, function, args);
The first two arguments to the call method are context object and scriptable object of the JavaScriptEngine to let it know of the initialization parameters.
The next parameter is the compiled function that needs to be called.
The final parameter is an Object array consisting of parameters being passed to the function being invoked.

3. Parameter passing / Object sharing between JavaScript and Java.
As seen in the code above, I am passing two parameters to the method sayHello : one input string and one output string. Both these are Java String Object types. They are successfully binded and passed to the function sayHello, which appends "Hello" to the input string and then assigns the result to output string and returns the output string back. On obtaining the output string, the java runtime can successfully use it for further operation.

4. Java method invocation from within javascript engine
From within the javascript function, any of the Java methods can be invoked, even objects can be passed from the java code to the javascript as initialization parameters or arguments and then they can be used by the javascript in any way possible.

Thursday 5 December 2013

What am I writing after all


So, What am I writing after all....

So...many of the readers might be wondering what am I writing here...some technical topics, some management stuff, some general stuff - difficult to categorize. Well, most might as well be wondering, am I really writing because it has been a really long gap after which this post is coming up. Well, there have been things that I was held back with and even now there are things, but it gives me pleasure to see that I am still writing..or rather blurting whatever is coming to my mind. And that is what I do with every post. That is why there is a mix of stuff out there.

 To be honest, when I started blogging, I had no definite plans as to what I would write on. So, as and when things have been popping up, I have been jolting them down (That sounds nice..isn't it ? :) , of-course there is one more underlying 'strong' reason for the mix of topics you see here, that being, I am more of a jack of all trades...and of-course master of none :D ) So, I am assembling here different kind of posts with the hope that everyone finds something here that interests them (I know not every topic will be of interest to everyone ) and I know that the readers are intelligent enough to dig down and get what interests them :) . Thus, i can go for stunt of writing diverse things under the roof of a single blog when i am blessed with such an intelligent set of readers. So, I am listening to the instincts for now that says - just go for it :)

Jokes apart, now a few posts down the line I have started reflecting on the aspects of blogging which I had not even thought of initially when I started on. For instance, it would be a pleasure to look back and see what all I had got to share, as the posts grow old (and you and me too :) ). Also, it gives such a nice space to not only share your thoughts, little observations from life (like hope), but to vent out your feelings wrapped in a nice coat of sugary words (like the post Who is a development manager ). You get to connect with like minded people and when the topics are diverse, then you get to connect to many like minded people who share your thoughts on the different topics...
So, thanks for bearing with the different posts so far,and do hang on, i will be writing soon again :) . Lots of stuff is piling up, need to slowly jolt them down and present forth, which I would do soon.
If you like my writing, you can award me an excellence medal :P i really like the one below :) :)