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:

Wednesday, April 30, 2008

RIA with Adobe Flex and AIR

I just happen to attend a talk on Rich Internet Applications with Adobe Flex and AIR, and like to share it with you on 26th April' 2008.

Making most out of the web, in the sense it started with Java Applets and Microsoft ActiveX, but they have some issues due to security and page loading which takes time (Applets). With the transition in the Web, AJAX came into the picture and people used it.
In 2002, Macromedia came with RIA i.e. Rich Internet Applications. It is a combination of flexibility, responsiveness and ease of use both for the Web and Desktop level.
Adobe came with RIA in form of Adobe Flex (http://www.adobe.com/products/flex/) and AIR (http://www.adobe.com/products/air/).

With Flex, it works on MXML i.e. a declarative XML language use to describe the UI and its behavior. One could make the Flex Applications using the Adobe Flex Builder and at the browser side one only needs the Adobe Flash Player and on the Desktop Adobe AIR (both are cross browser and cross-platform respectively).

When one creates the Flash .swf file (which has all the rich UI), when invoked it connects to the Adobe Flash Player -> which in-turns contacts the Web Server where all the business logic is invoked through the set of tags. One could as well call the Web Service methods.

Using Adobe Flex Builder which is very powerful IDE for making Flex applications has suite of controls like Data Grid, Tables, to name a few.

We looked at a very simple application, where in some controls were added to it and .swf file was created. The same code was used in Desktop Application and was deployed.

One more feature, that makes it pain free is packaging. It provided a packaging functionality where Desktop application was packaged and deployed at the system.

Sunday, April 20, 2008

BarCamp on Sunday...


I went to BarCamp at IIM, Bangalore on Sunday.. gosh, I missed my sleep, that what I was thinking before, but after going there I wasn't feeling too much bad. There were few good topics which were presented and discussed.

The crowd was very less as compared to last BarCamp in Bangalore. Though I met few of the people, whom I knew in last BarCamp, some them who inspired me to write blog even, and here I am...

Though, I first went to MySpaces.com presentation, though it sounds nice, the presenter presented on the rich API MySpace provide in order to build application. But I guess I was looking much more interesting topic so I quit from there.

Next, I went to a presentation where Naveen (if I am correct!!!), was presenting on integration of BIRT to the RIA, though it was interesting as I worked on BIRT sometime back, but I missed the initial thing, hence joined people arguing about one way or the other using Business Intelligence tool.

Next, I had a cup of coffee; I was bit sleepy now, so I rejuvenated myself.

So, I thought to go to a discussion which is totally apart from my domain of interest. So I went to VoIP a.k.a. Voice over Internet Protocol. They were discussing why VoIP is still not favored by Indian government and how its been popular West and South – East countries. But we explore it using Skype like software but it yet has to become legal so that we could call locally within India using VoIP. We should protest, and I am ready…

By now, it was lunch time, so I went to have lunch. As I said, it takes pro-active nature to get to know people. Hence I met few good people in discussion talks and during lunch hours who are working closely in the domain of my interest and not of my interest as well, but doing cool things. I get to know how people are scaling their applications in order make a mark in the market. All Web 2.0 technologies are not hot piece but some are. There were talks about how Adobe is making use and aggressively marketing its Web 2.0 tools, though they are paid frameworks. People also talking about OSS tools as well and how Web 2.0 is making buzz in the computing market.

So lunch was good, now I went to talk / discussion, where Vinayak from Akamai, talking about High Performance Websites. Though I herd of High Performance Computing, but High Performance Websites, what is this… ????

Vinayak, talking about how you could make your website popular from 10 hits per day to 100,000 hits per day and increase them. He talked about the caching technology, As he was from Akamai, which deals in providing services to companies like MySpace in server and stuff, so he knew where the bandwidth is eaten up in your web site. He talked that with little tweaks, you could make your website fast downloadable when network is slow. That’s nice one, I must say…

Next, I was searching but tired how you could make your software as a service, but I couldn’t find. Then, I thought, lets call for the day, and called my chauffer, and went back home…

Wait a sec, I also got a shirt as well as a souvenir to remember BarCamp the whole year and look forward to participate in it next year… So kudos to all who started this concept and its really worth appreciation….

The Water Horse: Legend of the Deep


So, again this weekend I was forced to watch a movie... nahin...... dosto.. main mazak kaar raha tha.. not forced, but invited... (no.... friends.... i am joking)

I was totally talli (drunk) before watching this movie and after watching I became some what more talli... what a movie is it ... first of all this movie is in English that too with no hot scenes and all cartoon dragon moving here and there, and the little kid running after it to save from not so cruel people, arre bhai saab log dushman nahin hote hain (all people are not enemies)



well it was ok movie worth not watching ... hall was empty and we were bound to keep are legs above the chairs, as if its our theater... one couple was sitting in the row below us, they moved couple of row below .... I told my friends to behave in very gentle man manner, but ....

anyways.. Ashish Bhai, I am again insisting that it was very good movie.. thank u once again... aapkee choice kaa jawaab nahin... hahahhahehehehehehe

Thursday, April 17, 2008

BarCamp - the next big thing in Bangalore



So BarCamp is here once again... It will be my second time I will meet my old and new friends all of similar interest. Its the one place where like / unlike minded people meet, share ideas, collaborate and speak together. Yes, there is lot of technology transfer, meeting new people whom you dont know and like to know. But all said, one has to make an initiative. The BarCamp is self less initiative by group of collectives who present their ideas, talk and present some very cool stuff.

Not to forget the very warm and welcoming IIM Bangalore, which host the event, the one of the best place in Bangalore. I just luv it....

What to get to know about in BarCamp. Well if you just browse to the BarCamp 6 you will catch up several interesting topics. If you are technology enthusiast, you could catch up with upcoming and Web 2.0 talks or VoIP or Signal Processing and much more, social networking people could catch up with Future of Social Networking, Marketing, Digital Media all under one roof...

So gear up, get ready and hit the floor... Catch you all at the BarCamp... at IIM Bangalore...