Sunday, November 19, 2006

Using Apache Abdera with JRuby

A while back I attempted to play with one of the initial versions of Abdera in JRuby, just to see how difficult it would be to get some scripts that manipulate Atom data with Abdera doing the low level heavy lifting. It seemed like a match made in heaven, but unfortunately the theory was somewhat better than the practice, mainly due to difficulties with the JRuby/Java integration and the way that early versions of Abdera expected you to access the default versions of various interfaces.

Fortunately, the way Abdera has evolved between then and now happens to make it considerably easier to use from JRuby. Rather than static variables defined on the org.apache.abdera.parser.Parser classes you now use a top level org.apache.abdera.Abdera object to access the default implementations of org.apache.abdera.parser.Parser, org.apache.abdera.factory.Factory, and other major interfaces. This works a LOT better with JRuby.

So without further ado, here's a simple little example, which uses the not-quite-out-yet Abdera 0.2.0 release to parse the Atom feed that lists Abdera's releases from within a JRuby 0.9.1 script:

require 'java'

include_class 'org.apache.abdera.Abdera'
include_class ''

abdera =

parser = abdera.parser

url ='file:///Users/garrett/Code/abdera-site-trunk/releases.xml')

doc = parser.parse(url.open_stream(), url.to_string())

feed = doc.root

print "#{feed.title}\n"

for entry in feed.entries do
if and
author =
elsif and
author =
author = "Someone"

print " #{author} posted '#{entry.title}' on #{entry.updated}\n"

Then run it like this:

$ ./bin/jruby parse.rb
Apache Abdera - Releases
Abdera PPMC posted 'Abdera 0.2.0-incubating' on Thu Nov 09 19:00:00 EST 2006
Abdera PPMC posted 'Abdera 0.1.0-incubating' on Thu Nov 09 19:00:00 EST 2006

Note that in order for this to work the jar files for Abdera and it's dependencies will have to be in your CLASSPATH. Naturally, once the release is final that Atom feed will be located on our actual web site, and those updated dates will need to be tweaked, but you get the idea. I'm absolutely going to be looking at using Abdera via JRuby the next time I want to mess around with some Atom data.