Nov 10, 2008

Glassfish with RMagick

Deploying a JRuby on Rails app with Glassfish is a relatively simple process, and in my opinion much easier than with Mongrel. Takes about 15 minutes.

Unless of course, you have a fancier Rails app which has dependencies. Then, sometimes Warbler doesn't always link the proper files, and you end up with a WAR file that can't properly connect to your gems. The one I had a big problem with was RMagick. See, this one is already an issue for JRuby, since it was written in C and JRuby is in Java, and so you can't load it. I have said before though how to get around that with a little gem called rmagick4j which ports the functionality of RMagick (supposedly there are a lot of things not built yet, but I haven't had any problems).

UPDATE: You can fix this by using the warbler config:
cd /path/to/Rails/app
jruby -S warble config
In the generated file config/warble.rb, find a line that goes:
# config.gems += ["activerecord-jdbcmysql-adapter" ...
Uncomment it, and add "rmagick4j" at the end of the array. This will fix the rmagick4j thing. However, feel free to keep reading, as I learned a bit of stuff from all that and you might too.

REST OF POST:

Unfortunately, the gem is not recognized from Glassfish. There is an alternative to RMagick called ImageVoodoo which you can use if you like, however I need to use RMagick because I use other gems that use RMagick (like gruff).

Here's how we fix it. First, you need to freeze the rmagick4j gem. There's a nice little thing called Gems on Rails, which freezes your gems in your Rails project folder. Install and use it like this:
jgem install gemsonrails
cd /path/to/Rails/app
gemsonrails
jruby -S rake gems:freeze GEM=rmagick4j
Now you're almost set. There's a problem with Rails. It seems to think that since we have this gem called rmagick4j in our app, that there should be a rmagick4j.rb file in the gem's folder. It's really a reasonable assumption, since that is the convention, but rmagick4j has to break the convention in order to be compatible with the original RMagick. So you have to do a little tiny hack here in order to get things working.

Create a file called rmagick4j.rb with this in it:

require File.dirname(__FILE__) + "/RMagick"

Put it in the vendor/gems/rmagick4j-0.3.6/lib folder (replace 0.3.6 with the version of rmagick4j that you have). Now you can use Warbler to package up your app, and it will work fine.

4 comments:

Nick Sieger said...

Hey Rob, what's the error you get when you try to warble your application before you create the rmagick4j.rb file? Could you send it to me in an email? nick at nicksieger dot com. Thanks.

Rob Britton said...

I did some further inspection and it does not seem to be a problem with Warbler, it's a problem with Rails loading in the plugins.

Martijn Storck said...

Instead of using Gruff, which requires rmagick(4j), I've started using JFreeChart java library. It can produce quite lickable charts with a little tweaking.

http://www.jfree.org/jfreechart/

Rob Britton said...

Yeah I've been hearing about all sorts of other graphing libraries available other than Gruff.
Other than the one you mentioned, or ImageVoodoo, there is:
Google Charts: http://code.google.com/p/gchartrb/
ProcessingJS: http://ejohn.org/blog/processingjs/

I like Gruff because it's written in Ruby and is quite straight-forward, so it's easy for me to go in and tweak things if it doesn't offer the functionality I want. I suppose I could do this with other libraries too, it's just really simple with things like gemsonrails to modify a Ruby gem.