View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.spring;
17  
18  import java.util.Properties;
19  
20  import org.springframework.beans.factory.FactoryBean;
21  import org.springframework.core.env.ConfigurableEnvironment;
22  import org.springframework.core.env.PropertiesPropertySource;
23  import org.springframework.core.env.PropertySource;
24  
25  import com.google.common.base.Preconditions;
26  
27  public class EnvironmentPropertySourceFactoryBean implements FactoryBean<PropertySource<?>> {
28  
29  	ConfigurableEnvironment env;
30  	boolean quietly = false;
31  
32  	@Override
33  	public PropertySource<?> getObject() {
34  		Preconditions.checkNotNull(env, "'env' cannot be null");
35  		Properties source = getProperties();
36  		return new PropertiesPropertySource("environmentProperties", source);
37  	}
38  
39  	protected Properties getProperties() {
40  		if (quietly) {
41  			return PropertySourceUtils.getAllEnumerablePropertiesQuietly(env);
42  		} else {
43  			return PropertySourceUtils.getAllEnumerableProperties(env);
44  		}
45  	}
46  
47  	@Override
48  	public Class<PropertySource<?>> getObjectType() {
49  		return null;
50  	}
51  
52  	@Override
53  	public boolean isSingleton() {
54  		return false;
55  	}
56  
57  	public ConfigurableEnvironment getEnv() {
58  		return env;
59  	}
60  
61  	public void setEnv(ConfigurableEnvironment env) {
62  		this.env = env;
63  	}
64  
65  	public boolean isQuietly() {
66  		return quietly;
67  	}
68  
69  	public void setQuietly(boolean quietly) {
70  		this.quietly = quietly;
71  	}
72  }