Friday, May 23, 2008

An Intro to Open Source Software Development

This article tries to give a mild introduction for those Java programming geeks who want to contribute to open source softwares but who get lost in this maze of tar balls with documentation, source code, third party jars and binaries.

this is not another creative writing attempt from the author.... lets get down to the business now.

For anyone who can understand the English language it is obvious that an Open Source Software is a piece of software which has it's source code open to everyone in this world who want to study or modify it. For someone who is better than a layman in software industry, an open source software is not just free software but also has a community of developers associated with it who are constantly working towards contributing to its development and also providing the required support through forums and mailing-lists.

basically what the author is trying to say is since they are several fellows work on the same thing, one or another will help you out if you have any problem.

For a more book type definition of what an open source software is, you can refer to [1].
Some organizations like the Apache Foundation [2] have plethora of open source softwares for various purposes.

what is there in it for an average programmer .... ;-) not me

Several of these open source softwares are written by experienced developers who want to solve some problem using a software. Hence most of them are well-written and could be studied by less experienced programmers who want to improve their coding skills.

what is there in it for an experienced programmer?

Well experienced programmers can show case there problem solving skills and good coding practices.

Now getting down to what you need to do to work with an open source Java software. First of all you need Computer of course, a JDK, an IDE like Eclipse. What you have to do is:
  1. Download the source code.
  2. Download the binaries.
  3. Download documentation like user manual or javadocs.
Thats all ! you are ready to go ........

  1. You have to import the source into eclipse as a project.
  2. You need the helping jars to compile the source code. (You can find them in the source or the binaries zip).
Since you can compile and build the source code you can make changes to it ! and even contribute to it.

Just let the code flow through your fingers ......Happy Open Sourcing !!!

References:

[1] http://en.wikipedia.org/wiki/Open_source_software
[2] http://jakarta.apache.org/







Monday, October 29, 2007

JVM Monitoring using JMX

We can manage Java applications through JMX API. Java supports JMX from jdk1.5 onwards. JConsole is a tool for monitoring local or remote JVMs. This entirely works on JMX technology. To do what JConsole does we need to write a JMX client. This client would get access to the MBean exposed by JVM by connecting to the JMX Agent in that JVM.

How to write a client is explained clearly by this blog [1]. We can modify the code given in the blog to access the memory usage details of the JVM. We have to gain access to the MemoryMXBean[2].

There are some issues which have to be taken care of when running the application to be monitored.[3] The java application should be run using the following parameters:

-Dcom.sun.management.jmxremote.port=5009
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

The Remote Client connects to a JMX agent using an RMI connector with the following URL: [4] service:jmx:rmi:///jndi/rmi://hostName:5009/jmxrmi.



References:

[1] http://blogs.sun.com/jmxetc/entry/how_to_retrieve_remote_jvm
[2] http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/MemoryMXBean.html
[3] http://72.5.124.55/javase/6/docs/technotes/guides/management/agent.html
[4] http://forum.java.sun.com/thread.jspa?threadID=5212843

Monday, September 17, 2007

Settings Parameters in HttpServletRequest

Sometimes we need to set some parameters in a request when forwarding the request to a new jsp or a servlet. We can achieve this thing using the class javax.servlet.http.HttpServletRequestWrapper.

We have to extend a the above class and define few methods like setParameter() and getParameter() and we are ready to go !!


public class RequestWrapper extends HttpServletRequestWrapper {
private HashMap fakedParameters;
private RequestWrapper(HttpServletRequest nested)
{
super(nested);
fakedParameters = new HashMap(nested.getParameterMap());
}
public void setParameter(String key, String value)
{
fakedParameters.put(key, value);
}
public String getParameter(String name)
{
return (String) fakedParameters.get(name);
}
}


we can then use this wrapper object as a new request !!


Tuesday, August 28, 2007

Synchronization Simplified

Most of the Java applications are multi-threaded due to the obvious reasons of better performance and better utilization of resources.

Hence in such applications there are many occasions when two or more threads share an object. Which may lead to situations called “race condition” in which two or more threads are trying access the methods of same object. For Example there may be a case when one thread is writing something to an object while another thread is trying to read from it. In most situations, a race conditions is more subtle and less predictable, because you cannot be sure when the context switch will occur. This can cause the program to run right one time and wrong the next.

