Thursday, January 28, 2010

Working with Java Native Access [JNA]

There could be a requirement that an application needs to interface with Windows processes. It could be accessing the Dynamic Link Library [DLL], which forms the heart and soul of Windows processes, or accessing some hidden processes.

To satisfy this, we may require to write C/C++ code or use Java Native Interface [JNI] framework. To me, JNI is bit complex and if you think the same, look no further. Now we could use Java Native Access [JNA]. Java Native Access allows the Java programs to access the native shared libraries without using the Java Native Interface [JNI].

Apart from accessing Windows processes, one could use Java Native Access to access the processes of UNIX and Macintosh machines.

Recently I have used Java Native Access in one of the project to create an API such that it will access the base native shared libraries. To start with, I have to identify the native library. So I came to know that there are three basic native libraries which could provide all the major required functions. They are:
<> 
DLL
Description or Use
GDI32.dll
Graphics Device Interface [GDI] functions for device output. For Ex: functions for accessing print devices
Kernel32.dll
Low Level OS functions for memory management
User32.dll
Windows functions, such as the window name, the window file name, etc.
Table 1: Window Native Library Summary

This above table helped me a lot to charter my way to use the required Native library in order get going. Java Native Access [JNA] is beautifully layered architecture API which could be easily accessed to use the Native Library functions.
To get started with Java Native Access, here is simple example to access the kernel32.dll

import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;

public interface Kernel32 extends StdCallLibrary {

Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);

long GetCurrentThread();

boolean Beep (int freq, int duration);

}

public class HelloJNA {

System.out.println(Kernel32.INSTANCE.GetCurrentThread());

System.out.println(Kernel32.INSTANCE.Beep(37, 2000));

}

Listing 1.0: Show method implementation of Kernel32 Native Window Library

The listing 1.0, shows the current thread ID implementing the GetCurrentThread() method of the Kernel32. It also shows the implementation of the Beep() method. Upon compiling and executing the above implementation, it will show the current process ID and there will be a beep sound for 2 seconds.

You will also require jna.jar, to be placed in the CLASSPATH to run this above program successfully.

Thursday, January 14, 2010

Java Design Patterns

Java Design Patterns are in essence is a true understanding of the problem and applied methodologies to achieve generic solution. I will be showcasing my understanding of the Java Design Pattern[s] along with collaborative UML diagram[s] and JUnit Test Case.

1. Factory Pattern (FP)
In the Factory pattern, objects are created based on a condition. Now, which object to return, is decided at run time based on the condition[s]. It is best described by an example. For instance, we have a Juice factory i.e. JuiceFactory. JuiceFactory manufactures juice[s] based on the type. Type could be a ‘fruit’ or a ‘vegetable’.
The juice is prepared in Juicer, is measured and then packed. These are standard operations. They will be same for all type of juices. Hence, they will be captured using the JuiceInterface[code can be fetched from the Resources].



Figure 1 Blueprint of Juice
So, as per the Figure 1, we have Juice class which implements JuiceInterface. We also have a VegetableJuice and a FruitJuice classes which inherit the properties of Juice class to prepare the juice, measure it and then pack the juice.


Figure 2 Blueprint of Juice Store Implementation

Now, as per the Figure 2, we will create the JuiceFactory, whose main task is to give the JuiceStore, the appropriate Juice based on the customer’s choice i.e. fruit or vegetable juice.
Implementation: Let us frame our juice factory
// Create JuiceFactory, which manufactures juice object
public class JuiceFactory {

public static Juice typeOfJuice (String type) {

Juice juice = null;
if (type == null)
return juice;

if (type.equalsIgnoreCase("vegetable"))
juice = new VegetableJuice();
else if (type.equalsIgnoreCase("fruit"))
juice = new FruitJuice();
return juice;
}
}
} // end of JuiceFactory
Now we will create the JuiceStore class, to see how the implementation of Factory Pattern is progressing. Below you may see [in the shaded], that all the logic of comparing the type of juice to choose is taken care by JuiceFactory.
// Order Juice
public class JuiceStore {

JuiceFactory juiceFactory;

public JuiceStore() {}

public JuiceStore (JuiceFactory juiceFactory) {
this.juiceFactory = juiceFactory;
}

public Juice orderJuice (String type) {
Juice juice;

if (type == null)
return null;

juice = JuiceFactory.typeOfJuice (type);

juice.prepare();
juice.measure();
juice.packed();

return juice;
}
} 
The rest of the implementation you may find in the code attached [Resources].
Try to test your implementation using JUnit. You may find plenty of reasons in Resources or when you search on the net. Below you find a small test of the above implementation.
public void testJuiceStore () {
JuiceStore myStore = new JuiceStore ();

assertEquals(new JuiceStore().orderJuice("vegetable").prepare(), myStore.orderJuice("vegetable").prepare());
assertEquals(new JuiceStore().orderJuice("vegetable").measure(), myStore.orderJuice("vegetable").measure());
assertEquals(new JuiceStore().orderJuice("vegetable").packed(), myStore.orderJuice("vegetable").packed());

assertEquals(new JuiceStore().orderJuice("fruit").prepare(), myStore.orderJuice("fruit").prepare());
assertEquals(new JuiceStore().orderJuice("fruit").measure(), myStore.orderJuice("fruit").measure());
assertEquals(new JuiceStore().orderJuice("fruit").packed(), myStore.orderJuice("fruit").packed());
}
So, above we have seen, how the implementation of the Factory pattern solves a particular use case of calling the class object based on certain condition.

Resources:
1. Source Code for the above implementation. Soon the files could be downloaded from Google Docs.
2. Why to Test programs
3. One stop shop for all design pattern can be found at Gang of Four book Design Patterns, Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley, 1995)
4. Learn Design Pattern from AllAppLabs

Access JBoss AS with IP Address

When I installed JBoss Application Server [AS] 5.1.x on Remote machine, I wanted to access it using the IP address of the remote machine.

Though, you can access the JBoss AS welcome screen on a machine on which you installed the JBoss AS, but not on the remote machine. It is because the JBoss Server is locked on 'localhost'. Hence, you could access the JBoss AS using either localhost[:port] or 127.0.0.1[:port].

To fix this issue, you must have to run the JBoss AS using following command line argument:

$ $JBOSS_HOME/bin/run.sh -b IPADDRESS
where
-b, --host= Bind address for all JBoss services
IPADDRESS could be your remote machine IP Address on which you installed the JBoss AS

For the reference, check out this link: