Cool Groovy features 3 – ConfigSlurper

In Java configuration properties can only be key-value pairs e.g.

db_user="admin"
db_password="letmein"

Wouldn’t it be nice to be able to group configuration properties? This is exactly what you can do in Groovy using the ConfigSlurper class. It allows you to write config files like the following:

db {
    user="admin"
    password="letmein"
}

commands{
    setup="GET_DATABASE,GET_JBOSS_SERVER_FILES,CHECK_JBOSS_SERVER_FILES"
    refresh="GET_DATABASE"
    check="CHECK_JBOSS_SERVER_FILES,CHECK_APACHE_WORKER_PROPERTIES"
}

These properties are read in as a map of maps, and you can access each property by using the dot operator on the configuration object:

config = new ConfigSlurper().parse(new File("myApp.config").toURL())
dbUser = config.db.user
dbPassword = config.db.root

For more info, see:

http://groovy.codehaus.org/ConfigSlurper

This entry was posted in Groovy and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

503,487 Spambots Blocked by Simple Comments

HTML tags are not allowed.