My first open source project

I recently released my old, long-term project called survey-rocket (a simple online survey tool) to google code. I started to refactor the code to make it more readable and maintainable. Although I invested many hours, there is still a long way to go until it can be considered good code. Basically I used this project for applying all the best practices found in Clean Code.

Check out the software here

The latest features added to the software are

  • added email support for sending URLs
  • added workspace reset
  • after clicking “save” once, everything is autosaved

old blog post:
I am proud to announce my new webservice suRRvey.com (service moved!). It is a tool for easy survey creation. It is free and requires no sign up. It is heavily XML and XSL based and only uses AJAX for communicating with the server. It is fully implemented in Java. I am aware of some of the bugs which are still unresolved. Laeve me some feedback if you have some suggestions or you found bugs!!! Happy survey-creation!

Add tools.jar to maven repository

A way to fix the following error:

6/23/12 1:50:22 PM CEST: Missing artifact com.sun:tools:jar:1.4.2:compile

The tools.jar is part of all JDK’s and therefore there is no need to fetch this dependency from any repo. Therefore the easiest thing is to add this jar to the local repository.

Locate the tools.jar in the JDK and run this command:

mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=“C:Program FilesJavajdk1.6.0_26libtools.jar”

 

As reference the dependency:

<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
</dependency>

persitsing an interface type with JPA

Consider this scenario. You have several classes implementing the same interface and you’d like to persist this interface type within an entity. The JPA is not able to handle that, but there is a workaround!

Lets make an example: A Plate (entity) has the attribute of type Fruit (interface). The apple, orange and the banana implement Fruit.

Now it would be great, if we could simply call plate.setFruit(apple) for example. The problem is, that JPA cannot handle multitype attributes (yet) . Use the following workareoud to make this functionality working:

The entities used for the exampe are:

Now the trick is to create a class with references to all of the Fruit interface implementing classes, which is later embedded in the Plate class:

This class is then embedded in the Plate entity:

Please consider this mapping possibility as bad practice, slow and hard/expensive to query!

Simple multithreaded Java Webserver

The following code (estimated 150 lines of code) implements a simple multithreaded Web Server in Java. The code is easy to read and contains no (really) complicated stuff. There are 5 classes:

HTTPException.java

This class extends Exception and overrides the getMessage(String) Methode, which is responsibe for example for the Error 404 Not Found message.

MimeTypeResolver.java

This class is a static Map of file type to mimetype mappings. It can be extended easily.

RequestWorker.java

This class extends the Thread class and can and is run as a thread therefore. For every request occurring in the Server class, a new RequestWorker is created which handles the user request. Note that this class is kept ultra simple. It converts the path part of the HTTP request to a file path and servers this file back to the user.

Server.java

This class also inherits from the Thread class and is run as such. It creates a ServerSocket and creates a RequestWorker thread for each request.

Status.java

This is an Enum containing the HTTP codes and its corresponding error messages.

When started, the server serves the content in the root folder. Open your browser and type http://localhost:8080. When everything is okay, you see the welcome website.

Download the source code here: SimpleJavaWebServer.

Sort collections in java

Usual problem: When you pass a java list to Collections.sort(List list), the elements are sorted according to their returned values retrieved by calling the objects compareTo() methods. Well, what to do if you suddenly want to sort the same list by another criteria? Then a comparator is your friend.  A comparator knows how to compare two objects in the list to sort, and is basically defined by:

get sun java on debian based system

just type: