org.psikeds.common.config.PropertiesConfigurer.java Source code

Java tutorial

Introduction

Here is the source code for org.psikeds.common.config.PropertiesConfigurer.java

Source

/*******************************************************************************
 * psiKeds :- ps induced knowledge entity delivery system
 *
 * Copyright (c) 2013 Karsten Reincke, Marco Juliano, Deutsche Telekom AG
 *
 * This file is free software: you can redistribute
 * it and/or modify it under the terms of the
 * [x] GNU Affero General Public License
 * [ ] GNU General Public License
 * [ ] GNU Lesser General Public License
 * [ ] Creatice Commons ShareAlike License
 *
 * For details see file LICENSING in the top project directory
 *******************************************************************************/
package org.psikeds.common.config;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;

/**
 * Extension of
 * org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
 * You can specified relative filenames for properties files, that will be
 * resolved within the config directory.
 * 
 * @author marco@juliano.de
 */
public class PropertiesConfigurer extends PropertyPlaceholderConfigurer {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesConfigurer.class);

    public static final String PROPERTIES_FILE_ENCODING = "UTF-8";
    public static final boolean IGNORE_MISSING_RESOURCES_AND_PLACEHOLDER = false;
    public static final int DEFAULT_SYSTEM_PROPERTIES_MODE = PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_FALLBACK;

    public PropertiesConfigurer() {
        this(DEFAULT_SYSTEM_PROPERTIES_MODE, IGNORE_MISSING_RESOURCES_AND_PLACEHOLDER, PROPERTIES_FILE_ENCODING);
    }

    public PropertiesConfigurer(final int systemPropertiesMode, final boolean ignoreMissingResources,
            final String encoding) {
        super();
        setSystemPropertiesMode(systemPropertiesMode);
        setIgnoreResourceNotFound(ignoreMissingResources);
        setIgnoreUnresolvablePlaceholders(ignoreMissingResources);
        setFileEncoding(encoding);
        if (LOGGER.isDebugEnabled()) {
            final StringBuilder sb = new StringBuilder("Config: System-Properties-Mode = {}\n");
            sb.append("Ignore missing Resources = {}\n");
            sb.append("File-Encoding = {}");
            LOGGER.debug(sb.toString(), systemPropertiesMode, ignoreMissingResources, encoding);
        }
    }

    /**
     * @see org.springframework.core.io.support.PropertiesLoaderSupport#setLocation
     *      (org.springframework.core.io.Resource)
     */
    @Override
    public void setLocation(final Resource location) {
        this.setLocations(new Resource[] { location });
    }

    /**
     * @see org.springframework.core.io.support.PropertiesLoaderSupport#setLocations
     *      (org.springframework.core.io.Resource[])
     */
    @Override
    public void setLocations(final Resource[] locations) {
        final int len = locations == null ? 0 : locations.length;
        for (int idx = 0; idx < len; idx++) {
            final Resource loc = locations[idx];
            FileSystemResource fsr = null;
            // We are only looking for files or URLs pointing to local files!
            if (loc instanceof org.springframework.core.io.FileSystemResource) {
                fsr = new FileSystemResource((org.springframework.core.io.FileSystemResource) loc);
            } else if (loc instanceof org.springframework.core.io.UrlResource) {
                try {
                    fsr = new FileSystemResource((org.springframework.core.io.UrlResource) loc);
                } catch (final IOException ioex) {
                    LOGGER.debug("Failed to resolve config file.", ioex);
                    fsr = null;
                }
            } else if (loc instanceof FileSystemResource) {
                // just for logging/debugging reasons
                fsr = (FileSystemResource) loc;
            }
            if (fsr != null) {
                locations[idx] = fsr;
                LOGGER.debug("Resolved config file to {}", fsr);
            }
            // Else: Do not touch Resources that we could not handle.
            // Whatever it is, the Spring Framework hopefully knows
            // what to do.
        }
        super.setLocations(locations);
    }
}