Twitter 406

Integrating with social sites, such as twitter, seems to be so easy, and it actually can be, once you get the configuration set up correctly. Last day however, I spend most of my time trying to hunt down a 406 error code when trying to login using a twitter account. According to the twitter site I have to :

  • Think of a name for my application, well that was not too hard
  • Enter a description
  • Tell on which domain your twitter application will be hosted, but you can enter a placeholder for this if you don’t know that yet, or want to test

And then you should be set and ready to go. Read more…

Categories: Java, Cloud Tags: , ,

Legacy code: treat it with silk gloves

15/02/2012 3 comments

Legacy code is tough to deal with. It may be the toughest thing we have to deal with in our everyday jobs.

Before we start, let’s align our definition of what legacy code is. Legacy code is untested code. Legacy code may be old, but not necessarily. Unfortunately some projects are actively producing legacy code.

Most programmers have to work with legacy code frequently. Even worse, they often must make changes in the legacy code. They must fix bugs in the legacy code, or change it to implement new features. At the end of last October I started working on a project that at the time, fully consisted of legacy code. I won’t share any specific details about that client and their projects, but I like to share how we work with the legacy code. Read more…

Categories: iPROFS, Java Tags: , , ,

Bye bye XMLGregorianCalendar (part 1)

Introduction.

We all know that working with dates in the Java language isn’t perfect, at least when you just look at the standard JDK. Not only is more then half of them methods in java.util.Date deprecated, but in order to get the Date of a java.util.Calendar you have to use the getTime() method. So when the base object aren’t that well to start with, things can only get worse when you start to mix in more technologies like XML.

When you are getting started with marshalling and unmarshalling java objects to XML you’re bound to run into the javax.xml.datatype.XMLGregorianCalendar. Did you ever try to get the Date value for a field unmarshalled from XML and wrote the following code:

Date date = xmlGregorianCalendar.toGregorianCalendar.getTime();

Why isn’t an XMLGregorianCalendar a GregorianCalendar just like a ArrayList is a List or a HashMap is a Map?

Oke, some smart people discovered these problems as well and started to specify a new Date and Time API (JSR 310).
Their idea are great although an anwser in the original JSR does give me some worries:

2.11 Are there any internationalization or localization issues?
No issues are expected.

In my opinion internationalization and localization are the major issues when working with dates and times, but hey, that’s propably just me.

So the expert group started on 13 februari 2007 and finished the early review on 28 march 2010.
I’m really wondering in which version of the JDK this well be included.
And of course once this has been introducted into the JDK: How long will it take to get it supported as the default datatype for  XML dates?

Solution.

Fortunately there is already a solution available: Joda-Time.
With it’s DateTime, Period, Interval and Duration objects it provided a mature, easy to use and comprehensive feature set.

There is only one slight problem: JAXB doesn’t know anything about Joda-Time and it’s DateTime object.
By default fields in an XML-file with a xsd:datetime type will be unmarshalled to an XMLGregorianCalendar.

In various projects I have seen DateConverter utility classes for conversion from and to the Joda-Time DateTime object.
Wouldn’t it be nice if you just could use the DateTime field and have the conversion be done as some form of ‘custom marshalling‘?
But wait: you can! The JAXB API specifies an XMLAdapter, which “Adapts a Java type for custom marshaling

What we need is an XMLAdapter, which allows us marshal and unmashal between a ValueType and the BoundType.
The ValueType is some class, which JAXB knows how to handle out of the box (Strings, Lists or an Calendar and the BoundType is some type that JAXB doesn’t know how to handle. The Adapter will specify how transform from one type to the other.

So I’ve written a DateTimeAdapter, which unmashals from a Calender to DateTime and marshals a DateTime to a Calendar.

public class DateTimeAdapter extends XmlAdapter {

	@Override
	public DateTime unmarshal(Calendar calendar) throws Exception {
		return new DateTime(
			calendar.get(Calendar.YEAR),
			// Correct for the difference between Joda DateTime
			// and JDK Calendar.
			calendar.get(Calendar.MONTH) + 1,
			calendar.get(Calendar.DATE),
			calendar.get(Calendar.HOUR_OF_DAY),
			calendar.get(Calendar.MINUTE),
			calendar.get(Calendar.SECOND),
			calendar.get(Calendar.MILLISECOND),
			DateTimeZone.forTimeZone(calendar.getTimeZone()));
	}

	@Override
	public Calendar marshal(DateTime v) throws Exception {
		Calendar calendar = Calendar.getInstance(v.getZone().toTimeZone());
		calendar.setTime(v.toDate());
		return calendar;
	}

}

Now the tricky part: You have to tell the XML (un)marshaller that it should use the custom adapter for mashalling certain fields in your objects.
But as you propably could have guessed: the JAXB-API provides a solution for that as well:
Just add an XmlJavaTypeAdapter annotation to the field in your object and the Marshaller and Unmarshaller will invoke the Adapter during the process.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "book", propOrder = {
    "author",
    "title",
    "publicationDate"
})
public class Book {

