software development

Tomahawk and Spring Webflow

Posted in java, software development on March 20th, 2009 by Joerg – Be the first to comment

I recently ran into a configuration problem when using Apache Tomahawk and Spring Webflow together. When using some of the more advanced components like <t:inputDate> I got an error message:

...
java.lang.IllegalStateException: ExtensionsFilter not correctly configured. JSF mapping missing.
...

I did configure the MyFacesExtensionsFilter like shown in the documentation but that was not enough:

 <filter>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
     ...
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>

After some debugging it turned out that this filter was never called. The solution is simple. When you combine Spring Webflow and JSF the Faces Servlet has no real function. It just needs to be there for some compatibility reasons. The real servlet that needs to be filtered is the Spring MVC Dispatcher Servlet. So you need to change the filter mapping like this:

<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
</filter-mapping>

This also applies for all other filters that are usually applied to the faces servlet, like the Trinidad filter.

XML to POJO via Groovy

Posted in groovy, java, software development on March 8th, 2009 by Joerg – 9 Comments

Transforming data in XML to several Java-Objects is a pretty common task. There are a lot of technologies that support this. One example would be JAXB. It is also not uncommon that the structure of the original XML and the Java objects does not match. The obvious choice for XML transformation in such a situation would be XSLT.
There is another approach that uses Groovy to transform XML directly into a POJO structure in one step. I will show you how this looks like.

Lets assume, we have a XML-file like this:

<data>
  <person>
    <firstname>Otto</firstname>
    <lastname>Mueller</lastname>
    <street>Lange Strasse</street>
    <city>Berlin</city>
  </person>
  <financial>
    <netincome>120000</netincome>
  </financial>
</data>

The target structure are two POJOs that look like this:
Target Objects

Both structures differ. There are also some small conversions to do. The XML includes yearly income, where the POJOs needs monthly income. The POJOs contains a field for zipcode, but the XML does not deliver it.

So, here is the Groovy-Code that does all the transformation:

public class XmlTransformer
{

  Person transform(File xml)  //1
  {
    def xmlData = new XmlSlurper().parse(xml)   //2

    Person person = new Person()

    person.firstName = xmlData.person.firstname   //3
    person.lastName = xmlData.person.lastname

    Address address = new Address()
    address.city = xmlData.person.city
    address.street = xmlData.person.street
    address.zipCode = ZipCodeFinder.find(xmlData.person.city.toString(),   //4
            xmlData.person.street.toString())
    person.address = address

    BigInteger yearlyIncome = new BigInteger(xmlData.financial.netincome.toString())
    person.monthlyIncome = yearlyIncome.divide(12)  //5

    return person
  }

}

What happens in detail (numbers on the list match the numbers in code comments):

  1. This is a simple Groovy class that contains one method called transform. This method gets the XML-file as input and returns a Person object. The Person class itself is defined in Java.
  2. XMLSlurper  is the Groovy XML-Parser that allows the easy access to all xml elements. For more details on it see here.
  3. This uses some Groovy magic. On the left side we assign the value to the property firstName of the person object. This is a shortcut for using person.setFirstName(). On the right side we see the slurper at work to get us the XML value.
  4. We can call any Java-class from within the transformation code. Here we call an example helper-class, that would return a zipcode for a given city and street.
  5. Finally we also do some calculations within our transformation to convert the yearly income to a monthly value.

Groovy can be called from Java in several ways. If you do not need to change the transformation often I recommend to just compile the Groovy code. In that case it will be called from Java as if it would be Java code:

XmlTransformer transformer = new XmlTransformer();
Person person = transformer.transform(inputFile);

This kind of transformation code can easily be embedded into a Java project. There are several advantages, when using the Groovy method:

  • Just one step for transformation including conversions.
  • Easy debugging, when using an IDE like IntelliJ IDEA.
  • Java-like syntax, no learning of XSLT required.
  • Easy code-structuring. The transformation can use all features of a dynamic object-oriented language.
  • Simple unit-testing. If you split the code into several methods, you can test each logical unit using established frameworks like JUnit or TestNG.

So what is your opinion? What are your current methods of transforming XML to POJOs?

My Book Slide

Posted in books, management, software development on February 25th, 2009 by Joerg – Be the first to comment

Jurgen Appelo of noop.nl had a nice post of the books he was reading recently called “My Book Slide“. He is reading a lot. I found some of the books were the same I was reading lately. So here is My Book Slide:

Sep. 2008

Robert C. Martin: Clean Code *****

Not much to say about this book except You have to read it. It is already a classic.

Oct. 2008

Neal Ford: The Productive Programmer ***

Nice book about all those little habits that make a difference in ones productivity. My favorite quote is: “There are people running their computers and others that are just walking them.”

Nov. 2008

Dan Roam: The Back of the Napkin **

Small book about explaining things visually. Dan Roam explains some easy to remember methods on how to find the right picture. For whatever reason I was expecting more of this book than it delivered.

Dec. 2008

Tom DeMarco: Adrenaline Junkies and Template Zombies ****

Great read. Nice little patterns of what are good or bad behaviours in projects. Each pattern is really short, only 2-3 pages. So it’s an ideal book to read in small breaks. The only drawback is that they are trying to follow the “Pattern-Trend”.

Nicholas Carr: The Big Switch **

Nicholas explains the analogy between utilization of electricity in the early twentieth century and the current situation in our industry. Some nice ideas, but most of it not as new or revolutionary as the cover promisses.

Roman Pichler: Scrum ****

The bestselling german book on scrum. Explains scrum very well and has also some good ideas on how to scale scrum.

Jan. 2009

Andy Hunt: Pragmatic Thinking and Learning – Refactor Your Wetware *****

This is my book of the year so far. It explains a lot around how we think and how we learn. It shows ways to improve this for everybody in a language that software developers can easily understand. But the best thing in the book is the Dreyfus-model. It explained some phenomenons I was wondering about for a long time.

Rothman, Derby: Behind Closed Doors ****

They are using the concept of story telling to explain important techniques of management. I found a lot of tips for my work. The book is just a bit short, not even 200 pages.

Currently reading

Pete McBreen: Software Craftsmanship

I like the concept of software developers being craftsmen since the I first read about it was in The Pragmatic Programmer of Hunt and Thomas. So I am curious, what this book will say about it.