Jewel thief
Long have we lamented the dearth of text processing libraries in Java. So many more options are available in scripting languages like Ruby that Databinder decided several months back to throw in the towel. Instead of writing our own implementations (as if any Java programmer could be bothered!) we’ll call into theirs. Suckers.
This weekend I had some time to make it happen. It’s simple to call into Ruby code with XML-RPC, as we knew already, making the operation both easy to set up and efficient to use was the challenge.
One early realization I had was that it’s not okay to add any new dependencies for this feature. It’s not something everyone will use, and we need to keep our requirements as low as possible (even though Maven makes it so easy to pile in the JARs). It was time to go multi-project. With out further ado, behold Databinder dispatch. (Not as impressive as the iPhone debut, but still.)
To use it, change the Databinder dependency in your POM from “databinder” to “databinder-dispatch”. Then you can use the RedClothLabel component for Textile processing in your code.
The meat is in the parent XmlRpcLabel class, which hosts the client communications object and an Ehcache. The cache allows us to not worry about the overhead of the network call. (Or the dreadfully slow interpretation of Ruby code, right? Right? Psh.)
So, then what happens? The cool part. Add an XML-RPC component to your page. Then just go to the page. The RPC connection will fail, of course, but if you’re in development mode the component will redirect you to a special page. A very special page. It lists the error, and gives you the text of a Ruby script to run the required server. Niiice. But not just that, it offers a command string that looks something like this:
curl http://localhost:8080/…/databinder-dispatch.rb | ruby
Whoa. You see what that does? Scary.
Of course this won’t work unless you have Ruby set up with the libraries required in the script. But aren’t you already “evaluating Ruby” for your boss? Get that interpreter installed, and RubyGems. After that it’s a cinch to install RedCloth and any other gem Databinder dispatch chooses to purloin in the future.
I’m already using this Ruby bridge to transform markup for the Databinder site with my crazy non-CMS. Works like a charm.
Add a comment