Tuesday, May 29, 2007

Sampling Java SE 6

A lot of new features and API enhancements have been introduced in Java 6 (Mustang). Some of them are discussed briefly.

Compiler API

The new Java Compiler API allows invoking a compiler, gives access to diagnostics, and control over how files are read through a file manager. The file manager allows applications such as IDEs and JSP servers to keep all files in memory, which significantly speeds up compilation. Sun's open source Java EE implementation, GlassFish claims to have already benefited from this API.

//get the java compiler reference
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

//compile the Java class
int compilationResult = compiler.run(
null,null,null, "C:\\Person.java");

The code snippet shows how to compile a Java file using the Compiler API.

Web Service

The new web services stack provides support for writing XML web service applications. The java classes can be exposed as a .NET interoperable web service with a simple annotation. Java SE 6 adds new parsing and XML to Java object-mapping APIs, previously only available in Java EE platform implementations or the Java Web Services Pack.

To create a web service on Mustang create a java class, annotate it as shown below.

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class Hello {
public String echo() {
return "Hello THBS";
}
}


To deploy the web service we just need to write the main method.

public static void main (String [] args) throws Exception {
Hello server = new Hello();
Endpoint.publish("http://localhost:9090/Hello", server);
}


The deployment can be verified by displaying the wsdl file on the browser
http://localhost:9090/Hello?wsdl. This feature is intended to be used for testing only.

Scripting for the Java Platform

Java SE 6 provides support for scripting, which enables the developers to integrate Java technology and scripting languages by defining a standard framework and interface to do the following:
• Access and control Java based objects from a scripting environment
• Create web content with scripting languages
• Embed scripting environments within Java technology-based applications

The Java Platform, Standard Edition (Java SE), does not mandate any particular script engine, but it does include the Mozilla Rhino engine for the JavaScript programming language.

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
jsEngine.eval("function helloWorld(){"
+ " println('Hello, world!'); "
+ " return 's';" + "}");

Invocable invocableEngine = (Invocable) jsEngine;
invocableEngine.invokeFunction("helloWorld", (Object[]) null);

The example shows how to invoke a JavaScript function using java.

See: Carpe diem: Mustang released

Database

The Java SE 6 development kit co-bundles the all-Java JDBC database, Java DB based on Apache Derby. Out of the box developers have a database that can be used that implements the latest version of JDBC and is embeddable within desktop Java applications. Java DB has a small memory footprint and is fully transactional.

Also provided is JDBC 4.0, a well-used API with many important improvements, such as special support for XML as an SQL datatype and better integration of Binary Large OBjects (BLOBs) and Character Large OBjects (CLOBs) into the APIs.

Desktop Development

Desktop application development just became simpler with Java 6. It provides better platform look-and-feel in Swing technology, LCD text rendering, and snappier GUI performance overall. Java applications can integrate better with the native platform with things like new access to the platform's System Tray and Start menu. At long last, Java SE 6 unifies the Java Plug-in technology and Java WebStart engines, just makes sense. Installation of the Java WebStart application got a much needed makeover.

A new class added to the Java platform is java.awt.Desktop. It allows Java applications to launch associated applications registered on the native desktop to handle a java.net.URI or a file.


Desktop d = Desktop.getDesktop();
//opens the application associated with the txt file in your OS
d.browse(new URI("c:/text_file.txt"));


Similar enhancements have been made to make the development of a java desktop application simpler.

Class file structure update:

The class file structure in Java SE 6 has been modified to provide a much faster and simpler class verification process. The idea is to split the old verification process into two phases: type inferencing (compile time) and type checking (runtime). The new verifier (called the type-checking verifier) no longer needs to infer types because it uses type information embedded in the Java class file to check byte code type consistency. Removing the type inferencing process from the verifier significantly reduces the complexity of verifier code and improves verification performance. This provides performance improvements in byte code verification and thus class loading and start up time.

Monday, May 21, 2007

Mustang :- The Desktop bonus

One of the problems with java over the years is the lack on Desktop support. Sun has taken a major step with the release of Java 6.These changes have brought in a whole new flavour to the java desktop applications.

Java 6 now has system tray support.Now icons in the system tray can be added with menus and event handlers.Some applications that run in the background (eg mail client) can be seen in the system tray. Notifiers can also be provided to sent notification messages. The code demonstrates the use of the new functionality.
package com.sash.java6;

import java.awt.*;
import java.awt.TrayIcon.MessageType;
import java.awt.event.*;
import java.io.File;
import java.net.URI;

public class SystemTrayTest {

public SystemTrayTest() throws Exception {

if (SystemTray.isSupported()) {

SystemTray tray = SystemTray.getSystemTray();
//image for tray icon
Image image = Toolkit
.getDefaultToolkit().getImage("any_image.gif");

ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}};

//pop menu for the tray
PopupMenu popup = new PopupMenu();
MenuItem alertItem = new MenuItem("Message");
popup.add(alertItem);

MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);

final TrayIcon trayIcon = new TrayIcon(image, "Tray Demo", popup);

alertItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage(
"Finally",
"Desktop support for Java",
MessageType.INFO);
}
});

trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Tooltip test");
tray.add(trayIcon);
} else {
System.err.println(
"System tray is currently not supported.");
}
}

public static void main(String[] args) throws Exception {

//the system tray icon test
new SystemTrayTest();
}
}


Other enhancements include, the introduction of the Desktop API. Using this API now its very easy to open the default email client etc. It also provides an API to open any given file with the default application associated in the OS.

Desktop d = Desktop.getDesktop();
//guess what?
//opens the application associated in your OS with the jpg file
d.browse(new URI("c:/text_file.txt"));

This code opens the default program associated with the text files, on my windows system it opens the file in notepad.

API enhancements have been made to the getTotalSpace, getUsableSpace and getFreeSpace methods have been added. These are very useful enhancements made.

Very nicely done by the sun guys, finally adding some much needed support for the desktop applications.
:)