Friday, November 12, 2010

Pattern: How to start a thread of execution in Fantom

In Fantom, how do you start a new thread of execution? The pattern below is very simple and nice.

    const class Main {
        Void main() {
            svc := Actor(ActorPool()) { doSomething }.send(null)

        Obj? doSomething() {
            // Do something like binding to a port and listening for requests.
            // Or you can call another method that does that.

            return null

The code above might seem a little odd at first. All that it does is to create an Actor with a code block that will execute the doSomething method. By sending a dummy message (".send(null)"), the Actor instance is started.

The key here is that sending a dummy message to an Actor starts the Actor. This same trick may be applicable to other Actor based languages as well.

Tuesday, November 02, 2010

Lazy initializing default values

One of the tasks you will come across often is to look up a value in a map, while providing a default value for the looked up value. For e.g.

Properties prop = ...
Object value = prop.get("key", "defaultValue")

The issue with this pattern is that, what if arriving at default value is a costly operation. Even worse, there may be no need to compute the default value at all, may be because the get operation will always find a value in the properties.

During such times "lazy evaluation" comes to the rescue. This is not a new pattern, it has been around for long time. Just that some APIs are written with lazy evaluation in mind, and some not. In the example above, instead of taking the value itself, if the get method takes an argument that is callable or a lambda function, then it can use that function only under the situation that the value is not present.

The example below is from Fantom language when you attempt to get a value from a map.
    class Main {
        Void main() {
            myMap := [Str:Str]["k1":"v1", "k2":"v2"]
            echo(myMap.getOrAdd("k3", |Str k->Str| {return "v3"}))

What the piece of code above does is to compute the value of the key only when it is not present in the map. In the example above when you look up "k3" which is not found in the map, the lambda function is invoked to compute the value once.

Thursday, October 28, 2010

Initial thoughts on Fantom

Recently I started playing with Fantom language. So far my reaction is "Wow!". Its a beautiful language with a lot of cool things. I also ran into a few gotchas, but none that falls in the category of "ugly!".

Here are the things I really liked about the language (not necessarily in any order):
  1. Before any technical merits, the first thing I liked is the documentation. Most of the time, when I look for some documentation for a open source project, they notoriously suck. Fantom is the second project whose documentation I really liked and found my way around most of the time. (The first one is SLF4J.)
  2. The language is statically typed, with room for dynamically invoking methods on objects using a mechanism called "trap".
  3. One central theme I observed in the language is being able to reliably create an immutable objects ("const" classes). This simplifies a lot of analysis when it comes to concurrent programming. Also the language supports creating immutable Lists and Maps.
  4. The language has a few forms of literal expressions that the compiler understands and creates objects for you. For e.g. Map, List, Uri and Duration have equivalent literal forms. One of the places where supporting literal forms aces is during serialization. Any object that is serialized is humanly readable (There is provision for overriding this as well!). Likewise you can create objects from string literal forms that can either be read from a file or just constructed by the program on the fly. What that means? One particular case I can think of is ease of creation of test data.
  5. Any field you specify is automatically accessed through getter and setter generated behind the screen. If you want, you can add more checks and behavior to the getters and setters.
  6. For concurrency, the language provides Actors framework. This is one level of abstraction above thread. In short: actors respond to messages and these messages are immutable. In a way, it simplifies the thinking about concurrent programming.
  7. The language supports anonymous function blocks. i.e. functions are objects too! That helps you in having some neat and cool patterns in your code. Like "10.times { ... }" or "aList.each { ... }". That also means the language supports closures.
  8. The language has mixins support. Its a useful concept when you want to assemble behavior on a type.
  9. Java Annotations equivalent is called as facets. You can decorate a field, method or a type with facets. BTW, fields and methods are called as slots in Fantom. I guess that term came from Smalltalk.
  10. You can use any of the Java classes just by adding a "using ..." statement. That gives you enough power and you wouldn't be missing any libraries that you may not find in Fantom.
  11. The code you write runs in JVM, CLR and (almost in) JavaScript. The work to have full support on JavaScript is actively underway.

Okay. Now the gotchas.
  1. You cannot control how many actors you want to be simultaneously active. The language takes care of it for you. Sometimes, you may want to have control on this one.
  2. There is no single queue and multiple consumers paradigm in actors. Either the producer can keep enqueuing the work in the same actor's queue or enqueue work with multiple actors in a round-robin fashion. In a circuitous way, you can implement this.
  3. The way a a collections member field (a List or a Map) is serialized and deserialized, hmm ... still I am not quite clear. I need to read about that section again.

Please understand that these gotchas may not be true, as I am a beginner and may be doing things in a wrong way.

Above everything else, I like the community of Fantom. Those guys are just amazing in terms of responding to my questions and helping to understand the concepts. I hope to write more about my adventures in Fantom-land. Stay tuned.

Tuesday, October 19, 2010

SecretKeyFactory is broken in JDK 1.6 update 22

If you upgrade JDK to 1.6 update 22 (build 04), the SecretKeyFactory is broken. As a result you will not be able to load any PKCS12 key stores. You will get NoSuchAlgorithmException thrown.

I have reported this issue in the bug database. You can view the bug here. I guess it will take upto a day for this bug to be externally visible, if you don't have a SDN account.

Here is the sample program to reproduce the issue:
public static void main(String[] args) {
        SecretKeyFactory instance = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        System.out.println("Returned instance: " + instance.getAlgorithm());

You will get the exception below:
Exception in thread "main" PBEWithSHA1AndRC2_40 SecretKeyFactory not available
    at javax.crypto.SecretKeyFactory.<init>(DashoA13*..)
    at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
    at main.TestClient.main(

Work around: Until the issue is fixed, revert back to JDK 1.6 update 21. I will update my blog once if I find any fix or work around.

Update on 10/25: Actually I figured one thing: I had update 21 as the project's default JRE in my Eclipse, but tried to run the application using update 22. When I changed the project's default JRE to update 22, I didn't get the error anymore. I am guessing that could be the problem. I am not having the issue any more.

Saturday, October 16, 2010

Performance of BigInteger.toString(radix)

You are given a byte array, that represents a number in big endian format (the most significant byte first). You have to convert the byte array to its equivalent hex string. What is the most efficient way to do it?

There are many ways to do it. But let us start with the easiest and correct one and optimize our solution. BigInteger class provides a constructor to convert the byte array to a BigInteger. We can convert that BigInteger to a hex string using the BigInteger.toString(radix) method. The solution is given below:
public static final String toHexStringUsingBigInteger(byte[] input) {
        BigInteger bi = new BigInteger(input);
        return bi.toString(16);

We can also come up with a hand crafted solution that extract nibble by nibble and convert them into their equivalent hex character and finally forming a string. This solution is given below:
public static final String toHexStringUsingCharArray(byte input[]) {
        int i = 0;
        if (input == null || input.length <= 0)
            return null;

        char lookupArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] result = new char[input.length * 2];

        while (i < input.length) {
            result[2*i] = lookupArray[(input[i]>>4) & 0x0F];
            result[2*i+1] = lookupArray[(input[i] & 0x0F)];
        return String.valueOf(result);

For comparison purposes, let us write a test program and see how much time each implementation takes. The test program is given below:
public static void main(String[] args) {
        byte[] input = new byte[]{0x7D, 0x7D, 0x7D, 0x7D, 0x7D, 0x7D, 0x7D, 0x7D};
        long start = System.nanoTime();
        for(int i = 0; i < 100000; i++) toHexStringUsingBigInteger(input);
        long end = System.nanoTime();
        long start2 = System.nanoTime();
        for(int i = 0; i < 100000; i++) toHexStringUsingCharArray(input);
        long end2 = System.nanoTime();
        System.out.println(String.format("Using BigInteger  : %15d", (end-start)));
        System.out.println(String.format("Using char array  : %15d", (end2-start2)));

Here is the output of few runs of the program:
Using BigInteger  :       702013524
Using char array  :        42074621
Using BigInteger  :       711340129
Using char array  :        41369504
Using BigInteger  :       707484052
Using char array  :        41221440

WOW! You see the difference between the BigInteger version and the hand crafted version? Hand crafted version is almost 16 times faster. Why?

You can take a look at how BigInteger.toString(radix) is implemented in the OpenJDK here. The most important point is, it is making use of Conversion.bigInteger2String() method. You can view the source code of that here. Ultimately, bigInteger2String() method is written such a way that it can cater to different radices and different locales. Most of the complexity in that function is around this concern. That is the reason why BigInteger.toString(radix) is terrible in performance.

Moral of the story:
If you are planning to use BigInteger and convert that back and forth to hex string, you are better off writing your own version of toHexString as the one given above, instead of using what you get with the library. You can invoke the BigInteger.toByteArray() and pass the byte array to your toHexString method.

Most important caveats:
Beware of handling of negative numbers between the two approaches. In case of the second approach, you will not get the sign right. You will always get a hex string for the bytes given as argument, without any special interpretation of the bytes. Where as, the BigInteger approach treats the bytes as a signed integer value. Consider the following test program:
public static void main(String[] args) {
        byte[] input = new byte[]{(byte)0x8D};
        System.out.println("From BigInteger : " + toHexStringUsingBigInteger(input));
        System.out.println("From char array : " + toHexStringUsingCharArray(input));

And the output is:
From BigInteger : -73
From char array : 8d

Depending on your application, you will have to choose one usage over the other.

The other important thing is that the code given above doesn't skip the leading zeroes. You can make a small modification and make it to skip leading zero bytes.

Thursday, October 14, 2010

Issue of autoboxing and reflection

Problem: Consider that you want to have a method called "Object invokeAndGet(Object targetObject, String methodName, Object... args)". This method should be able to invoke the given method on the target object and return the value returned by the invocation. If the method has overloaded forms, then depending on the argument types, the invokeAndGet method should invoke the correct form.

Though this problem looks trivial and seems like it can be solved by using reflections APIs, it gets really tricky when you have to deal with primitive types. This is due to the fact that autoboxing converts the primitives into their corresponding wrapper types. For e.g. an integer value is autoboxed into Integer object, etc.

To begin with let us assume that we have the following implementation of the method:

    public static Object invokeAndGet(Object obj, String methodName, Object... args) {
        try {
            Class<?>[] argsTypes = new Class[args.length];
            for(int i = 0; i < argsTypes.length; i++) {
                argsTypes[i] = args[i].getClass();
            Method m = obj.getClass().getMethod(methodName, argsTypes);
            Object retObj = m.invoke(obj, args);
            return retObj;
        } catch (Exception e) {
            System.out.println("**** Exception thrown: " + e);
            return null;

What this code is straight forward. It gets the types of the arguments. Attempts to find out if the target object's class has any method matching the method name and the argument types. If one is found, then that method is invoked. Otherwise null is returned.

The tricky part is this. Assume that the target object's class has a method "void setX(int x)". If you attempt to do invokeAndGet(targetObject, "setX", 1), it will fail. The reason is because of the fact that the argument 1 is converted to its wrapper type, which is Integer. So when you look up, you are looking up for the method with the signature "setX(Integer)", where as the method that is present in the target object has the signature "setX(int)".

There is no bullet proof solution for this problem. One of the ways you can try to solve this issue is by retrying with primitive types when you get a NoSuchMethodException. The modified version of the code is given below. It works for most of the cases:
    public static Object invokeAndGet(Object obj, String methodName, Object... args) {
        try {
            Class<?>[] argsTypes = new Class[args.length];
            for(int i = 0; i < argsTypes.length; i++) {
                argsTypes[i] = args[i].getClass();
            Method m = null;
            try {
                m = obj.getClass().getMethod(methodName, argsTypes);
            } catch (NoSuchMethodException nsme) {
                boolean signatureModified = false;
                for(int i = 0; i < argsTypes.length; i++) {
                    Field field;
                    try {
                        field = argsTypes[i].getField("TYPE");
                        if(Modifier.isStatic(field.getModifiers())) {
                            argsTypes[i] = (Class<?>) field.get(null);
                            signatureModified = true;
                    } catch (Exception e) {
                        // Ignore these exceptions. There is nothing we can do by catching them.
                    m = obj.getClass().getMethod(methodName, argsTypes);
            Object retObj = null;
            if(m != null)
                retObj = m.invoke(obj, args);
            return retObj;
        } catch (Exception e) {
            System.out.println("*** Exception thrown: " + e);
            return null;

What I am doing is very simple. Whenever NoSuchMethodException is thrown, I convert all the wrapper classes to their respective primitive classes by accessing the "TYPE" static field in them. For e.g. Integer.TYPE will give me int.class. Then I retry to get the new method, if I the signature had been modified since the last attempt.

I said that almost works. Here are the corner cases:
1) If your method has mixed type arguments, some of them are primitives and some of them are wrappers, the code given above will not work. For e.g. if you have a method with signature "setXIf(int oldValue, Integer newValue)", then invokeAndGet(targetObject, "setXIf", 1, 2) will fail.
2) There is a remote case when one of the argument class has a field called TYPE which is of type Class. In such cases also the above code will fail.

Please let me know if you know of a better method than the one given above to reflectively invoke a method on an object. I will appreciate that.

Tuesday, October 12, 2010

Creating a HashMap with entries

Problem: Create an instance of HashMap in Java with entries in it. I don't want to create a new HashMap instance and keep adding entries to it. I want to have a concise way of creating a HashMap with entries.

Solution: Guava library comes with factory methods that can create a HashMap without using the verbose form of "Map myMap = new HashMap()". You can simply say "Map myMap = newHashMap()", assuming you have done a static import of the Maps.newHashMap method. But that is not sufficient. It would be better to provide a utility method that looks like this: "Map myMap = newHashMapWithEntries(firstKey, firstValue, secondKey, secondValue)". That way it is easy to create static-final maps instead of writing a separate method to populate them or to populate them from constructor, as given below:
public class MyClass { 
   private static final Map<String, URL> serviceUrls = createServiceUrlsMap(); 
   private static Map<String, String> createServiceUrlsMap() { 
    Map<String, URL> retVal = new HashMap<String, URL>(); 
    retVal.put("google", new URL(""); 
    retVal.put("yahoo", new URL(""); 
    return retVal; 

That is too verbose. What I want is something like:
public class MyClass { 
   private static final Map<String, URL> serviceUrls = newHashMapWithEntries( 
       "google", new URL(""), 
       "yahoo", new URL(""));  

So here is one possible implementation of newHashMapWithEntries:
    public static <K, V> Map<K, V> newHashMapWithEntries(K firstKey, V firstValue, Object... rest) {
        if(rest.length%2 != 0) {
            throw new IllegalArgumentException("Expected the number of args to be even.");
        Class<? extends Object> keyType = firstKey.getClass();
        Class<? extends Object> valueType = firstValue.getClass();
        Map<K, V> retVal = new HashMap<K, V>();
        retVal.put(firstKey, firstValue);
        for(int i = 0; i < rest.length; i += 2) {
            if(!keyType.isAssignableFrom(rest[i].getClass())) {
                throw new IllegalArgumentException("Expected all keys to be of type <? extends " +
                        keyType.getName() + ">, but found " + rest[i].getClass().getName() + ".");
            if(!valueType.isAssignableFrom(rest[i+1].getClass())) {
                throw new IllegalArgumentException("Expected all values to be of type <? extends " +
                        valueType.getName() + ">, but found " + rest[i+1].getClass().getName() + ".");
            retVal.put((K)rest[i], (V)rest[i+1]);
        return retVal;

Saturday, October 09, 2010

Bottle - A simple web framework in Python

I recently came across the Bottle. It is a web framework written in Python. Its very simple to learn and use. It is ideal for prototyping kind of work or you want to run a web application in no time.

Sunday, September 26, 2010

Understanding Java Memory Model

I was browsing through the Linux kernel documentation and came across this excellent documentation on Memory Barriers. I was able to relate many of the concepts explained in this document with the issues that used to exist in the older buggy JVMs. I would strongly recommend to anyone to go through this kernel document to easily understand the 1.5+ Java Memory Model, especially the concept of happens-before ordering. The same author has written another excellent paper "Memory Barriers: a Hardware View for Software Hackers", which covers the same topic in a bit more depth.

Paul McKenney and David Howells - thanks a lot for your excellent document that helped me understand the key concepts behind memory barriers.

Tuesday, August 10, 2010

Is Java getting more and more complex

Recently I read an article in Artima, titled "Have Generics Killed Java?". This article is not the first one to complain about the complexities introduced into Java due to Generics. I used to a C++ programmer for around 6 years and been a big fan of the language. When I switched to using Java, during the pre-Generics era, I simply loved the simplicity and lucidness of Java. After reading the section on Generics in Effective Java, and having learnt the IFs and BUTs of the Generics, I realized how many things one has to remember to make effective use of Generics.

I rememberd an answer given by Bjarne Stroustrup, the creator of C++, a few years before the introduction of Generics. The gist if his answer was that every language commercially successful and used in large scale follows exactly the same path C++ and (now) Java is following. That is to start simple and eventually getting more and more complex.

After reading the FAQ for C++0X and some of the modules in Boost library, I now feel that C++ is conquering complexity, especially concurrent programming, using some elegant abstractions. Despite the attempt to conquer complexity, I personally feel that C++0X has too many things to remember :-(

Saturday, August 07, 2010

FEST libraries - a useful set of tools for testing

I came across an article in that talked about the FEST libraries. I felt I should have known about the FEST libraries a earlier. They are pretty powerful in terms of expressing test cases in a human readable form. In FEST site itself I found a link to internal DSLs in Java which was quite an interesting reading.

Friday, August 06, 2010

Eclipse Helios JEE and Tomcat issue

I installed the latest Eclipse Helios JEE package. Every time when I try to start my Tomcat instance, I started getting 100% CPU and an non-responsive Eclipse. I figured out that this was an issue with the WTP that comes by default with the Eclipse package (which I think is 3.2.0) and this issue was fixed with WTP version 3.2.1. If you try to update this package by using "Help->Check For Updates" it won't update. So here is how you can update:
  1. Go to "Help->Install New Software".
  2. In the "Work with:" input box, give the URL "".
  3. Wait for all the package information to be downloaded and select "Web Tools Platform (WTP) 3.2.1" option. If you are planning to use any other package, you can select them. But make sure you select the ones from 3.2.1 version distribution.
  4. Click on Next and follow the prompt to complete installation. 
If you restart Eclipse, you should be fine. You should no longer see any 100% CPU issues.

If you are interested in knowing more about the issue, please refer here.

Monday, August 02, 2010

Cobertura and Spring auto proxying

If you are using Cobertrua to get coverage reports, you may run into the error message shown below (lines folded for clarity):
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed;
nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type
'$Proxy28 implementing net.sourceforge.cobertura.coveragedata.HasBeenInstrumented, org.springframework.aop.SpringProxy, org.springframework.aop.framework.Advised' to required type 'com.mydomain.MyDao' for property 'myDao';
nested exception is java.lang.IllegalStateException: Cannot convert value of type
[$Proxy28 implementing net.sourceforge.cobertura.coveragedata.HasBeenInstrumented, org.springframework.aop.SpringProxy, org.springframework.aop.framework.Advised] to required type [com.mydomain.MyDao]
for property 'myDao': no matching editors or conversion strategy found
 Specifically if you are using AspectJ auto proxying, like below:
 To fix the issue, just add the following property:

    <aop:aspectj-autoproxy proxy-target-class="true" />
That should fix the exception shown above. To know more about this property please refer to Spring documentation.

Bean properties printer - a useful debugging tool

There are many instances when I wanted to just log the properties of a given object (most of the time its a bean). This will be useful in two ways: 1) learn about the concrete type of the object 2) serves as a good learning aid to understand what are all the properties that can be get/set in that object. Of course, if you have the documentation and the source code for the class in question, that would be the ultimate aid.

Nevertheless, the following piece of code would be useful to print the properties of the given object/bean, using the getter methods that are available in the object.
    public static void printProperties(String msg, Object o) {
        String className = o.getClass().getName();
        if(msg != null && msg.length() > 0)
  "{} (type {})", msg, className);
        Method[] methods = o.getClass().getMethods();
        for(Method m:methods) {
            if(m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
                try {
                    Object returnValue = m.invoke(o);
          "  {} -> {} (type {})", toObjArr(m.getName(), returnValue,
                } catch (Exception e) {
                    logger.error("Exception thrown while invoking [{}]: {}", m.getName(), e);
You can view the source code of toObjArr() here. This method can be improved a bit. For e.g. methods starting with isXXX are not invoked in the code. Also, a recursive way of printing the properties (with an optional max depth) can be provided.

A sample output could look like this:
Contents of the SAML request (type org.opensaml.saml2.core.impl.AuthnRequestImpl)
   getSubject -> null (type org.opensaml.saml2.core.Subject)
   getOrderedChildren -> [org.opensaml.saml2.core.impl.IssuerImpl@15e14d9, org.opensaml.saml2.core.impl.ScopingImpl@1aacada] (type java.util.List)
   getConditions -> null (type org.opensaml.saml2.core.Conditions)
   getRequestedAuthnContext -> null (type org.opensaml.saml2.core.RequestedAuthnContext)
   getProtocolBinding -> urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST (type java.lang.String)
   getAssertionConsumerServiceIndex -> null (type java.lang.Integer)
   getAssertionConsumerServiceURL -> http://localhost:8080/mySP/resources/saml/SSO (type java.lang.String)
   getAttributeConsumingServiceIndex -> null (type java.lang.Integer)
   getProviderName -> null (type java.lang.String)
   getNameIDPolicy -> null (type org.opensaml.saml2.core.NameIDPolicy)
   getScoping -> org.opensaml.saml2.core.impl.ScopingImpl@1aacada (type org.opensaml.saml2.core.Scoping)
   getVersion -> 2.0 (type org.opensaml.common.SAMLVersion)
   getID -> a3hj688f209c170h433b6eh0h73e0j2 (type java.lang.String)
   getDestination -> http://localhost:8080/myapp/profile/SAML2/POST/SSO (type java.lang.String)
   getIssuer -> org.opensaml.saml2.core.impl.IssuerImpl@15e14d9 (type org.opensaml.saml2.core.Issuer)
   getIssueInstant -> 2010-08-02T18:37:33.528Z (type org.joda.time.DateTime)
   getSignatureReferenceID -> a3hj688f209c170h433b6eh0h73e0j2 (type java.lang.String)
   getConsent -> null (type java.lang.String)
   getExtensions -> null (type org.opensaml.saml2.common.Extensions)
   getValidators -> null (type java.util.List)
   getSignature -> null (type org.opensaml.xml.signature.Signature)
   getParent -> null (type org.opensaml.xml.XMLObject)
   getDOM -> null (type org.w3c.dom.Element)
   getSchemaType -> null (type javax.xml.namespace.QName)
   getElementQName -> {urn:oasis:names:tc:SAML:2.0:protocol}AuthnRequest (type javax.xml.namespace.QName)
   getIDIndex -> org.opensaml.xml.util.IDIndex@1ab000c (type org.opensaml.xml.util.IDIndex)
   getNamespaces -> [xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol"] (type java.util.Set)
   getNoNamespaceSchemaLocation -> null (type java.lang.String)
   getSchemaLocation -> null (type java.lang.String)
   getClass -> class org.opensaml.saml2.core.impl.AuthnRequestImpl (type java.lang.Class)

Hope this serves as a useful tool in your troubleshooting/debugging.

Friday, July 16, 2010

Logging three or more arguments in slf4j

I use slf4j for logging purposes. All the logging methods (, Logger.debug, etc.) provide an efficient way of passing one or two argument objects. For e.g."The response from server is [{}]", serverResp);
logger.debug("Key [{}], Value [{}].", key, value);
 But if you want to pass three or more arguments you have to create an object array yourself and pass. Like this:"Status [{}], message [{}], time taken [{} ms].", new Object[]{status, msg, timeTaken});
Doing a new everywhere in the code doesn't seem like an elegant way of doing it. I was thinking about a cool way of doing it, and this is what I came up with. Here is a utility method that makes use of varargs:

public static Object[] toObjArr(Object... args) {
    return args;

In the code we can use this utility function like this.
import static com.mydomain.Utils.toObjArr;
..."Status [{}], message [{}], time taken [{} ms].", toObjArr(status, msg, timeTaken));
A little-bit better readable than before.

Wednesday, June 23, 2010

Creating a Collection with a single element

Question: What is the most efficient way of creating a collection (Set, List or Map) with a single element?

Answer: The most efficient way of creating a collection with a single element would be to make use of the Collections.singletonXXX() methods.
  • Collections.singleton - To create a set that has only one element.
  • Collections.singletonList - To create a list that has only one element.
  • Collections.singletonMap - To crate a map that has only one entry.
The collections returned from these methods are immutable.

Compare these methods with Collections.unmodifiableXXX() methods. The umodifiableXXX methods already accept a collection as an argument.

Sunday, June 20, 2010

Java Native Access - An essential tool in Java tool box

I recently learned about Java Native Access (JNA) and was simply amazed at how easy it is to make native calls. I have used JNI earlier in my projects to access native code, but it is a bit painful experience. JNA makes it a bit easier to make those native calls. I haven't experimented much with passing structures and getting structures (or pointers to structures) as return values. As far as the arguments and return types are one of the primitive types, the interface is very easy to define and use.

Give it a try, you will like it.

Tuesday, May 18, 2010

Intercepting method calls of @WebService annotated classes

I ran into this issue recently and I thought sharing this with everyone will help in saving some time.

I am making use of Metro + Spring for my web service implementation. I have a requirement that I should intercept all the calls in the web service implementation (the class that has is annotated with @WebService) and log the time taken to execute each call.

So I created the following AOP advice:

public class WebServiceMethodsAspect {
    @Around("execution(* com.mydomain.service.*Impl.*(..)) && args(request)")       
    public Object interceptWebServiceCall(ProceedingJoinPoint pjp,
            Object request) throws Throwable {

Once created the aspect I enabled the annotation based aspects using the following tag in my applicationContext.xml file:

< bean id="webServiceMethodsAspect" class="com.mydomain.aspects.WebServiceMethodsAspect"/>

I was expecting everything to be working fine as soon as I restarted my app. But I got the following exception (I folded the lines for clarity):
Error creating bean with name 'MyServiceBinding' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Cannot create inner bean '(inner bean)' of type [org.jvnet.jax_ws_commons.spring.SpringService] while setting bean property 'service';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)':
FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException:
class $Proxy13 has neither @WebService nor @WebServiceProvider annotation

If you carefully look at it, the problem is due to the fact that I am intercepting calls directly in the web service implementation. When I provided the Aspect to intercept the calls in the web service implementation, Spring automatically created a proxy for my web service implementation. The @WebService annotation does not have the @Inherited meta-annotation on it. Hence the proxy doesn't have the @WebService annotation on it. Hence the object that was given to the binding didn't have the @WebService annotation on it.

To solve this problem, I introduced a delegator layer on top of the web service implementation layer and annotated the delegator with the @WebService. Delegator simply calls the underlying web service implementation (it just delegates the calls to the respective web service implementation). Now I can intercept the calls to the web service implementation, as only the delegator is used by in the binding.

If you need access to the WebServiceContext, you must explicitly pass that from the delegator to the web service implementation class.

Friday, April 23, 2010

Note on allocationSize parameter of @SequenceGenerator while using JPA

I ran into what I thought as an issue while I was using the sequence ID generation strategy in JPA. The JPA provider I am using is Hibernate. I think sharing my experience will save someone some time.

To use a sequence (For e.g. Oracle sequence) to generate values and assign them to the ID field of your persistence object, you will following something like this:

@Column(name = "ITEM_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="ItemIdSeqGenerator")
@SequenceGenerator(name="ItemIdSeqGenerator", sequenceName="ITEM_ID_SEQ", allocationSize=1)
private long itemId;
This means the following things:
  1. The @Id annotation says that the field itemId is a primary key.
  2. The @Column annotation says that the corresponding column in the database is ITEM_ID.
  3. The @GeneratedValue says that the value that needs to be populated in the itemId should be generated, while that object is persisted. The strategy to generate the value is to make use of a sequence. The details of the sequence are provided in the following annotation, that has the generator name "ItemIdSeqGenerator".
  4. The @SequenceGenerator annotation describes the sequence generator named "ItemIdSeqGenerator". The sequence in the database that needs to be used is called ITEM_ID_SEQ. 
So far so good. So roughly it implies that every time you try to persist the object, the itemId will be populated using a SQL command (in case of Oracle) "SELECT ITEM_ID_SEQ.NEXTVAL FROM DUAL". So to persist a new object, you will go to the database twice (once to get the NEXTVAL and once to insert the new row).

Imagine a situation where performance is critical and you are going to do a lot of inserts and a lot of IDs need to be generated. To help in such cases, the allocationSize comes to our help and that's exactly what this blog post is about.

allocationSize=N means that "Go and fetch the next value from the database once in every N persist calls. And locally increment the value by 1 in between.".

Let me give an example. Assume that the sequence is set up like this in the database:
And assume that all the values starting with 1 are legal (1, 2, 3, 4, ...) for the ITEM_ID column. In such case you will set the allocationSize=10. So the first persist call will go and fetch the ITEM_ID_SEQ.NEXTVAL from database. The subsequent persist calls will not go to the database, rather they will return last value+1 locally until the value reaches 10. That saves 9 database reads.

What if there are two entity managers that try to do the same thing? When the first entity manager calls the ITEM_ID_SEQ.NEXTVAL it will get 1 and the second one will get 11. Hence the first one will go on like 1,2,3,...10 and the second one will go on like 11,12,13...20, before fetching the next ITEM_ID_SEQ.NEXTVAL.

What if the allowed values are like 1, 11, 21, 31, ... and still you would like to make use of the allocationSize? So far I could not find how to do this! It would have been possible if we can specify an increment, like "allocationSize=100, increment=10" and set up the sequence as follows in the database:
As of now, if you have such requirement, you will have to set the value of allocationSize=1 and suffer the cost of going to the database every time when an object is persisted.

Remember another thing in mind. The J2EE documentation for the allocationSize says  an ambiguous thing which is quite different from the one I described above:
The amount to increment by when allocating sequence numbers from the sequence.
As per my experiments, the increments were always 1 :-(. This parameter just controlled how frequent to go to the database to fetch NEXTVAL.

If you are using an allocationSize greater than 1, it is important to remember that you should not assume that lower ID means earlier to invoke the NEXTVAL from the sequence. Don't try to interpret the ID in anyway other than just an ID to identify the row.

Tuesday, March 16, 2010

Tomcat JDBC-pool

There is a new JDBC connection pool implementation that comes with Tomcat, called JDBC-pool. You can read more about this pool implementation here.

I can already see that quite a few projects (including the Spring tc server) have started using or supporting this connection pool. I am currently hooked to DBCP, yet to try this pool.
There is also an article that compares DBCP and JDBC-pool.

Wednesday, February 24, 2010

Python 3 I/O

I came across this useful page that helps you understand whats new in Python 3 I/O and what are all the significant performance enhancements you can achieve/expect.

Returning an empty collection instead of null

For one of my recent projects, I had to work with legacy code. There were methods that are returning collections, like lists or sets. These collections were created by reading data from the database. Whenever there is no data to form the collection, these methods return null. Now that is bad!

One of the issues of returning null is that the caller always have to check for null. Most of the time the caller either searches for a particular value in the returned collection or iterate through the elements in the returned collection. The code will look like this:

List rows = myDao.getRowsFor("key");
if(rows != null) {
  for(Row row:rows) {
    // Do something with the row

As you can see, it is easy to avoid the null pointer check if the getRowsFor() returned an empty list instead of a null pointer when there is no data in the database.

The clutter caused out of the null pointer checks all over the code. You can read more about this simple principle in Effective Java and an excerpt of that can be found here.

SublimeText 3/Anaconda error

When I installed Anaconda manually by downloading and untarring the file (as given in the manual installation instructions here ), I got th...