Friday, December 7, 2012

Cool Feature of Java 6- JSR 223 & JSR 199

JSR 223- Scripting for the Java Platform

   Java introduced the support for scripting languages for java platform from Java- 6.

  1.  JSR 223 - Scripting for the Java Platform helps developers integrate the standard java code with the scripting language by a standard framework and API. With this API we can
    1. Access and control Java objects from a scripting environment
    2. Create web content with scripting languages
    3. Embed scripting environments within Java based applications
  1. Java 6 by default ships Javascript Engine Implementation called Rhino provided by the Mozilla
  2. New scripting engines that compliance JSR 223 can be plugged in to avail the feature. So now the Python, Ruby scripting advantages can also be availed.
  3.  With this new API, now the power of scripting can be availed from the java.

Example: In the below piece of code, a javascript function called printName(name) is evaluated from java and the argument is passed is the java object.
public static void main(String[] args) {
            // create Script-Engine-Manager object
            ScriptEngineManager factory = new ScriptEngineManager();
            // Get Script Engine from factory
            ScriptEngine engine = factory.getEngineByName("js");

            // Create binding variable to pass value to script
            Bindings bindings = engine.createBindings();
            bindings.put("name", "JAS Technologies");

            // evaluate JavaScript code from String
            try {
                  engine.eval("printName(name);" +
                                    "function printName(name){" +
                                          "print(name+' company');" +

            } catch (ScriptException e) {

JSR 199 – java Compiler API

1.       One of the cool features introduced in java 6 is to compile the java source file dynamically.
2.       As a part of JSR 199-JavaTM Compiler API, there is a standard java Compiler API which defines the interfaces for java compiler functions and a standard service provider framework by which vendor can provide implementation for the interfaces.
3.       The API for compilation is now available part of javax.tool package and ships with Java 6
4.       So having said above what a developer can do with this is
a.       Dynamically compile java source file
b.      Can compile multiple files
c.       Can compile a java source from a string object
d.      Also several advanced options are also available. Check the API for complete details.
  Below is the small example for compiling the java source file dynamically

      public static void main(String[] args) {
            //File to be compiled
            String fileName = "D:/jas/workspace/com/test/compile/";
            //Get the java compiler
            JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
            //Compile the java file. Look at the API to find more on options
            int returnVal =, null, null, fileName);

   Content from used in the above example

package com.test.compile;

public class CompileMe {
      public static void main(String[] args) {
            System.out.println("The JAS Technologies");

Brief on JVM Heap Memory

let’s take a look at how the JVM organizes memory into different areas. This has been helping several garbage collection algorithms and memory management to work efficiently. These areas are called Generations which are categorized based on the life of the objects.

1.       There are separate pools that hold objects of different age. They are
a.       Young Generation
b.      Old Generation
c.       Permanent Generation

2.       Young Generation:
a.       Most of the objects are initially placed in the young generation space.
b.      Young Generation Garbage collection occurs frequently and operates at high speed since the young generation space contains lots of objects that are less referenced.
c.       In Java HotSpot VM, Young Generation again categorized into
                                                               i.      Eden Space : Most objects are initially allocated in the Eden space
                                                             ii.      Survivor Space: Objects that survive atleast one young garbage collection is moved to survivor space.

3.       Old Generation:
a.       Objects that survived Garbage Collection (From Survivor Space) are tenured or Promoted to the Old Generation
b.      Garbage Collection algorithm that works on this space is designed to be more space efficient since Old generation occupies most of the heap.

4.       Permanent Generation (PermGen) :
a.       Permanent Generation usually contains the objects that describe classes & methods and also classes & methods.

Handling the comment block in Facelets/JSF2

Even if the block of code is commented using the HTML comment (<!--     -->) the JSF lifecycle still process the part of code and it is rendered in the HTML source although the component is not rendered in the HTML page. This shall be viewed in the browser source view option. This reveals the sensitive information to the third party which is not intended to be known.


In the below piece of code, a part of the code is commented using HTML based comment (<!-- -->)

Even though the Browser doesn’t render the component in the browser, still the code in the HTML contains this piece of information. And also JSF processes this component.

HTML Source viewed from the Browser.

Possible solutions:
      Configure web.xml to inform Facelets to skip comment.

2.       Use Facelets ui:remove tag to comment the code block.