When two or more threads need to access an object which is a shared resource they need some way to ensure that the resource will be used by only one thread at a time. The process by which this achieved is called synchronization.

The access to the object can be serialized by the process of synchronization. This is achieved by the use of the keyword “synchronized”. You can synchronize your code in two ways:

Modify the methods of the object with the synchronized keyword. Which makes the object “thread-safe” which implies it is safe to use this object in a multi-threaded application. For Example a Vector is thread-safe whereas an ArrayList is not.

If you want to use an object which is not thread-safe in a multithreaded application we have to use synchronized blocks.

For Example if Person is not thread safe and p is an object of person with Thread t.

Class Person

{

int age;

public void setAge(int age);

}

To use p in a thread safe manner we use

synchronized(p)

{

p.setAge();

}

A Simple Example for understanding Synchronization.

class Person

{

int age;

public void setAge(int age)

{

this.age = age ;

}

public int getAge()

{

return age;

}

}

class MyThread extends Thread

{

Person p;

int age;

public MyThread(Person p, int age)

{

this.p = p;

this.age = age;

}

public void run()

{

p.setAge(age);

}

}

public class TestSynchronization

{

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

{

Person p = new Person();

MyThread t1 = new MyThread(p,1);

MyThread t2 = new MyThread(p,2);

MyThread t3 = new MyThread(p,3);

t1.start();

t2.start();

t3.start();

System.out.println(“”+p.getAge());

}

}

The output of this simple program is unpredictable because the three threads t1,t2,t3 go into a race condition and depending on the context switch the execution of threads take place. The output can be 1, 2, 3 when we expect 3.

To make the program behave in a predictable way we use the synchronized keyword.

We just have to make the methods setAge() and getAge() synchronized.

Hence it is advisable that in multi-threaded applications we use thread-safe classes like Vector, HashTable, etc.

If it is inevitable to use some of the collections in multithreaded environment we can use the Collections class to make synchronized ArrayList, HashMap etc.

ArrayList synchronizedArrayList = Collections.synchronizedList(arraylist);

By the way the HttpSession class is also not thread safe !!!.

That’s all about synchronization….. happy multithreading ….be thread-safe ;-)

Friday, August 17, 2007

JProfiler

Profiling is a set of techniques for estimating the amount of time spent in various portions of your program.

The size of the program unit being profiled is called the granularity.
For example, you can profile

Routines

Loops

Statements

Addresses

The profile will tell you how much of the execution time can be attributed to each grain you have selected. The most common grain is routine level profiling.

The output of a profiler indicates what fraction of the execution time was spent in each grain.
Presumably, to improve performance, you focus on the grains that are taking the most time.

Any tool which will enable you to do profiling of your program is a Profiler.

There are several commercial as well as open source java profilers.

(http://java-source.net/open-source/profilers)

Using Java profilers you can do profiling of a Java Application, Applet, J2ee components like servlets and JSPs and even J2me Midlets.

My experience with profiling has been plain old logging statements weaved in the production code and using JProfiler (a commercial Java Profiler) .

http://www.ej-technologies.com/products/jprofiler/overview.html

I have recently used JProfiler for profiling a J2ee application. I am very impressed with its easy to use wizard to integrate with any J2EE application server. I could easily integrate the Resin application server with this tool. The “cpu view” section of the profiler gives information about the method level profiling, about the threads in different states in the application. You can read about some reviews about this tool on

http://weblogs.java.net/blog/simongbrown/archive/2005/02/jprofiler_minir_1.html

I have managed to dig out some of the methods to concentrate on to improve the performance of the application.

All in all JProfiler is a very useful and easy to use tool …definitely not another buzzword for my CV. ;-)

Monday, November 13, 2006

A Small Note on Artificial Neural Networks and GIS

Artificial Neural Networks or ANNs as we popularly known are models that are designed to imitate the human brain through the use of Mathematical Models.

ANNs have been applied to problems like classification, time series analysis, wave/wind speed predictions, etc.

Most of the natural phenomenon are inherently random in nature and cannot be effectively represented or formulated as a series of Mathematical Equations.


ANNs have been proved to be successful in modelling such phenomenon as we can see in several publications citing to Wave Speed Prediction or Wind Speed Prediction [1].

( Iam citing Civil Engineering applications for the obvious reasons).



