Archive for April 2008

Going to JavaOne!

Late last week I was asked by my manager if I would be interested in going to JavaOne this year. My answer was along the lines of “Hell Yes!”. Well, everything has been officially approved, and I’m now in the middle of booking my hotel and flight to San Francisco. A few of my colleagues here at Orbitz, Matt O’Keefe and Doug Barth, will be presenting on some of the cool event processing work we’ve been doing here at Orbitz. In addition to attending their presentation, I also plan on attending sessions on design, debugging, scripting on the JVM, tools, frameworks, and more. I can’t wait. It’s going to be a great experience!

Ubuntu 8.04 (Hardy Heron) is out!

I’ve been an Ubuntu user for about a year now. Before that, I played around with SuSE, RedHat, and a couple other Linux distros. However, Ubuntu is the first one that I use on a day to day basis. Installing applications is easy. Getting and installing updates is easy. Everything just works. It is the first Linux distro that I would recommend to non tech savvy users. It runs my web, Subversion, FTP, and and other servers at home, and I run it on my laptop. It is very easy to use, the community is very helpful, and I’ve never had a major issue with it. It, and a whole bunch of applications that are one click away from installation, are completely free. Who says you get what you pay for!

Previously, if you wanted to try out Ubuntu, you needed to boot into it using the Live CD, which loaded the OS into memory, and greatly limited what you could do. However, with Hardy Heron, you can now install Ubuntu right on your Windows box, without disturbing your Windows installation or your system’s boot record. Since Ubuntu lives on your file system, and not just in memory, you can now give it a real test run to see how you like it. And, if you decide not to stick with it, all you have to do is uninstall it like any other Windows program.

So, go download it and give it a shot.

Java checked exceptions VS runtime exceptions

One of my responsibilities at work is to interview candidates for open technical positions. Given that we are a Java shop, most of our questions naturally revolve around Java, both the language itself and the common open source tools and frameworks that are used by Java developers. An area that we often focus on during the interview is the Java exception hierarchy.

Every Java developer has to deal with exceptions in one way or another. They write code to handle exceptions, throw exceptions, and design APIs that use exceptions in a way that clearly communicates exceptional conditions to their users. Java is one of the few, if not the only programming language (I personally don’t know of any other) to have the concept of checked exceptions. Checked exceptions consist of the java.lang.Exception class and all of its subclasses; except for java.lang.RuntimeException and its subclasses (see below). Checked exceptions are exceptions that the Java compiler forces you to handle by either catching the exception, or re-throwing it. The failure to handle a checked exception results in a compiler error. Every Java developer knows what a checked exception is by the mere fact that they have to deal with them in order to get their code to compile.

Here is a simple example that shows some basic handling of checked exceptions, minus the nitty-gritty details.

// All checked exceptions that a method can throw must be declared in
// the method's throws clause
public void executeSqlStatements(String sqlFileName)
        throws IOException {

   // File operations can throw an IOException.  Listing IOException in
   // the throws clause allows this method to simply re-throw the
   // exception if it is encountered.  No other handling is necessary.
   BufferedReader input = new BufferedReader(new FileReader(sqlFileName));
   String sqlLine = null;
   try {
      while ((sqlLine = input.readLine()) != null) {
         // Executes a DB query, could throw an SQLException (checked)
         executeSql(sqlLine);

         // Could throw an InterruptedException (checked)
         Thread.sleep(1000);
      } 

   // SQLException and InterruptedException are not listed in the throws
   // clause, so they must be caught and handled here.
   } catch (SQLException e) {
      // IllegalArgumentException is a runtime exception, not required
      // to be listed in the throws clause (although you should for
      // documentation purposes)
      throw new IllegalArgumentException("Error executing SQL: " +
            sqlLine, e);

   } catch (InterruptedException e) {
      // Log and swallow
      logger.error("Unable to sleep!", e);

   } finally {
      input.close();
   }
}

