Google Go Websocket server and Smoothie Charts client

Recently I enjoyed getting my hands dirty with Google Go a fairly new GPL targeted at systems programming. To get to know the language a little better I came up with the idea to program a websocket server that sends out some data to a browser that visualizes those in a chart. I got the inspiration for this from Joe Walnes blog entry about Smoothie Charts. Smoothie Charts is a little JavaScript library to visualize real-time data in a HTML5 canvas.

The websocket server implementation is pretty simple as Go already has some build-in websocket support. The server sends out the sine function with a value range from -1 to +1 to the client. The result can be seen in the following pictures and all source code be downloaded from github: https://github.com/svenlange/websocket

New value every 500ms. Alternation +0.05

New value every 5ms. Alternation +0.01

The second pictures sine curve looks a bit creepy because my computers CPU utilization was at a maximum and somehow my computer was not able to take a better screenshot. The chart was smoother on my monitor.

This setup/technology could maybe be used to visualize real-time stock exchange trends or the current bandwidth utilization in a data center etc. within a browser. Very nice.

Thanks to Gary Burd for his websocket example. It helped me to get my job done.

FOSDEM 2011

This january I attended the FOSDEM in Brussels for the first time. Before I only visited those expensive enterprise conferences like JAOO, JAX and the like. It really was a great experience. A free conference (in terms of registrations fees) about free and open source software. I hope I can attend it next year again.

I stayed at the Java Dev-Room for most of the time. The speakers there were not the best presenters (cause they are real geeks) but still they gave me inspiration and I could widen my horizon by learning new stuff from them.

One nice thing was that some companies (Oracle, tarent, …) were sponsoring a dinner. The conversations with the participating Java folks were very nice. We talked a little about everything concerning IT stuff. From different hardware architectures to design patterns and political stuff.

One neat tool I have to look into is SystemTap. It enables one to do sophisticated application diagnosis on a Linux OS. For example trace down a Java method call through the JVM down to the native system call. Pretty detailed and interesting tool if one is faced with very tricky performance or functional problems.

yUML is only for simple UML class diagrams

I sat down for a while and extended the Grails Create Domain UML Plugin that I wrote about a couple month ago in this post. This plugin helps to generate UML class diagrams with yUML from a Grails domain model. Unfortunately the diagrams don’t show aggregations or compositions and the associations don’t have a name. I fixed that, but the generated diagrams are not really useable, because yUML is only for very simple UML class diagrams as you can see in the following pictures.

Neat top down

Neat left right

Conclusion: yUML looks nice, but is only usable for very simple diagrams.

Remove user from cache in Grails app using Acegi Plugin

The Grails Acegi Plugin has user caching activated by default. If you make any changes to a user those changes won’t be available immediately. Here are two solutions to change this behavior.

Remove the user from cache

The Acegi plugin provides a userCache bean which can be injected automatically. With a removeUserFromCache method call on the userCache bean a user can be removed from the cache.  The next time the user is requested by the system the user will be retrieved from the database and all updates to the instance are available immediately. Following code fragment shows the described solution.

def userCache

def someAction = {
	GrailsUser user = authenticateService.principal() as GrailsUser
	userCache.removeUserFromCache(user)
}

Disable the cache

The second solution is to simply disable the cache. This should not have any impact on applications with little usage. This of cause increases the communication between your application and a database server. To disable the cache add the following line of code to the <app-root>/grails-app/conf/SecurityConfig.groovy file.

cacheUsers = false

Silverlight testing landscape

I did some research to find out how to test Silverlight 3 applications. I wanted to have an automated build on a CI server (TeamCity) that does some unit and integration testing plus a code coverage report. Soon I saw that there are a lot of tools and frameworks out there. To get an overview of whats available and useful, I started the following list. Feel free to share your thoughts.
Continue reading

JAOO 2010

This years JAOO conference was quite nice again. I was not able to attend all the sessions I wanted to, but just being there and meeting new people is worth it already. For example Mr. Gosling. ;-) Or of cause Rohde who was so kind to give us a place to stay during the conference. Thanks mate.

What I really have to look into is how concurrent programming will be done in the future on the Java platform. Brian Goetz did a really interesting talk about this topic.

Besides that I couldn’t get more inspiration from this years conference. But that is OK. Denmark is a great country and it is always a good thing to stop by. After the conference I had some time to travel up to Aalborg and Skagen and then drive back to Germany along the Danish coast side visiting Klitmøller etc.

HTML5 offline capabilities for web applications

Inspired by a HTML5 GTUG-Campout Meeting I decided to play around a little with HTML5. In this post I will show an example how to create book instances in a book management web application while being offline and push all the books into the database by the time the user is online again.

The use case for my little example is the following: A user is offline and wants to enter some new books into a book management web application.

