no.dusken.common.plugin.PluginPropertyPlaceholderConfigurer.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.common.plugin.PluginPropertyPlaceholderConfigurer.java

Source

/*
 Copyright 2006 - 2010 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 no.dusken.common.plugin;

import org.kantega.jexmec.store.PluginStore;
import org.kantega.jexmec.store.PluginStoreProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

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

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 * Reads the configfile resources/{pluginUid}.conf and checks if the pluginstore has
 * other values. If the store has, the plugin uses those instead.
 */
public class PluginPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private PluginStoreProvider pluginStoreProvider;

    private DuskenPlugin plugin;

    private Properties exposedProperties;

    public PluginPropertyPlaceholderConfigurer(DuskenPlugin plugin) {
        super();
        this.setLocation(new ClassPathResource(plugin.getPluginUid().concat(".conf")));
        this.plugin = plugin;
    }

    /**
     * Return a merged Properties instance containing both the
     * loaded properties and properties set on this FactoryBean.
     */
    @Override
    protected Properties mergeProperties() throws IOException {
        PluginStore store = pluginStoreProvider.getStore(plugin);
        Properties properties = super.mergeProperties();
        for (String s : properties.stringPropertyNames()) {
            String value = store.getString(s, null);
            if (value == null) {
                store.setString(s, properties.getProperty(s));
            } else {
                properties.setProperty(s, value);
            }
        }
        if (exposedProperties != null) {
            properties.putAll(exposedProperties);
        }
        return properties;
    }

    @Autowired
    public void setPluginStoreProvider(PluginStoreProvider pluginStoreProvider) {
        this.pluginStoreProvider = pluginStoreProvider;
    }

    public void setExposedProperties(Properties exposedProperties) {
        this.exposedProperties = exposedProperties;
    }
}