Sunday, May 10, 2015

Java SE 6... What's New? (Part 02/04)

I have provided some of the most important core language enhancements for JDK 6.0, along with code samples. The examples provided below can be directly pasted into your IDE and you may name the class as provided.
 
Communication Between Sender and Receiver (SOAP Web Services)
 

From JDK6, Java allows the creation, publishing and lookup of web services through a mixture of annotations and command-line utilities. The below is located in package 'jdk6.features'
 package jdk6.features;  
 import javax.jws.WebMethod;  
 import javax.jws.WebService;  
 import javax.xml.ws.Endpoint;  

 /**  
  * @author sumith_puri  
  *  
  * credits: https://weblogs.java.net/blog/vivekp/archive/2006/12/webservices_in.html  
  *   
  * apt -d sample jdk6_WebServices.java  
  * java -cp sample jdk6.features.jdk6_WebServices  
  * wsimport -p client -keep http://localhost:8080/calculator?wsdl  
  */  
 @WebService  
 public class jdk6_WebServices {  
      @WebMethod  
      public int add(int a, int b) {  
           return (a+b);  
      }  
      public static void main(String[] args) {  
           jdk6_WebServices calculator = new jdk6_WebServices();  
           Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator);  
      }  
 }  

In the same folder as the root of the above source file/package, you should type the following:
apt -d sample jdk6_WebServices.java  

Then type the following, to deploy the Web Service at the specified URL.
java -cp sample jdk6.features.jdk6_WebServices

Now, it is time to create the client. You may use the following command to create the client stubs.
wsimport -p client -keep http://localhost:8080/calculator?wsdl 

We can import the generated sources from the 'client' folder now to Eclipse. Using the imported client stubs, you can create client application using the following. Execute it either on command-line or from Eclipse (Run As > Java Application)
 package client;  

 public class JDK6WebServicesClientApp {  
      public static void main(String[] args) {          
           Jdk6WebServicesService service = new Jdk6WebServicesService();  
           Jdk6WebServices serviceProxy = service.getJdk6WebServicesPort();  
           int result = serviceProxy.add(12, 12);  
           System.out.println("Sum: " + result);  
      }  
 }  


Java Compiler API
A powerful feature in JDK6 that will be of really help to create artifacts such as plugins, filters, extensions for dynamic (customer based or client based) execution - is Dynamic Compilation using the Java Compiler API.
 public class jdk6_DynamicCompilation {  

      public static void main(String[] args) throws Exception {            
           System.out.println("Testing Dynamic Compilation....");  
           System.out.println("plugin\\Plugin.java");       
           String fileToStream = "plugin" + java.io.File.separator + "Plugin.java";  
           JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();                 
           FileOutputStream fileOutputStream = new FileOutputStream("errors.txt");  
           int compilationResult = javaCompiler.run(null, null, fileOutputStream,   
                                                          "-verbose", fileToStream);            
           System.out.println("Compilation Result..." + compilationResult);  
      }  
 }  

The following code should be provided under 'plugin'. You may name it as 'Plugin.java'
 package plugin;  
   
 public class Plugin {  
      public boolean filter(String data) {            
           boolean flag=false;  
           if(data.contains("fish")) {                 
                flag=true;  
           }  
           return flag;  
      }  
 
     public static void main(String[] args) {  
      }  
 }  

You may now run the jdk6_DynamicCompilation and obtain Plugin.class as dynamically compiled class under the 'plugin' folder.  Now execute the generated class either using reflection (which you may use in real-world products or solutions) or from the command-line.
java -cp . plugin.Plugin


Scripting Language Support
With JDK6, you may execute JavaScript (Tcl, Ruby, Groovy, etc.) from within Java. This is possible through ScriptingEngine. The example below shows the usage of JavaScript within Java.

 package jdk6.features;  
 import java.io.File;  
 import java.io.FileReader;  
 import javax.script.Invocable;  
 import javax.script.ScriptEngine;  
 import javax.script.ScriptEngineManager;  
   
 public class jdk6_Scripting {  
      public void todaysLogic() throws Exception {            
        // dynamically change script - invoke code in groovy, js - even tcl!  
        ScriptEngineManager manager = new ScriptEngineManager();  
        ScriptEngine jsEngine = manager.getEngineByName("JavaScript");  
        System.out.println(jsEngine.toString());  
        if (jsEngine instanceof Invocable) {  
             try {  
                  File file = new File("market.js");  
             } catch (Throwable e) {  
                  e.printStackTrace();  
             }              
             FileReader reader = new FileReader("market.js");              
             jsEngine.eval(reader);              
             ((Invocable) jsEngine).invokeFunction("current");  
         }  
      }  
      public static void main(String[] args) throws Exception {  
           jdk6_Scripting scripting = new jdk6_Scripting();  
           scripting.todaysLogic();  
      }  
 }  

You must place the following JavaScript code in your classpath, maybe within the root folder of your Eclipse Project. You should name it 'market.js'
 function current() {  
   var x = "", i;  
   for (i=0; i<100; i++) {  
        x="";  
        x = x + "The Number Is " + i;  
        println(x);  
   }    
 }  


Happy Programming with JDK6!

No comments: