From Ruby on Rails to Java
I have been tasked with making some extensions and changes to a system written i Ruby on Rails with some parts written in Java. For reasons I will not write about at this time we have decided that we want to move away from Ruby on Rails and instead develop using java and the excellent Spring framework.
Leaving one development framework for another is often a painful experience, where lots of code needs to be thrown away and rewritten in one step. For this particular situation doing that massive rewrite, technology change has been something we wish to avoid and here are some info on how we plan to do that.
The first step was to move both the java and the ruby environment into the same namespace from the point of view of the web browser. Since we already use the Apache httpd as a front end for the Ruby environment—executing it via mod_fcgid—this was a simple matter of configuring the same apache httpd instance to proxy all requests to the java environment. That way we can make relative links from pages created in he ruby environment to pages in the java environment.
The next problem to tackle was session management. More specifically, when a user logged in using Ruby on Rails we and then navigated to a page served by the java environment we needed to propagate the information about the logged in user to the java code, hopefully without doing any drastic changes to the ruby code.
It turns out that doing that was not that difficult. The way our Ruby on Rails environment is configured it uses Active Record to store session information associated with a specific cookie in the web brower in the database. Since both ruby and java lives on the same webserver from the point of view of the brower, any cookies set by rails is also sent by the browser as a part of the reqests that ends up in the java environment.
I have then written code that uses the cookie to look up the session data in the database from java. That data consists of a Base64-encoded binary data using the Marshal.dump() facility of Ruby. It turns out that data is not particularly difficult to parse relevant bits from. Looking for the ASCII string of the key of our user id value in the session object in Ruby, then look for the next double qoute char and parse all ASCII digits before the next double qoute occurs seems to be a fully workable solution.
The code to do this can be looked at in RailsSessionIntegrationHelper.java. If you want to get rid of the Spring Framework dependency, just replace the method getSessionData() with one that does raw JDBC instead, and remember to close your connection when you’re done.
Filed under Geeky, Programming | Comment (0)Image resizing in java
Doing good image resizing in your favourite software development environment shouldn’t be hard. After all, lots and lots of software that has a graphical user interface of some kind needs to do image resizing. However, in java it isn’t as easy to do as it should be. I had some resizing needs, more specifically I needed code to resize a large black-and-white image into thumbnail size.
After googling a bit I came up with a recipe from the official Java 2D FAQ at sun.com and used that. After all, the creators of Java should know how to do it right. However, I was surprised to find out that the visual result of the resizing was terrible. Have a look for yourself:

I started out with this letter a in black and white. When resizing that one to a version 40 pixels wide with the code that sun suggests, it looks like this when magnified:

As you see, there is some grayscale smoothing going on but not much and the overall impression is quite jagged. Before anyone asks, yes I’m using the bilinear interpolation option. Compared to the result when resizing in GIMP, ImageMagick or just about any other tool the result is terrible. So i tried around, googled and looked at code here and there.
I was on my way to accept that java just didn’t do this, and restort to calling a command line tool from my web application when I found a piece of code that compares the speed and results of different scaling methods. It turns out that if you use the getScaledInstance() method of the java.awt.Image base class with the Image.SCALE_SMOOTH as last parameter, the result looks much better.
Here is a blown up version of a 40 pixels wide rescaling using that method instead:

Ah, much better. Why is this? I don’t know. If there is anyone out there that can give me details on why this is I’m more than happy to be educated.
So, if you want to copy my method, please have a look at ImageResizer.java. The version calling a verbatim copy of the suggested solution from Sun’s FAQ is in the method sunResize() and the better looking version is in resize().
Filed under Geeky, Programming | Comment (0)It will be ready on time, Lord Vader
Well, that’s debatable. At last however, me and my dear darling has managed to finish building 10143, a.k.a. the Lego Death Star II. Thanks for the fantastic gift, ordningsfrun and family!
Filed under Geeky | Comments (3)
So, I wanted encrypted access to multiple websites
Multiple websites on a single server that provide encrypted access is traditionally done by adding one IP address per website. However, that is no longer necessary now that modern web browsers has support for Server Name Indication which enables multiple HTTPS websites sharing a single IP address. All that is needed is to enable support for this on your webserver.
On the Linux distribution I use on my servers, CentOS 4, that was a bit tricky. My first plan was to update the openssl package to a version that supports SNI, but that turned out to be seriously difficult since the library has changed major version between the version shipped in CentOS 4 and the version that includes SNI support and that would mean recompiling many parts of the core system.
However, I found that there is an alternative apache module to the mod_ssl shipping in CentOS called mod_gnutls. It provides the same basic functionality but does so without using the openssl library. So, I pulled the latest stable version of mod_gnutls and made an RPM package of it. It depended on newer versions of a few packages that I could pull from Fedora rawhide and recompile for CentOS 4. If you want to use the packages I built, they are available from a special yum repository. Adding this repository and installing mod_gnutls will upgrade the system provided libgcrypt and gnutls packages to newer versions.
Filed under Geeky | Comment (0)Updates day
I use OSX, Linux and boot Vista and XP from time to time. Today seems to have been a curious confluence of large updates. First i updated OSX to 10.5.3 (I hope that they have fixed the 802.11n bug that kills my d-link AP, will test later.) Then it was time to boot Vista to print on the printer in the new office that only can use color in Windows, which after the first updates reboot informed me that it was time to install Vista Service Pack 1. It took a while. Of course, Fedora 9 had some 50 megs of updates today also, so the updates total for today was pretty serious.
Filed under Geeky | Comment (0)Flash player on Fedora 9 x86_64
I installed Fedora 9 on my x86_64 box today, and one of the early tasks was to get Adobe’s flash player working. Unfortunately it didn’t work out of the box, but once I understood what went wrong the fix was easy:
yum -y install nspluginwrapper.i386 libflashsupport.i386
(this assumes that you have installed the flash with the yum method described at Adobe’s site)
I hope that our friends at Adobe sorts this out to be a bit more user friendly in the future, perhaps by licensing their player in a Fedora compatible way so that it can be a part of the distribution.
Filed under Geeky | Comment (0)
