Thursday, February 26, 2009

JRuby and soap4r :: its the only way to live ...

Oh yeah I got them working nicely!
Although I have to hack httpclient (I know there must be a way not to hack it!). Some if not most of the posts/tutorials out there might or might not work. So here's how I got it working.

I have to implement this functionality requiring to invoke a web service. The developer I was working with gave me a rpc/encoded wsdl file (as oppose to a document literal). (What's the fuzz is rpc/encoded & document literal???)

Now there are two ways to use soap4r: read the wsdl at runtime and invoke its method(s) dynamically or create the stubs with your local wsdl file. In my case, I chose to create the stubs (consumer) using wsdl2ruby which comes with soap4r. Here's how you can install soap4r and generate the stubs.

Now for the fun part ...
  • If you start your rails app and fails because of jruby-openssl missing, you probably need to modify ssl_config.rb and set SSLEnabled to false (from httpclient gem). httpclient is a dependency of soap4r.
  • As mentioned above, soap4r generates you .rb files (4 of them), you actually only need the *driver.rb in your code. To give you an idea how to use it, have a look at the generated client stub (*Client.rb).
  • Authentication => Since the web service I was trying to invoke requires me to do basic authentication, I have to pass in options to my driver object.
my_object.options["protocol.http.basic_auth"] << [MY_ENDPOINT, MY_ENDPOINT_USERNAME, MY_ENDPOINT_PASSWORD]
  • Testing => so aside from doing your unit test, what I found helpful was testing the web service using soapUI. soapUI generates a service request as well as a dummy service for you based on your wsdl file. There are many tutorials out there that would surely help you.
That's about it for getting JRuby and soap4r to work together. I know there's a way to get httpclient gem to work without hacking into it or maybe I just misconfigured it. If you found a way to solve this issue, feel free to comment on my post.


JRuby on Rails: Rolling with Oracle 10g and Weblogic 10g (Part 1)

I've ran a sample app using JRuby (1.1.4 I think) and Oracle Weblogic 10 last October 2008. I had some challenge back then as you can see from this post and at the same time beginning rails.

Recently, I was working an 'enterprise' JRuby on Rails app requiring me to run it again on Weblogic 10 with Oracle 10g as database. I thought it will be seamless since I've done it before and I thought running on Oracle 10g is as simple as configuring the driver like what I always did back in J2EE. Unfortunately I ended app pulling out my hair once again trying to figure it all out. It was one hell of JRuby/Warbler/Jar/Windows/Oracle/Cygwin/Weblogic war!

To give you a background, I started running off with the ff:
  • Windows XP - as much as I don't want to develop on it,
  • JDK 1.5 - forgot which update; but don't worry I replaced it later on
  • JRuby 1.5
  • Rails 2.2.2
  • Cygwin

Rolling with Oracle 10g

I was quite happy with the app running very well on WEBrick and mySQL 5.x. All requirements (CRUD, screens, audit trail, logging, AJAX dropdown menus, etc.) were all satisfied. Then I first decided to migrate the database to Oracle 10g. So I installed Oracle XE and below was what I did to get it running.

Step 1: Installed Oracle 10g Express Edition - painless; I don't think I have to upgrade JDK at this point.
Step 2: Change you database.yml to point to your Oracle instance. Some things to note below:
  • If you are using Express Edition, your Oracle instance is XE.
  • Create a new user and give it access to creating a table, sequence, indices (if needed). Privileges depends on how you wrote/generated your migration script/s.
  • I used a JDBC adapter to connect to Oracle 10g. Install activerecords-jdbc-adapter. I followed this but as far as I could remember it didn't work because its for Ruby. So I followed this and works well with JRuby.
  • I ended up with the following configuration in my database.yml file. I needed UTF-8 encoding for I18n (which is a different story).
adapter: jdbc
driver: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@
username: username
password: password
encoding: UTF8

Step 3: Download ojdbc.jar here for 10g (for 11g). Copy the jar file to your $JRUBY_HOME\lib.

Step 4: Run db:migrate and db:fixtures:load (if you need to). You should be able to see your instance with new tables and data.

Step 5: Run WEBrick.

Issues I encountered:

And now for fun stuff ....
  • I started off installing oracle adapter and configure my database.yml appropriately. I later found out it was meant for Ruby. I think I also went on the path of installing ruby-oci8, but it was unsuccessful. Stick with the JDBC adapter which was meant for JRuby.
  • I played around with different configuration parameters and values for my database.yml file (because that is what you'll read in Oracle website I mentioned above and I got configuration for oracle adapter and ruby-oci8 all messed up in my head). I ended up with the parameter/value combination above.
  • I ran with using system as user and running db:create before db:migrate. And boom! This messed up my schema.rb reflecting all tables (system tables) in my instance. Create a user and don't use system as user! It's best practice. I was getting impatient.
That's it for Part 1. It was seamless to develop with Oracle 10g like I did in mysql before. Only that it eats up a lot of memory and loading takes sometime. I have to turn it off manually in Windows Services. If you got a pimped machine or probably running Mac or Linux, it shouldn't be a problem.

Part 2 will be running the app on Oracle Weblogic 10.