Grails-ConfigSlurper

Grails-ConfigSlurper

ConfigSlurper is a utility class within Groovy. ConfigSlurper scripts support native Java types and are structured like a tree.

1. Common Usage
Properties for example:
log4j.appender.stdout = "org.apache.log4j.ConsoleAppender"
log4j.appender."stdout.layout"="org.apache.log4j.PatternLayout"
log4j.rootLogger="error,stdout"
log4j.logger.org.springframework="info,stdout"
log4j.additivity.org.springframework=false

Sample to load the config:
def config = new ConfigSlurper().parse(new File('myconfig.groovy').toURL())
assert "info,stdout" == config.log4j.logger.org.springframework
assert false == config.log4j.additivity.org.springframework

And the properties can be changed as follow:
log4j {
    appender.stdout = "org.apache.log4j.ConsoleAppender"
    appender."stdout.layout"="org.apache.log4j.PatternLayout"
    rootLogger="error,stdout"
    logger {
        org.springframework="info,stdout"
    }
    additivity {
        org.springframework=false
    }
}

2. Converting to and from Java properties files
java.util.Properties props = //
def config = new ConfigSlurper().parse(props)
props = config.toProperties()

3. Merging configurations
def config1 = new ConfigSlurper().parse(..)
def config2 = new ConfigSlurper().parse(..)

config1 = config1.merge(config2)

4. Serializing a configuration to disk
Each config object implements the groovy.lang.Writable interface that allows you to write out the config to any java.io.Writer.

def config = new ConfigSlurper().parse(..)

new File("..").withWriter { writer ->
    config.writeTo(writer)
}

5. Special "environments" Configuration
The properties file Sample.groovy is as follow:
sample {
foo = "default_foo"
bar = "default_bar"
}

environments {
development {
sample {
foo = "dev_foo"
}
}
test {
sample {
bar = "test_bar"
}
}
}

The loading of the properties:
def config = new ConfigSlurper("development").parse(new File('Sample.groovy').toURL())

assert config.sample.foo == "dev_foo"
assert config.sample.bar == "default_bar"

config = new ConfigSlurper("test").parse(new File('Sample.groovy').toURL())

assert config.sample.foo == "default_foo"
assert config.sample.bar == "test_bar"

6. How to Config Spring beans
spring configuration in my project
PropertyPlaceholderConfigurer implementation class from spring

<bean id="dataSource" destroy-method="close" 
  class="org.apache.commons.dbcp.BasicDataSource"> 
  <property name="driverClassName" value="${jdbc.driverClassName}"/> 
  <property name="url" value="${jdbc.url}"/> 
  <property name="username" value="${jdbc.username}"/> 
  <property name="password" value="${jdbc.password}"/> 
</bean> 

jdbc.driverClassName=org.hsqldb.jdbcDriver  
jdbc.url=jdbc:hsqldb:hsql:production:9002 
jdbc.username=sa  
jdbc.password=root

ConfigSluper configuration is as follow:
def normalize(s){  
return s.toUpperCase()  
}   
 
jdbc{  
  driverClassName=org.hsqldb.jdbcDriver  
  url=jdbc:hsqldb:hsql:production:9002 
  username=normalize('sa')  
  password=normalize('root')  


And we need to implements a new class named GroovyPlaceholderConfigurer
package com.sillycat.easywebflow.core;

import groovy.util.ConfigObject;
import groovy.util.ConfigSlurper;

import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;

public class GroovyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

private Resource[] locations;

protected void loadProperties(Properties props) throws IOException {
ConfigObject configObject = new ConfigObject();
ConfigSlurper configSlurper = new ConfigSlurper();
for (Resource location : locations) {
configObject.merge(configSlurper.parse(location.getURL()));
}
props.putAll(configObject.toProperties());
}

public void setLocations(Resource[] locations) {
this.locations = locations;
}

public void setLocation(Resource location) {
this.locations = new Resource[] { location };
}

}

<bean class="com.sillycat.easywebflow.core.GroovyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" /> 
  <property name="locations"> 
    <list> 
      <value>classpath:config.groovy</value> 
      <value>classpath:config.properties</value> 
      <value>classpath:config2.groovy</value> 
    </list> 
  </property> 
</bean> 

The latest config2.groovy will overwrite the config.groovy/config.properties from my testing during merging process. But we need to change the style of config file content as follow:
//################################################
//# velocity path
//################################################
velocity.file.path="file://d:/work/easy/easywebflow"

references:
http://groovy.codehaus.org/ConfigSlurper
http://jroller.com/0xcafebabe/entry/using_groovy_configslurper_to_configure



猜你喜欢

转载自sillycat.iteye.com/blog/1567542
今日推荐