Nov 23, 2013

Dependency Injection and Sisu

Dependency injection is certainly one of the most effective design patterns for writing clean, maintainable and testable code.

For these reasons it's been very popular over the past few years and a number of libraries/frameworks have been created to provide support for dependency injection. The most well known implementations are probably Google Guice, the Spring Framework and the OSGi Service Registry while Dagger is possibly the most recent yet significant addition to this group.

A standardization effort in dependency injection began in 2006 which resulted in JSR 299 first and then in JSR 330, now supported by Guice and Spring which changed their API to comply with the proposed specification.

So what is the best choice today for those who want to use dependency injection in their code? Well, in my humble opinion the answer is.... Eclipse Sisu!. The Sisu home page describes Sisu as
"a modular JSR330-based container that supports classpath scanning, auto-binding, and dynamic auto-wiring. Sisu uses Google-Guice to perform dependency injection and provide the core JSR330 support, but removes the need to write explicit bindings in Guice modules." 
The project is still in incubation phase, and the documentation is not yet complete, but after playing with Sisu for some time, I can state that it is an excellent product, one of those rare Java libraries that focus on doing just one thing but do that thing exceptionally well. The code itself is very well written and an interesting reading for every Java developer (apart from the unusual C-like formatting :-)).

Sisu is the type of middleware component you would normally expect to find at Apache.org, and the fact that its actually hosted at Eclipse foundation may be yet another indication that Apache is no more as succesful as it used to be.

Here are some great features of Sisu:
  • Sisu can be used in both a Java SE environment and in an OSGi environment, and dependency injection becomes as simple as annotating relevant classes and letting Sisu magically discover them via class file scanning, or providing an index file.
  • No knowledge of Google Guice is required, but usage of the Google Guice API is supported.
  • Not only OSGi support is first class, but the author has also set the goal of integrating Sisu with the Equinox Registry and the OSGi Service Registry. Once accomplished this will bring a single, unified programming model helping portability between JavaSE and OSGi. 
Download Sisu now and start experimenting with it, chances are you'll be really amazed!

May 30, 2013

Tinybundles, a little gem for OSGi testing

In my OSGi applications I often make use of the OSGi extender pattern. An excellent example is available on Kai's Toedter blog.

As Peter Kriens explained:
In the Synchronous Bundle Listener event call back, the subsystem can look at the resources of the bundle and check if there is anything of its liking. If it is, it can use this resource to perform the necessary initialization for the new bundles.

Writing unit tests for a Synchronous Bundle Listener is often a tricky task. To test the behaviour of your extender you need to install and start (and later stop and uninstall) bundles via the OSGi API.

The API allows you to install a bundle by passing in an InputStream opened from a bundle file. So, the simplest solution usually consists in packaging a small test bundle as a binary resource in the test case  package, and to use the OSGi API from the test case class to install the bundle in the OSGi framework. This approach certainly works but is quite annoying because you need to repackage the test bundle every time you have to make a change.

Some time ago I found a better solution: the Tinybundles library. The project documentation is here, while sources for the library are available on github.

Tinybundles provides a convenient API to create an OSGi bundle from resources and classes available in your test case classpath:

TinyBundle bundle = TinyBundles.bundle()
            .add( TestActivator.class )
            .add( HelloWorld.class )
            .set( Constants.BUNDLE_SYMBOLICNAME, "test.bundle" )
            .set( Constants.EXPORT_PACKAGE, 
                HelloWorld.class.getPackage().getName() )
            .set( Constants.IMPORT_PACKAGE, 
                "org.apache.commons.collections" )
            .set( Constants.BUNDLE_ACTIVATOR, 
                TestActivator.class.getName() );

InputStream is = bundle.build(TinyBundles.withBnd());        
Bundle installed = getBundleContext().installBundle("dummyLocation", is);
installed.start();

In this way you can generate bundles on-the-fly within your test case for the purpose of testing your bundle listeners.

Simple, isn't it?

Apr 27, 2013

Edit keyboard shortcuts in Nautilus 3.6

After upgrading to Ubuntu 13.04, I was a bit annoyed by a short-cut change: I have always used a lot BackSpace in Nautilus to go up one folder. Now, in Nautilus 3.6 this has been changed to ALT-UP.

Luckily enough, even if there is no GUI for this, you can change Nautilus key bindings pretty easily. To change "go up one folder" from ALT-UP to BackSpace, just edit

~/.config/nautilus/accels 

with your favorite editor (mine is geany) and add this line at the end:


(gtk_accel_path "/ShellActions/Up" "BackSpace") 

Source: https://bugs.launchpad.net/ubuntu/+source/nautilus/+bug/1108637