001/**
002 * Copyright 2010-2014 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.common.util.spring.service;
017
018import java.util.Arrays;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.commons.lang3.StringUtils;
023import org.kuali.common.util.Assert;
024import org.kuali.common.util.CollectionUtils;
025import org.kuali.common.util.spring.PropertySourceUtils;
026import org.springframework.context.ConfigurableApplicationContext;
027import org.springframework.core.env.PropertySource;
028
029public final class DefaultPropertySourceService implements PropertySourceService {
030
031        public DefaultPropertySourceService(SpringService springService) {
032                Assert.noNulls(springService);
033                this.springService = springService;
034        }
035
036        private final SpringService springService;
037
038        @Override
039        public PropertySource<?> getPropertySource(Class<? extends PropertySourceConfig> config) {
040                return getPropertySource(null, null, null, config);
041        }
042
043        @Override
044        public PropertySource<?> getPropertySource(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<? extends PropertySourceConfig> config) {
045                return getPropertySource(beans, defaultProfiles, activeProfiles, config, null);
046        }
047
048        @Override
049        public List<PropertySource<?>> getPropertySources(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<?> config) {
050                return getPropertySources(beans, defaultProfiles, activeProfiles, config, null);
051        }
052
053        @Override
054        public List<PropertySource<?>> getPropertySources(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, String location) {
055                return getPropertySources(beans, defaultProfiles, activeProfiles, null, location);
056        }
057
058        protected PropertySource<?> getPropertySource(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<?> config, String location) {
059                List<PropertySource<?>> sources = getPropertySources(beans, defaultProfiles, activeProfiles, config, location);
060                Assert.isTrue(sources.size() == 1, "sizes != 1");
061                return sources.get(0);
062        }
063
064        protected List<PropertySource<?>> getPropertySources(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<?> config, String location) {
065
066                Assert.notNull(springService, "springService is null");
067
068                SpringContext context = new SpringContext();
069                context.setContextBeans(beans);
070                if (!StringUtils.isBlank(location)) {
071                        context.setLocations(Arrays.asList(location));
072                } else if (config != null) {
073                        context.setAnnotatedClasses(CollectionUtils.asList(config));
074                } else {
075                        throw new IllegalArgumentException("Must supply either a location or annotated config");
076                }
077                context.setActiveProfiles(activeProfiles);
078                context.setDefaultProfiles(defaultProfiles);
079                ConfigurableApplicationContext ctx = springService.getApplicationContext(context);
080                ctx.refresh();
081                return PropertySourceUtils.getPropertySources(ctx);
082        }
083
084        public SpringService getSpringService() {
085                return springService;
086        }
087
088}