April, 2008


26
Apr 08

An embedded Java DSL for manipulating hierarchical JavaBeans

Just yesterday I was thinking on how to improve the DSLs I’m working on at the moment and got an idea, which wasn’t too useful in the context of SQL, but could be used for something else.

Imagine we have three JavaBeans (I’m omitting getters/setters for readability):
[java]
class Company {
String name;
Address address;
Collection employees;
}

class Address {
String country;
String zip;
String town;
String street;
}

class Employee {
String firstName;
String lastName;
Address address;
}
[/java]

It’s a straightforward task to build a DSL for manipulating this data. It could look something like that:
[java]
Company c = new CompanyBuilder()
.name(“ZeroTurnaround”)
.address()
.country(“Estonia”)
.finish()
.addEmployee()
.firstName(“Jevgeni”)
.lastName(“Kabanov”)
.address()
.country(“Estonia”)
.finish()
.finish()
.toCompany();
[/java]

The trick is that the AddressBuilder hiding behind the address() call is used in two contexts — as an employee address and as a company address. In a large domain such situations are very common and we would like the corresponding builders to be reused.

What we can do is make every builder keep in mind it’s parent context like this:
[java]
public class EmployeeBuilder
implements Builder {
PARENT parent;

public EmployeeBuilder(Employee e, PARENT parent) {
this.parent = parent;
}

//Field setters …

public AddressBuilder>
addAddress() {
return
new AddressBuilder>(this);
}

public PARENT finish() {
return parent;
}
}

public class AddressBuilder
implements Builder {
PARENT parent;

public AddressBuilder(Address a, PARENT parent) {
this.parent = parent;
}

//Field setters …

public PARENT finish() {
return parent;
}
}
[/java]

This way the generic type becomes nested and holds the whole path to the root of the hierarchy in a typesafe way. Of course we also need to return the actual value independently of the parent, but it’s easy to do by passing a corresponding closure from the parent and calling it in the finish:

[java]

public class AddressBuilder
implements Builder {
//…

interface Closure {
void address(Address address);
}

public PARENT finish() {
addressClosure.address(address);
return parent;
}
}
[/java]

This trick can be reused for any kind of hierarchical composition — JavaBeans, XML and so on. For XML you could generate the DSL from the Schema, which is a relatively painful process. However for JavaBeans you can generate a builder per bean in a very straightforward way from the reflection information. Such a builder could be used for modifying JavaBeans as well as for constructing them.

It would most probably be simplest to output the bytecode directly and generate .class files or JAR for this DSL, which makes it an ideal test case for the typesafe bytecode engineering DSL library I’m working on :) Most likely I’ll implement this language as an example of using typesafe bytecode engineering, so if you’re interested keep an eye on the blog.


24
Apr 08

Live from Jax.de – OpenJDK keynote

This afternoon was a keynote by Dalibor Topic on OpenJDK and the Future of Open Source Java on GNU/Linux. I downloaded the OpenJDK 7 couple of weeks ago and have been using it to run some of the desktop Java applications. Hearing about the development processes behind the JDK was great.

OpenJDK is the JDK released under GPL v2, not vanilla GPL license but a modified one with a CLASSPATH Exception. It is 25,169 source files of which 4% is binary only (called “plugs”) and 8% licensed under a different license than GPL v2. The objective is to get everything out in source form. The parts that are causing problems currently are:

  • Font rasterizer
  • Graphics rasterizer
  • Sound engine
  • Crypto provider

The fun fact was that after JDK went opensource, the Gentoo ebuild came out in couple of hours, the Fedora source RPM int the same night. Those enthusiasts :)

As it is only possible to compile OpenJDK with Sun JDK (Sun JDK having proprietary extensions) the IcedTea project was launched, currently it is already possible to compile OpenJDK with OpenJDK :)