OK the first question that arises is: How can a client detect whether it is on- or offline? The answer is it shouldn’t be automatically and you can find a good answer to that here. So the user needs to be aware whether he is online or not and triggers a syncronisation with the web server by himself.

In HTML5 you have a client side storage where you can place stuff. It is a key/value pair storage with a maximum size of 5MB per origion. To persist an object (in my example a book) you best better use JSON. The following code snipplet shows a JavaScript function called preserve that collects all entered details for a book from an input page and places the result in the local storage. The local storage can be accessed with localStorage as you can see in line 15.

function preserve() {

	function getValueById(id) {
		return document.getElementById(id).value;
	}

	var JSONbook = {
		"title" : getValueById("title"),
		"isbn" : getValueById("isbn"),
		"author" : getValueById("author1")
	};

	var bookString = JSON.stringify(JSONbook);

	localStorage.setItem(localStorage.length + 1, bookString);
}

Due to the fact that you can only place strings into the key/value store one has to convert the JSON object into a string. This can be achivied with the help of the following free javascript stringifier/parser which I use here in line 13.

Of course localStorage.length + 1 is not a good way to generate a key, but for this experimental example it suits the needs.

So now when a user is back online and decides he wants to push all the newly created books into the database server a little Javascript like the following could do the job for him. For example by clicking on a button the persistAllBooks gets invoked. This function sequentially reads all book objects from the localStorage and calls the post_to_url function for each book. post_to_url builds up a form, fills it with the books attributes and pushes them to the server.

function persistAllBooks() {
	for (var i = 1; i <= localStorage.length; i++) {
		var item = localStorage.getItem(i);
		var parse = JSON.parse(item);
		post_to_url(parse);
	}

}

function post_to_url(params) {
	var form = document.createElement("form");
	form.setAttribute("method", "post");
	form.setAttribute("action", "/bookmngt/book/save");

	for (var key in params) {
		var hiddenField = document.createElement("input");
		hiddenField.setAttribute("type", "hidden");
		hiddenField.setAttribute("name", key);
		hiddenField.setAttribute("value", params[key]);

		form.appendChild(hiddenField);
	}

	document.body.appendChild(form);
	form.submit();
}

Thats it. Of cource the book creation page needs to be offline available to the client and HTML5 is helping here too.

Thanks to Thomas Asel for brainstorming and talking about HTML5 which led to this post.

HTML5 Resources

The following list contains some good HTML5 resources I found on the net while experimenting a little.

Dive Into HTML 5 – diveintohtml5.org
Browsers with Wings – slideshare.net/remy.sharp/browsers-with-wings
HTML5 on Wikipedia – en.wikipedia.org/wiki/HTML5
“Offline”: What does it mean and why should I care? – www.html5rocks.com/tutorials/offline/whats-offline

Disable Ehcaches UpdateChecker in Grails application

I found it pretty annoying that Ehcache always reminded me that there is an update to a newer version of its framework available. After searching the web I found an interesting thread in the Terracotta forum saying that the UpdateChecker is not only looking for updates. In addition it also submits information about the applications environment to Terracotta by default. And that is not a good thing in my opinion.

Here you can see youself: http://svn.terracotta.org/svn/ehcache/tags/ehcache-1.7.1/core/src/main/java/net/sf/ehcache/util/UpdateChecker.java

Fortunately you can disable this behavior by adding an Ehcache configuration to your Grails application. I have tested this with a Grails 1.2.0 application which includes Ehcache 1.7.1.

Simply place an ehcache.xml file in your <app-root>/grails-app/conf folder containing your Ehcache configuration. For this post I am using the configuration that ships with Ehcache as default (ehcache-failsafe.xml) and modify it.

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />

</ehcache>

With the attribute updateCheck=”false” in the root tag the haunting is gone.

XML Schema for Ehcache config can be found here: http://ehcache.org/ehcache.xsd

More Information about Ehcache configuration: http://ehcache.org/documentation/configuration.html

Generating UML class diagrams from Grails domain model

Today I tried some Grails plugins that generate UML class diagrams from the existing domain model in an Grails application. I found the following two plugins in the offical Grails plugin repository to play with.

The application I used is a small book management app that manages a couple hundred books at my workplace.

Continue reading

Using storeXPath() in your Grails Webtest

Today I had to figure out how to save some content from a site during a running Webtest so that I am able to restore all changed data afterwards. Luckily Webtest provides the step storeXPath() which does the job.

But it was not that easy to realize my task due to the lack of good documentation and finding the correct xpath expression.

The following code snippet grabs the text value from a text input field with the id surname and saves it in the ant property xyz.

def testSomething() {
   webtest('Some description') {

      // saving initial value
      storeXPath(xpath: "id('surname')/@value", property: "xyz")

      // restore initial value
      setInputField(name: "surname", value: "#{xyz}")

   }
}

Pretty simple in the end, but it took a while till I got it. Maybe it helps someone.