Tag Archives: java

Logging in Cocoon 2.2

I’ve had to try and understand logging in Cocoon 2.2 for a project at work recently. It’s been “interesting,” so I thought I’d blog the process in case anybody else needs to o this…
Normally, logging in Java is quite simple: you add log4j to your classpath, then create a log4j.properties to say what gets [...]

Bootstrapping Spring

Recently, I’ve been converting a project to use Spring. My main method looks something like this.

ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
Main main = (Main) context.getBean("main");
main.run();

There’s a problem though. Several of my beans need to be configurable. Normally, I’d resolve this by using a PropertyPlaceholderConfigurer (or more likely [...]

Exceptional Origins

I’ve just noticed something rather nice in Eclipse. The “Mark Occurrences” feature () will show you where an exception is thrown. For example, here I’ve clicked on the IOEXception in the method definition.

You can clearly see that read(), write(), flush() and close() are points at which the IOEXception can be thrown.
Similarly, you can [...]

Dates & Time Zones in Java

I’ve just found an annoying bug in a product at work. I was formatting a date (as part of a test) and expecting to get back a known value. The easiest way to do this is:

Date when = new Date(0); // 1970-01-01 00:00:00
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd’t'HH:mm:ss");
String whenStr = [...]

Exceptional Eclipse Tip

By default, when eclipse creates a try/catch block for you, you end up with something like this:

try {
doSomething();
catch (EvilException e) {
// TODO auto-generated catch block.
e.printStackTrace();
}

This is worse than useless, as it (effectively) covers up the exception1. [...]

Mapping a servlet to /

An interesting problem cropped up today. We want to use a servlet for the home page in a Spring Web MVC project. Initially we had this in web.xml.

<servlet>
<servlet-name>foo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>foo</servlet-name>
[...]

Writing XML in Java

Last night, I was looking at generating SAMS XML from Java objects. It made me realise two things:

Java sucks badly at creating XML (by default).
I should really be looking at XML Binding.

I’m interested in point 1. I’ve done it before, trying to write XML using DOM (as there doesn’t appear to be a [...]

Character Encodings Bite Again

A colleague gave me a nudge today. “This page doesn’t validate because of an encoding error”. It was fairly simple: the string “Jiménez” contained a single byte—Latin1. Ooops. It turned out that we were generating the page as ISO-8859-1 instead of UTF-8 (which is what the page had been declared as [...]

Java Concurrency in Practice

I’ve recently purchased a copy of Java Concurrency in Practice. I was always a little bit scared of threaded code, but we have a small amount at work, so I figured I’d better get my head around it. Wow. The big takeaway from the book was that I was nowhere near scared [...]

Character Encodings

Q: When a program reads input, what is it reading?
A: Bytes.
i.e. not characters.
If you want characters, you have to convert from one to the other. Thanks to decades of ASCII and Latin-1, with one-to-one byte to character mappings, most programmers have never even noticed that there’s a difference.
But there is. And as soon [...]