• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ugo Cei Presentation
 

Ugo Cei Presentation

on

  • 4,388 views

 

Statistics

Views

Total Views
4,388
Views on SlideShare
4,385
Embed Views
3

Actions

Likes
2
Downloads
38
Comments
2

2 Embeds 3

http://www.techgig.com 2
http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • 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/
    Are you sure you want to
    Your message goes here
    Processing…
  • Outstanding display. Very clear and useful
    Teisha
    http://dashinghealth.com http://healthimplants.com
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Ugo Cei Presentation Ugo Cei Presentation Presentation Transcript

    • 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. • 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”
    • 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 Programmers”
    • Shameless Plug: The Open Source Zone http://oszone.org Ugo Cei: “Ruby for Java Programmers”
    • Shameless Plug: Evil or Not? http://evilornot.info Ugo Cei: “Ruby for Java Programmers”
    • Shameless Plug: Evil or Not? if /<title>(.+)</title>/ ... Search? http://evilornot.info Ugo Cei: “Ruby for Java Programmers”
    • “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”
    • How? Ugo Cei: “Ruby for Java Programmers”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • Running Ruby on Rails Ugo Cei: “Ruby for Java Programmers”
    • Running Camping • http://jruby.codehaus.org/The+JRuby+Tutorial+Part+2+- +Going+Camping Ugo Cei: “Ruby for Java Programmers”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • 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”
    • Thank You! Slides will be available at http://agylen.com/ and at http://www.sourcesense.com/ Ugo Cei: “Ruby for Java Programmers”