If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Monday, September 27, 2010

Conditional breakpoints in Eclipse

If you ever have to debug a large for-loop in your code and you only want to inspect an iteration if some condition holds true, you should set a conditional breakpoint.

Higher order functions in XSLT

I read this very interesting article which almost sounds like using clojures in java but then for xslt. Anyway... when i have the time i'll do a few tests and see if I can directly benefit from the approach mentioned over here.

Behaviour Driven Development

One of the latest hypes is BDD. In Scala there are quite a few BDD libraries available. E.g. Specs.

I was curious to see if there was any library available for Java as well and I found a few:

My personal favourite based on the examples would be EasyB which allows you to write stories with a combination of human understandable language and groovy's expressiveness to test the actual behaviour.

I will shortly try to explain what BDD tries to offer next to good ol' plain interfaces and what some shortcomings of normal interfaces are.

Let us start with discussing following interface:

So this interface should form 'the contract' for the concrete implementation. But what would prevent me from writing following implementation which does nothing from the behaviour we intended.

And for all we know the method could be invoked like addRecipient("Robby Pelssers") which is not exactly a valid email address.

So if we were to add behavioural conditions to our interface, we might be better off. Let us take a look at a revamped version of our interface. The syntax will of course depend on the BDD library, but the idea should be clear.

Sunday, September 26, 2010

Hibernate and Spring - Part 6

Everybody who uses hibernate in a project is bound to run into the famous LazyInitializationException problem. By default, collections are loaded lazy. That's in most cases also what you want. E.g.: If you query Facebook for users with firstname 'John', you don't want to fetch all available data about those users in 1 go. You only want to get their complete names. If you click on the name, you want more detailed information.

You should definitely read Hibernate Fetching Strategies

Below default behaviour (fetch=FetchType.LAZY) which can be omitted.

One way to make sure you never run into lazyinitalization exceptions is to fetch eagerly. But be aware for the penalty on performance and memory consumption. You might be fetching big parts of your database if you use this strategy.

One other way to solve this issue is to add following snippet to the web.xml

Hibernate and Spring - Part 5

So let's take a look at one of our domain object's DAO interface and implementation:
  • JobDao
  • JobDaoImpl

As you can see the only thing we still had to implement was the method getEntityClass(). I also added a method to find a job based on its workflowname and itemname and used my convenience methods.

It calls the findListByProperties on the DaoImpl which dynamically creates a HQL string and calls the findByNamedParam(String query, String[] parameters, Object[] values) on our hibernate template.

The querystring we end up with looks like

Friday, September 24, 2010

Hibernate and Spring - Part 4

Ok.. So now that we do have our Domain Objects annotated and ready to be used, we need to write our Service or DAO layer which provides following methods for our domain objects:
  • find/query
  • delete
  • insertOrUpdate
So we already have defined our SessionFactory and HibernateTemplate. Time to make good use of it. First we define a generic interface and next a generic implementation.

One remark: Many methods are just one-1-one mapped to the typical hibernate template methods but I added some extra convenience methods like
  • findUniqueByProperty(String propertyName, Object value)
  • findUniqueByProperties(String[] propertyNames, Object[]values)
  • findListByProperty(String propertyName, Object value)
  • findListByProperties(String[] propertyNames, Object[]values)

Hibernate and Spring - Part 3

Now let's take a look at 2 of my DomainObject's: Job and JobEvent.
The relationships between both domainobjects are simple:
  • A job has 1 to many JobEvents
  • A JobEvent belongs to 1 Job.

Hibernate and Spring - Part 2

I think it's always good practice to extend some generic superclass for your Domain Objects. Even if they don't inherit anything at first, you might decide otherwise later on and it becomes very easy to add common methods and variables. For my use case I decided that every Domain Object had to be uniquely identifiable.

Hibernate and Spring - Part 1

I have an application which locally runs against a mysql database but for QA and PROD we're using Oracle 10G. It's quite easy to setup a generic application context by injecting the correct property values. I'm using Cocoon-Spring-Configurator do load the correct properties once the application Context is initialized.

Below my configuration:

Some example property values for Mysql:

Some example property values for Oracle:

Wednesday, September 22, 2010

Unwanted behaviour with Numeric converters in JSF

It seems that you have to be carefull as what to expect from the standard JSF converters. Let us take a look at following jsf snippet which should convert the value entered into a java.lang.Long.

It does a good job if the user enters something that is not convertible to a long.

However, when you enter nothing and leave the Job ID field blank, JSF does a not so nice job of converting the value to 0 (zero) while you were expecting a null value.

Check out following articles as well as how-to solve this:


I tested with the solution from BalusC's blog but a custom converter didn't even help solving the issue on tomcat6.

So the option left is to add following startup parameter to tomcat's launch configuration and use the standard jsf converter as mentioned on top of this article.

Tuesday, September 21, 2010

JSF form reset problem

Suppose you have a search form with 2 required fields. If you want to reset the form and the values on the controller (@scope=session), you will find that JSF will not let you submit the form as the validation phase will already raise an exception. And let's also assume you want to do some business logic validation before proceeding with the actual action invocation. The code below shows you a quick way to get the job done. The solution is to NOT put the @required=true in your jsf page but accomplish the same behaviour through your own validation method.

Friday, September 17, 2010

New free goodies (developer tools) from Google

Google just announced it is making a few extra developer tools available for free.

Sure google has its own agenda but I'm always excited when they give something back to the open source community.

Thursday, September 16, 2010

Quartz Job Scheduling

I had to create quite a few cronjobs for a recent project. I've noticed that the easiest way to manage this was creating a war project so that on deployment the quartz job scheduler kicks in immediately. I faced one problem and that was that on some occasions a job was started before the previous ended and this led to tasks fetched from the database being executed twice.

It's quite easy however to workaround this and below is an example:

Tuesday, September 14, 2010

creating indexes for Exist DB

Below an example using range indexes for a collection in Exist DB. You basically specify tagnames which have to be indexed and it's the recommended approach.

Creating reusable modules in Exist DB

If you see reusable patterns in your xml extraction / transformation process, Exist offers you the possibility to create modules in their own namespace.

Below an example of such a module containig several functions and an example of an xquery (re)using that module.

Example of a module:

Example of xquery using that module:

Monday, September 13, 2010

Generating html data for Excel using UTF-8 encoding

Today i had an issue where I was generating an html table containing Chinese characters. The table was used for importing in excel but excel did not use the correct character encoding.

It appeared following code snippet was not enough:

Following META information had to be included in the generated html as well:

Copying files between 2 servers

In case you can't use FTP (FileZilla) or WinSCP, you might have to copy files from the command line.

Here's a little example which copies myapplication.war from the folder home/user1/deployment on myserver1.com to the folder /appl/tomcat/webapps on the machine where you are logged in.

Tuesday, September 7, 2010

Profiling your Java application

I faced some severe memory issue a while ago and I have played around with 2 Java Profilers which are both very nice tools. Next time I face out-of-memory exceptions i'm sure i will use both of them again.

Yourkit Java Profiler
BEA JRockit Management Console

Monday, September 6, 2010

Basic security with Spring 3.0

I had to create a webapplication with some basic authentication for several pages which allowed the user to administer the application. The view-only pages should not require authentication.

Since I use the SpringFramework for most of my applications lately I decided to take a quick look at Spring Security and blog this quick how-to guide for basic authentication. Remark: I use eclipse as IDE.

So let's start with adding some required maven dependencies:

I also had to add the Spring repo's to my pom.xml:

So next you might run 'mvn eclipse:eclipse' again and refresh the project in eclipse.

We also need to make some adjustments in our web.xml:

Next I decided to create a separate application-security.xml with the default namespace set to "http://www.springframework.org/schema/security" so it would avoid me having to type the security prefix for all elements. One problem I faced was that the authentication-provider used to be a root element where as it is now a child from authentication-manager.

As you can see all pages with the pattern /pages/secure/** are made secure now and only accessible to users with ROLE 'ROLE_SUPERVISOR'. Secure pages are forwarded to the login-form (/pages/login.faces).

Once the form get's submitted the configured authentication-provider authenticates the user.

A little remark about my login page which is using facelets... Using jsf tags will give you some trouble because the form action and user and password form names have to match the ones as specified below:

Other usefull blogs: