New Year starts with Scala

Slowly up to speed

New Year is here, smoke calming down after a couple of nights of excessess with fireworks (and some other stuff too).

I think tonight, the sky will be clear, and the air fresh again.

After lazying around with my family during the Christmas Break, its time to actually work again.

Preparing lecture for  JG.DKs annual conference

I am preparing my lecture for the Danish Java Groups Annual Conference, it will be held at Hindsgavl Castle on my Island (=Funen) in Denmark. The program is here.

My talk will be about a new language – Scala – which is bytecode compactible with Java, and runs smoothly on the JVM. I claim that Scala is going to give us back Object Oriented progamming – without having to do some of the awkward workarounds, that java is forcing us to do.

Out of the host of new languages that run on the JVM, like JRuby, Groovy and Jython – I really think Scala stands out.

What is so special about Scala?

It is a functional language. It is an object oriented language. It is a type strong language – exactly like Java.

It has traits – an ability to add methods to an interface – a language feature that we have been missing since

It is pure and well designed. Invented by Martin Odersky – who designed the Java generics.

Eventually, Java 7 or 8 will be patched with closures, and traits. But those will be patches. I am right now reading a book by Odersky, Spoon and Venners – Programming in Scala – highly recommendable.

Your hello world in Scala


Lets try Scala out.

First, lets get the convenient Eclipse plugin (you need Eclipse 3.4 + to make it work) from its update site:

Select New>Scala Project.

This gives you a … Scala project.

Now in the src folder, add a Scala Application (runnable):package application

object HelloWorld extends Application with FirstTrait{
override def main(args: Array[String]) {
println(“Hello, world from Scala!”)
println(“From trait ” + greet(“world”))
print(arg+” “)


Make your first trait:package application

trait FirstTrait {

def greet(name:String):String = “Hello “+name


Run the application (Run > Scala Application), to see the output:

Hello, world from Scala!
Now – hello from trait Hello world

Thats it – hello world in Scala.

Hello World in Scala

Screenshot from Eclipse: Hello World in Scala


I attended JAOO in Århus this year – what a fantastic experience, this conference has a special atmosphere. Denmark is such a small country, you keep meeting old colleauges, classmates, bosses – whatever.
This years buzz was all about functional languages – we finally need to decide what to do with side effects. One possible solution is to have the language give clear, big warning signs.
This is the old semantics vs syntax discussion: Giving clear names, typesafety etc doesn’t help to achieve the big golden dream of (finally!) having reusable software components. What we need to agree is semantics – what does it do, not syntax – how to call it.
I was enchanted by Bill Verner, and his Scala talk, and I am really looking forward to his newcoming Scala book. A typesafe dynamic language that runs on the JVM – I am kind of convinced that this is exactly what we need. To bad the book isn’t coming out before the conference is over – I would have loved a signed copy.
I also enjoyed Michael Nygards talk “Failure comes in flavors” almost without breath – his rant about patterns and antipatterns when time comes to deploying and keeping our applications alive was full of cliff-hangers. Respect. If you ever have a chance to hear this man talk – and if uptime is of any importance to your next web-app – don’t miss him.
I also spent some time discussing lean/agile architecture with James Coplien and his gang. He is promoting DCI – data, contexts, interaction, which is a new paradigm in structuring your applications architecture. James wants us all to move us away from class-oriented programming to true object oriented programming. We spend a couple of hours hanging out, discussing how to translate his example into different programming languages. I myself tried to convince James that you can do it -in Java too. Sitting together, 5 people, around a table, we discovered how we all have been searching for the same answer. Magic of coincidence, or is it the world allready moving in a new direction?

Sending html mails with embedded pictures, that Thunderbird can understand

Users of Thunderbird have for long known, that html messages with embedded content look “strange” – you see a list of attachments part1.1, part1.2 – when you click on one of them, you see the html text message, or the pictures that should have been embedded.

I have just discovered a way of sending html mails with embedded pictures, that Thunderbird can display correctly. I use Java 6, and the javax.mail package.

Basically, you need to build a mime message in the following format:

<multipart alternative>

<mimebodypart:empty text>

<multipart related>

<mimebodypart:html cid:picture_1, cid:picture_2, … >


</multipart related>

</multipart alternative>

Here is how to do it in Java code.

import javax.activation.DataHandler;
import javax.activation.URLDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

// ... some other stuff
public Message getAsMessage(Session session) {
try {
Message message = new MimeMessage(session);
// set subject
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);

InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < addressTo.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
message.setRecipients(Message.RecipientType.TO, addressTo);

//create top multipart
MimeMultipart topMultipart = new MimeMultipart("alternative");

// prerprocess dynamic text

if( htmlTemplate != null ){
//create related multipart, wrap into bodypart, add html part
MimeMultipart relatedMultipart = new MimeMultipart("related");
MimeBodyPart relatedBodyPart = new MimeBodyPart();

//load html part
BodyPart htmlBodyPart = loadPartContent(htmlTemplate, null);
preprocesText( htmlBodyPart, HTML);
// htmlBodyPart.setHeader("MIME-Version" , "1.0" );
// htmlBodyPart.setHeader("Content-Type" , htmlBodyPart.getContentType() );

//fill up the related multipart with bodyparts for images
for (ImageContainer image : images) {
BodyPart imageBodyPart = loadPartContent(image.getUrl(), image.getContentId());
if( textAlternative != null){
BodyPart alternativeTextBodyPart = loadPartContent(textAlternative, null);
preprocesText( alternativeTextBodyPart, PLAIN);
//add first poormans alternative, later the related part of the message

if( topMultipart.getCount() == 0){
MimeBodyPart emergency = new MimeBodyPart();
emergency.setText("No content");
return message;
} catch (MessagingException e) {
if (Optimization.LOG && LOG.isWarnEnabled()) {
LOG.warn("Could not create the mail message for session "
+ this);
return null;

Normally, alternative is used to enable mail-clients that do not understand html to select the text alternative. By setting the sub-type of the alternative multipart as “mixed”, this trick is forcing Thunderbird to display both (the empty) text message, and the html message.

Here follows the example in code:

New job – new technology stack.

Well I HAVE worked as an RCP developer at APC Denmark. I do not work there anymore, since my values do not conform to the company values – whatever that means … well, probably this means that I will leave RCP development for a while – right now I am really fascinated by cool serverside web technologies, like jsp, glassfish, grails …
Luckily, I was out of job for only a week, I have found some great guys at a small team in Wannakey. We work right now hard on releasing the next killer community portal, which enables users to share and trade all kind of stuff. Watch out on