Spring enable your Resources

October 1, 2008

In the current system that we are building, we are a communications hub between a half dozen third party systems. In order to enable us to do some extensive integration and load testing, we’ve been forced to develop simulators that can stand in for the various systems we will be linking to since we probably won’t be integrating with our actual partners until pretty late in the game.

I’m currently working on a simulator for a pick, pack and ship partner. We will be POSTing product orders as xml and asynchronously receiving order status and shipment notification updates that are POSTed back as xml. The simulator is functionally equivalent to a car with an empty engine compartment. All of the interfaces that a user (our system) would see are there but when you press the gas pedal (send an order), it just makes “vroom, vroom” noises (returns mock data). It is a fully functional web application with a REST style WS interface and several internal JMS processing queues. The processing, however, is simplistic and deterministic.

You might be asking yourself what any of this has to do with Spring or Resources. I was just getting to that.

In our main application, we have a ‘hidden’ feature that allows us to send a canned (static) xml document to this pick, pack and ship partner for testing. Yesterday, after standing up the PPS simulator and trying to send it a message from our main application, I ran into a problem.

The original developer of this feature put the static sample xml that we are sending inside one of the classes as a string:

private static String ORDER_XML = "<order>..."
                                               + "<customer>...."
                                               + "<address>....";

I almost started crying when I saw this.

Much as I discussed yesterday, it turns out that I had to change these canned document because somewhere along the way, we had added namespace handling to our processing logic and these documents weren’t using namespaces.

After cursing this developers name under my breath and then out loud, I decided some refactoring was in order–I saw additional changes coming and I didn’t want to have to maintain this kind of embedded xml again in a few weeks. I’d much rather have these canned xml samples as external xml files rather than embedded in the code.

Anyone who’s done much enterprise Java development has at one time or the other needed to load a resource from the classpath. It typically looks something like:

String resourcePath = .....;
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
// Do something with the stream

Fortunately, Spring makes this quite a bit easier. You can just declare the resource that you need in your target bean:

private Resource orderXmlResource;

public void setOrderXmlResource(Resource resource)
    this.orderXmlResource = resource;

Then, in the bean config define it in the following way:

<property name="orderXmlResource" value="classpath:pathToYourSampleFiles/order.xml"/>

Spring is smart enough to turn this String property into the appropriate resource type (ClassPathResource) and inject it into your bean.

Then, it is a simple matter of getting the xml as a String. With the help of Commons-IO, it’s a one liner:

String orderXml = IOUtils.toString(orderXmlResource.getInputStream());

Now, isn’t that much cleaner? I think so!



2 Responses to “Spring enable your Resources”

  1. Wow, i’m impressed by what can be done by velocity. It’s a very good solution for processing templates wherever you have that requirement. Will definitely take a closer look

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: