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;
017
018import java.util.Properties;
019
020import org.springframework.beans.factory.FactoryBean;
021import org.springframework.core.env.ConfigurableEnvironment;
022import org.springframework.core.env.PropertiesPropertySource;
023import org.springframework.core.env.PropertySource;
024
025import com.google.common.base.Preconditions;
026
027public class EnvironmentPropertySourceFactoryBean implements FactoryBean<PropertySource<?>> {
028
029        ConfigurableEnvironment env;
030        boolean quietly = false;
031
032        @Override
033        public PropertySource<?> getObject() {
034                Preconditions.checkNotNull(env, "'env' cannot be null");
035                Properties source = getProperties();
036                return new PropertiesPropertySource("environmentProperties", source);
037        }
038
039        protected Properties getProperties() {
040                if (quietly) {
041                        return PropertySourceUtils.getAllEnumerablePropertiesQuietly(env);
042                } else {
043                        return PropertySourceUtils.getAllEnumerableProperties(env);
044                }
045        }
046
047        @Override
048        public Class<PropertySource<?>> getObjectType() {
049                return null;
050        }
051
052        @Override
053        public boolean isSingleton() {
054                return false;
055        }
056
057        public ConfigurableEnvironment getEnv() {
058                return env;
059        }
060
061        public void setEnv(ConfigurableEnvironment env) {
062                this.env = env;
063        }
064
065        public boolean isQuietly() {
066                return quietly;
067        }
068
069        public void setQuietly(boolean quietly) {
070                this.quietly = quietly;
071        }
072}