Connecting to In-Memory H2 database using a SQL Client

In order to setup a in-memory H2 instance and connecting to it using your database client start h2 with following –

jdbc.url.default=jdbc:h2:h:/shivbox2;AUTO_SERVER=TRUE

More



Converting ASCII encoded file to UTF-8

You get a file whose encoding you don’t know and want to convert it to UTF-8 encoded file using java. How to do it?

Below should work –

Click to continue reading “Converting ASCII encoded file to UTF-8”

More



Eclipse Code Formatter – IntelliJ IDEA

When working with a team of developers spread across globe, one challenge is to maintain code formatting.

Most of teams using Eclipse as their primary IDE, rely on Eclipse code formatter that can be configured by enabling project specific setting and then adding /.settings directory to version control system. This directory contains org.eclipse.jdt.core.prefs which has formatting details.

Since these files are part of version control, all developers now has these settings file and formatting changes gets applied automatically on saving file.

But if you are an IDEA user, then there is was no way to use these Eclipse code formatting.

To make life somewhat easy, over the weekend I created a small plugin which basically let you use the Eclipse Code Formatter from IDEA. The main feature of this is that you can apply these on a Change List. Change List in IDEA is basically list of files that you have modified/created and are going to commit.

For more details visit project page – eclipse-code-formatter-on-change-list

The plugin is also available in IDEA plugin repository.

More



Java Map Interface Examples

Recently I was explaining someone about Java Map and various implementations that are available as part of JDK.

I had wrote a simple program to explain him that. Thought of putting it here. Below it is –

TreeMap – The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Below example you will notice that treeMap toString shows that entries are ordered by performing natural ordering on Int keys.

HashMap – This class makes no guarantees as to the order of the map. Uses object equality to check if key already exist in map and replace value if it exist. As in example while trying to put key11 as it equals with key1 value “One” is replaced with “Eleven”.

IdentityHashMap – Unlike above HashMap, this uses reference-equality in place of object-equality when comparing keys. As in following example even after key11 equals key1, but as their references are not equals so a new key/value is inserted.

LinkedHashMap – This implementation keeps track of order in which keys were inserted into the map. In below example you will notice that the toString shows entries in same order in which they were inserted. This also has another feature discussed next.

LRU Caching using LinkedHashMap – Another feature of LinkedHashMap is that it provide constructor to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order). In following example, you will notice that when elements are inserted and none is accessed yet, the order of Entries is same as that of above example. But after accessing elements, you will notice that the entries order is changed based on when last it was accessed. Using this feature one can create a LRU(accessed) cache as shown in example. You will need to override removeEldestEntry method of LinkedHashMap and add your logic for caching. Each put operation will call this removeEldestEntry by passing least-recently-accessed element to it and based on removeEldestEntry’s return value it will either be kept or removed from the map.

WeakHashMap– In this implementation keys are held as WeakReferences. This way if there is no strong reference for key, on next Garbage Collection this key and corresponding value will be removed from the Map. Read this article for details on Java References. As in below example we are adding three keys to map and then clearing one strong reference to key6 by setting it to null. Then we request GC by calling System.gc() and in next statement you will notice that the key/value for key6 is removed from Map.

Click to continue reading “Java Map Interface Examples”

More



Publishing Webservice on Multiple Network Interfaces

Recently our IT support group decided to move one of component to a new VCS Cluster.

This component is a service component exposing various web services which are consumed by another .Net based UI component.

The initial attempt was not successful as the component was incompatible with multi-homed hosts (those with multiple IP addresses / network interfaces), because it listen to port 80 on the primary network interface only.

As currently the component was running on a single-homed host, there’s no problem, since there’s only one IP address, and no ambiguity.

But VCS clusters are multi-homed devices – they have local IP, and cluster IP.

And in VCS world, nobody is connecting to host name / IP directly, connection should be established to cluster name / IP.

So to make the component publish web service on all interfaces, instead of using localhost or host address, use 0.0.0.0 as address. With 0.0.0.0 you tell it to ‘listen’ to incoming connections to any ip-address (associated with box).

More



TestDox and IntelliJ IDEA 9

I really missed TestDox plugin for IDEA. There is no official version as of now for IDEA 9.0

I tried to install latest available version but its not compatible. I played around with plugin.xml by removing until-build=”IU-93.94″, it got enabled but then threw following exception,

Caused by: java.lang.NumberFormatException: For input string: "IU-93.94"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	at java.lang.Integer.parseInt(Integer.java:449)
	at java.lang.Integer.parseInt(Integer.java:499)
	at org.codehaus.testdox.intellij.IntelliJApiFactory.<init>(IntelliJApiFactory.java:24)

I had patched the IntelliJApiFactory and updated .class file in the testdox-plugin-1.1.12-diana.jar. I am using it for some time and my usage is limited to navigate between class and its test. Not seeing any exceptions but overall IDEA 9.x plugin api are changed a lot and using a patched plugin might crash IDEA.

But for those who can’t live without it, can download it here. Extract it to your IDEA plugins directory.

More



Maven, dbdeploy, HSQLDB without Ant

Believe me, integrating Maven and dbdeploy is a pain using Ant. For some weird reasons, dbdeploy ant task was causing maven build to crash.

So tried to integrate dbdeploy with maven without ant on my Snow Leopard –

Click to continue reading “Maven, dbdeploy, HSQLDB without Ant”

More



Integrating Spring and EHCache

Using Spring Modules and EHCache, one can transparently cache method results. Spring Modules uses a proxy which intercepts call to the method of bean; consults the cache to check if method was called with same parameters before, if so will return cached result.

EHCache is the actual provider of caching solution and Spring Module handles method interception and result storing in cache.

Click to continue reading “Integrating Spring and EHCache”

More



Jetty Remote Debugging using IntelliJ IDEA

Recently wanted to remotely debug Jetty instance, which was using mvn jetty:run.

Using following MAVEN_OPTS worked for me –

$ export MAVEN_OPTS=”-Xms256m -Xmx512m -agentlib:jdwp=transport=dt_socket,address=7848,server=y,suspend=n”

After setting above executed –

mvn jetty:run

Then configured IDEA Remote Server by connecting to server on above configured port 7848


More



JSF Validations

Using JSF Standard Built-in Validations
JSF provides built-in support for validations of input component tags. You can supply it a attribute required=”true” which will check if a value is supplied or not. There are built-in validators which you can nest in all JSF input tags.

Click to continue reading “JSF Validations”

More



Next