Replacing Java Incrementally


Published on

At an event hosted at LinkedIn on November 2, 2010, Noah Gibbs delivers a presentation on how is using JRuby to leverage its old Java codebase.

** Check out the video from this presentation at: **

Noah talks about:
- The advantages of using JRuby to leverage a legacy Java codebase
- Integration with JSP pages
- Java models and Active Record wrappers
- Call Java-to-Ruby

...And if you're looking for more educational videos on Ruby and open-source, head on over to:

Published in: Technology
1 Comment
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Replacing Java Incrementally

  1. 1. Replacing Java Incrementally (. 'l‘. "t' I: -:e= s't, at Cn-Siiemcom) Noah Gibbs: noah_gibbs@yahoo. com http: //github. com/ on-site http: //github. com/ noahgibbs
  2. 2. Who Is On-Site. com? We sell subscription software to landlords Over 10 years old, well before Rails existed Engineering team of about 15 people Small company, not much time for infrastructure Profitable with actual customers —— don't lose them!
  3. 3. What JRuby Gives Us Access to a huge existing Java codebase Integration with lots of JSP pages Calling JSP tags directly Routing integration with Cocoon/ Hibernate ActionPack and ActiveRecord And we keep adding more. .. With Rails!
  4. 4. JSP Tags We wrap them as partials: <% jsp "overlay", :element_id = > id, zbody = > body %> "jsp" is a JSP helper class we wrote, but it's straightforward to write it for your own environment.
  5. 5. JSP Tags (cont) To call Ruby from a JSP, you can use the jruby-rack tag: <jruby—rack: rack path= "/web/ sales/ selector" params= '<%= "user_id= " + userID %>'/ > That's in JSP, not ERB.
  6. 6. Java Models Just Java classes with validation code and accessors You already have them, and rewriting sucks. ActiveRecord objects? With JRuby, close enough. No Form helpers, at least in Rails 2. Cope. Rails 3 and ActiveMode| promise reeeeally interesting improvements here.
  7. 7. Java Models (cont) This looks a lot like plain Rails: class Notecontroller < Applicationcontroller import com. onsitemanager. lease. Note LOG = Logger. get_logger "Notecontroller" before_filter : require_support_user def update @note = Note. by_id(params[: id]) # Java!
  8. 8. Java Models Plus AR Wrappers Often it makes sense to have both Java and ActiveRecord code for the same database table. This lets you partially ditch Java, but be careful of validations and consistency issues. If a single class can touch both, be *very* careful about return types. You may want to give the JRuby object a different name.
  9. 9. Java Models Plus AR Wrappers class GuestCard < ActiveRecord: :Base set_table_name "onsite. guest_cards" set_primary_key "guest_card_id" set_sequence_name "onsite. guest_cards" belongs_to zproperty after_save : saVe_note import com. onsitemanager. lease. GuestCard
  10. 10. Java Models Plus AR Wrappers We use a RubyC| ass. j naming convention: class Guestcard class << self def j com. onsitemanager. lease. GuestCard end end end
  11. 11. Still working that part out. .. It's hard to fully transition a class from Java to Ruby without really good tests. Our oldest stuff is also our worst tested, and used in the most places. You really need to distinguish between the Java and Ruby classes. You don't want two versions of code, but you want easy conversion between Java and Ruby. This is a hard problem. Writing many tests and rewriting in Ruby may be easier in most cases. Oof.
  12. 12. Calling Java-to-Ruby Write a Java interface. Create a Ruby object that includes it. Then, pass it to Java. class RubySideFooFinder include com. onsitemanager. util. FooFinder def findByFoo(foo) foo. java_side_method. map {lobjl
  13. 13. Calling Java-to-Ruby (cont) We use this to let Java call our new ActiveRecord models for newer Ruby code. It's the next logical step after "do both", and works better.
  14. 14. Cheating shamelessly JRuby also lets you use Ruby more directly from Java: return (List<Dispute>) RubyUtil. eval("ConsumerDispute. find_for_tena nt : lease_id = > " + tenant. getCurrentLease(). get() + " : tenant_data_id = > " + tenant. getTenantDataId()); I Obviously this is chosen for realism, not beauty. But see? Call any Ruby code. There's no wrapper around the ConsumerDispute finder. We're still tweaking this.
  15. 15. Miscellaneous Notes Just adding Rails views on top of Java models is easy, straightforward and doesn't disturb anything. It's probably your best first step. It's what we did. With good testing, you can replace existing Java objects with Ruby objects completely. Without good tests, this is really hard. ActiveRecord has better caching than your homebrew Java objects. This is a selling point. Yell it out.
  16. 16. Miscellaneous Notes Even when you're still writing Java code (you will for awhile), test it with JRuby. When interacting with Java, Int/ Long issues will annoy you often. When in doubt, convert to Java objects before passing them to Ruby. When calling Ruby-to-Ruby, JRuby doesn't auto-snake- case. Even if you're implementing a Java interface. This will bite you, but it's not too bad.
  17. 17. Questions? Ask away. You can also email me at "noah_gibbs@yahoo. com".
  18. 18. Thanks for Listening! class OnSite < Railsshop include Javashop # TODO: remove methods def hiring? true end def github_url "http: //github. com/ on—site" end end