Friday, November 27, 2009

Liberate your code

Do you still read your portable Java code in proprietary Microsoft fonts?
If so, you can consider liberating it from corporate license chains.

Some time ago the folks at Red Hat decided to create free equivalents for popular Microsoft fonts.
The result was Liberation fonts.You can download them for free from here. For Windows you would need the ttf flavor.
Once you install the new fonts, you can start using them in Eclipse.
Open Window > Preferences. In the dialog navigate to General > Appearance > Colors and Fonts. On the right expand Basic, select Text Font and change it to Liberation Mono.


Liberation Mono is the equivalent of Courier New® but looks cleaner and is more condensed vertically so more code will fit on your screen.
Here is a sample


Enjoy!

Thursday, November 19, 2009

Yet another fast Java decompiler

Today a colleague of mine pointed me to a really nice piece of software JD Java Decompiler.
Besides .class files it also opens .jar files and here is where it shines. All known types are hyperlink-ed so you can quickly jump between classes in the same .jar. You have Open Type, Java Search, Type Hierarchy, etc. - it's a mini Eclipse.
Speaking of Eclipse, JD also provides a plug-in for the ubiquitous Java IDE.

Did I mention it's very fast? That's because it is powered by native C++ ;). (Don't worry Linux version is also available.)

And it's also free. Still you are encouraged to support the author if you find it useful.

My .jar's have been associated with WinRar for years - time for a change.

Saturday, November 7, 2009

Serving images and other resources with wicket

It took me couple of days and mailing list posts to figure out how to display images with wicket in a satisfactory way.
In my case images are stored in JCR but this could be any other repository like db or file system where arbitrary images could be added or removed over time.
I also want that images have stable (bookmarkable) URLs, which makes them suitable for indexing by web crawlers and caching by web browsers.
I first checked the wicket "bible" Wicket in Action but this use case is not treated there.

Finally it turned out wicket does provide a way to do it in simple Java - shared resource with parameters.

Define a class to serve images
Create a class e.g. Images extending WebResource. Essentially this class has to implement the method getResourceStream(), which should return an IResourceStream implementation. (IResourceStream provides the actual data stream in terms of InputStream, data size, content type, etc.) The important thing here is that Images class can call inherited method getParameters() to get the query parameters (as ValueMap) from the URL. These parameters can be used to identify the specific image to be streamed. Something like this
    String imageId = getParameters().getString("id");

Bind the image serving class
The Images class created above can be bound to a specific URL, so whenever this URL is requested, the bound object is used to stream the response. This is done in the application init() method.
    getSharedResources().add("images", new Images());
This would produce image URLs like resources/org.apache.wicket.Application/images. To shorten the URLs to resources/global/images, add this again in init() method
    getSharedResources().putClassAlias(
        org.apache.wicket.Application.class, "global");

Display an image
Finally, use this to put an image on a page
    new Image("image", new ResourceReference("images"), 
        new ValueMap("id=" + imageId))
This will result in image URL like resources/global/images?id=image105

Of course this mechanism could be used for any kind of resource (e.g. PDF documents). It is only a matter of Content-Type.