Cenqua: here I come 1
The rumours are true, come the new year I’ll be starting with Cenqua - the creators of Fisheye, Clover & Crucible. I’ll be working remotely for them from my little corner of north London or to put it another way, I’ll be their premier UK employee. ;) My task will be to design, develop and support new and existing software development tools you will actually use.
This move has been a long time in the making - I first interviewed with them (a beer in hand, if I recall) back in the middle of the dot-com crash when they were transitioning from a service company to a product company. Unfortunately, like many, they’d just laid off staff and weren’t in a position to take me on. More recently this year I did some work for them but found my 9-5 racket was proving too exhausting to deliver anything substantial or on-time. Now the timing seems right for me to come on board full time.
You gotta love a company that calls its head office the “Central Command” and describes my role as “Senior Software Engineer / Architect / Code Poet”.
Matt, Conor, Brendan and Pete - thanks for giving me the chance to work on some great products! I can’t wait!
RSpec on JRuby - an interview 1
Last month Charles Nutter, Aslak Hellesoy and myself were all interviewed by Pat Eyler on getting RSpec framework working on JRuby. This was quite a milestone for the project and Pat thought it deserved a bit of publicity. It also came at a time when my current employer issued the “thou shalt only use Java” edict so you might detect a bit of emotion in some of my responses.
The version that made it onto InfoQ provides some nice soundbites and the full interview is well worth the read. Enjoy!
JRuby 0.9.2 out - get it while it's hot!
The JRuby trio have just released version 0.9.2 of everyone’s** favourite scripting language for the JVM.
New and notable in this release:
- Extensions openssl and readline now working
- Code for a new graphical irb console
- Partial support for iconv and bigdecimal extensions
- RSpec now supported
- Improved Rails support
- Fixed all known block and scoping bugs
- Enhanced parser performance
- More compiler and performance work
- Refactored variable scoping logic
- 127 Jira issues resolved since 0.9.1
My modest contribution to this release included:
- Getting RSpec supported
- Implementing the Enumerable::Enumerator class and associated Kernel methods
- Fixing the Java::clazz.naming.Binding syntax implementation to properly work
- Fixes for visibility issues with some Kernel methods
So go and download it now! What are you waiting for?
** Favourite to all but those nice Groovy folk, oh and those BSH and Javascript/Rhino wierdos
Handling Java exceptions in JRuby 2
At my enterprisey day job I’m finding the JRuby platform to be the perfect glue for bringing together a (sadly necessary) slew of Java libraries and middleware (3rd-party Ant tasks, Jakarta VFS, ServiceMix, Maven, Weblogic) to solve a complex problem crucial to our enterprise. Stellar support for exception handling is obviously important when integrating so many libraries and middleware so how well does JRuby help with this? Is it just a Java fair-weather friend?
Thankfully the answer is no. JRuby allows you to catch Java exceptions in Ruby code and handle them as if they were Ruby exceptions. These Ruby-ised exceptions provide, for free, a backtrace which spans your Ruby stack and your Java stack.
The least intrusive way to handle Java exceptions is to just pretend it’s a Ruby exception:
require 'java'
begin
# some bad Java mojo
rescue RuntimeError => e
puts "Java or Ruby exception: #{e}"
raise
endrequire ‘java’ loads in Java support from within JRuby and is necessary any time you want to call Java code.
All Java exceptions, when caught in the ether between your Ruby code that made the call and the Java code that fielded the code, is wrapped in a subclass of RuntimeError (more on this in a moment) and raised. By rescuing RuntimeError you are catching any Java exceptions that were thrown. Of course you’re also rescuing all Ruby RuntimeError exceptions that were thrown in the block too.
If you wish to segregate your Java exceptions from those raised from Ruby code then JRuby provides support for this too.
require 'java'
begin
# some bad Java mojo
rescue NativeException => e
puts "Native exception's cause: #{e.cause}"
endNativeException is a JRuby-specific Ruby exception which wraps any Java exception and provides access to it via the ‘cause’ method. It is the RuntimeError subclass caught in the first example. If you rescue a NativeException you will thus be rescuing all Java exceptions thrown from within the begin / rescue block but no others.
The cause methods provides access to the underlying Java exception. Unfortunately version 0.9.0 of JRuby has a small shortcoming with this. The Java exception returned by cause is not in a form that enables you to easily call its Java methods (ie its not proxied in JRuby). I think this is an enhancement that needs looking at and I have raised JIRA issue 104 accordingly.
In the mean time, the following code should give you and idea of how to access the underlying Java exception in a Ruby-friendly fashion:
begin
# some bad Java mojo
rescue NativeException => e
#creates a Ruby proxy for the Java exception
proxied_e = JavaUtilities.wrap(e.cause)
proxied_e.print_stack_trace
endThe final and most natural way of handling a Java exception is by making JRuby aware of it and then rescuing it like you would a regular Ruby exception:
require 'java'
include_class 'java.lang.IOException'
begin
# bad IOException-inducing mojo
rescue IOException => e
puts "Java IO exception: #{e.message}"
endinclude_class takes the Java class ‘java.lang.IOException’ and makes it available to your Ruby code through the name ‘IOException’.
By rescuing an IOException and assigning it to a variable ‘e’ you get an instance of NativeException with a cause method returning the native java.io.IOException. I think this is slightly confusing as you asked to rescue an IOException but got a NativeException - e should really be a proxied java.io.IOException but I can see some implementation difficulties with this when I think about it. I may raise this issue with Charles and Thomas.
Again e.cause isn’t proxied but a call to JavaUtilities.wrap(e.cause) will fix that.
So it’s that simple! JRuby provides great integration with Java’s exception system although it obviously needs a small amount of polish before it will be completely seamless. Onward and upward!
Java snippet: Debugging a Java library used from Ant
I recently needed to debug a library which seemed to be misbehaving when used within Ant. After a bit of research here’s the best way I found to debug within Ant that doesn’t require sprinkling your bin/ant or bin\ant.bat with temporary JPDA pixy dust.
Ant doesn’t directly accept the -X arguments needed to set up the JPDA settings so the question is: how do you pass these from a shell or command line? In this, the ANT_OPTS environment variable is your friend. Its contents will be directly passed to the Java process Ant calls.
So here’s how you do it:
Step 1: Set ANT_OPTS to the debug options appropriate for your JVM:
for Java 1.4:
>> export ANT_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8453
for Java 1.5:
>> export ANT_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8453
Step 2: Run ant like normal:
>> ant
Step 3: Set your breakpoints.
Step 4: Now attach your debugger.
There might be better ways to do this but this was the least invasive route for me.