Dec 16, 2012

Java Advent Calendar

On 14.12.2012 I took part of initiative called Java Advent Calendar with a post about Functional Java collections. The idea for the Calendar is very simple, one technical article will be published per day between the 1st and 24th of December. This initiative is done for other languages like Perl or PHP and even some frameworks like Perl Dancer. You can directly sign up on the RSS feed or follow via e-mail. How can you help? Sharing on the social networks is highly appreciated. You can also visit the original hello world post or read on Wikipedia about Advent Calendars.
Image of Advent calendar using the actual windows of the German city of Hünfeld's town hall in Advent © Wikimedia

Nov 26, 2012

Temporary files and directories in Java 7 and before

Sometimes we want to create a temporary file, whether to save some data that gets written by some other application or just to temporary store stuff. Well, usually applications have their own temporary folder where they do this and it gets somehow configured. But why not use the underlying OS specific file like "/tmp/" in Linux so there must be some system property that has this info and there is. The key is "" resulting in "/tmp" in my case or by code:

String tempDir = System.getProperty("");

We can use tempDir  folder as a temporary place to store files, but there are a lot nicer ways to work with files like this even in JDK6 not just in JDK7:

import java.nio.file.Files;
import java.nio.file.Path;

public class TempFile {

   public static void main(String[] args) {

    try {
      // create a temp file
      File tempFile = File.createTempFile("old-file", ".tmp");
      System.out.println("Temp file : " + tempFile.getAbsolutePath());
      // nio style
      final Path path = Files.createTempFile("nio-temp", ".tmp");
      System.out.println("Temp file : " + path);
      //call the same delete on exit

      Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
          try {
            System.out.println("deleted file at "+path);
          } catch (IOException e) {

    } catch (IOException e) {




The JDK 7 way is the use the Files class and use Path 's that can easily enable us way to create and work files and their attributes and permissions. Calling tempFile.deleteOnExit() or path.toFile().deleteOnExit() will enable the files to be deleted automatically after the virtual machine terminates. Files are deleted in the reverse order that they are registered and besides that we need to be careful about what the path is.
Deletion will only happen if JVM is closed under normal condition and not for example using kill -9 (SIGKILL) under Linux.

One other interesting option is to do the deletion with a shutdown hook using "Runtime.getRuntime().addShutdownHook(new Thread() {...}" that registers  a new virtual-machine shutdown hook. This shutdown hook is simply an initialized thread, but it is not started. The thread will only be executed when the virtual machine begins its shutdown sequence during normal shutdown.

Related Links

Oct 9, 2012

Input this - input that, HTML5 new input types

We are used to inputting types like "submit", "radio" or "hidden" but these days there are more components that are so common that are part of almost every web framework out there no matter if it is in Java, Ruby, Python or any other language. I'll give a short overview of what is being added in HTML5. The current state of them is that well... they sort of work, basically not all of the features are supported in all the modern browsers or IE.
Opera seems to support most of them and they have some good initial look there.

Date selection related.

Many times you need to create or integrate some control that will be a date-time picker, I've done this way too often and I'm sure that you also have. But why should we do something like this, this should be part of HTML and we should just style it with CSS. HTML5 makes this very simple with the date input types.

<input type="datetime" />
<input type="datetime-local" />
<input type="date" />
<input type="week" />
<input type="time" />

Live example:


Emails, Phone and URL

Email validation example on Opera 12
Input type email on iOS
Why would you need input types for this, well first thing is validation that otherwise you would need to do with JavaScript on the client side.
You could also style with CSS using input type specific selectors like input[type=radio]. Best thing about the input types by far is that they are semantically correct so mobile phones open input type specific view on the keyboard.
    <input name="email" type="email" pattern="[^ @]*@[^ @]*" value="" />
    <input id="url" type="url" />
    <input id="phone" type="tel" />
    <input type="submit" />
Live example:
Input type phone on iOS



Input type that results in color picker.

<input type="color" />

Isn't this what you always wanted? ... at least when it comes down to html input.

TL;DR version
NEW HTML5 input types, awesome!!! see demo

Related Links

Demos -
W3 input element spec -
Support test -
Mozzila developers article -
Web Platform page -

Jul 15, 2012

Mustaches in the world of Java

Mustache is templating system with implementation in many languages including Java and JavaScript . The templates are also supported by various web frameworks and client side JS libraries.

Mustache has simple idea of "logic-less" system because it lacks any explicit control statements, like if, else or goto and also it does not have for statement however looping and conditional calculation can be achieved using custom tags that work with lists and lambdas.

The name unfortunately has less to do with Tom Selleck but more with the heavy use of curly braces that look like mustache. The similarity is more than comparable.

Mustache has implementation for most of the widely used languages like:  Java, Javascript, Ruby,Net and many more.

The client side template's in JavaScript

Let say that you have some REST service and you have created a book view object that has an additional function that appends amazon associates id to the book url:

var book = {
  id : 12,
  title : "A Game of Thrones",
  url : "",
  amazonId : "myAwesomeness",
  associateUrl : function() {
    return this.url + '?tag=' + this.amazonId;
  author : {
    name : 'George R. R. Martin',
    imdbUrl : '',
    wikiUrl : ''
  haveInStock : true,
  similarBooks : [{
    id : 13,
    title : "Decision Points"
  }, {
    id : 13,
    title : "Spoken from the Heart"
  comments : []

The standard way of rendering data without using templates would be create an output variable and just append everything inside and at the end just place the data where it should be.

jQuery(document).ready(function() {
  var out = '<div class="book-box"><h3>' + book.title + 
  '</h3><span> is awesome book get it on <a href="' +
 book.associateUrl() + '">Amazon</a></span></div>';

This is fairly simple but if you for example want to change the span element with div it takes a little bit of time to figure where it should be closed and often you can miss if the element should be in single quotes or double quotes. The bigger issue here is that the content is peaces of strings that need to be easy to styled via CSS and JavaScript.  As the code gets bigger this becomes unmanageable and changes to anything become slower especially if you add on top of this jQuery's manipulation functions like appendTo() or prependTo(). This direct use of out+= type of creating the content reminds me a lot of HttpServlet style of using  print writer and doing out.print() and for the same reason why this was almost abandoned we should not do this in JavaScript.
To simplify work we can add template engine like Mustache that is one of many client side tempting engines. So how does a template in mustache looks like, well for the example above with the book it would look like :

    <script id="book-template" type="text/x-mustache-template">
      <div class="book-box">
      <span> is awesome book get it on 
        <a href="{{associateUrl}}">Amazon</a>

So this template can be placed anywhere on the page and then selected and rendered when you need it:

jQuery(document).ready(function() {
  var template = jQuery('#book-template').html();
  var renderedData = Mustache.render(template, book);

The render method accepts the content of the template and the view object book, what is great here is that the template looks almost the same as html thus make it easy to style, change and maintain.
Also you can use section like :  {{#conditon}}  code or data here{{/condition}} where  if it  evaluates to true, the section is rendered, otherwise the whole block is ignored.
If the conditions returns nonempty list this can be iterated using the same construct.
Inverted condition is done using  {^conditon}}  code or data here{{/condition}}. 
Dot notation can be used to access subelements (not in every implementation), for example if you wanted to render the authors imdb page from the previous example it would be like {{author.imdbUrl}}.There are structures called partials that can be used if we need render time inclusion of partial elements, also if needed some of the standard behavior of Mustache JS can be overridden.
You can get the example from or play around with the following jsfiddle

Server side rendering Mustache in Spring MVC

There is an implementation of Mustache templates for Java called and another one called JMustache . As far as usage in web frameworks  is needed there are few articles out there about using Mustache in Java web applications based on Struts for example but I went with the Spring MVC option since I found it more interesting for my use.
For the example I used the mustache-spring-view  that is fairly simple to add using maven: 
This will automatically retrieve jMustache  :

 +- com.github.sps.mustache:mustache-spring-view:jar:1.0:compile
  \- com.samskivert:jmustache:jar:1.2:compile

The next part is including the view in the servlet context and adding the appropriate paths:

<beans:bean id="viewResolver" 
 <!-- FIXME reload every time-->
 <beans:property name="cache" value="false" />
 <!-- The default view path is below -->
 <beans:property name="prefix" value="/WEB-INF/views/" />
 <!-- The default suffix path is below -->
 <beans:property name="suffix" value=".mustache" />
 <beans:property name="templateLoader">
   class="org.springframework.web.servlet.view.mustache.MustacheTemplateLoader" /> 

As you can see the config is extremely simple you just need to add the path where the templates will be stored the suffix that the templates will end in, the actual template loader and if the templates should be cached. It is very good during development to set this property to true since that way you will get instant update on the changes in your templates.
The controller will be very simple one since this is just a small proof of concept:
* Hello Mustache.
public class HomeController {

  private static final Logger logger = 

  * Simple controller that 
  * redirects to home and adds map and date objects.
  @RequestMapping(value = "/", method = GET)
  public String home(Locale locale, Model model) {

    Date date = new Date();
    DateFormat dateFormat = 
        DateFormat.LONG, locale);

    Properties properties = System.getProperties();

    Map<String, String> map = 
       new HashMap<String, String>((Map) properties);

    String formattedDate = dateFormat.format(date);
    model.addAttribute("serverTime", formattedDate);
    model.addAttribute("props", map.entrySet());
    return "home";

The controller just fills the serer system properties and the current time add passes the model to towards the template.  Acorrding to the previous configuration the template name is home.mustache and it is located in /WEB-INF/views/.  
Template is as simple as it gets, the idea here was just to illustrate how java maps can be iterated.
<html lang="en">
<meta charset=utf-8>
<title>Hello Mustache</title>
<div id="container">

  <p>Current server time is {{serverTime}}</p>
  <p>All the current system properties</p>
         <li>{{key}} = {{value}} </li>
So how does it work? Well if we take serverTime it can be java property, key or a method which makes it very simple and readable.  The full example can be retrieved from my github page.
It's just the regular maven clean package and run, it's tested on tomcat 6 with Java 1.6 but it simple enough to work anywhere. While I can't yet say that I have used the servers side rendering in a production environment they most definitely look promising. There are plugins for vim, emacs and textmate but there is no plugin for eclipse. On eclipse you can do a workaround Eclipse->Preferences->General->Content_Types add the *.mustache to be recognized as html, at least you will get the html syntax highlighting.
The cool thing with Mustache is that you can very easily switch whether you gonna use server side rendering on client side additionally it makes it very hard to add unnecessary logic in the templates that makes development simpler.
Don't  forget your app is only hot if it has awesome mustaches.

mustache js
I got few questions regarding the use of partials with the Spring resolver. So here is the clarification. When using partials you need to use the full relative name:

{{> /WEB-INF/views/partial.mustache}}

The updated example can be found on that demonstrates the use of partials.

There is a pull request that improves the syntax a little bit on but it is not yet accepted.

Other links

Jul 6, 2012

Google Guava for cleaner code

Some time ago I did a short talk on Google Guava for our local JUG. It was a basic intro into Guava and how it makes stuff simpler, better and cleaner.

It is true that there is an overlap with Apache commons but Guava is build with expectation that there is a Function and a Predicate class as well as various builders which makes it really cool and simple for many use cases.
The talked covered most of the* classes  and basic use of functions in collection and Google collections  and few other features that are part of Guava and I find them very useful.
Source code of the examples can be found on github and here is the actual presentation:

Note one last recommendation that is not just mine but  also part of Guava's wiki :
Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava, and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps.

Related links 

Jun 27, 2012

Getting Real

getting real

When a colleague recommended a book from 37singals I was skeptical, to say the least, not because it is a bad company, but I do kind of find them very cocky and also my first impression was "Isn't that going to be a something about Ruby?". But  I gave it a try and liked it enough to write a few good words about it.
So what is this book about? In my opinion, it's about agile software development at its core and to quote the authors "Getting Real" is a book about:
The smarter, faster, easier way to build a successful web application.
When I started reading it became obvious right away that this book is a great collection of short stories, tips, quotes and lessons from various companies and software developers and not just 37signals, although it had some very good ones from them also. In some places, the authors were direct about what is their standing, sometimes even cocky but definitely in a really good way.

Getting Real is about doing software with fewer features, fewer docs and less of everything that is not essential. The authors goal is to show a way of doing the software version of the famous Einstein's quote:
"Everything should be made as simple as possible, but not simpler."
The books are organized in various chapters about "priorities", "staffing", "code"... where there are great lessons like:
  • "It's a problem when it's a problem" 
  • "Hire the Right Customer"
  • "You can't Fake Enthusiasm"
  • "Actions, Not Words" and,
  • "Start with No"
While I can't say that I agree with them on all of the lessons I can say  for sure it's a book that should be on every programmer's reading list. Another awesome thing is that, even though, the book can be ordered in hard copy from places like Amazon, 37signals have made it available for free in pdf on the book's site.
I will finish with a quote found in the introduction that is not originally intended for software development, but it fits like a glove:

Vigorous writing is concise. A sentence should contain no unnecessary words, a paragraph no unnecessary sentences, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts. This requires not that the writer make all his sentences short, or that he avoid all detail and treat his subjects only in outline, but that every word tell.
                        -    William Strunk Jr., Elements of Style

Jun 16, 2012

HTML 5 data-* attributes, how to use them and why

It is always tempting to add custom attributes in HTML so that you can use the data stored there to do X. But if you do that there is no way of knowing if your HTML attribute will not be overridden in the future and used for something else and additionally you will not be writing valid HTML markup that can pass HTML 5 validator and with that you can create some very bad side effects. That is why there is a spec in HTML 5 called custom data attributes that enable number of useful features.

You may go around and read the specs, but the basic idea is very simple, you can add any attribute that starts with "data-" and that attribute will be treated as non-visible data for that attribute. By non-visible I mean that it is not something that gets rendered to the client so it does not affect the layout or style of the page, but it is there in the HTML so in no way this is private.
So let's get right into it, the following snippet is a valid HTML5 markup

  <div id="awesome" 
     data-hash="3e4ae6c4e30e50e4fdfc7bf439a09974">Some awesome data</div>

Great so now how do you read the data from there? Well, you can go through the elements and get the attributes you need or you can go and use jQuery if it is already there. Using jQuery's .data() API  you can work with the "data-*" elements. One method is .data(obj) added in jQuery1.4.3 that basically retrieves data for the selected element.
For example, if you want to get the value of the data-hash attribute with the following snippet:

 var dataHashValue = jQuery("#awesome").data('hash');

You can also use a JSON syntax in the data fields  for example if you have the following HTML:

<div id="awesome-json" data-awesome='{"game":"on"}'></div> 

Accessing data from js can be done directly just by adding the JSON key to the result:

  var gameStatus= jQuery("#awesome-json").data('awesome').game;

You can also set the data using .data(key,value) directly from JS. One important thing to remember is that the "data-*" attributes should be linked to the element in some way. They should add additional info about that element or the data it contains and not act just as a storage for everything 

You can go around and play with it in the following jsfidde.

Related links:

Jun 12, 2012

Available meta data in Confluence and JIRA

Atlassian uses  something called AUI or Atlassian User Interface that is basically a set of reusable, cross-browser tested UI components (markup, CSS and Javascript) that are based on jQuery and jQueryUI. They are used in most of Atlassian products like JIRA an Confluence.
Common trick for saving some meta-data about the web app and the currently logged in user is used.

<meta name="ajs-context-path" content="/wiki">
<meta name="ajs-version-number" content="3.6.0">
<meta name="ajs-build-number" content="XXX">
<meta name="ajs-remote-user" content="someusername">
<meta name="ajs-static-resource-url-prefix" content="/confluence/s/en/2155/5/_">

There are a few utility functions that can be useful:
AJS.Data.get("some key") ex.AJS.Data.get("remote-user")

AJS.params.somekey  ex. AJS.params.loggedInUser
and if you want to see list of all available params in FireBug console just write
AJS.params and magic happens. You can get info about the current user's username, the locale or the app context path. Please note the depending on the version and the app that you are using like Confuence, Jira or other you will get a lot of params in the list that are version or app specific.

Jun 11, 2012

NSND 2012 Кирилица - Struga

Java and Python joined together
Nista se nece dogodite (NSND)  or roughly translated into English - Nothing will happen is a geek/hackers unconference  where people get together to share knowledge about pretty much anything, starting from programming and computer networks going to growing food on top of buildings and women's rights. The full list can be found on the flowing ether-pad This year the main event in Macedonia was in Struga and there were very cool talks. I missed some of them but  so here are few that I can take a note of:
  • Happy food production by Luca Pescatore(
    File:CDC South Aquaponics Raft Tank 1 2010-07-17.jpgHe talked about aquaponics, windows farming, deep water culture, compositing and basically how to do farming almost anywhere and not just that but also how to farm efficiency. His main idea was to encourage everyone to just try things out. One cool experiment that he did was creating a small farm on top of building.

  • Why python has special methods and why in the world would I use them anyway - Baze Petrushev(twitter)  
    He talk was mostly about the magic methods in python and how they can be used Some considered this a good thing other bad but anyway it was an interesting way to see the operator over-overloading in Python used for good.
  • Wireless Mesh Networks with sensors, Internet Of Things, Open Network project Valent, Valentino, Jasna, Aleks (,
    As the title states the talk was basically about running open networks and how one can setup various sensors and then use the data is gathered via the sensors.
  • ZeroMQ in python - Baze Petrushev and Damjan Gergievski (
    Basically, they covered the idea behind ZeroMQ and they showed us a few small examples on how to get started:
  • I didn't get the chance to see: 
    • Rabsbery Pi by Glisha and Chombe(Chombium)
    • Non-Commercial potatoes by Andrea
    • Free Android by Ivo 
    • Hacklab collaboration disquisition  by Marcel 
      , but from what I heard they were all really inspirational and cool.
  • And last but not least was Women in IT + Open Eduction discussion that became very heat-et. We raised the question on:
  • IBM computers in 1957, 1:1 male female ratio  
    • Do male and female programmers  have equal opportunity?
    • Why is there so small percentage of girls in engineering?
    • Why their only a handful open-source project leaders that are female? 
    • Are program's like Google summer of Code and Gnome women outreach good for improving that percentage and is that artificial?
    • What do Hacker-spaces do right or wrong to have so small percentage of females?

      I cannot say that we had unanimous answers or a simple solution on any of these questions but most definitely the talks were interesting and led to various opinions on the subject.
Females in Engineering talks

Overall it was a great NSND where we had a lot of fun and I hope to see everyone on the next one. Until then:

Jun 7, 2012

Git ignore for Java/JVM projects

Github has this great project hosted on ... GitHub.  What so great about it? Well, it contains git-ignore files for various development environments and programming languages. This gives you a great starting point when creating a new project in any language and any environment.

So this is my starting git ignore on every new java project:


# eclipse specific git ignore

# Intellij 

# External tool builders

# Locally stored "Eclipse launch configurations"

It covers maven based projects with eclipse and using of combination for overriding  other *.properties that I prefer. Additionally it removes all eclipse specific "settings" that I'm opposed to having them in version control since most of the time it makes no sense for them to be there since they are generated. It's my believe that never eclipse settings should be part of the version control since for almost all of them there are appropriate  maven plug-ins.
The same thing can also be done for InteliJ files.

Mar 29, 2012

JavaDoc incremental searchbar for Firefox and Chrome

Javadoc is very old but it is a great way to create documentation and now with Java 7 we have better CSS in Javadoc.

Grease Monkey
But there is one thing always missing in the generated documentation and that is a search bar in the left frame where the class list is displayed. Since there are tons of java projects and with that tons of Javadoc it's very probable that most of the Javadoc will remain a little bit ugly.  Because we cannot change the Javadoc on other sites we can change it  how they are displayed on our side.

There is this super awesome project on google code  where using GreaseMonkey and some client side JS they have added incremental search. So now when viewing Javadocs, this script replaces the package and class frames with a single search frame that allows searching incrementally through all packages and classes at once. There is plugin for Firefox and Chrome available.  You would need to have GreaseMonkey installed first on the Firefox version.
So now we have a search bar that is awesome and it works remote as well as local pages.

Code Google project page

Mar 26, 2012

Basic Authentication with RestTemplate

Spring Rest Templates are very good way of writing REST clients. By default they work with basic HTTP so if we need to use Basic Authorization we would need to init the rest template with custom HttpClient. This way the Rest Template will automatically use Basic Auth and append to the HTTP headers "Authorization: Basic BASE64ENCODED_USER_PASS".

HttpClient client = new HttpClient();
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("USERNAME","PASS");
  new AuthScope("", 9090, AuthScope.ANY_REALM),
CommonsClientHttpRequestFactory commons =
     new CommonsClientHttpRequestFactory(client);

RestTemplate template = new RestTemplate(commons);
SomeObject result = template.getForObject(

In EE application this would probably be managed by DI framework like Spring Core and only initialized once since  RestTemplate is stateless.

HTTP Basic Auth
Spring Rest Templates
Spring Rest Templates JavaDoc
Apache HTTP components

Jan 17, 2012

MySQL create UTF database

The one command that I use very often and I always forget exactly how it was.  So here is how one creates UTF8 database in MySQL:

CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;

Jan 14, 2012

Aria2 - awesome command line download manager

Back in my Windows days aka the dark ages, I was very into download managers.  This was probably because those days we had slow Internet like dial-up and cable with speeds up to 128 kbps down / 64 kbps up. So at this kind of speeds it makes more sense to use download managers, mostly  because you are waiting all the time and these managers made you feel that things moved faster even if sometimes that was not the case. Also, IE was very dominant browser at the time and when it crashed  you would have to re-download instead of just continue, that was the biggest reason why I was using FlashGet, Go!Zilla and of course Download Accelerator Plus.
I am still under the impression that most of these programs were "placebo" like, just making you feel that things are moving faster.

Nowadays download speed is rarely the issue, but sometimes we like to automate downloads. Great way to automate is using terminal based download manager. Aria2 has some very interesting features that we can use. Basically aria2 is a lightweight multi-protocol and  multi-source command line download manager. It has support for HTTP/HTTPS, FTP, BitTorrent, and Metalink

There are many other alternatives that come to mind like wget or cURL but with aria2 you can download files from several URIs using different protocols and also if you give aria2 list of files it will download them concurrently.
aria2 download very well: aria2 cancels slow servers and use the faster server to finish the download. In other words, it is very clever and reliable in many different situations and it does most of the downloads in parallel.


You can download it directly from SourceForge. 
I'm using the Linux version, but there are releases of Windows and Android, but I can't say that I have tried them. There are also packages for most of the Linux distributions.

For Debian based (Ubuntu, Linux Mint):

sudo apt-get install aria2 

Arch Linux

sudo pacman -S aria2 
Fedora and yum based
sudo yum install aria2

Few simple examples 
  • Download a file

 aria2c  location-or-URL-of-file

  • Download from multiple sources

 aria2c  location1 location2 location3 ... 

  • Download torrent

 aria2c location-of-file.torrent 

  • List files in a torrent

aria2c -S location-of-file.torrent 

  • Download specific files from torrent

aria2c --select-file=1,2,3... location-of-file.torrent

index of files ( 1,2,3 ) can be  found with -S command
  • Limit speed for upload ( on torrent files, for example )

aria2c --max-upload-limit=200K location-of-file.torrent 

  • Limit speed for download

aria2c --max-download-limit=100K location of file

  • Pause and resume
In order to pause just press Ctrl+C and to resume enter the same command at the same location. Aria2 keeps .aria2 files where it memorizes the current download progress.
  • Download using multiple connections

aria2c -j7 -x2 location-of-file  other-location-of-file...

this way we instruct aria2c to use 7 concurrent connections with max 2 connections per server
  • Download  a list of files from URI provided in a file

aria2c -i file-with-uris.txt 


Follow by Email