001/**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ken.util;
017
018import java.io.IOException;
019import java.util.Properties;
020
021import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
022import org.springframework.core.io.FileSystemResourceLoader;
023import org.springframework.core.io.Resource;
024import org.springframework.util.DefaultPropertiesPersister;
025
026/**
027 * PropertyPlaceholderConfigurer subclass that overrides any statically defined properties and properties locations,
028 * with configuration settings from a properties file specified in a System property.
029 * @see PropertyPlaceholderConfigurer
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class NotificationPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
033    private static final String CFG_LOCATION_PROPERTY = "notification.config";
034
035    /**
036     * @see org.springframework.core.io.support.PropertiesLoaderSupport#mergeProperties()
037     */
038    @Override
039    protected Properties mergeProperties() throws IOException {
040        Properties properties = super.mergeProperties();
041        return transformProperties(properties);
042    }
043
044    /**
045     * This method consolidates config properties.
046     * @param properties
047     * @return Properties
048     * @throws IOException
049     */
050    protected Properties transformProperties(Properties properties) throws IOException {
051        String cfgLocation = System.getProperty(CFG_LOCATION_PROPERTY);
052
053        if (cfgLocation != null) {
054            Resource resource = new FileSystemResourceLoader().getResource(cfgLocation);
055            if (resource != null && resource.exists()) {
056                new DefaultPropertiesPersister().load(properties, resource.getInputStream());
057            }
058        }
059        
060        return properties;
061    }
062
063    /* I would have liked to just dynamically add a new Resource to the list of resource locations and
064     * delegate entirely to the superclass hierarchy for loading, but unfortunately the locations member
065     * is inaccessible, so we have to load the properties into the existing properties object ourselves,
066     * in the process overridding any existing entries
067     */
068//    public void afterPropertiesSet() throws Exception {
069//        String cfg = System.getProperty(CFG_LOCATION_PROPERTY);
070//        if (cfg == null) return;
071//        
072//        Resource resource = new FileSystemResourceLoader().getResource(cfg);
073//        // add resource to locations
074//    }
075}