How Java becomes agile riding Rhino Xavier Casellato
How Java becomes agile riding Rhino Xavier Casellato VP of engineering, Liligo. com
Let the ride begin !
Change request : Simon says We need a white rectangle Tuesday we want a circle but only if it rains The rectangle should be blue Why don’t we do a triangle ? The border should The is blue The circle must berectangle be yellow except if and green The rectangle should green it’severy sunny. Monday The rectangle must be blue when it’s be yellow if sunny and white temperature is less when it rains than 23° The rectangle is circle Didn’t we say it if temperature is less should be orange ? than 0° and it’s sunny Who decided it must be a rectangle ?
Flexibility is required Many inputs affect the final result § Many rules can be applied § Rules change often §
Modeling flexibility Database needs a model XML needs a DTD Home made data storage ! Are you sure it’s a solution ? None of these solutions are really flexible.
Stuck !?
Constraints Application behavior must be modified quickly § Deploying compiled code is time consuming § Compiled code required IT resources §
Needs Empower business decision § Reduce change requests delay § Introduce customizable business logic in application §
Rhino, inside the beast
Rhino History Netscape project started in 1997 § First Release in 1998 § Embedded in JVM since Java Standard Edition 1. 6 §
What is Rhino ? § § Full-java API Rhino contains All the features of Java. Script 1. 6 A Java. Script shell for executing Java. Script scripts A Java. Script compiler to transform Java. Script source files into Java class files § A Java. Script debugger for scripts executed with Rhino § § § Embed and execute Java. Script codes inside a Java application
General concepts § Context § § Scope § § stores information about the execution environment of a script. set of Java. Script objects Host § Host objects implement special Java. Script features like dynamic properties.
How it works
Basic Rhino Execution sequence // Create Context cx = Context. enter(); // Initialize standard objects Scriptable. Object scope = cx. init. Standard. Objects(); // Build scope. put(“input”, input 1); // use Script … // Exit Context cx. exit();
Access javascript variables with java Object x = scope. get("x", scope); if (x == Scriptable. NOT_FOUND) { System. out. println("x is not defined. "); } else { System. out. println("x = " + Context. to. String(x)); } “X” variable is read from script
Using javascript function in Java Object f. Obj = scope. get("f", scope); if (!(f. Obj instanceof Function)) { System. out. println("f is undefined or not a function. "); } else { Object function. Args[] = { "my arg" }; Function f = (Function)f. Obj; Object result = f. call(cx, scope, function. Args); String report = "f('my args') = " + Context. to. String(result); System. out. println(report); } Call expects two scriptable objects : § scope to execute the function relative to § Java. Script this object
Create java object in Javascript String script = "var x =5; System. out. println(x); "; result = cx. evaluate. String(scope, script, “<cmd>", 1, null); “X” variable is declared in the Java. Script and console output is called from Java. Script. You can access to native java classes from Java. Script.
Sharing java object with javascript Object wrapped. Out = Context. java. To. JS(System. out, scope); Scriptable. Object. put. Property(scope, "out", wrapped. Out); Object is wrapped as a scope element. Method calls uses reflection.
Host object Host is a java class that can be manipulated as a Java. Script prototype. § Naming pattern should be applied to the methods § Constructor § Getter and Setter § Functions §
Declare a constructor for Host object Host class signature Javascript public class Counter extends Scriptable. Object { public void js. Constructor(int a) { … } … var counter = new Counter(7); …
Getter and setter syntax Host class signature Javascript public int js. Get_field() { return field; } … var x =counter. field; public void js. Set_field(int value) { field = value; } counter. field = x; …
Host object : functions Host class signature Javascript public int js. Function_increment() { field++; } … counter. increment(); …
Declare host object Scriptable. Object. define. Class(scope, Counter. class); Host object definition must be declared in the scope. The line above declares the counter class a Java. Script prototype. Java class becomes a Java. Script prototype. You can refer it using as a Java. Script object.
Scope types § Shared scope Use it to share object defined in a scope without modifying their values between to concurrent scope execution. § Sealed shared scope Object sealed properties can not be added/deleted to the object and the existing object properties can not be changed § Dynamic scope Allow to enclose common functions and shared objects in a multithreaded environment.
Rhino performances Execution time almost 3 times faster with compiled code Even compiled code is almost 10 times slower than java compiled code.
Performances concerns § There is a huge difference between evaluated script and compiled ones. § To keep good performances you must compile your script before execution and focus the Rhino usage to the relevant code part.
Rhino in action
Identify use cases Identify logical components which requires adaptive behavior. Need to have a flexible behavior Identify complex rules Various input data and outputs Estimate execution frequency Java. Script is slower than compiled Java
Embedding strategy Out-of-box integration Script Deep integration Script Host objects Java Native classes Function JAVA
Storing the script Flexibility works with a correct storage support ! Don’t miss your target ! SCRIPT MUST BE EASILY MANAGEABLE
Customizable function JAVA BEAN Javascript function Script Source is an attribute of the object. Script execution is a bean method.
Customizable process Java bean initial state (Host) Bean Manager (Context) Java. Script function State change Java bean new state Host object
Rhino Use case : Workflow Manager ( Context manager ) Workflow task Host object Workflow definition
Liligo use case 1 : Categorization Package offers Storage process Ski Week-ends Script Storage rules Cruises
Liligo use case 2 : Flexible processing XML HTML CSV Data processing Script Data Host object Text file
A few more words J 2 SE integrates a higher level of integration which allows you to decide the scripting engine you want to use. § JRuby, Jython are alternative to Java. Script and Rhino. § Helma framework embeds Rhino to provide a full Java. Script server-side framework. §
To learn more www. mozilla. org/rhino/ § https: //scripting. dev. java. net/ § http: //java. sun. com/developer/technical. Articles/J 2 SE/De sktop/scripting/ §
Conclusion Rhino is a really powerful tool. Rhino is easy to integrate especially since the J 2 SE 1. 6. Performance concerns imply to clearly identify the right embedding strategy. Properly used in your application it will convert a monolithic java application into a customizable user-oriented one !!!
Any questions ? “ and don’t hesitate to contact me with questions or if you were interested in working in our team: xavier. casellato@liligo. com ”
- Slides: 39