Monthly Archives: January 2014

Deploying an Applet with JNLP

JNLP is supposed to be the latest and greatest in the Java Applet world post 1.6 release! Personally, I was never a great fan of Applets. I have always felt that Applets are a pain in the wrong place. Inherently difficult and cumbersome to use and deploy.

But some of these concerns have been addressed with the deployJava javascript API. For example, it is now possible to ask the users to automatically install Java plugin or upgrade to a higher version with no extra effort on part of the developer. Java realised this quite late in the game that its very important to make it as user friendly as possible.

Anyway, enough of ranting :), lets outline the steps one by one.

Creating the Applet

An applet is any class which extends the javax.swing.JApplet. A typical applet is shown below:

Note that the initialization code which actually adds components to the contentPane should be included in the init() method.

Creating a Jar with proper entries in its Manifest file

Its recommended that we create the following entries in the Jar’s Manifest file:

Application-Name: CountDownLatchDemo
Permissions: sandbox
Codebase: palashray.com

The first parameter is the name of the application. The second is the kind of permission that the Applet needs to run. And the third is the domain name from where the Applet will be hosted. Please refer to this for details.

This can either be achieved by manually creating the jar and specifying the manifest file with the m option:

jar -cvmf my_manifest_file.txt myjar.jar

Or it can be done elegantly with Maven. This is the sample pom.xml:

 

Signing the Jar

We need to sign the Jar that has been created above. You can refer to my earlier blog here.

Creating the JNLP file

Please note that this jnlp file should be accessible by: http://palashray.com/abc/xyz/def.jnlp
And the my.jar and def.jnlp should be in the same directory.

Use the JNLP file in a HTML file to display the Applet

Since we are invoking the applet through the deployJava API, the user will be prompted for installing or upgrading the java plugin in the browser.

For more information, please refer to this url.

CountDownLatch example

Click here to see the Applet in action.
Click here to see the Applet in action.

As shown above, the 4 progress bars start one after another after the start button is clicked.

The following code is for the JPanel with the 4 progress bars:

This is the Thread using the CountDownLatches to coordinate between each other:

Finally, this is the Applet code:

The sources can be found here: https://github.com/paawak/blog/tree/master/code/Demo

 

[Mockito] Mocking void methods to set a passed in parameter

Consider the following badly designed code:

 

This is being used as below:

How can I test this method with Mockito? Look at the following snippet:

The code can be found here: https://github.com/paawak/blog/tree/master/code/MockitoExample