Sunday, January 10, 2016

Geek: working around a Java bug...

Geek: working around a Java bug...  On Friday I ran smack into a weird bug in a most unexpected place, while doing something seemingly very straightforward.  I was creating a socket factory for SSL connections to a web site, using Jetty (which relies on standard Java library classes).  To do this, the socket factory needs to know about your certificates.  These are usually kept in a keystore file, which is what I am doing.  The standard API has you pass in the path to the keystore file, along with the password for it.  When I passed in the path, I got an exception (below with the first few lines of the stack trace):

Caused by: java.lang.RuntimeException: default directory must be absolute
   at sun.nio.fs.UnixFileSystem.(UnixFileSystem.java:54)
   at sun.nio.fs.LinuxFileSystem.(LinuxFileSystem.java:39)
   at sun.nio.fs.LinuxFileSystemProvider.newFileSystem(LinuxFileSystemProvider.java:43)
   at sun.nio.fs.LinuxFileSystemProvider.newFileSystem(LinuxFileSystemProvider.java:36)
   at sun.nio.fs.UnixFileSystemProvider.(UnixFileSystemProvider.java:55)

The default directory path was absolute, so this error made no sense.  Eventually I tracked down the source of the issue: a known Java bug (JDK-7181721)!  That's something one doesn't run into every day – virtually 100% of the time, the bug is either in your own code, or someone else's code that you're relying on – not the code that's actually part of Java.  This is so unusual that I was suspicious I was wrong at first.  So I came up with a workaround that avoided this code, and sure enough that fixed it.

The workaround?  It turns out that the socket factory as a completely separate way to get the certificates.  You can create a Keystore object (in memory) and load it yourself, then pass that in.  I did that this morning, and bye-bye problem.

I'd be quite happy to not run into another one of these!  On the other hand, finding and fixing such a bizzaro problem is kind of satisfying...

No comments:

Post a Comment