ANNs can be tightly coupled with GIS to create highly intelligent decision support systems(DSS).

One such example can be a GIS based Navigation System:-

On any given day vehicular traffic will vary through a city with respect to time of day, road network capacity and weather amongst other factors. A GIS can map the road network easily enough, but imagine an accident or some other event causing this flow of traffic to change. Traffic congestion would change with respect to other routes, those nearer the event becoming more congested. ANN input might include the location of the accident causing resultant congestion on arterial roadways, current weather conditions which influence speed and time of day, which relates to load. Using ANN all variables with respect to the accident could be processed resulting in a determination of optimum re-routing until the traffic flow is stabilized. In such a case, GIS mapping is used and spatial data acts as one of the input variables into the ANN. Taken one step further, a map server could update with latest conditions and transfer those to vehicles and or PDA – allowing individual drivers to follow the best selected re-routing. This would also be quite useful for emergency vehicle access purposes. [2]


Intellligent Planning Tool

Other example would be an intelligent GIS based tool for Fire Station Locations planning for future. This would involve prediction of location of fire incidents based on the location of past data. As I believe that ANN can extract a pattern from the past fire location data to predict the future locations. The Data which is obtained based on ANN predictions can be fed to a GIS based Simulation Software [3] which could predict the optimum location of fire stations for future years. I would also like to mention that this is a GIS-T application since it involves travel time optimisation on road networks.
[ I would like to work on this if I get funding !! for it ]

Finally I conclude that we would get the maximum utilization of GIS based Decision Support Systems and Artificial Intelligent techniques like ANN, heristics, etc when they are coupled together to solve real world problems.



Refrences:

[1] "Wind Speed Analysis using Artificial Neural Networks", Sandeep Kumar Jakkaraju, B Tech Project, Civil Engineering Department, IIT Bombay. (2001) [unpublished]

[2] "GIS & Artificial Neural Networks: Does Your GIS Think?" , Jeff Thurston - January 2002 GISCafe.com.

[3] Simulation of Fire Company Response Times, Jean-Claude Thill, Irene Casas, Sandeep Kumar Jakkaraju (SUNY-Buffalo) ,50th Annual North American Meetings of the Regional Science Association International, Philadelphia, Pennsylvania, Nov 20-22, 2003.


[ copyright Sandeep Kumar Jakkaraju , 2006]

Monday, July 31, 2006

Errors Encountered While Integrating the Libpqxx code with our Library.

  1. While integrating the libraries all of them should have the “Runtime Library” property set to “Multi-threaded Debug DLL (/MDd)”. Even if one of the libraries has different runtime library the console application project will give Linking errors. The errors are mostly “error LNK 2005” errors. For example like the one, TestRouteXMLGenerator error LNK2005: "public: __thiscall std::_Locinfo::_Locinfo(char const *)" (??0_Locinfo@std@@QAE@PBD@Z) already defined in msvcprtd.lib(MSVCP71D.dll).

Solution: All the libraries should have the “Runtime Library” property set to “Multi-threaded Debug DLL (/MDd)”.

  1. We were building all of our libraries in DEBUG mode and linking the Test console project with DEBUG version of static libraries except that of libpqxx as we did not have its DEBUG version. We got the following (Debug Assertion Failed) error at the point where we used to make the call to the libpqxx code when we linked with the release version of the library.

We did not get this error in the Release version since we were linking with the release version of libpqxx library.

Solution:

We created the DEBUG verion of the libpqxx library the libqxxd.lib and linked with it.

  1. The struct member alignment problem: The libpqxx library was created using the default struct member alignment of “8 Bytes”. All of our libraries are created using the struct member alignment of “1 Byte”. We got the following error just before the libpqxx code was ended.

Solution: Used #pragma pack to toggle between the two struct member alignments.

We put all the header files of libpqxx between the statements #pragma pack(push,8), to change to 8 bytes and #pragma pop (to get back to original stuct alignment of 1 byte ).

Wednesday, July 26, 2006

Hi

I have recently used this TinyXml to create XML DOM Document. It is very easy to use.
It is very light weight as in it just a few header and source files unlike the famous apache xerces.
I have modified the TinyXml package to give XML DOM Document as a string. I have just added a few functions in the tinyxml.h and tinyxml.cpp files.