Home Fabio Cortesi Stefan Jäger

Welcome to the Joint-Venture Blog from Fabio Cortesi and Stefan Jäger.

 

 
 

 

In this post, I will cover again the topic JiBX. This time, I will focus on a very special case, the bidirectional mapping with JiBX. To realize that without affecting a currently used domain model (as it will happen if we use pre-set, as described in http://jibx.sourceforge.net/tutorial/binding-extend.html), we need to implement an JiBX Marshaller and an JiBX Unmarshaller.

But let me start with the simple problem. We do have two simple POJOs, Customer and Person, which do know each other. If you are using for example Hibernate, bidirectional linking is not a rarity. With the JiBX binding XML we used in our last examples, the bidirectional linking is not set. To demonstrate this, I added these two lines of code in the main application:

System.out.println(customer.toString());
System.out.println(customer.getPerson().getCustomer().toString());

The second line will result in a NullPointerException, because JiBX is not setting this bidirectional linking. To get that working without affecting the two classes Customer and Person (!), we have to create our own Marshaller and Unmarshaller.

First of all, we have to prepare our mapping file. The best way for a bidirectional mapping is to define an own mapping section for the class Person (which was previously part of the Customer mapping part).

The reason for this step is simple, but important. Our customized Unmarshaller should let JiBX unmarshal the Person into an object and our Unmarshaller should only extend the object with the bidirectional mapping to the Customer. For this reason, JiBX needs an own mapping section for this class.

After preparing the JiBX mapping, our next step is to create a Marshaller and Unmarshaller class. For the bidirectional mapping, we will just need an Unmarshaller. But because we do have a JiBX mapping file for both directions, we do have to define a Marshaller and a Unmarshaller. 
We create for this example a new class with the name BidirectionalMapper, which implements the interfaces IMarshaller, IUnmarshaller and IAliasable. These Interfaces are delivered with the JiBX libraries. In the implementation, there are two remarkable parts in the method unmarshal. The first one just forwards the real unmarshalling action to the JiBX context and the second one creates the bidirectional linking.

Object unmarshalledObject = ctx.unmarshalElement();
Person person = (Person) unmarshalledObject;
...
Object parent = ctx.getStackObject(0);
person.setCustomer((Customer) parent);

In the marshal method, we also let JiBX marshal our person. Because the bidirectional linking has no affect to this method, this method is just simple.

ctx.marshalCollection(listOfElements);

After we created our Mapper, we need to define the marshallers in the JiBX mapping file. For this reason, we expand the element structure with the marshaller and unmarshaller class path.

If we run now our application again, there is no NullPointerException anymore and we can use our previously used domain model without changing anything at these classes.

If you are interested, you can download the whole eclipse project with all the sources from here.

 

 

 
 

 

In a software project, Software Engineers need to ensure, that the source code meets the architectural rules. One of a usual architecture rule are the dependencies between the layer and packages. There are a lot of tools, which can be used to ensure, that no class breaks this rules.

If you know, that there are some specific classes, which do not meet these rules, a dependency code analysis is needed. Therefore, I found a free tool called CDA (http://www.dependency-analyzer.org/), which is very helpful in such a situation. It can also be used for repeated check to ensure architecture compatibility, but in this blog entry, I will focus on code analysis.

First of all, just install this tool as described on the website (http://www.dependency-analyzer.org/#Installation). CDA doesn’t need any installation (just unzip the compressed file) and can be run with Java 1.5.

After finishing the installation procedure, you need to create for every project a Workset. A Workset can contain different code sources, which are analysed together. First, define a Name. After that, go to the Classpath register. The easiest way to use CDA is to analyse a JAR file. Add all JAR files, which you want to analyse.

clip_image003

If you are finished, save the Workset. CDA will load now all classes from the JAR files into a tree. With CTRL + F you can search for a specific class or you can navigate with the package name to a specific class. With right click, you can analyse all dependencies (on which classes/packages does the class depends on) or you can analyse all dependants (which classes/packages depends on the currently selected class).

clip_image005

If you are analysing the dependencies, you can show the classes or packages, on which the specified class depends on. Furthermore, you will see all third-party libraries, on which the class depends on. If you do not want to see some specific packages, you can apply a filter (button “edit filter”), on which you can define excluded packages.

clip_image006

The generated graph looks like following. It is almost the same as the textual dependency view, with the exception, that no third-party libraries are shown.

clip_image008

This tool is user friendly. If you are using this tool, you will very fast get to work with it. I use this tool to find dependants on a class and to draw simple UML diagrams of a class (very useful if you want to draw a UML diagram from just one class with all it dependencies).

 

 

 
 

 

After the ultimate shortcut, which I mentioned here, there’s another nice shortcut in Eclipse: CTRL + 1. This shortcut helps you almost in every situation. Let me mention two examples (there a lot more, just try it out):

 

Use CTRL + 1 to assign a parameter of a constructor as a field:

image

image

 

Or use CTRL + 1 to extract a String or another variable to a constant:

image

 

 

 
 

 

In a previous post I mentioned the Putty Connection Manager. I found this week a very nice feature. In the Putty Connection Manager, it is possible to create Login Macros. A Login Macro is based on some shell commands, which are send after the login. Now, you can just open a log file with one double click.

  1. As first step, you have to create a new Putty Connection Manager Database (File – New – Database). In this database, you can store connections. Unlike the Putty sessions, you can add to this connections some Login Macros.
     
  2. Now, define a name and a host for the connection. Also enable the Login Macro mode
    clip_image002
     
  3. Define your macro. You can just define some steps like cd … and less …, to open a log file directly.
    clip_image004
     
  4. As last step you have to link the connection with a putty connection.
    clip_image006
     

This is it. Now, you can just open a log file with one double click from the "Connection Manager" panel. This feature is time saver if you have to observe a lot of logfiles.