Wednesday, December 31, 2008

Double Check in Java is broken .. not

Coders from the C world are most likely to use the so called double check idiom at some point of their coding career. If those same coders (like me) do their first multithreaded app in java will probably run into some problems.After my first trials with java several years ago I ran into some pretty weird problems. Sometimes my nice apps would expose the weirdest behaviour. After searching around I got educated in a newsgroup.
Before I start to explain what the problem is I should probably explain what double-check is.
Let's say there is a certain object that needs to be initialized. An initialization should only take place once. Sometimes it might be required to postpone the initialization until the first use of the object (lazy init) or until certain conditions in the systems are met (service orientated architectures).
The following piece of code illustrates the naive approach:>

Test initValue=null;

if(initValue==null){
synchronized(this){
if(initValue==null){
initValue=new Test();
}
}
}
So we first check if the variable has been initialized, if not we synchronize and check again and if this second check is succeeds we do the init work. The second check is done because another thread might have initilaized in the meantime.
The problem is that this won't work in Java. The JDK does a lot of work under the hood. On of these things is that threads can keep a local copy of variables. This behavior completely breaks double check.
That's where my story ended some years ago. I never touched it or bothered researching into it again.
Until I recently discovered atomic variables in Java and stumbled over the new memory model introduced with JDK 5.
After reading up on it I finally found the solution to fix double-check:
The volatile keyword.
Volatile indicates that the variable will be modified by several threads. This tells the JVM that threads are simply not allowed to keep local copies of variables.
Fixed double check locking looks like this:
volatile Test initValue=null;<br />if(initValue==null){<br />    synchronized(this){<br />        if(initValue==null){<br />            initValue=new Test();<br />        }<br />    }<br />}

Yep, I got my double-check locking back but there are still some things to consider:
  • Performance: If using volatile on a variable better do some benchmarking.
  • JDK 5: Yep, volatile only works like this with JDK 5 and later.



Wednesday, December 10, 2008

Spring, @Comparable, OSGi, Eclipse RCP in Eclipse Magazin

I wanted to post how I got Spring to use AspectJ for DI in Eclipse RCP applications. The post won't happen as I wrote an article about it for the German Eclipse Magazin. As soon as the article is out you will find a link to it in here.

Thursday, November 27, 2008

JAXB vs Interfaces

A more detailed post about my battle with Spring (I WON) will follow soon.
Right now I want to talk about some JAXB stuff.
JAXB is our tool of choice to store scene descriptions to a xml file. We are not working with a predefined schema but with annotated classes.
After all the trouble that people gave us by including a (IMHO) outdated version of JAXB  in earlier JDK 6 versions (Note: There is STILL no nice way to replace libraries shipped with Java with different versoins. Is it really that hard to provide Java with some hooks that would allow developers to do that??)  we were very happy when JAXB 2 finally found its way into later releases.
But that's where the happiness ends. Don't get me wrong JAXB is a nice tool to handle  lot of different scenarios. But why the hell did they have to make it so complicated to use interfaces?
If you start googling for 'JAXB and Interfaces' you will find a ton of people having problems to get this to work.
One of the first links you will stumble over is probably this one. A nice round up of the whole issue.
But still.
Right now I need to annotate a class in Designer using @XmlElement(Object.class).
The reason is that I need to keep a reference to a class that implements a given interface. I can't assume that the interface is implemented by a certain class.
I can think of several reasons why this is a problem for an xml-file but there has to be a nicer way to do this.


Monday, November 24, 2008

Fragments!!!!!!!

Another hard lesson learned.
In my quest to get Spring-DM plus Equinox plus @Configurable to run the following fact almost killed the remains of my sanity:
A plugin and its fragment need to be in the same directory for equinox to actually aply the fragment.
So, if you, like me, are a person that likes to keep things nicely separated into different directories you are screwed.
I battled for hours. In an act of desparation I copied everything into one directory (thereby destroying my nice targte-platform-structure) and suddenly it worked. I sorted out my stuff and finally came to this conlusion.
Yeah, seems that I got everything running by now.
*sigh*

Sunday, November 23, 2008

OSGi + Spring = ?

It took me a while to get back to my beloved Spring.
Before I start raving about this great framework some intro why I am doing this.
Dependency Injection is one of my most beloved patterns. It removes a lot of boilerplate code from your software if correctly applied.
When we started the Designer project we were looking into spring-osgi but decided to wait for the first final release to incorporate it.
Lookign back I don't know if that was the best way to go. We had to implement our own DI-Service in OSGi. So far it has worked pretty good but with a growing project you want get rif od as many headaches as possible.
One headache is that now almost all aspects of Rifidi are using our custom DI-Service and that means we have to deal with new features (unbelievable, some people really want software to evolve).
It would be great to get replace it with Spring-DM (the Spring OSGi project).
So today is the great day:
I will try to get services injected into an eclipse view.

Things accomplished this morning:
  1. Target platform is up and running (will be up in Rifidi subversion as soon as I am done fighting with it)
  2. Services get deployed to OSGi
  3. Dependencies get injected (right now using xml config files)
Next steps:
  1. Get AOP up and running.
  2. Inject services using annotations
  3. Get a view injected with a service

Wednesday, March 26, 2008

JAXB vs OSGI

A side note to those poor souls who have to get JAXB running with OSGI:
It works, but it is ugly.
A little story about the problem:
We have a ton of classes implementing different interfaces.
After putting a lot of work into refactoring the first release of designer I had created a ton of new interfaces.
We came to the point where we wanted to save all those nice classes with all the nice interfaces they implement to an XML file.
Since JDK 1.6 update 4 JAXB 2 is included in the JDK and we figured it might be good to give it another shot.
To make it short: as soon as one of our properties had an interface as its type the saving failed.
We followed the instructions on this site but JAXB kept telling us that it is unable to handle interfaces.
After some fiddling in a sandbox and reading through some osgi stuff I finally found out what to do:
Add the following JVM parameters and you are good:

-Dosgi.compatibility.bootdelegation=true -Dorg.osgi.framework.bootdelegation=*

It's a little brute force to do it this way and you will have a small performance impact on your app but at least it works.