Advanced OpenSocial Development

Very recommended

JAXB and creating elements from schema types

Working in the Java EE arena usually mean plenty of integrations – with other modules, with client applications, with third party providers, etc. Today, integrations always imply XML, and XML means some sort of definition – XML Schema, RelaxNG or “here is an example and you can figure it out yourself”. Personally I like XML schema – it’s a standard and relatively easy to understand (I know some will argue on this point). The best part is the fact I can use it to create objects for my application and save all the boilerplate marshalling and unmarshalling code. If there is one thing I hate, is to write code that should have be written by the compiler.

My tool of choice in the last few years is JAXB 2. Again, simple to understand, simple to work with, relatively descent documentation and integration with maven. However, there is one nagging problem with JAXB. When I need an element to be the root element of a document, as well as a child element I go into a corner. The trivial thing is to define a type, and then define an element of this type, in the following manner:

<?xml version="1.0" encoding="UTF-8"?>
<complexType name="SomeType">
<element name="first-name" type="string"/>
<element name="last-name" type="string"/>
<element name="some-element" type="tns:SomeType"/>

Naturally, I want to have the following code to get the simple XML:

SomeType s = new SomeType();

This should yield


Unfortunately, the generated code lacks one important annotation from the generated class – the @XmlRootElement annotation, which lets the JAXB to know that it can use this element as the root element of the XML, and let it know the name of the root element.
Apperently I’m not the only one who have encountered this problem. Once I’ve received an XML schema from a content provider whom I should have written the integration with, and the instructions were: “If you want the sample code to work, generate the classes using XJC, then go to classes X, Y and Z and add the @XmlRootElement annotation to the class”. I can’t say I was very happy with this.

However, in the current schema I design, I found a solution which is not as simple as I wanted it to be, yet it solves the problem. The definition of some-element was changed to inherit from SomeType and now it looks like this:

<complexType name="some-element">
<extension base="tns:SomeType"/>

The generated code is quite similar:

@XmlType(name = "some-element")
public class SomeElement
extends SomeType
{ }

Now, all I need to do replace the first line in the above code sample to:

SomeElement s = new SomeElement();

Douglas Crockford: “Advanced JavaScript”

After yesterday’s introduction lecture, here is the advanced one:

Douglas Crockford: “The JavaScript Programming Language”

One of the best sources to learn JavaScript from – the language creator! Here is the entire lecture

If you made it up to here, you may be interested in the advanced JavaScript lecture as well

Pigeon: Impossible

Not my usual stuff, but this is truly one of the best:

JMX remote monitoring on Linux

When running Java based application servers such as JBoss or Tomcat it is essential to monitor the status of the process – it’s memory consumption, threads and so on. In order to do that, Sun had introduced the JConsole application in Java 5, and now we have its successor visualvm.

In order to monitor the java process, you need to add the following system properties to the command line:

In the visualvm you just use the connection string host:9999.

However, sometimes the RMI listener listens to the wrong IP address, one which is inaccessible to the visualvm. Thanks to Pavel’s tip, I found a  way to overcome this is by adding the following parameters:


Now it works like a charm!

For completeness, I’d mention you can secure the connection to the JVM, either by requiring user/password or by using SSL. If you are interested, please see this guide.

Live patterns: Switching to the visitor design pattern

For more than a two years now, I’ve been working on the WorldMate Live application. In its essence, it is built around the concept of managing itineraries, and on top of that, it provides plenty of services. The entire system consists of mobile applications, currently BlackBerry and SmartPhones, an Outlook add-in and my team’s part – the web application and the back end services. This intro came so I can describe one of my favourite design decisions we made early during the development, which proved to be very successful over time, and this is the usage of the Visitor design pattern. As you can see in the interface, each itinerary can contain five different types: Car Rental, Flight, Hotel, Meeting and Public Transportation. These types differ from each other in most of their attributes, so no wonder that algorithms that using them need to provide different behaviour for each type. A good example is the algorithm that decide what to display in the UI – the display of a meeting is very different from that of an hotel (register and see!) The traditional way (or C style) to do that is to have some thing like the following:

switch(item.type) {
case CAR_RENTAL: ...
case FLIGHT: ...

And the Java incarnation of this code also looks like this:

if(item instanceof CarRental) {
} else if(item instanceof Flight) {
} else ...

I’m sure that this looks awfully familiar to you. This code has three major problems to it:

  • It is very hard to spot places where one type is not handled – on purpose or by mistake. Since in most cases the actual logic spawns several lines of code it is hard to spot such cases.
  • In case there is a need to add a new type, we need to go over all the places where we need separate logic and add the special case. This is a tedious and error prone process. Furthermore, since it is easy to to forget to throw an UnknownItemTypeException in the end, is will be also easy not to get any exception in places the new type was forgotten.
  • The code is very cumbersome -there are large blocks of code which handle all types, and switch-like ifs (like above) are mixed with other logic control structures.

The solution we chose to all these is to use the visitor design pattern. Using implementations of an ItemVisitor solves these issues:

  • You must always implement all visitXXX() methods. If there’s no need for specific logic for a certain type, then the code clearly shows this by having an empty method. This also adheres to the single choice principle – one of the fundamental principles of software engineering.
  • When new types will be added, then by simply adding new visitNewType() the compiler will notify where all the new logic should be added – since existing visitors won’t have this method.
  • The code is cleaner – instead of having large code block, we have 1-3 lines (depends if we need to return a value from the visitor), and the actual logic is neatly organised into methods

Wireless modem on Ubuntu

Due to an upcoming trip abroad, I was given a wireless modem so I wouldn’t be depended on the WIFI network which may not exist.

My laptop is unique in my company – not only it is the only Dell (apart from the iPhone team all the other laptops are Lenovo), it is the only one who runs Linux, Ubuntu 8.10 to be exact. This of course had caused some headache to our system administrator, but him being a great guy I’ve always managed to work perfectly.

I was given is the Novatel Merlin U740, together with some remarks that this will probably won’t work since I’m using Linux. At first I started to look for guides at the usual places – the wiki, ubuntu forums, etc. While doing so I’ve inserted the modem and opened the network configuration. Lo and behold, a ready mobile broadband connection was already waiting, created automatically by the system. All I had to do was to give it a meaningful name and set the correct number, and that’s it – I’m connected. The whole process took less than 2 minutes, most of them was the needless search for instructions. It was way faster than any installation of the same modem on any Windows based laptop…


Kudos to the Ubuntu guys!

Source Control HOWTO

Eric Sink has a great blog which he writes on software development both from a technical and business issues. After long time of inactivity, a new post was recently published promising an updated version of Eric’s source control howto series.

As it is quite outdated I believe many haven’t heard of this series, so I strongly recommend it as a must read for any software developer. It does not explicitly go into details of what do when, but it gives an excellent look under the hoods of how source control software works. The updated revision should also go into the details of the distributed SCM that had become very popular in the recent years.

Well, I’m anxiouly waiting for the new revision…

Load CSV file to MySQL

In many cases it is very convenient to import CSV files directly into the database, so it can be analyzed, referred to and other useful actions. MySQL’s can easily load tables from files, using the LOAD DATA INFILE command, but its default separators do not match the CSV format. Here is the LOAD command, fitted to load CSV files:

LOAD DATA INFILE '/tmp/file.csv'
INTO TABLE the_table