Using Jwt with JRuby
After getting the Jwt Hello world working with Clojure , I was notified by Shantanu Kumar about his Jettify library, which is a wrapper for the Jetty Servlet Container. Using it with Clojure was a breeze, but it also gave me an easy path to deploy Jruby based Jwt apps in Jetty!
And so immediately started to translated the Hello World in Jruby, getting it working nearly instantly.
It was also very easy to add listeners, thanks to what JRuby calls Closure Conversion which converts a Ruby block or closure to an appropriate Java interface .Thanks to that feature, adding the listener for the button click is as simple as this:
button.clicked.add_listener(self) do greeting_.setText(nameEdit_.getText) end
The only problem I encountered and which I have not solved yet are setting the margin of the button. This line
button.setMargin(WLength.new(5.0), Side::Left)
causes the error "no setMargin with arguments matching", which I think is due to setMargin expecting its second argument to be a EnumSet.
The other problem I still need to solve is stopping the Jetty server when the script is stopped with CTRL-C. I thought to trap the signal like this:
trap "SIGINT", proc{ instance.stop }
but JRuby warns that this signal is handled by the java platform. I'll look further to solve these 2 minor problems, but I wanted to already share this working code:
require 'java' import "eu.webtoolkit.jwt.Side" import "eu.webtoolkit.jwt.Signal" import "eu.webtoolkit.jwt.Signal1" import "eu.webtoolkit.jwt.WApplication" import "eu.webtoolkit.jwt.WBreak" import "eu.webtoolkit.jwt.WEnvironment" import "eu.webtoolkit.jwt.WLineEdit" import "eu.webtoolkit.jwt.WMouseEvent" import "eu.webtoolkit.jwt.WPushButton" import "eu.webtoolkit.jwt.WText" import "eu.webtoolkit.jwt.WLength" import "org.bitumenframework.jettify.JettyServer" import "eu.webtoolkit.jwt.WtServlet" class MyApplication < WApplication def initialize(env) super setTitle("Hello World") getRoot.addWidget(WText.new("Your name, please ? ")) nameEdit_ = WLineEdit.new(getRoot) nameEdit_.setFocus() button = WPushButton.new("Greet me.", getRoot()) getRoot().addWidget(WBreak.new) greeting_ = WText.new(getRoot()) wapp=self button.clicked.add_listener(self) do greeting_.setText(nameEdit_.getText) end nameEdit_.enterPressed.add_listener(self) do greeting_.setText(nameEdit_.getText) end end end class MyServlet < WtServlet def createApplication(env) MyApplication.new(env) end end instance = JettyServer.new instance.addServlet(MyServlet.new, "/*") instance.start