| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
These steps will get you from zero to a working JRuby on Rails app that can access Spring configured beans using maven to manage your dependencies.
Basic Steps:
How to create Java classes in JRuby.
blah = java.util.Date.new
We don't need Maven a whole lot, if at all, for Rails development, however it is handy to use it to manage our dependencies, especially if you have parent poms or other re-usable components already in your organization.
Courtesy of this awesome blog post, we have a pom that will copy all of our needed jar files into the rails app's lib/ directory. Note that you have to run this manually whenever you change your dependencies. Your pom.xml will look something like:
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<artifactId>railsApp</artifactid>
<name>railsApp</name>
<dependencies>
<dependency>
<groupId>com.company</groupid>
<artifactId>java-legacy-app</artifactid>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>railsApp</finalname>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupid>
<artifactId>exec-maven-plugin</artifactid>
<executions>
<execution>
<id>create-mock-web-descriptor</id>
<phase>compile</phase>
<configuration>
<executable>/bin/sh</executable>
<workingDirectory>.</workingdirectory>
<arguments>
<argument>-c</argument>
<argument>
mkdir -p src/main/webapp/WEB-INF
touch src/main/webapp/WEB-INF/web.xml
</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>copy-needed-jars-into-lib</id>
<phase>package</phase>
<configuration>
<executable>/bin/sh</executable>
<workingDirectory>.</workingdirectory>
<arguments>
<argument>-c</argument>
<argument>
rm -f lib/*.jar
cp target/railsApp/WEB-INF/lib/*.jar lib
rm -rf target/railsApp*
rm -rf src
</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>create-final-war</id>
<phase>package</phase>
<configuration>
<executable>/bin/sh</executable>
<workingDirectory>.</workingdirectory>
<arguments>
<argument>-c</argument>
<argument>
rm -rf *.war tmp/war
jruby -S warble && \
mv *.war target/railsApp.war
</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Then, to update dependencies:
mvn package # downloads jars into target/ dir,
# then copies them to the lib dir
Finally, Rails needs to load all of these when it starts up. Create config/initializers/01_java_jars.rb:
Dir.entries("#{RAILS_ROOT}/lib").sort.each do |entry|
if entry =~ /.jar$/
require entry
end
end
This basically "loads" all the libs you had maven copy into your app's lib directory. You'll need to restart rails to see this.
One thing that wasn't clear to me with JRuby is what "package roots" are available by default. For example, my company has code with a package rooted at poscore, and ruby code like poscore.model.type.Customer.new just didn't work. It seems that only certain common top-level packages are automatically available without qualifying, so you can certainly do:
c = Customer.new
I opted to add some methods to application_controller.rb, since I'd be needing these a lot. There are other methods of simulating Java's import something.* hanging around, but this worked for my purposes:
# Get the poscore package from Java def self.poscore; Java::poscore; end
One of the coolest things about JRuby and Rails is that you can package up your Rails app as a .war and dump it into a J2EE app server:
jruby -S warble config # this sets up stuff to make a war jruby -S warble # creates blah.war in local dir
Basically, you need to get a list of all the spring configuration files you intend to load. If your XML configuration files are somewhere in the classpath in a directory called config, you can get a list of them as such
# Our config files live in src/main/resources/config
def beans
["configurationContext", "otherContext", "dataContext" ].map { |c|
"classpath:config/#{c}.xml"
}.to_java :string
end
You can then load them as such:
context = org.springframework.context.support.ClassPathXmlApplicationContext.new(beans)
# We have a bean named "someService" configured, so we get it
service = context.getBean("someService")
You should probably put this in an initializer, naming it to ensure it happens after your java jars are loaded. Using the naming scheme from above, the name config/initializers/02_spring.rb would work as such:
SPRING_CONTEXT = org.springframework.context.support. ClassPathXmlApplicationContext.new(SPRING_XML_CONFIG_FILES)
Again, this assumes that our config files are on the classpath in a config subdirectory. Your setup may need to be different.
Now, you can use SPRING_CONTEXT anywhere in your code to access beans. With Ruby's awesome meta-programming, it's not hard to envision some slick method_missing means of getting access to your beans:
class << context
alias old_method_missing method_missing
def method_missing(sym,args)
if args.empty?
getBean(sym.toString)
else
old_method_missing(sym,args)
end
end
end
context.someService # gets our someService bean
That's it! You now have a JRuby on Rails application that can access your existing java-defined Spring beans.
| Back | FazBrowse Home | New Git URL |