Functional Programming Principles in Scala

For everyone who is interested in having a closer look at Scala and functional programming:

Martin Odersky, the author of this programming language, is giving an introduction into the Scala on coursera. It will be a seven week long, free online class with weekly home-work assignments and a certificate at the end.

You can signup here.

Custom Authentication Provider in Spring

The Spring Security module supports by default some standard ways to retrieve user information for authentication from databases, LDAP or other commonly used storages. But unfortunately the Spring documentation does not say much about creating connections to custom data sources. There are actually several different ways to do it.

But let’s say we have a Spring MVC application with a login form which contains a user name and password field, but the application does not use any of the supported methods to store the user data. How can we authenticate the user anyway?

The easiest way in my opinion is to create a new authentication provider. So we simply need to implement the AuthenticationProvider interface.

The authenticate() function of the class must return an UsernamePasswordAuthenticationToken instance if the authentication is successful or null otherwise. You can choose another token, simple check the classes implementing AbstractAuthenticationToken. But for our scenario this should be enough.

It is important to populate the list of authorities we grant the user. I used the standard user role (“ROLE_USER”).

In the real-world you might want to add a member variable to the authentication provider pointing to a bean which contains the code for authenticating an user, here I just hard-coded it (name must be “admin”, password “system”).

public class CustomAuthenticationProvider implements AuthenticationProvider {

	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		String name = authentication.getName();
		String password = authentication.getCredentials().toString();
		if (name.equals("admin") && password.equals("system")) {
			List<GrantedAuthority> grantedAuths = new ArrayList<>();
			grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
			Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
			return auth;
		} else {
			return null;

	public boolean supports(Class<?> authentication) {
		return authentication.equals(UsernamePasswordAuthenticationToken.class);

Now we need to declare the new authentication provider in our configuration:

	<authentication-provider ref="customAuthenticationProvider"/>

And that’s it!

Free MongoDB developer online class

During the past few years we saw a steady rise of NoSQL databases. While most of them are for very specialized scenarios there are a few multi-talents which can be a good replacement for a more traditional storage solution.

One of the more well-known is MongoDB, a document-oriented, schemaless database, which I started to like very much. MongoDB is easy to set up and allows me to store objects into the database right away, without thinking about table layout and hacking SQL installation scripts. Of course it also has its downsides, but later more on it.

Currently I take an online class on MongoDB for developers, which is provided by the makers of the database at for free. The online class consists of a couple of video lectures and regular home work assignments. After seven weeks the class will end with a final programming project and of course a 10gen MongoDB developer certificate.

The homework is easy and the course is progressing with a slow pace which is ideal if you cannot or don’t want to spend to much time on it.

I don’t think you can join the current class, but at the end of February (25th) a new course starts:

“M101J MongoDB for Java Developers”

So if you are interested in taking a closer look, then join the course. So far I like it. It gives a good introduction, talks not just about the programming API, but also about best practices in document design and performance tips. You will not be a professional mongo developer after the course, but I think it provides everything you need to start using MongoDB and is also a good starting point to dive deeper into the issue.


This blog is not dead …

… it just slept for a long while. But I am about to resurrect it. So stay tuned.

I started this blog for making notes for myself, share code snippets and help fellow programmers who got stuck with similar problems. But the internet moved on during the past two years and there seem to be dozens of web sites now which are probably better suited for doing all these tasks. Nevertheless I like blogs, they are much more personal than tweets and Gist code dumps. So I decided to continue sharing code, ideas and opinions … just don’t expect me to be as frequent as two years ago ;-)

Before this post becomes completely useless, here is a small brain teaser for our Javascript ninjas out there:

What is the following expression evaluating to?

"3" -+-+-+ "1" + "1" / "3" * "6" + "2"

The answer and in depth explanation can be found on which makes me regularly laugh and cry.

Simple Multi-Threaded Application in Haskell

Haskell has a powerful concurrency library which is surprisingly easy to use. The Control.Concurrent library is shipped with the standard installation of ghc and provides everything needed to get started.

As a small test I want to write a programme which reads numbers from stdin, spawns a new process to calculate the Fibonacci number for each input on a different CPU and prints the result to stdout.

First we need a function which is called when spawning a new process, which should calculate the result and print it to the console. In my case this is a simple IO Monad, which gets a MVar as it’s first argument. The function will wait for input from the MVar and then call the Fibonacci function.

This MVar can be thought of as a synchronised box where a thread can store a value and another thread can read from it. So MVars are just the poor men’s message channel which can hold one value at the time only. Of course there are also real channels in Haskell which act like the message queues in Scala or Erlang, see Control.Concurrent.Chan for more informations.

printResult mvar =  do
	value <- takeMVar mvar
	print $ "fib(" ++ show value ++ ")=" ++ show (fib value)

fib 0 = 0
fib 1 = 1
fib n =  fib (n - 1) + fib (n - 2)

Now we need to read the input from stdin, create a MVar object and invoke the printResult function. The cool thing about Haskell is the forkIO function which is really convenient. It takes an arbitrary IO expr and invokes it in a new thread:

import Control.Monad -- needed for "forever"
import System.Exit     -- needed for exitSuccess

main = forever $ do
	line <- getLine
	when (line == "quit") exitSuccess
	mvar <- newEmptyMVar
	forkIO $ printResult mvar
	putMVar mvar (read line :: Integer)

Now we have to compile our programme with the “-threaded” flag:

ghc -threaded fib.hs -o fib

The RTS allows us now to define the number of processors the application runs on with the -Nnum flag. If I want to use three processors for parallelisation I can run the programme simply with:

./fib +RTS -N4

Writing A Synthesizer With Brainfuck

Brainfuck is a small esoteric programming language which comes with just eight commands and Turing completeness (given enough time or memory). And that is just enough to write a small synthesizer in it. Some people have definitely too much time ;-)

Easter Eggs in Open Source Software

Some programmers definitely have to much time, see this link to an arstechnica article:

Cracking open five of the best open source easter eggs