What continuously surprises me is the number of Java developers I interview that have no idea what a runtime exception is or how it can be used. Runtime exceptions serve the same purpose as checked exceptions; to communicate exceptional conditions (unexpected failures, etc) to the user. They can be thrown and caught just like checked exceptions. However, handling runtime exceptions is not enforced by the compiler. This is how exceptions work in other programming languages. I was first introduced to the usefulness of runtime exceptions in Java while reading Professional J2EE Design and Development, by Rod Jonson, the creator of the Spring Framework. In that book, Rod questioned the over use of checked exceptions in Java. Rod pointed out that using a checked exception forces the caller of a method to handle that exception, even if they do not know how to handle it. Often times, developers will end up catching the checked exception, only to re-throw it (or another exception). If the code throws a new exception that does not wrap the original exception, the stack trace of the original exception, priceless for debugging, is lost. And, I’m sure we’ve all seen the dreaded “Log and Swallow” anti-pattern when the code doesn’t know what to do with an exception.

What’s the point in catching an exception if nothing can be done about it, or it has to be handled by another layer? You simply have to write a bunch of boiler plate code to catch and re-throw the exception until it gets to somebody that can handle it. Or, you have to pollute the throws clause of your method signature with a bunch of exceptions that your method itself does not throw, or with exceptions that may not be relevant to what that particular method is supposed to do. This is where runtime exceptions save the day. Since runtime exceptions can simply “bubble up” the stack, to either somebody who can handle the exception or to a catch-all, you only have to deal with the exception in one place. This reduces the amount of code you have to write, and reduces of number of places a bug can creep in. It also saves methods from having to catch or re-throw exceptions that they can’t recover from.

I think checked exceptions do still serve a purpose. I believe checked exceptions should be thrown when the caller is expected to handle the exception. For example, imagine you have an object that only does one thing: write data to a database.

public class DataWriter {

   public void updateRow(MyDataObject theData)
         throws DatabaseConnectionException, ConcurrentModificationException {
      // Make the DB call to update the row
   }
}

Let’s say it throws two exceptions, DatabaseConnectionException and ConcurrentModificationException. Of these two, I’d say that the ConcurrentModificationException could be handled by the caller. Maybe the caller will read the row that was updated, and present the user with their update and the concurrent update, asking her to either merge or overwrite the data. Maybe the calling code can handle this automatically. Either way, it’s recoverable. So, I might make this a checked exception, forcing the caller to handle it. However, the DatabaseConnectionException is a bit trickier. The caller of updateRow() may not know how to initialize a new database connection, or how to clean up an abandoned connection. I would probably make this exception a runtime exception, and let it propagate up to the top of the stack. This way, we could either show the user a general error message saying that the operation could not be completed (asking them to retry) or try to clean up the first attempt and re-try the operation for a second time automatically. The beauty about this is that this exception can be caught and handled, just like a checked exception, at any layer in the stack. So the layer that knows how to recover from the exception can catch it, and take the necessary action. All of this without having to catch and re-throw the exception once and without having to pollute our method signatures with throws DatabaseConnectionException.

One of the common complaints about using runtime exceptions is that since they do not have to be declared in the throws clause of the method that throws them, like checked exceptions do, it is often unclear what exceptions may be thrown by a method. But, just because they don’t have to be listed in the throws clause doesn’t mean that they can’t be. I believe that every method should list, in its throws clause, what exceptions it throws (checked and runtime). This way it is clear to the user what to expect out of the method. Adequate JavaDoc explaining why each of the exceptions may be thrown is also very useful. If your exception hierarchy is designed well, and you have a good separation of layers in your architecture, no individual method’s throws clause should become too verbose.

It is also important to use some form of catch-all at the top of your stack to prevent any runtime exceptions from slipping out and crashing the VM. I’ve found it helpful to properly log any exceptions caught in the catch-all, so that code can be added to handle or wrap them in a more appropriate place.

The Spring Framework uses runtime exceptions extensively. And at Orbitz, we’ve been exclusively using runtime exceptions for some time now. I think they make the code easier to read, maintain, extend, and comprehend. I can’t even imagine going back to using checked exceptions exclusively.

Accurev blogs about Orbitz technology

Dzone just picked up on a post to the Accurev blog about some of the cool things our Development Technologies team has been doing at Orbitz. I’m proud to say that we have some really bright, talented people working on that team, and I’m glad that they are starting to get some attention outside of the company (those inside already know how great they are). This team innovates like crazy, constantly coming up with ways to make our lives as developers easier. They do it all, from writing scripts to automate complicated repetitive tasks, managing our continuous integration environment, keeping core infrastructure components like our wiki and bug reporting tool up and running to their full potential, and creating custom applications to improve upon all areas of our development process.

