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”
4. What’s in it for me?
Ugo Cei: “Ruby for Java Programmers”
5. Shameless Plug: The Open Source Zone
http://oszone.org
Ugo Cei: “Ruby for Java Programmers”
6. Shameless Plug: The Open Source Zone
http://oszone.org
Ugo Cei: “Ruby for Java Programmers”
7. Shameless Plug: Evil or Not?
http://evilornot.info
Ugo Cei: “Ruby for Java Programmers”
8. Shameless Plug: Evil or Not?
if /<title>(.+)</title>/ ... Search?
http://evilornot.info
Ugo Cei: “Ruby for Java Programmers”
9.
10. “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”
12. 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”
13. 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”
14. 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”
16. 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”
17. 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”
18. 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”
19. 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”
20. 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”
21. 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”
22. 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”
23. 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”
24. 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”
27. 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”
28. 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”
29. 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”
30. 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”
31. 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”
32. 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”
33. 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”
34. 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”
35. 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”
36. Thank You!
Slides will be available at
http://agylen.com/
and at
http://www.sourcesense.com/
Ugo Cei: “Ruby for Java Programmers”