/* * Copyright 2008 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.constretto.spring; import org.constretto.ConstrettoConfiguration; import org.constretto.internal.converter.ValueConverterRegistry; import org.constretto.spring.internal.converter.SpringResourceValueConverter; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.Resource; import java.util.Properties; /** * An adaption of the spring framework {@link PropertyPlaceholderConfigurer} class, using an implementation of the * {@link ConstrettoConfiguration} interface to resolve keys * <p/> * <h2>Example :</h2> * <p/> * <pre> * <bean class="org.constretto.spring.ConstrettoPropertyPlaceholderConfigurer"> * <constructor-arg ref="someProvider" /> * </bean> * * <bean id="myBean" class="com.example.MyClass"> * <property name="myProperty" value="${propertyKey}" /> * </bean> * </pre> * <p/> * Note : The provider property of the placeholder configurer are marked as @Autowired, and can be autowired in a * annotation config application context. * * @author <a href="mailto:kaare.nilsen@gmail.com">Kaare Nilsen</a> * @see ConstrettoConfiguration * @see PropertyPlaceholderConfigurer */ public class ConstrettoPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private ConstrettoConfiguration configuration; private boolean ignoreUnresolvedPlaceHolders; public ConstrettoPropertyPlaceholderConfigurer(ConstrettoConfiguration configuration) { this.configuration = configuration; ValueConverterRegistry.registerCustomConverter(Resource.class, new SpringResourceValueConverter()); } @Override public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) { this.ignoreUnresolvedPlaceHolders = ignoreUnresolvablePlaceholders; super.setIgnoreUnresolvablePlaceholders(ignoreUnresolvablePlaceholders); } @Override protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) { String value = null; if (ignoreUnresolvedPlaceHolders) { value = configuration.evaluateTo(placeholder, ""); } else if(placeholder.contains(valueSeparator)) { final String placeholderValuePart = placeholder.substring(0,placeholder.indexOf(valueSeparator)); final String defaultValuePart = placeholder.substring(placeholder.indexOf(valueSeparator)+1,placeholder.length()); value = configuration.evaluateTo(placeholderValuePart,defaultValuePart); } else { value = configuration.evaluateTo(String.class, placeholder); } return null != value ? value : null; } }