/* Copyright 2006 - 2009 Under Dusken 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.pegadi.webapp.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.Resource; import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.support.ServletContextResourceLoader; import org.simplericity.datadirlocator.spring.DataDirectoryPropertyReplacer; import javax.servlet.ServletContext; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; /** * Created by IntelliJ IDEA. * User: bjorsnos * Date: Oct 30, 2005 * Time: 9:42:21 PM * To change this template use File | Settings | File Templates. */ public class PropertyReplacer implements BeanFactoryPostProcessor, ServletContextAware { private ServletContext servletContext; public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { ServletContextResourceLoader loader = new ServletContextResourceLoader(servletContext); Properties properties = new Properties(); File file = (File) servletContext.getAttribute(DataDirectoryPropertyReplacer.SERVLET_CONTEXT_ATTR); properties.setProperty("dataDir",file.getAbsolutePath()); List<Resource> resources = new ArrayList<Resource>(); resources.add(loader.getResource("/WEB-INF/pegadi.conf")); /* Check wether dataDir/pegadi.conf exist. If it does add it as a location * Also add /WEB-INF/pegadi.conf, so that if the file doesn't exist use default values */ File configFile = new File(file.getAbsolutePath(), "pegadi.conf"); if(configFile.exists()){ resources.add(loader.getResource("file:" + configFile.getAbsolutePath())); } PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); configurer.setLocations( resources.toArray(new Resource[]{})); configurer.setProperties(properties); configurer.postProcessBeanFactory(configurableListableBeanFactory); } public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } }