Arbitrary Scripting Languages in XPages
Thu Apr 19 20:12:00 EDT 2012
I think I've settled on JSR 223, the generic "Scripting in Java" specification, as the likely best way to embed Ruby. It seems like the "correct" way to do it and generally the cleanest. I don't like the notion that the way to customize the runtime is by setting system properties, so I'm still a little wary, but it'll do for now, in any event.
The side benefit of JSR 223 (and this would be true of BSF as well) is that it supports a crapload of languages, and it does so in a very unified and generic way. Accordingly, I modified my ViewHandler
and created a GenericBindingFactory
to browse through the list of non-Ruby available languages and create EL bindings for each.
The upshot is that, when this code is active, any JSR-223-compliant language in your server's classpath will become available for "#{whatever: ... }
" bindings just by virtue of its presence. Note, though, that that doesn't necessarily mean it will be a good experience. For one, the page's variables (like param
, facesContext
, view
, and so forth) aren't just available - you'd have to make something like the method_missing
method I wrote for Ruby to automatically resolve them via FacesContext.getCurrentInstance()
. Furthermore, some languages are problematic: for some reason, Jython throws NullPointerException errors for even the most basic formulas on almost every page load, while PHP still requires <?php ?>
tags and spits anything else out to the server console.
I know what question is on the tip of your tongue, though, so I won't keep you waiting: yes, you can use your home-grown string-concatenation operators in Scheme! You can breathe a sigh of relief now:
<xp:text><xp:this.value><![CDATA[#{scheme:
(set! +
(let ((original+ +))
(lambda args
(if (and (not (null? args)) (string? (car args)))
(apply string-append args)
(apply original+ args)))))
(+ "Hello " "from " "Scheme")
}]]></xp:this.value></xp:text>
Yes, that works, and no, there's no reason to do it. If you really like Lisp, though, Scheme and Clojure can now be in your XPages toolbox.
Once I'm able to post to OpenNTF, I'll include this in my first Ruby-in-XPages release, though I may leave the applicable code commented out by default. Who knows what horrors this could unleash?