de.kaiserpfalzEdv.commons.jee.spring.CommonsConfigurationConfigurer.java Source code

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.commons.jee.spring.CommonsConfigurationConfigurer.java

Source

/*
 * Copyright 2015 Kaiserpfalz EDV-Service Roland Lichti
 *
 * 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 de.kaiserpfalzEdv.commons.jee.spring;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationConverter;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
 * A PropertiesPlaceholder
 *
 * @author fjm
 * @since 2012Q1
 */
public class CommonsConfigurationConfigurer extends PropertyPlaceholderConfigurer {
    private static final Logger log = LoggerFactory.getLogger(CommonsConfigurationConfigurer.class);

    private Configuration configuration;
    private Resource[] locations;

    public CommonsConfigurationConfigurer() {
        super();
    }

    /**
     * Set a location of the configuration file to be loaded.
     */
    public void setLocation(Resource location) {
        this.locations = new Resource[] { location };
    }

    /**
     * Apply the given Properties to the given BeanFactory.
     *
     * @param beanFactory the BeanFactory used by the application context
     * @param props       the Properties to apply
     * @throws org.springframework.beans.BeansException
     *          in case of errors
     */
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {

        Properties commonsProperties = ConfigurationConverter.getProperties(configuration);
        log.debug("got properties " + commonsProperties);
        super.processProperties(beanFactory, commonsProperties);
    }

    protected void loadProperties(Properties props) throws IOException {
        if (this.locations != null) {
            for (Resource location : this.locations) {
                log.info("Loading properties file from {}", location);

                String filename = getFileNameForResource(location);

                if (filename != null) {
                    try {
                        loadProperties(props, filename);
                    } catch (ConfigurationException e) {
                        log.warn("Could not load properties from " + location, e);
                    }
                } else {
                    log.warn("Could not load properties from {}", location);
                }
            }
        }
    }

    private void loadProperties(Properties props, String filename) throws ConfigurationException {
        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(filename);
        configuration = builder.getConfiguration();

        Properties commonsProperties = ConfigurationConverter.getProperties(configuration);

        log.debug("got properties " + commonsProperties);
        for (Map.Entry<Object, Object> entry : commonsProperties.entrySet()) {
            props.setProperty((String) entry.getKey(), (String) entry.getValue());
        }
    }

    private String getFileNameForResource(Resource location) {
        String filename = null;
        try {
            filename = location.getFilename();
        } catch (IllegalStateException ex) {
            // resource is not file-based.
        }
        return filename;
    }
}