RJB - another choice for Ruby and Java interoperability<br />Akio Tajima aka arton<br />
Who am I ?<br />I like interface (bridge, mediator) programming.<br />IActiveScript interface on top of Ruby<br />-> Activ...
RESTful design for Java <br />EJB guy moved to REST<br />
Ruby Java Bridge<br />http://www.artonx.org/collabo/backyard/?RjbMechanism<br />
require ‘rjb’<br />FooBar = Rjb::import(‘com.example.FooBar’)<br />foobar = FooBar.new<br />foobar.baz = ‘123’  # foobar.s...
installing RJB<br />sudoJAVA_HOME=/usr/java  gem install rjb<br />・runnable with Sun J2SE 1.4.2 (maybe), 1.5.x, 1.6.x  (po...
Why RJB ?<br />I need glue language so I can easily write the interaction  between Java classes for testing.<br />To try a...
Who use RJB ?<br />When migrating to MRI Rails app from Java Web app.<br />Can use Java business objects on Rails.<br />Us...
Design strategy<br />Start with a simple plan.<br />Class is Class : can create java instance with .new<br />eg)  o = Rjb:...
Design strategy<br />JRuby compatibility (suggested by JRuby team)<br />Transform methods into ruby style<br />class -> Ru...
writing script for JRuby and Rjb<br />MRI can’t require java but JRuby can require rjb. <br />----------------------------...
howto<br />Rjb converts returned value into a proxy object if it was an object.<br />In many case, if the returned value i...
Future plans<br />Strategy to determine selection of polymorphic methods<br />ClassLoader enhancement<br />Easy way to inh...
Conclusion<br />You can write Ruby script using Java objects.<br />with JRuby<br />or <br />  MRI / Rubinius with Rjb.<br />
Upcoming SlideShare
Loading in …5
×

RJB - another choice for Ruby and Java interoperability

5,644 views

Published on

Ruby Java Bridge, how, what , why
@lchin helped to translate this slide. many thanks.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,644
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
10
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

RJB - another choice for Ruby and Java interoperability

  1. 1. RJB - another choice for Ruby and Java interoperability<br />Akio Tajima aka arton<br />
  2. 2. Who am I ?<br />I like interface (bridge, mediator) programming.<br />IActiveScript interface on top of Ruby<br />-> ActiveScriptRuby<br />JNI interface under Ruby<br />-> RJB<br />
  3. 3. RESTful design for Java <br />EJB guy moved to REST<br />
  4. 4. Ruby Java Bridge<br />http://www.artonx.org/collabo/backyard/?RjbMechanism<br />
  5. 5. require ‘rjb’<br />FooBar = Rjb::import(‘com.example.FooBar’)<br />foobar = FooBar.new<br />foobar.baz = ‘123’ # foobar.setBaz(‘123’)<br />p foobar.baz # foobar.getBaz<br />foobar.snake_method # foobar.snakeMethod<br />How to use RJB<br />Importing class<br />Instantiating class<br />Calling setter<br />Calling getter<br />Calling method<br />
  6. 6. installing RJB<br />sudoJAVA_HOME=/usr/java gem install rjb<br />・runnable with Sun J2SE 1.4.2 (maybe), 1.5.x, 1.6.x (possible with OpenJDK) (beware 32 / 64 selection)<br />・buildable with MRI, Rubinius<br />Tested with OSX snow leopard, Windows7, Solaris 10, some Linux<br />http://rubyforge.org/projects/rjb/<br />http://github.com/arton/rjb<br />
  7. 7. Why RJB ?<br />I need glue language so I can easily write the interaction between Java classes for testing.<br />To try and see Java object’s behavior and call reflections freely.<br /> At that time there was no JRuby yet.<br />I like to write and define interfaces. It’s fun.<br />
  8. 8. Who use RJB ?<br />When migrating to MRI Rails app from Java Web app.<br />Can use Java business objects on Rails.<br />Using Java component in Ruby script.<br />Jakarta POI<br />JDBC<br />
  9. 9. Design strategy<br />Start with a simple plan.<br />Class is Class : can create java instance with .new<br />eg) o = Rjb::import(‘foo.bar.bazClass’).new<br />Exception as Exception (user request)<br />Can rescue and read the properties<br />begin<br /> ….<br />rescue SQLException => e<br /> puts e.error_code<br />end<br />When writing the rescue line, SQLException was not defined (no need to Rjb::import),<br />dynamic language is really nice.<br />
  10. 10. Design strategy<br />JRuby compatibility (suggested by JRuby team)<br />Transform methods into ruby style<br />class -> Ruby’s , getClass-> Java’s<br />Require expansion (contributed by aronge)<br />require ‘rjb’<br />require ‘rjbextension’<br />require ‘/foo/bar/FooBar.jar’ # dynamic jar import(*)<br />p java.lang.Math.abs(-30) #=> 30 no import call<br />* Only effective before loading JVM<br />
  11. 11. writing script for JRuby and Rjb<br />MRI can’t require java but JRuby can require rjb. <br />------------------------------------------<br />begin<br /> require ‘java’<br />rescue LoadError<br /> require ‘rjb’<br /> require ‘rjbextension’<br /> require ‘jrubybridge’ # def include (vaporware)<br />end<br />Or post a message into RubyForge help forum.<br />http://rubyforge.org/forum/forum.php?forum_id=8190<br />
  12. 12. howto<br />Rjb converts returned value into a proxy object if it was an object.<br />In many case, if the returned value is a primitive type class (ex: java.lang.Integer) , it’s better to convert into a Ruby object (ex: Fixnum).<br />The behavior is controlled by <br />Rjb::primitive_conversion = true<br />default : false<br />
  13. 13. Future plans<br />Strategy to determine selection of polymorphic methods<br />ClassLoader enhancement<br />Easy way to inherit from existing classes<br />
  14. 14. Conclusion<br />You can write Ruby script using Java objects.<br />with JRuby<br />or <br /> MRI / Rubinius with Rjb.<br />

×