Archive

Archive for January, 2010

Error handling differences between Servlets and Filters

January 29th, 2010 1 comment

If an exception is thrown from within a servlet’s init() method, I would expect Tomcat to act on it by not allowing the context to load. This makes sense to me.

But I have recently discovered that Tomcat allows exceptions to be thrown from within the initialisation phase of a servlet‘s life-cycle without side-affects, while exceptions thrown from within the initialisation phase of a filter‘s life-cycle causes a Catalina SEVERE error resulting in the context not loading.

Hmmm…. Not sure why this is the case, but until someone can explain it to me I will treat it as a bug within Tomcat. We are using v5.5.25.

FYI, I came across this while trying to implement an application configuration servlet which loads on start-up setting various application properties based on environment variables et al. Due to the described ‘bug’, I have ended up using a filter for the job instead of a servlet as it behaves as I would expect. A better solution would be to use Spring, I know, but we are still having to maintain some complex legacy apps which are not worth spending the time converting to Spring.

Categories: Bug, Exceptions, Filter, Servlet, Tomcat Tags:

H2 db unit testing tip

January 28th, 2010 1 comment

I’ve found that occasionally the unit tests for the vle fail with database errors. Really odd once such as “Table XYZ not found”. This generally prompts for checking the test schema.

The table is being created. So why does the test fail?

It would appear the h2 is possibly a little more advanced than HSQL by default. The reason I can’t see the table is that I haven’t committed it.

By adding ‘commit;’ as the last statement to the db schema file the errors will be corrected and the tests should pass (assuming that was the only problem).

The reason for this:

In some places (especially when testing repositories or if junit runs tests in parallel) you might end up with several connections to the in memory database. This could happen when you as a repository for something and it fires a second query while retrieving data from a previous one.

The result is that the first connection where I created the tables can see them because they are a part of its working data. The second connection can’t as they haven’t been committed back to the proper database for everyone else yet.

Gerwood

Categories: H2, SQL, Unit Testing Tags:

Programmatically setting Log4j properties

January 28th, 2010 Comments off

Have you ever needed to set a Log4j property on the fly (as opposed to setting it in the log4j.properties file or the log4j XML configuration file)??? Well I needed to so I set about finding out how to.

I was updating our Library’s electronic document repository system, eReserve, when I noticed that it was using a Log4j SMTPAppender for cases when documents where not found where expected (HTTP 404). The Appender was setup to send an email to the Library’s document manager in order to keep on top of dud links. However, it was not working because in a recent iteration eReserve was updated to useĀ  a Maven build instead of an Ant build. This resulted in Ant tokens not being processed and hence the ‘To’ property of the SMTPAppender not being set. So the email was going nowhere.

Now, for each environment we have a different ‘To’ address to use because we don’t want the Library’s document manager to receive all of our emails generated during development and system testing. For this situation we make use of Tomcat environment variables. But we can’t set log4j.properties to get values from JNDI. So we have to set the ‘To’ field programmatically (that is, at run time as part of the code). Here is the code I used:


// This will set the To option of the SMTPAppender instead of it being set in the Log4j properties file
// this allows this value to be determined programmatically, in this case from an environment variable
Logger logger = LogManager.getLogger("erez.email");
SMTPAppender appender = (SMTPAppender)logger.getAppender("erezEmail");
appender.setTo((String)pageContext.findAttribute("erez_email"));
appender.activateOptions();

Note the activateOptions() method of the SMTPAppender object. This is like the ‘save’ button on forms… was hard to find that this was needed.

The Log4j API can be found here (note the version): http://logging.apache.org/log4j/1.2/apidocs/index.html

Categories: Log4j Tags:

Hello world!

January 28th, 2010 Comments off

Welcome to UNE’s blog for Java Developers! Feel free to post your findings for the rest of the community to read.

if (you.doCare()) {
rss.subscribe(); //DO IT!
} else {
return new Lame();
}

Categories: General Tags: