jump to navigation

Blog Closing August 21, 2012

Posted by Phill in General J2EE.
comments closed

Sadly, this blog is now closed. I have essentially changed career – I am no longer working in software development.

I will leave this blog here for future reference if necessary, unfortunately the information is going to become more and more out of date as I won’t be updating it any more!

Spring Aggregating MessageSource March 26, 2010

Posted by Phill in General J2EE, Spring.
Tags: , , ,
comments closed

If you’ve ever worked on a modular project which needs to be internationalised, chances are you’ll have run into the requirement of combining messages from various different modules.

By that I mean, each module has its own message source, but you want them to be combined into one MessageSource. This could be required for various different reasons which I won’t go into here, but suffice it to say that this is possible with Spring.

I created an AggregatingMessageSource which would scan the ApplicationContext for instances of MessageSource. When a message was requested, it would search each one sequentially for the relevant message.

Anyway, if you need such a class, I have uploaded it to get you started. Obviously, customise it to your own needs: AggregatingMessageSource.java.

JAI October 19, 2009

Posted by Phill in General J2EE.
Tags: ,
comments closed

Recently I had a project where I needed to use Java Advanced Imaging (JAI) to convert TIFF files into JPG. (Unfortunately Java doesn’t provide a TIFF decoder out of the box).

I had an issue today whereby I needed to deploy the application on a server (it’s a web app). I kept coming up with errors about JAI not being able to find the TIFF decoder:

java.util.NoSuchElementException
     at javax.imageio.spi.FilterIterator.next(Unknown Source)

For anyone who is Googling it!

Anyway, it turns out that I had got jai_core.jar and jai_codec.jar in my WEB-INF/lib folder (these are both part of the standard JAI Distribution), but for some reason I also needed jai_imageio.jar (which is part of the JAI-ImageIO package).

I can’t quite fathom why you need so many JAR files, but it seems to work now, so there you go. Hope this helps someone else out!

Wicket September 17, 2009

Posted by Phill in Frameworks, General J2EE, Presentation Layer.
Tags: ,
comments closed

The past few days I’ve been prototyping for a new project we’re starting up at work. Previous web projects we’ve done have mainly been using JSF, however for this project I’ve been looking into Apache Wicket.

My first impressions are, I like it. I like it a lot, in fact. It seems to achieve a much more clean separation between code / components and HTML than other frameworks do. It does take a while to get your head around it though – I’ve only been working with it for a few days so am still finding my way round!

But I expect I will blog up some info when I’ve got my head around it.

Ant, dependencies, and slow servers January 26, 2009

Posted by Phill in General J2EE.
Tags: , ,
comments closed

At work I’m working on an application which uses an Apache Ant script to build it. Unfortunately we haven’t yet got around to using Apache Ivy, however all dependencies are stored in a text file and then downloaded from a central server in the build script. Not an ideal solution but it works for our needs.

The problem comes when the central server we use to store the JAR files slows down to a crawl (which has happened quite a lot recently). The past few days, the build script has taken over 4 minutes to complete – and most of that time is spent downloading JAR files (unfortunately there are two JAR files which needed to be downloaded each time – it’s too complicated to go into now…)! So I decided to make a change to the build script.

Now, it should only download when I update the text file with the list of dependencies. How is this accomplished? By using the <condition> task:

<condition property="lib.uptodate">
<and>
<uptodate srcfile="${basedir}/lib-jarlist" targetfile="${lib.dir}/lastupdated.txt" />
<uptodate srcfile="${basedir}/lib-jarlist-compile-only" targetfile="${lib.dir}/lastupdated.txt" />
</and>
</condition>

What this code is doing is checking two files (lib-jarlist and lib-jarlist-compile-only). If either of these two files have a newer timestamp than the target file (lastupdated.txt), than the condition will be TRUE. Then, all I need to do is put this in the download dependencies target:

<target name="download-deps" unless="lib.uptodate">
(download dependencies....)
<touch file="${lib}/uptodate.txt">

So, the target will only run if one of the JAR list files have been updated. Note the touch command at the end – this is important: it updates the file timestamp so that next time the script will see the update dependencies and not go off to the slow server for them.

It’s not a perfect solution, but will do until we can move to Ivy!

How to Over-Analyse December 15, 2008

Posted by Phill in General J2EE.
Tags: , , , ,
comments closed

Apologies for the random post, but we were having a discussion on a question on StackOverflow about how to detect whether a string is a number in Java.

I posted up a possible WTF solution involving regexes.  (You’ll have to read the original question for all the background). Someone suggested instead using NumberFormat. Jon Skeet replied with a different solution as well.

After all the discussion I thought I’d do a quick speed test. It turned out to be a rather overblown, over-engineered speed test… but it produced some interesting results 🙂

Firstly, the code. I’ve uploaded it on my website so you can check it yourself.

The code tests three methods of checking to see whether a string is a number: using a (basic) regex, using NumberFormat.parse(…), and using the JonSkeet (TM) method. Each method is called 1,000,000 times per test and each test is run five times and then averaged.

These are the results on my machine:

--------------------
Testing: Regex Tester
--------------------
2672 2578 2562 2547 2594
Average time: 2590
--------------------
Testing: NumberFormat Tester
--------------------
9641 9531 9375 9391 9578
Average time: 9503
--------------------
Testing: Jon Skeet Tester
--------------------
235 218 235 234 235
Average time: 231

