Dynamically Importing Spring Context Files

We needed to change the underlying storage mechanism (JPA2/JCR/inMemory…) dynamically from configuration and actually the neat and perfect solution for our problem is Spring Profiles from Spring 3.1 as you can see from this excellent post by Chris Beams. But since the beans with Profiles are not there yet and the Spring 3.1 GA release is planned for September 2011, here is another solution using import tag to dynamically load the spring context files depending on the active storage type:

	<import resource="${repository.type.config}"/>

Unfortunately the import tags are not resolving .properties files, so you can only use them with system properties since the imports work before bean initialization as mentioned here. If you want to define/add the system properties with Spring here is a suggested solution but as I tried this would not work as easy as expected since the system properties are not loaded before the importing. So if you want to add system properties from .properties file at Spring startup, you can extend the ContextLoaderListener and override the contextInitialized method to merge the properties from .properties file that you defined as context init parameter in your web.xml.



Spring MVC/JPA2 Quick Startup Projects

You Must Believe in Spring

Image via Wikipedia

I know there are many Spring MVC/JPA2 tutorials and projects out there, and in the previous years I have forgotten how many new projects I have started projects using Spring MVC and JPA2, so I added two template projects to Google Code, mainly for myself.

Spring MVC Quick Startup: This project is a simple template project for Spring 3 MVC.

Spring MVC-JPA2 Quick Startup: This project is a simple template project for Spring 3 MVC, JPA2(I favor JPA2 over Hibernate) and also the basic Spring Security.

These projects are really quick startup template projects, I changed some of my projects in 10 min. So there might be some issues but at least it should be enough for the initial setup.

Please feel free to use.


Enhanced by Zemanta

Amsterdam Startup Weekend is over!

Wow, what an amazing weekend and Amsterdam Startup Weekend is now over. Here are the projects that pitched and their websites/projects. Most of these projects were launched in less than 54 hours!

The Winners:

  1. Goddess Alert
  2. Shuffle Wear

The other projects that pitched:

Congratulations to the winners and all the startups! Thanks to the organizers of Amsterdam Startup Weekend it was an amazing and inspiring weekend!

Here is Mike Lee’s post How to Win: Lessons from Startup Weekend Amsterdam.

Maven JAXB plugin for code generation from XSD

We can use Maven JAXB 2.1 Plugin to easily generate JAXB2 code from XSDs at build.

Here are the maven dependencies for JAXB at pom.xml:


and here is the JAXB xjc plugin settings at pom.xml, of course you can change the output directory configuration, I wanted to generate the code at build to target/generated-sources folder:

			<!-- JAXB xjc plugin that invokes the xjc compiler to compile XML schema into Java classes.-->
					<!-- The schema directory or xsd files. -->
					<!-- The package in which the source files will be generated. -->
					<!-- The working directory to create the generated java source files. -->

As a side note for code generation: code generation convention is using “target/generated-sources/” folder, it is added as source folder by default.

After adding your XSD file at “resources/xsd” you can generate your code with mvn jaxb2:xjc or mvn clean install commands.

Jericho HTML Parser

I needed to parse and merge HTML for the current project I am working on. After searching and playing with several HTML parsers I decided to use Jericho which is a light-weight and easy HTML Parser. I also checked the CyberNeko HTML Parser which is another strong alternative but here is the simple reason why I did not choose it :)

The maven dependency is:


Here is a simple code using Source to construct the source data, which can be a String, Reader, InputStream, URLConnection or URL.

    public String read(InputStream is) throws IOException {
        Source source = new Source(is);
        return source.getTextExtractor().toString();

and you can merge the HTML using StreamedSource which is preferred for performance with the memory. Now let’s insert a title at the head segment:

    public String merge(InputStream is) {
        Writer writer = new StringWriter();
        try {
            StreamedSource source = new StreamedSource(is);
            for (Segment s : source) {
                if (s instanceof StartTag) {
                    if (((StartTag) s).getName().equals(HTMLElementName.HEAD)) {
                } else {
        } catch (IOException e) {
        return writer.toString();

As you can see Jericho is really easy to use!

Getting content type from file extension

There are several ways to get the content-type from a file using the extension, you can also use Spring’s ConfigurableMimeFileTypeMap which uses javax.activation.

import org.springframework.mail.javamail.ConfigurableMimeFileTypeMap;

import javax.activation.FileTypeMap;
import java.io.IOException;

public class ResourceContentTypeResolver {

    private FileTypeMap fileTypeMap;

    public ResourceContentTypeResolver() {
        fileTypeMap = new ConfigurableMimeFileTypeMap();

    public String getContentType(String fileName) throws IOException {
        if (fileName == null) {
            return null;
        return fileTypeMap.getContentType(fileName.toLowerCase());


There is also a great article about Getting the Mime Type from a File.

Winter cleaning :)

Girly Sound
Image via Wikipedia

Hi again after 1 year :)
I updated my blog’s theme, which is not very girly now :) I also removed some old posts, especially about JSF, because actually it has been a long time since I used JSF, and they were really very old…

Enhanced by Zemanta

log4j Custom Appender For Separate Log Files

Handbook issued to passengers on Campania

Image via Wikipedia

We needed different log files for a common web project, this gateway project simply loads web services dynamically and generates the wsdd files at runtime, but the problem was huge size of the common log file used by all services. So we needed different log files for different services.

Here is the step by step solution to separate&customize the log files:

Grails Startup with IntelliJ IDEA

welcome-to-grailsI started a toy project just to give Grails a try and want to share the project setup, and other steps. I were really impressed by the demo applications: painless, easy startup and CRUD applications, but yes a bit too late but I were really busy at my new job, and also trying to get used to my new country, and of course I admit that I am a bit lazy too :D
After reading the Getting Started with Grails free booklet, and well prepared Grails Documentation I started playing with it :)

Read the rest of this entry »

New Year Gifts :)

The books we ordered have just arrived: great gifts for the new year :)

I will start with classic book of software engineering – The Mythical Man-Month: Essays on Software Engineering first which was first published in 1975 by Fred Brooks. This anniversary edition includes four new chapters and I really wonder about the evolution of software engineering and the human factor in the past 33 years. I can’t wait especially for the legendary The Mythical-Man Month and No Silver Bullet essays.

Read the rest of this entry »