001/**
002 * Copyright 2005-2015 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.config;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Properties;
021
022import javax.servlet.ServletContext;
023
024import org.kuali.common.jdbc.project.spring.JdbcPropertyLocationsConfig;
025import org.kuali.common.util.log.LoggerUtils;
026import org.kuali.common.util.properties.Location;
027import org.kuali.common.util.properties.PropertiesService;
028import org.kuali.common.util.properties.spring.DefaultPropertiesServiceConfig;
029import org.kuali.common.util.spring.service.PropertySourceConfig;
030import org.kuali.rice.core.api.config.property.Config;
031import org.kuali.rice.core.api.config.property.ConfigContext;
032import org.kuali.rice.core.api.config.property.ConfigPropertySource;
033import org.kuali.rice.sql.spring.SourceSqlPropertyLocationsConfig;
034import org.kuali.rice.xml.ingest.RiceConfigUtils;
035import org.kuali.rice.xml.spring.IngestXmlPropertyLocationsConfig;
036import org.slf4j.Logger;
037import org.springframework.beans.factory.annotation.Autowired;
038import org.springframework.context.annotation.Bean;
039import org.springframework.context.annotation.Configuration;
040import org.springframework.context.annotation.Import;
041import org.springframework.core.env.PropertySource;
042
043/**
044 * Holds the property source for all of the different properties needed for starting up the KRAD
045 * Sample App.
046 * 
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 */
049@Configuration
050@Import({SampleAppProjectConfig.class, JdbcPropertyLocationsConfig.class, DefaultPropertiesServiceConfig.class,
051        SourceSqlPropertyLocationsConfig.class, IngestXmlPropertyLocationsConfig.class})
052public class SampleAppPSC implements PropertySourceConfig {
053
054    private static final String KR_SAMPLE_APP_CONFIG = "classpath:META-INF/sample-app-config.xml";
055
056    private static final Logger logger = LoggerUtils.make();
057
058    @Autowired
059    JdbcPropertyLocationsConfig jdbcConfig;
060
061    @Autowired
062    SourceSqlPropertyLocationsConfig sourceSqlConfig;
063
064    @Autowired
065    IngestXmlPropertyLocationsConfig ingestXmlConfig;
066
067    @Autowired
068    PropertiesService service;
069
070    @Autowired
071    ServletContext servletContext;
072
073    @Override
074    @Bean
075    public PropertySource<?> propertySource() {
076        // Combine locations making sure Rice properties go in last
077        List<Location> locations = new ArrayList<Location>();
078        locations.addAll(jdbcConfig.jdbcPropertyLocations());
079        locations.addAll(sourceSqlConfig.riceSourceSqlPropertyLocations());
080        locations.addAll(ingestXmlConfig.riceIngestXmlPropertyLocations());
081
082        // Default behavior is load->decrypt->resolve
083        // -Dproperties.resolve=false turns off placeholder resolution
084        Properties properties = service.getProperties(locations);
085        logger.info("Loaded {} regular properties", properties.size());
086
087        // Combine normal properties with Rice properties using Rice's custom placeholder resolution logic to resolve everything
088        Config rootCfg = RiceConfigUtils.getRootConfig(properties, KR_SAMPLE_APP_CONFIG, servletContext);
089
090        // Make sure ConfigContext.getCurrentContextConfig() return's the rootCfg object
091        ConfigContext.init(rootCfg);
092
093        // Make Spring and Rice use the exact same source for obtaining property values
094        return new ConfigPropertySource("riceConfig", rootCfg);
095    }
096
097}