So you want to be a ClassLoader champion?

It wasn’t until earlier this year when I started learning Scala that I had to venture into how a class was loaded in Java.  It wasn’t that Scala required you to know it, it was simply that they made reference to the Java class path loading basics. I think it was to demonstrate that if you wanted to use Java class, where it would be pulled from and when.

 Previously, at best the only thing I’d had to worry about the order that classes were instantiated and by whom, mainly when sorting out issues of Hibernate either having Lazy cglib instantiations when they were meant to be real classes and vice versa.  A completely different problem but the only time I’ve had to worry about the way objects and classes come to being.

Yesterday while flipping through Head First Design Patterns, I also came across the discussion about Class Loaders when talking about Singletons and having two threads instantiate a Singleton class each and how to avert the problem.

 Today, one of the senior developers was summarising an issue we had at work and mentioned that our custom ClassLoaders used by our webstartable app may have been the cause of the problem.

The universe is asking me to learn about classloaders, hehe….

 So, in short, there are three class loaders in the JVM.

  1. Bootstrap class loader – loads all the classes/jars in the JAVA_HOME/lib directory
  2. Extensions class loader – loads classes/jars in the JAVA_HOME/lib/ext/   (Something reminds me here of the libraries Quicktime would install, and if not, those hooks that Mercury Quicktest Pro would use to get into your java app to automate it)
  3. System class loader – looks at your CLASSPATH variable

So you can make your own classloaders in addition to these.  Wikipedia’s article sights reasons of encryption, rmi, basically any time you need some dynamic class loading :).

 A google search was rich with resources:

JBoss wiki contains a tutorial on ClassLoading

The basics of Java class loaders

OnJava’s article: Internals of Class Loading

Now for something Obvious

In this presentation about FIT, the presenter, David, takes a look at the meaning of the term ‘the build is broken’.  What it is now compared to 10 years ago.  He states that as technologists we should be proud that we’ve evolved the meaning from the literal code broke, won’t compile, to now a meaning saying that the tests are broken. 

Given that in Eclipse, I’m told in almost realtime where compilation errors occur as I write them, and how this acheives high build success rate, I wonder if Eclipse can precompile and debug JUnit tests so that as we are changing code that is used in a test class, the test runs and the results are fed back in almost realtime, allowing us to pickup on even more potential hazards.

Lots of pitfalls for the cynical but its so crazy it just may work.

Hibernate Synchroniser

This article on OnJava.com demonstrates the use of the HibernateSynchroniser plugin for Eclipse that apparently keeps hbm mappings in line with Java code. 

 The page is a bit old but references Eclipse 3.2, so hopefully the plugin is still useful.

 I’ve been using the JBoss Hibernate Tools which just entered 3.2 GA and it seems to be making leaps and bounds.  The only crux is that you need to do a lot of preconfig work, it may be harder to introduce into an already established project that does its hibernate config programmatically.

One to One Mappings in Hibernate

Simple One to One using assigned identifiers (meaning you hae to set the id yourself) http://www.xylax.net/hibernate/onetoone.html

Using Formula’s in hibernate mappings and some other neat tricks round computed fields http://www.onjava.com/lpt/a/6092

Hibernates Own Reference Documentation
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-declaration-onetoone

… Chapter 7 on associations gives some examples.  http://www.hibernate.org/hib_docs/reference/en/html/associations.html

Basics Refresher: Sun Application Server

This works on glassfish as well

Command Does
asadmin start-domain Starts the domain called domain1
asadmin start-domain –verbose domain1 As above but provides lots of logging information
asadmin stop-domain domain1 Stops the domain
asadmin start-database Starts the JavaDB database
asadmin stop-database Stops the database

The admin console will default to http://localhost:4848

Swing Explorer

Initially Swing Explorer looked like it was the one size fits all solution to replace QTP.

Ok, I was a bit nieve, its more aiken to the Object Spy of QTP, allowing to see the heirachy of a Java object just by clicking it.

The neatest thing it has though is a Player to help diagnose the drawing an layout of your objects.  I thought this was a player, ala QTP recording and playback, but no, its a different kind but still very useful if you are trying to debug layout in a Swing app.