March 31, 2008
When writing AJAX and SOFEA/SOUI applications, it’s a best practice to use a client side message bus to integrate UI components. People recognized this as a best practice lately for GUI development with Swing. As a sidenote Werx was excellent for this but sadly is dead.
One of the ways to do message buses in Javascript is the OpenAjax Hub implementation or TIBCO PageBus.
Subscribing to messages on the bus is easy with the subscribe method:
jo.ListComponent = function() {
OpenAjax.hub.subscribe("list.get") {
function(event, publisherData, subscriberData) {
// do something when message arrives
}
}
When you have lots of those subscriptions though, they clutter up your code. Werx had the nice idea to autowire methods to messages and use the methods as message handlers. I thought I should do the same for Javascript and OpenAjax Hub.
jo.ListComponent = function() {
this.$list_get = function(list) {
// do something with list
}
// connect the methods
jo.connect(this);
}
Some magic happens in the connect method.
jo.connect = function(object) {
for (var element in object) {
if (element.charAt(0) == '$') {
var event = element.substring(1).replace(/_/g, ".");
var callback;
// we need to capture the scope
(function() {
var fn = object[element];
callback = function(event, data) {
fn.apply(object, [data]);
}
})();
jo.Bus.subscribe(event, callback);
}
}
}
The same could be achieved with a map of message handlers which is easy with functions in Javascript whereas in Java it takes more boiler code. A solution with methods as messages handlers as seen above has the benefit though that the object can contain other helper methods and state, which a map of message handlers can not. Using autowired methods makes your message bus code cleaner and more readable.
Thanks for listening.
March 30, 2008
I needed a NAS urgently. There were several NAS in the game, the Zyxel 220 for the price, the Qnap 209 for the email notifications, the Infrant ReadyNAS because someone told me it was excellent and the Qnap 409, another 4-drive NAS. After some considerations, I chose the Qnap 409.
Why not the 209 or the Zyxel? As 2-drive NAS systems they both had no path for online migration to higher capacity drives. Also adding capacity is much easier for a 4-drive NAS than for a 2-drive NAS. Having two 750gb drives in the NAS and adding a third one will double the capacity from 750gb to 1.5tb. Whereas with a 2-drive NAS you have to replace the drives completely. The Infrant had some discussions on their forums with failed drives which stopped me from buying the ReadyNAS (Also they have been bought by Netgear). So the Qnap 409 is on it’s way with two WD 750 GP drives for an initial RAID-1 setup.
Hope they work with time machine, or I have to buy an additional time capsule ;-)
Update: Time machine works.
March 27, 2008
Just say no. When hearing about Cross Over Games for running your Games for Windows with Mac OS X I thought wonderful, no more need for Bootcamp! After trying to play Half Life 2 E2 with Cross Over Games, it doesn’t work. The installation from CD (Orange Box) took hours and then insisted on changing to the second CD. Problem was, my MacBook would not eject the first one because it was in use by the installer. Next try was downloading H2 with Steam. Took over 2 days and several tries when it stalled several times. In the end all was downloaded, but the game didn’t start. What a bad experience. In no way is this an alternative to Bootcamp.
Update: Got Episode 2 running in Bootcamp after resizing the partition and using iDefrag (worth the $35) and reinstalling XP. E2 is one of the best games I’ve played in 25 years.
March 24, 2008
I’ve written about my UTF-8 woes with Jersey. Digging through the code of Jersey hasn’t resulted in any clues yet. I’m still not sure how to set an UTF-8 content header for HTTP with Jersey. Other impression from the code: no JavaDoc. Usually not a good sign. I’ll keep you updated on my progress through Jersey code depths.
March 22, 2008
UTF-8 is always a problem. Unbelievable. 2008 and we still haven’t fixed this. One of my current projects is a Javascript frontend with a REST backend. The backend stores to MySQL (a famous UTF-8 trouble maker) and creates JSON to REST calls. The problems starts with UTF-8 characters. Somewhere in the callchain - as always - characters don’t get correctly written. MySQL and the JDBC driver should work, the JSP page is UTF-8 (@page and meta-equiv), jQuery - which does the AJAX - and JS do know UTF-8 and Jersey should be UTF-8 too. But with some experiments now I’m quite sure that Jersey (JSR 311 REST framework) is to blame. I’m not sure how to specify UTF-8, this
@ProduceMime("text/plain;charset=UTF-8")
doesn’t help. Funny, every major project with several frameworks along the call chain and several languages (JS, C, Java) makes UTF-8 problems somehow. I’m so fed up with this, it’s 2008.
Update: Jersey uses InputStreams for all encodings, especially StringProvider is relevant to me (se above). Does this work with Unicode?
March 18, 2008
I’ve been experimenting with ways to nicely generate JSON. There are many ways to generate JSON in Java, like XStream with Jettison, with JAXB or directly with REST API implementation Jersey. Often you don’t want to serialize objects or work mith maps though. Taking code from “The best Markup Builder I could build in Java” I’ve tried a builder approach.
@GET
@ProduceMime("application/json")
public String getList() {
ShoppingList list = service.getList("123");
return toJson(
$("items",
new List<ShoppingItem>(list) {
protected Node item(ShoppingItem shoppingItem) {
return $("description", shoppingItem.getDescription());
}
})
);
}
The generated JSON would be
{ items: [ { description: "Apple"}, { description: "Orange"} ]}
To create nodes for a JSON tree I first tried a node function. But having lots of node calls makes the code quite unreadable. Luckily Java allows $ as a method name. Using $ makes the code much more readable. The List object creates a list of nodes, taking input from a collection, Iterable or Iterator and calling item() for every element.
To reuse generation code one can create semantic methods like an items method:
public static Node items(Node... nodes) {
return $("items", nodes);
}
The nice thing is, with another render mechanism the tree of nodes can also be rendered to XML with a toXml() method, if XML works better for some REST calls. Next thing to add is support for XStream and Jettison to mix serialization in e.g. $("employee", employeePOJO); and experiment on how to make the code even nicer and shorter.
I also wonder how to remove the toJson() call with Jersey and to use a Jersey writer. Any ideas?
Thanks for listening.
March 14, 2008
Der 10. Tag im Streik, ich habe den 5. Tag Urlaub, komme aber nirgends hin weil nichts faehrt. Ich habe aber Durchhaltewillen! “Ihr Völker der Welt, schaut auf diese Stadt!” Was der Sowjetunion, die die Berliner niederringen und als Geisel nehmen wollte, nicht gelungen ist mit ihrer Blockade, wird Verdi mit dem Streik nicht schaffen!