In the beginning of the year the OpenJDK VCS has been switched to Mercurial, here is a list of repositories. So Linux is GIT and OpenJDK is Mercurial, great to see some large projects not running SVN or the old CVS for a change.

The best part of the speech was about getting Java apps as easily installable as the current C/C++ projects, just a “apt-get install”. Their stated requirements for OpenJDK are:

  • Fully free – this is nearly complete
  • Packaged correctly
  • Reviewed and approved by distros

They are currently talking to Fedora, Ubuntu, Debian, Gentoo and OpenSuse. He stated that if your favourite distribution is not listed just contact him or the OpenJDK team.


22
Apr 08

The JAX misadventures: booth-o-holics

So once we got to Wiesbaden, we had to actually set up a booth. Of course, being software developers we lacked both the basic mechanical skill and any kind of eye for design. Therefore we experimented a lot.

We started by putting up the back wall:

img_0018.jpg

Well, it turned out that it’s huge!

img_0022.jpg

In fact since we had a bit of a unfortunate location, we ended up using the wall to set up a ZeroTurnaround themed coffee corner:

img_0045.jpg

Once we were done with that we just had to set up the booth itself. It wasn’t easy:

img_0024.jpg

We tried a couple of variations on the booth setup. The first one was like this:

img_0033.jpg

But we changed it as soon as the monitor arrived and ended up with this setup:

img_0046.jpg

The Expo is open today from 10:00 to 20:00 and so far we managed to talk to about 50-70 people. It’s a great feeling to see people’s eyes light up when they understand what does JavaRebel do exactly and that there is no 200 MB download or 100 lines of configuration to write to make it work.


21
Apr 08

The JAX misadventures: the beginning

When a bunch of geeks go on a road trip you can be sure that something funky is gonna happen. When you add to that a flight at 6 AM and a night with barely any sleep that something’s gonna hit you hard.

In our case it started when we arrived to the office to pick up 40 kg worth of stuff we had to take to the conference. Toomas had the key and once we got in and disabled the alarm, we started taking out the bags. Of course we thought that we disabled the alarm, which seemed to disagree on the matter. After about two minutes the alarm hit us hard, both inside and outside the office. Slight panic ensued, as patrol would delay us by at least 20 minutes while they sort this out.

Since getting to flight in time was most important we made a field decision to grab the stuff and run (we are thick as thieves after all). We both grabbed some stuff (the majority was left in), and ran out. Of course by running out we locked the door, leaving the doorcard inside. So here we are, hour before the flight, most important stuff in the office with the key, patrol due to come and delay us some more, getting to conference in time for the opening an impossible feat.

Ever tried calling people at 5 AM on Sunday morning? Well, we went through the whole list of everyone who has a doorcard. “Fortunately” it was short, since I recently exchanged the SIM card and most of the phone numbers were left behind. And it’s not true that people hate receiving calls that early. Nowadays they just don’t get them.

Luckily for us after about 10 calls we did get to someone who not only picked up, but also lived nearby. I was too panicked to make photos before, but I did photograph the two taxies standing side-by-side before our office. So this ended up costing us no more than double taxi fare and about half a year of life each.

img_0002.JPG

Of course it would be prudent to assume that with such a beginning anything else could go right. Obviously when we got to Frankfurt our backdrop was missing from the luggage and of course we got out on the wrong train station on the way to Wiesbaden and had to wait for the next train for an hour. The train station was quite in the middle of nowhere:

img_0004.JPG

In the end all ended surprisingly well. The missing luggage was brought to our hotel room at about 10 PM. Turned out that it was forgotten in Estonia and then rushed to Frankfurt through Copenhagen. By now we have set up the booth and are fully ready for the expo.

img_0012.JPG


18
Apr 08

Google Reader bug

Recently I constantly run into the same problem — Google Reader shows that some category contains unread entries whereas when you click on them it show nothing. Check out the screenshot where I apparently have 8 unread items, but although I have “All items” selected nothing shows.

greader-mod.PNG