/* * Copyright 2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.beans.factory.config; 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; /** * Subclass of PropertyPlaceholderConfigurer that resolves placeholders from * a properties object loaded using a Groovy's ConfigSlurper. * * ConfigSlurperPlaceholderConfigurer also supports the concept of per environment * configuration via "environment" and "defaultEnvironment" properties. * The "environment" property will be typically set using a system property. * * Example XML context definition: * *
<bean class="org.springframework.beans.factory.config.ConfigSlurperPlaceholderConfigurer">
 *     <property name="environment" value="#{systemProperties['runtime.environment']}" />
 *     <property name="defaultEnvironment" value="production" />
 *     <property name="location" value="/WEB-INF/config.groovy" />
 * </bean>
 *
 * <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 *     <property name="driverClassName"><value>${dataSource.driverClassName}</value></property>
 *     <property name="url"><value>${dataSource.url}</value></property>
 *     <property name="username"><value>${dataSource.username}</value></property>
 *     <property name="password"><value>${dataSource.password}</value></property>
 * </bean>
* * Example config.groovy: * *
dataSource {
 *     driverClassName = "org.hsqldb.jdbcDriver"    
 *     username = "sa"
 *     password = ""                
 * }
 * environments {
 *     development {
 *         dataSource {
 *             url = "jdbc:hsqldb:mem:devDB"
 *         }
 *     }   
 *     test {
 *         dataSource {
 *             url = "jdbc:hsqldb:mem:testDb"
 *         }
 *     }   
 *     production {
 *         dataSource {
 *             url = "jdbc:hsqldb:file:prodDb;shutdown=true"
 *             password = "secret"
 *         }
 *     }
 * }
* * @author Marcel Overdijk * @see #setEnvironment * @see #setDefaultEnvironment * @see #setLocations * @see #setProperties * @see #setSystemPropertiesModeName * @see groovy.util.ConfigObject * @see groovy.util.ConfigSlurper */ public class ConfigSlurperPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private String environment; private String defaultEnvironment; private Resource[] locations; public void setEnvironment(String environment) { this.environment = environment; } public void setDefaultEnvironment(String defaultEnvironment) { this.defaultEnvironment = defaultEnvironment; } public void setLocation(Resource location) { this.locations = new Resource[] { location }; } public void setLocations(Resource[] locations) { this.locations = locations; } /** * Load properties into the given instance. * @param props the Properties instance to load into * @throws java.io.IOException in case of I/O errors * @see #setLocations */ @Override protected void loadProperties(Properties props) throws IOException { ConfigObject configObject = new ConfigObject(); ConfigSlurper configSlurper = new ConfigSlurper(getEnvironment()); for (Resource location : locations) { configObject.merge(configSlurper.parse(location.getURL())); } props.putAll(configObject.toProperties()); } private String getEnvironment() { if (this.environment == null || this.environment.trim().length() == 0) { return this.defaultEnvironment; } else { return this.environment; } } }