    @XmlElement(required = true)
    protected String author;
    @XmlElement(required = true)
    protected String title;
    @XmlElement(required = true)
    @XmlSchemaType(name = "date")
    @XmlJavaTypeAdapter(nl.iprofs.util.jaxb.adapter.DateTimeAdapter.class)
    private DateTime publicationDate;

    // Getters and Setters
    .....

}

Part 2: WsImport, Maven and XJC.

In the next blog about this topic I’ll dig into the topic of WsImport with Maven and XJC (XML to Java Compiler) which is being using in generating classes for webservices based upon a WSDL and Schema. If some generator is going to generate the java classes for the SOAP service, I want it to generate fields as DateTime objects and annotate them with my XMLAdapter.
Sound easy, but of course it isn’t just that.

Dependency Injection with Spring

Dependency injection is hot, and Spring Framework is one of the most popular frameworks that facilitates in dependency injection. Why is dependency injection so hot? If almost every programmer is using it, there must be a very good reason.

And there obviously is. The great thing about Object Orientation is polymorphism. Through polymorphism we can depend on abstraction at compile time, and at the concrete implementation at runtime. This is called dependency inversion; we invert the direction of our dependencies. It promotes decoupled systems and greatly reduces compile time, too some extend even in Java. And this is why everybody is so excited about dependency injection; it facilitates dependency inversion.
Read more…

HTML5 and CSS3 features and browser compatibility

HTML5 is the latest major milestone in HTML, XHTML, and the HTML DOM. It represents an enormous advance for modern web applications and promises to increase the capabilities of browsers and Web applications for years to come. The previous version of HTML – 4.01 – was released in 1999 and a lot has changed since then on the web. Therefor HTML5 has been brought to live in order to meet the demand of the users. When HTML5 is supported by a browser it will go hand in hand with CSS3. One fact is that it will improve the interaction with the user. This article will go into the features of HTML5 as well as CSS3 and the level of compatibility of the current browsers.
Read more…

Categories: Frontend Tags:

Improving CountdownLatch example from JDK-apidocs

Some time ago I discussed a use case for CountDownLatch, which was a thread safety issue.
If you read that very closely, you might have noticed I did not follow the exact pattern of the API specification of CountDownLatch.
I did that on purpose, because the example from the documentation is not perfect.
Here I will prove this claim and explain my own version.
Read more…

Categories: Java Tags: ,

Custom workflows in Hippo CMS 7

Customizing the CMS workflow is one of the things you eventually come across in almost every CMS implementation, be it minor tweaks or a complete rethink.
In Hippo CMS, the workflow configuration is part of, and stored in, the JCR content repository.
After some searching for workflows in Hippo CMS, you will ultimately find Hippo’s page about hooking into the default workflow.
It’s a fine step by step manual, and it serves as a good starting point, were it not for the fact that it’s quite outdated and does not function anymore.
Read along to find out how to make things work in Hippo CMS 7.6.
Read more…

Categories: Hippo, Java Tags: , , , ,

Java RAD and scaffolding tools

Most likely you also have found yourself in the following situation: a new project has just started and you have to set up a new project structure by creating the Java structure as well as configurations (like dependencies, storage etc.) manually which can take a lot of time. Writing, configuring and integration of  an application in progress can be as well a big pain. In this blog article I will describe what this tooling does and how it will help in these situations.

We will look at a few mainstream tools: JBoss Forge, Spring Roo and Play  and discuss their pros, cons and features. Read more…

Categories: Java Tags: , , , , , ,

Impediments or Tasks?

I recently had the opportunity to do a short experiment with self-organisation. I was a replacement Scrum Master for a team and did not have a lot of time for them, so I decided for myself not to accept any impediments, but to put them on the board as tasks.
Read more…

Categories: Scrum Tags: , ,

What is TDD anyway?

04/12/2011 4 comments

Over the past few months I noticed that many people who state that TDD does not work for them, seem to think TDD is something different than it really is. I can see why their version of TDD doesn’t work for them, and like to give them an overview of what TDD really is.

So what is TDD? Read more…

Categories: iPROFS, Java, Scrum Tags:
Follow

Get every new post delivered to your Inbox.