JRuby + Spring + Hibernate

Posted by Richard Sat, Nov 19 2005 15:39

Recently, I needed a way to load data into a database. The trick was using a Spring+Hibernate model to load the data since I already had that written. I decided to give JRuby a try. I have been using regular Ruby the last couple of weeks and I liked how easy it was to learn/use. I am no Ruby expert, but it hasn't taken much to pick it up.

Anyway, my goal was to explore using JRuby to perform the load using the Spring+Hibernate model I had at my disposal.

The first problem I had was getting all of the necessary jars into my classpath. To get over this, I ended up modifying the jruby.rb file to include any jars that were in my CLASSPATH environment variable. There may have been a better way to do this, but it worked and I only added the following lines to the jruby.rb.

env_cp = ENV['CLASSPATH']
env_cp = "" if env_cp.nil?

And for modifying the launch classpath.

launch.classpath = "#{classpath}:#{env_cp}"

The next issue I ran into was a hard one to track down even though the answer was in the JRuby documentation. I needed to be able to create a Java array of strings (i.e. String[]). I needed this for Spring's ClassPathXmlApplicationContext. I tried a number of different things and in different combinations and none worked. I finally had to walk away from it for a while. When I ended up figuring it out, this is what it looked like:

require 'java'

include_class 'java.lang.System'
include_class('java.lang.String') {|p,n| "J" + n }
include_class 'org.springframework.context.support.ClassPathXmlApplicationContext'

class BaseSpring
  attr_accessor :ctx
  def initialize
    configs = JString[].new(2)
    configs[0] = "/WEB-INF/applicationContext-testing.xml"
    configs[1] = "/WEB-INF/my-servlet.xml"
    @ctx = ClassPathXmlApplicationContext.new configs
  end
end

BaseSpring.new

Once I figured out the Java string array problem, writing the rest of the load was easy. What about future use of JRuby? For quick loads and diagnostics, I am definitely going to consider it. I might even try using it when writing unit tests.

Posted in  | no comments

Comments

(leave url/email »)

   Preview comment