Ruby for Java
programmers
Ugo Cei
Sourcesense
u.cei@sourcesense.com
Who should attend this talk
• Java programmers wishing to know how to include Ruby
  code in Java programs or vice-versa.
...
Agenda
• Why?
• How?
• Bridges
• JRuby
• XML-RPC
• SOAP
• Demos




                     Ugo Cei: “Ruby for Java Programmers”
What’s in it for me?




               Ugo Cei: “Ruby for Java Programmers”
Shameless Plug: The Open Source Zone




            http://oszone.org
                            Ugo Cei: “Ruby for Java...
Shameless Plug: The Open Source Zone




            http://oszone.org
                            Ugo Cei: “Ruby for Java...
Shameless Plug: Evil or Not?




       http://evilornot.info
                          Ugo Cei: “Ruby for Java Programmers”
Shameless Plug: Evil or Not?




if /<title>(.+)</title>/ ...                          Search?

                     http:...
“The era of islands is over for most
development scenarios. You don't have to
make one definitive choice. Instead, you
get...
How?




       Ugo Cei: “Ruby for Java Programmers”
RubyJavaBridge
• http://arton.no-ip.info/collabo/backyard/?RubyJavaBridge
• Follow the instructions in readme.txt and on t...
Sample Java Code
• Uses ROME (https://rome.dev.java.net).


public class Fetcher {
	      public static SyndFeed fetch(Str...
Client Ruby Code
 require 'rjb'

 Rjb::load('.:rome-0.7.jar:rome-fetcher-0.7.jar:jdom.jar:jdom.jar', [])

 fetcher = Rjb::...
YAJB
• http://www.cmt.phys.kyushu-u.ac.jp/~M.Sakurai/cgi-bin/fw/
  wiki.cgi?page=YAJB
• Same caveats and limitations as fo...
Client Ruby Code
require 'yajb/jbridge'
include JavaBridge

JBRIDGE_OPTIONS = {
  :classpath => '.:rome-0.7.jar:rome-fetch...
Making a Simple Java Class
 require 'yajb/jbridge'
 require 'yajb/jlambda'

 include JavaBridge

 c = JClassBuilder.new(qu...
Implementing an Interface
jimport quot;java.util.*quot;
c = JClassBuilder.new(quot;NumCompquot;)
c.add_interface(quot;java...
Using SWIG
• http://www.swig.org
• Jakarta POI, nifty library for manipulating Microsoft OLE 2
  Compound Document formats...
JRuby
• http://jruby.sourceforge.net/
• http://jruby.codehaus.org/
• JRuby is not a bridge between Java and Ruby but a ful...
JRuby Client Code
 require 'java'

 include_class 'Fetcher'

 feed = Fetcher.fetch(ARGV[0])

 feed.entries.each do | entry...
Calling Ruby Methods
• demo1.rb

class Demo1
 def foo
   print 'bar'
 end
end


• Demo1.java
IRuby runtime = Ruby.getDefau...
Using the Bean Scripting Framework
• The Bean Scripting Framework, when used with JRuby,
  will allow you more convenientl...
Name Clashes
 import_class ‘java.lang.String’
 import_class ‘java.net.URI’
• This won’t work as it conflicts with Ruby’s S...
Running Ruby on Rails




               Ugo Cei: “Ruby for Java Programmers”
Running Camping




• http://jruby.codehaus.org/The+JRuby+Tutorial+Part+2+-
  +Going+Camping


                           ...
Using the JDBC AR Adapter
jruby $JRUBY_HOME/bin/gem install ActiveRecord-JDBC --no-ri --no-rdoc

• As of 0.2.0 works for:
...
Using the JDBC AR Adapter


adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbname
username: ...
XML-RPC
• Pro: Established technology.
• Pro: No need to use bridge code or special interpreters.
• Con: Separate processe...
XML-RPC Java Server
• Uses Apache XML-RPC.

public class RPCFetcher {

  public static void main(String args[]) throws Exc...
XML-RPC Java Server
public Vector fetch(String url) throws Exception {
  URL feedUrl = new URL(url);
  FeedFetcherCache fe...
XML-RPC Ruby Client


require 'xmlrpc/client'
server = XMLRPC::Client.new 'localhost', '/', 8080
entries = server.call('fe...
SOAP
• Complex and heavyweight, but most of the complexity is
  hidden by tools.
• WSDL service description can be automat...
services.xml
• Maps service names to service classes:

<beans xmlns=quot;http://xfire.codehaus.org/config/1.0quot;>
 <servic...
SOAP Ruby Client
 require 'soap/wsdlDriver'
 WSDL_URL = 'http://localhost:8080/services/BookService?wsdl'
 driver = SOAP::...
Thank You!

  Slides will be available at
       http://agylen.com/
              and at
http://www.sourcesense.com/


   ...
Ugo Cei Presentation
Upcoming SlideShare
Loading in...5
×

Ugo Cei Presentation

2,927

Published on

Published in: Technology
2 Comments
2 Likes
Statistics
Notes
  • This slide is so useful for those who wish to learn further on Ruby for Java programmers. I would recommend this one.

    Roy Jan
    http://au.freepolyphonicringtones.org/ http://at.freepolyphonicringtones.org/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Outstanding display. Very clear and useful
    Teisha
    http://dashinghealth.com http://healthimplants.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,927
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
40
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide

Ugo Cei Presentation

  1. 1. Ruby for Java programmers Ugo Cei Sourcesense u.cei@sourcesense.com
  2. 2. Who should attend this talk • Java programmers wishing to know how to include Ruby code in Java programs or vice-versa. • Ruby programmers wishing for the same. Who should NOT attend this talk • Java programmers wishing to learn more about the Ruby programming language. • This is not a talk about the differences between Java and Ruby. Ugo Cei: “Ruby for Java Programmers”
  3. 3. Agenda • Why? • How? • Bridges • JRuby • XML-RPC • SOAP • Demos Ugo Cei: “Ruby for Java Programmers”
  4. 4. What’s in it for me? Ugo Cei: “Ruby for Java Programmers”
  5. 5. Shameless Plug: The Open Source Zone http://oszone.org Ugo Cei: “Ruby for Java Programmers”
  6. 6. Shameless Plug: The Open Source Zone http://oszone.org Ugo Cei: “Ruby for Java Programmers”
  7. 7. Shameless Plug: Evil or Not? http://evilornot.info Ugo Cei: “Ruby for Java Programmers”
  8. 8. Shameless Plug: Evil or Not? if /<title>(.+)</title>/ ... Search? http://evilornot.info Ugo Cei: “Ruby for Java Programmers”
  9. 9. “The era of islands is over for most development scenarios. You don't have to make one definitive choice. Instead, you get to hog all the productivity you can for the common cases, then outsource the bottlenecks to existing packages in faster languages or build your own tiny extension when it's needed.” David Heinemeier Hansson, Sep 13, 2006 Ugo Cei: “Ruby for Java Programmers”
  10. 10. How? Ugo Cei: “Ruby for Java Programmers”
  11. 11. RubyJavaBridge • http://arton.no-ip.info/collabo/backyard/?RubyJavaBridge • Follow the instructions in readme.txt and on the website and it should work (even on Intel Macs). • Encapsulate you Java code in high-level methods and classes to hide 3rd party libraries and Java idiosyncrasies from Ruby as much as possible. Ugo Cei: “Ruby for Java Programmers”
  12. 12. Sample Java Code • Uses ROME (https://rome.dev.java.net). public class Fetcher { public static SyndFeed fetch(String url) throws Exception { URL feedUrl = new URL(url); FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); FeedFetcher fetcher = new HttpURLFeedFetcher(feedInfoCache); SyndFeed feed = fetcher.retrieveFeed(feedUrl); return feed; } } Ugo Cei: “Ruby for Java Programmers”
  13. 13. Client Ruby Code require 'rjb' Rjb::load('.:rome-0.7.jar:rome-fetcher-0.7.jar:jdom.jar:jdom.jar', []) fetcher = Rjb::import('Fetcher') feed = fetcher.fetch(ARGV[0]) print feed.getTitle, “n” entries = feed.getEntries.iterator while entries.hasNext do entry = entries.next print quot;#{entry.getPublishedDate.toString} #{entry.getTitle}nquot; end • No mapping from Java iterators to Ruby loops. • No date type conversions. • No support for JavaBean properties. Ugo Cei: “Ruby for Java Programmers”
  14. 14. YAJB • http://www.cmt.phys.kyushu-u.ac.jp/~M.Sakurai/cgi-bin/fw/ wiki.cgi?page=YAJB • Same caveats and limitations as for RJB. Ugo Cei: “Ruby for Java Programmers”
  15. 15. Client Ruby Code require 'yajb/jbridge' include JavaBridge JBRIDGE_OPTIONS = { :classpath => '.:rome-0.7.jar:rome-fetcher-0.7.jar:jdom.jar:jdom.jar' } jimport quot;Fetcherquot; feed = :Fetcher.jclass.fetch(ARGV[0]) print feed.getTitle, quot;nquot; entries = feed.getEntries.iterator while entries.hasNext do entry = entries.next print quot;#{entry.getPublishedDate.toString} #{entry.getTitle}nquot; end Ugo Cei: “Ruby for Java Programmers”
  16. 16. Making a Simple Java Class require 'yajb/jbridge' require 'yajb/jlambda' include JavaBridge c = JClassBuilder.new(quot;MyClassquot;) c.add_field(quot;private int num;quot;) c.add_constructor(quot;public AAA(int a) {num = a;}quot;) c.add_method(quot;public int square() { return num * num;}quot;) puts quot;Simple class: #{c.new_instance(5).square}quot; • Uses javassist: http://www.csg.is.titech.ac.jp/~chiba/ javassist/index.html Ugo Cei: “Ruby for Java Programmers”
  17. 17. Implementing an Interface jimport quot;java.util.*quot; c = JClassBuilder.new(quot;NumCompquot;) c.add_interface(quot;java.util.Comparatorquot;) # must be full qualified class name c.add_method(<<'JAVA') public int compare(Object o1, Object o2) do int i1 = ((Number)o1).intValue(); int i2 = ((Number)o2).intValue(); return i2-i1; end JAVA sample = [9,1,8,2,7,3,6,4,5,0,10] list = :ArrayList.jnew sample.each {|i| list.add( i ) } :Collections.jclass.sort(list, c.new_instance) p quot;Sort:quot;,list.toArray Ugo Cei: “Ruby for Java Programmers”
  18. 18. Using SWIG • http://www.swig.org • Jakarta POI, nifty library for manipulating Microsoft OLE 2 Compound Document formats (Office files) uses SWIG to provide Ruby bindings. • POI compiled using gcj and Ruby bindings generated using SWIG. • http://jakarta.apache.org/poi/poi-ruby.html Ugo Cei: “Ruby for Java Programmers”
  19. 19. JRuby • http://jruby.sourceforge.net/ • http://jruby.codehaus.org/ • JRuby is not a bridge between Java and Ruby but a full- featured Ruby interpreter written in 100% Java. • Gives Ruby code instant access to all Java libraries. • Cannot load Ruby extensions written in C. • “Almost” Mostly able to run RubyGems and Rails. • Quick development pace. • Still slow compared to C Ruby, but quoting Charles O. Nutter: “I think it's now very reasonable to say we could beat C Ruby performance by the end of the year.” Ugo Cei: “Ruby for Java Programmers”
  20. 20. JRuby Client Code require 'java' include_class 'Fetcher' feed = Fetcher.fetch(ARGV[0]) feed.entries.each do | entry | p quot;#{entry.publishedDate} #{entry.title}quot; end • Can use “each” on Java collections. • Date type conversion. • Full support for JavaBean properties. Ugo Cei: “Ruby for Java Programmers”
  21. 21. Calling Ruby Methods • demo1.rb class Demo1 def foo print 'bar' end end • Demo1.java IRuby runtime = Ruby.getDefaultInstance(); runtime.loadFile(new File(quot;ruby/demo1.rbquot;), false); RubyClass rb = runtime.getClass(quot;Demo1quot;); IRubyObject obj = rb.newInstance(new RubyObject[0]); obj.callMethod(quot;fooquot;); Ugo Cei: “Ruby for Java Programmers”
  22. 22. Using the Bean Scripting Framework • The Bean Scripting Framework, when used with JRuby, will allow you more conveniently to pass your own Java objects to your JRuby script. You can then use these objects in JRuby, and changes will affect your Java program directly. BSFManager.registerScriptingEngine(quot;rubyquot;, quot;org.jruby.javasupport.bsf.JRubyEnginequot;, new String[] { quot;rbquot; }); BSFManager manager = new BSFManager(); JLabel mylabel = new JLabel(); manager.declareBean(quot;labelquot;, mylabel, JLabel.class); manager.exec(quot;rubyquot;, quot;(java)quot;, 1, 1, quot;$label.setText(quot;This is a test.quot;)quot;); Ugo Cei: “Ruby for Java Programmers”
  23. 23. Name Clashes import_class ‘java.lang.String’ import_class ‘java.net.URI’ • This won’t work as it conflicts with Ruby’s String and URI classes. • Solution: use a module. module Java import_class ‘java.lang.String’ import_class ‘java.net.URI’ end ... uri = Java::URI.new(‘http://example.com/’) • Or remap names: include_class(quot;java.lang.Exceptionquot;) {|p,n| quot;Jquot; + n } Ugo Cei: “Ruby for Java Programmers”
  24. 24. Running Ruby on Rails Ugo Cei: “Ruby for Java Programmers”
  25. 25. Running Camping • http://jruby.codehaus.org/The+JRuby+Tutorial+Part+2+- +Going+Camping Ugo Cei: “Ruby for Java Programmers”
  26. 26. Using the JDBC AR Adapter jruby $JRUBY_HOME/bin/gem install ActiveRecord-JDBC --no-ri --no-rdoc • As of 0.2.0 works for: • MySQL • PostgreSQL • Oracle • HSQLDB • Microsoft SQL Server • DB2 • Apache Derby • FireBird Ugo Cei: “Ruby for Java Programmers”
  27. 27. Using the JDBC AR Adapter adapter: jdbc driver: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/dbname username: admin password: secret Ugo Cei: “Ruby for Java Programmers”
  28. 28. XML-RPC • Pro: Established technology. • Pro: No need to use bridge code or special interpreters. • Con: Separate processes => must take network latency and failures into account. • Con: Overhead of HTTP communication, XML parsing, marshalling... • Need to convert types to something that XML-RPC is able to understand, i.e.strings, numbers, dates, Vectors, Hashtables and little else. Ugo Cei: “Ruby for Java Programmers”
  29. 29. XML-RPC Java Server • Uses Apache XML-RPC. public class RPCFetcher { public static void main(String args[]) throws Exception { WebServer server = new WebServer(8080); server.addHandler(quot;$defaultquot;, new RPCFetcher()); server.start(); } // Continued... Ugo Cei: “Ruby for Java Programmers”
  30. 30. XML-RPC Java Server public Vector fetch(String url) throws Exception { URL feedUrl = new URL(url); FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); FeedFetcher fetcher = new HttpURLFeedFetcher(feedInfoCache); SyndFeed feed = fetcher.retrieveFeed(feedUrl); Vector items = new Vector(); for (Iterator it = feed.getEntries().iterator() ; it.hasNext() ; ) { SyndEntry entry = (SyndEntry) it.next(); Hashtable map = new Hashtable(); map.put(quot;linkquot;, entry.getLink()); map.put(quot;titlequot;, entry.getTitle()); map.put(quot;publishedDatequot;, entry.getPublishedDate()); items.add(map); } return items; } Ugo Cei: “Ruby for Java Programmers”
  31. 31. XML-RPC Ruby Client require 'xmlrpc/client' server = XMLRPC::Client.new 'localhost', '/', 8080 entries = server.call('fetch', 'http://agylen.com/feed') entries.each do | entry | p quot;#{entry['publishedDate'].to_time} #{entry['title']}quot; end Ugo Cei: “Ruby for Java Programmers”
  32. 32. SOAP • Complex and heavyweight, but most of the complexity is hidden by tools. • WSDL service description can be automatically generated from server code. • Ruby’s SOAP library provides what is necessary to read WSDL documents and create classes and methods on the fly. • Sample Java server code based on XFire samples can be found here:http://agylen.com/2006/05/06/ruby-for-java- programmers-part-vi/. Ugo Cei: “Ruby for Java Programmers”
  33. 33. services.xml • Maps service names to service classes: <beans xmlns=quot;http://xfire.codehaus.org/config/1.0quot;> <service> <name>BookService</name> <namespace>http://sourcesense.com/BookService</namespace> <serviceClass>com.sourcesense.xfire.demo.BookService</serviceClass> </service> </beans> Ugo Cei: “Ruby for Java Programmers”
  34. 34. SOAP Ruby Client require 'soap/wsdlDriver' WSDL_URL = 'http://localhost:8080/services/BookService?wsdl' driver = SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver books = driver.getBooks(nil) p books.out.book[0].title book = driver.findBook(:isbn => '222') p book.out.title • The BookService#getBooks method in Java takes no arguments, but if you try to call driver.getBooks without arguments, Ruby complains about a missing argument. • XFire add this extra ‘out’ element to its generated schema. Ugo Cei: “Ruby for Java Programmers”
  35. 35. Thank You! Slides will be available at http://agylen.com/ and at http://www.sourcesense.com/ Ugo Cei: “Ruby for Java Programmers”
  1. ¿Le ha llamado la atención una diapositiva en particular?

    Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

×