Go Doug, Jill, and crew!

A cougar…in Chicago!?!?

Wow…this definitely falls into the “story of the day” category.

http://www.nbc5.com/news/15882607/detail.html

Over the past few years, there have been several cougar sightings in Northern Illinois. So many that websites have popped up to track them. However, although some of the sightings were reported by very reputable individuals (police, etc), the lack of photos/videos have made many equate cougar sightings to bigfoot sightings (I was a believer). But today, the skepticism ends.

It’s a shame the police had to shoot the animal, but I completely understand. They had the cougar cornered, and it either lunged at the officers, or tried to escape. Roscoe Village is a city neighborhood, full of families with small children. The police couldn’t risk letting the cat escape.

Bugzilla and SMTP Authentication

The last few months I’ve been helping out in an effort to upgrade Shotokan Karate of America’s membership management system. They’re rewriting the system from scratch, as the old system has grown to be unmaintainable. The code for the new system was about 85% complete when I signed up, so I offered to start testing the system while the sole developer working on the project finished up the code. There were no unit tests, so I figured that would be a good place to start. This naturally brought up the issue of how we would communicate problems and fixes, so I offered to install Bugzilla on my Linux box and suggested we use that.

It wasn’t until later that I found out that Bugzilla and SMTP Authentication do not get along (although I see that there is a fix in the pipeline). Bugzilla supports SMTP, but without authentication. It also supports sendmail, but my ISP isn’t too keen on people running their own mail servers, making the use of sendmail difficult. Since my ISP has a SMTP server for me to use, I figured I would use that. But, authentication is required on that server.

I toyed around with some ideas in an attempt to keep me from hacking the Bugzilla code. One involved parsing the data/mailer.testfile file (where mail is sent when the “testfile” mail option is set) for the To address and the email body, and sending the email via a custom script. But, that brought up some interesting race conditions if multiple users made a change that triggered an email at approximately the same time. We would need to periodically read that file to send the email, wiping out the contents afterward to avoid resending the same messages. If the timing was right, we could potentially wipe out a message that was never sent.

I took a look at the Bugzilla code, and found the module that was responsible for sending the email. The code was clean, and pretty easy to follow, even though I know very little perl. I found the point in the module where it calls out to the Mail Transport Agent to deliver the mail. I figured I could easily insert a call to a script to deliver the mail, using SMTP Authentication. That is exactly what I did. I modified BugMail.pm to include the “system” call below.

sub MessageToMTA {
    ....

    # --- Begin New Lines ---
    system('send-bugzilla-email.rb', $msg);
    # --- End New Lines ---

    $mailer->open($headers->header_hashref);
    print $mailer $body;
    $mailer->close;
}

$msg is a variable that contains the SMTP message to send. That message includes the To address, the subject of the email, and the body of the email. In other words, everything we would need to send the mail ourselves. I then wrote a ruby script to parse the message, and send the email. Here’s the script.

#!/usr/bin/ruby

require 'net/smtp'

from_address = 'username@my_isp.com'

# Break the message up into an array of strings
message_array = ARGV.first.split("\n")

# Pull out the To address and the subject
message_array[1] =~ /^To: (.*)$/
to_address = $1

message_array[2] =~ /^Subject: (.*)$/
subject = $1

# Delete some crap that we do not care about
4.times { |i| message_array.delete_at(0) }

# Combine what remains back into the body
body = message_array.join("\n")

message =  "From: Bugzilla <#{from_address}>\n"
message << "To: #{to_address}\n"
message << "Subject: #{subject}\n"
message << body

# Send the email
Net::SMTP.start('smtp.my_isp.com', 25, 'my_isp.com', 'username', 'password', :login) do |smtp|
  smtp.send_message message, from_address, to_address
end

File.open('/var/log/bugzilla_email.log', 'a+') do |file|
  file.puts("\n-----------------------------------------\n")
  file.puts(message)
end

Although not ideal, it does the job. And, if I do say so myself, it works quite well. I’ll be looking forward to upgrading to the new version of Bugzilla once it supports SMTP authentication. In the meantime, this will keep things rolling.