Using NumberFormat is 3x slower than using a Regex – although NumberFormat will parse a greater range of numbers (i.e. it parses numbers properly rather than using a rather dumb regex). But – Jon Skeet’s method is more than 10x faster than the regex. Ladies and gentlemen, I think we have a clear winner. Jon Skeet FTW! (But then, we all knew that anyway…)

Apologies for this interruption, normal blogging service will resume yada yada yada.

(Edit: I should point out that if you want the “Correct” method of checking whether a String is a number, you should use NumberFormat. This is maintained by Sun and will account for locales etc. Basically it is the canonical way of parsing a number. The regex method is probably good if you just want to check whether a String contains digits in one line of code, quick and dirty. And the Jon Skeet method is if you want correct AND fast, and can shove that method in a utility class somewhere).

CallableStatements, JdbcTemplate and jTDS October 15, 2008

Posted by Phill in Data Access Layer, General J2EE, Spring.
Tags: , , , ,
comments closed

I’ve been using the jTDS driver to connect to our SQL Server database. We have a stored procedure which I needed to call. In this project I would normally use iBATIS, but this particular call required the features of JdbcTemplate and CallableStatements (there are good reasons which I won’t go into now!)

The stored procedure has one out parameter, and also returns a ResultSet. I thought this would be pretty simple to set up – but all my attempts kept coming up with the error message “Could not set up parameter for remote server” (which the jTDS driver was sending back).

At the time I was using named parameters. What I’ve done is to switch to using positioned parameters (i.e. setString(1, “Parameter”); registerOutParameter(2, Types.VARCHAR)) and that has seemed to fix the problem.

A bit bizarre, but as I couldn’t find any info about this error anywhere on the internet I thought it was worth posting about in case it saves someone else a headache 🙂

Enhydra XA DataSource September 30, 2008

Posted by Phill in General J2EE.
Tags: , ,
comments closed

Just a quick follow up to my last post. xapool.jar comes included with JOTM. I just used the version of xapool which came with it… however, we’ve been having a number of impossible to reproduce problems where the data access layer throws an exception “Connection Closed”.

It turns out that the version of xapool.jar which is included with JOTM is out of date! I’d suggest switching to a newer version – 1.5.0 is the latest stable version, or there’s 1.6 beta.

I unfortunately can’t say for sure whether it has fixed the issue (due to its rather random nature), but it’s probably worth being on the newer version anyway.

Update: it didn’t fix the problem, or at least we had similar problems with it. I would not recommend using the Enhydra connection pool.

Another JSF Gotcha July 9, 2008

Posted by Phill in General J2EE.
Tags: , , , ,
comments closed

JSF has a number of “gotchas”. Today I managed to stumble across another one. Again.

Suppose you’re designing a user administration panel, and you want to be able to edit a user and select a list of roles for them. You’d think a fairly logical control to use would be a selectManyListbox (assuming, for the moment, it’s a fairly small number of roles).

So, in your backing bean you add in a List containing all the user roles, and you configure your SelectItems and converters. Except you still keep getting that damn error message: “Validation Error: Value is not valid”.

It turns out the problem is that JSF is so advanced, it can’t yet handle the Java Collections Framework: you have to use arrays instead. That’s right, in your backing bean, if you use List<MyClass> as the value binding for the list box, it will not work. You need to use MyClass[] instead.

I am using an older version of JSF (1.1 as opposed to 1.2) so that might be fixed now – still rather annoying to come across it though.

JSF Custom Components with Facelets and IceFaces June 3, 2008

Posted by Phill in General J2EE.
Tags: , , , ,
comments closed

I was having fun earlier trying to get a custom component working. I wanted to create a dynamic menu component, which I didn’t think was easily possible with standard JSF components.

What I wanted rendered was something like this:

<div class="navbar">
  <ul>
    <li class="selected"><a href="#">commandLink</a></li>
    <li><a href="#">commandLink</a></li>
  </ul>
</div>

Fairly simple, wouldn’t you think?

Well, actually it’s not that difficult – once you know how!

The problem comes with the way child components are rendered. I was creating a new HtmlCommandLink in my custom component, and then using encodeBegin(context) and encodeEnd(context) to render them.

However, I found that when you clicked on the links, nothing would happen! I looked at the source code, and couldn’t actually see anything wrong. It all looked fine… my only thought was that somehow IceFaces wasn’t picking up the components if I rendered them directly.

One idea was to actually add the commandLinks to the list of children, i.e. getChildren().add(commandLink). However, this doesn’t render components in the way that you want – i.e., I wanted all of the links surrounded by <li> and </li>!

In the end, what I did was create another custom component (ListElementComponent), and have it render an “<li>” and “</li>” in the encodeBegin(…) and encodeEnd(…) methods respectively. Then, I added the commandLink as a child of the ListElementComponent, and added the ListElementComponent as a child of my custom component.

Clear? Probably not!

Basically, the logic was as following:

for (each menu item) {
link = createCommandLink();
listElement = new ListElementComponent();
listElement.getChildren().add(link);
getChildren().add(listElement);
}

This seemed to force JSF / IceFaces to render the HTML as I wanted it.

I don’t know if there was an easier way of doing it, but unfortunately the documentation on custom components in JSF isn’t exactly brilliant!