Spring MVC Validation BindingResult

A quick note about using the BindingResult to detect and report errors in a form. One gotcha that got me was the need to set a name on the @ModelAttribute in order to properly relate the form:form commandName and the validation object. Essentially, if you don’t set a name then @ModelAttribute will get the command name from the name of the argument, and BindingResult will get the command name from the type of the argument, meaning that when you go to use form:errors nothing will be displayed....

September 7, 2011 · Nigel Sim

Programmatically getting the Maven version of your project

It is often handy to be able to extract the Maven version of your project at run time, either for displaying in an about box, or in debugging information. One option is to read /META-INF/maven/${groupId}/${artifactId}/pom.properties. However, this file is only available in the packaged version of your project, so during development the method will fail. The approach I’ve taken to fulfil this requirement is to create a text (properties) file in the project resources, and have Maven process this....

August 31, 2011 · Nigel Sim

Debugging connection pool leak in Apache HTTP Client

I recently had an issue using the Apache HTTP Client pooling library where after a while threads would just block when trying to open connections. It didn’t take too much to figure out that the thread pool was being exhausted, a quick thread dump revealed all the threads were waiting for a connection from the pool. The issue was, which method was not releasing the connection? In the Apache HC library a connection is released to the pool when the response....

June 17, 2011 · Nigel Sim

Preauth in Spring Security 3.x

Sometimes in a webapp you will be in a situation where a filter/app/container other than Spring will be responsible for authenticating a user and setting the user principal, leaving the authz to the Spring webapp. A portlet container is a typical example. There is a few examples floating around showing how to do this in Spring 2.x, but it appears some thing (packages, etc) have changed for Spring 3.x, so here is how to make it work....

January 20, 2011 · Nigel Sim

ZSI -> CXF: Parameters coming in as NULL

Recounting a strange little compatibility issue I had between ZSI 2.0 and CXF 2.0.x. I was using CXF as the server, running from Maven using Jetty, and ZSI as the client. The parameters from the ZSI were arriving at the service implementation as null. With the web services logging turned on I could see that the SOAP packet was arriving OK, and looked good. A little bit of digging round the web turned up this message....

November 17, 2008 · Nigel Sim

Zope3 Component Architecture (CA) style Adapters for Java

After programming for Zope3/Plone for the past year I’ve come to really admire the flexibility and elegance that their implementation of the adapter pattern gives us. And, after Martin Aspeli put the call out almost a year ago, and it has not yet been answered, I thought it was time to give it a go. How hard could it be. So, what I’ve developed is a very simple, no dependency, maven available library which should greatly improve the flexibility of your code....

October 15, 2008 · Nigel Sim

Some thoughts on a dynamic (lazy) data access layer for web services

The problem I’ve been address in my most recent work block has been to develop an interface to a relational data store which can be used either as a local DB, or via webservices. The concept is quite straight forward, we want CRUD operations on a set of data objects. The schema is fairly straight forward too, with a core hierarchy of elements with a few enumerated lists. As well as a few cuts across the hierarchy....

April 3, 2008 · Nigel Sim

Spring and JNDI (Tomcat or Jetty)

Recently I had need to deploy some Spring webapps which required predeploy configuration. Being the first time I had to find a serious answer I looked to the mythical JNDI for an answer. This document is meant to complement other Spring JNDI documents out there. Essentially the problem is this. We need to deploy a webapp. The webapp needs configurations (database and webservice endpoint locations). Editing properties files or XML config within the webapp isn’t nice, because on a redeploy the config will be lost....

February 14, 2008 · Nigel Sim

Maven classpath issues at compile time

Here’s a very weird Maven/Java issue. The error message (below) occurs in my build phase where JaxB is called to produce some Java objects from XML. JaxB calls HyperJaxB, and on some systems it crashes. [ERROR] XJC while compiling schema(s): org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.commons.logging.impl.Log4JLogger does not implement Log Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.commons.logging.impl.Log4JLogger does not implement Log Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.commons.logging.impl.Log4JLogger does not implement Log Caused by: org....

February 8, 2008 · Nigel Sim

ManyToOne reference loading with JPA/Hibernate

In this example an InvestigationType has many SampleType’s. If I load an InvestigationType via em (entity manager) find it also loads the samples. InvestigationType inv2 = (InvestigationType) em.find( InvestigationType.class, inv.getId()); System.out.println(“Inv2 ID="+inv2.getId()); System.out.println(“Inv2 Title="+inv2.getTitle()); assertTrue(inv2.getSample().size() == 2); However, if I load a SampleType the investigation field is null, unless I refresh the object. // Get a sample SampleType s3 = (SampleType) em.find( SampleType.class, inv2.getSample().get(0).getId()); em.refresh(s3); System.out.println(convertDataToXmlString(s3)); InvestigationType inv3 = s3.getInvestigation(); assertNotNull(inv3); From my understanding I should not have to do this as the fetching should be the same in both instances, ie resolving the parent by default....

January 8, 2008 · Nigel Sim