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.env;
17  
18  import java.io.File;
19  
20  import org.kuali.common.util.Assert;
21  import org.kuali.common.util.Mode;
22  import org.kuali.common.util.ModeUtils;
23  import org.springframework.core.env.Environment;
24  
25  /**
26   * <p>
27   * By default, an exception is thrown if a value cannot be located (unless a default value has been supplied).
28   * </p>
29   * 
30   * <p>
31   * By default, an exception is thrown if any placeholders cannot be resolved in any string values.
32   * </p>
33   * 
34   * <p>
35   * By default, environment variables are automatically checked if a normal property value cannot be found.
36   * 
37   * For example, given the key <code>db.vendor</code> the service will also automatically check <code>env.DB_VENDOR</code>
38   * </p>
39   * 
40   * @deprecated Use BasicEnvironmentService instead
41   */
42  @Deprecated
43  public class DefaultEnvironmentService implements EnvironmentService {
44  
45  	public static final boolean DEFAULT_CHECK_ENVIRONMENT_VARIABLES = true;
46  	public static final boolean DEFAULT_RESOLVE_STRINGS = true;
47  	public static final Mode DEFAULT_MISSING_PROPERTY_MODE = Mode.ERROR;
48  	public static final String ENV_PREFIX = "env";
49  
50  	private final boolean checkEnvironmentVariables;
51  	private final boolean resolveStrings;
52  	private final Environment env;
53  	private final Mode missingPropertyMode;
54  
55  	public DefaultEnvironmentService(Environment env) {
56  		this(env, DEFAULT_CHECK_ENVIRONMENT_VARIABLES, DEFAULT_RESOLVE_STRINGS, DEFAULT_MISSING_PROPERTY_MODE);
57  	}
58  
59  	public DefaultEnvironmentService(Environment env, boolean checkEnvironmentVariables) {
60  		this(env, checkEnvironmentVariables, DEFAULT_RESOLVE_STRINGS, DEFAULT_MISSING_PROPERTY_MODE);
61  	}
62  
63  	public DefaultEnvironmentService(Environment env, boolean checkEnvironmentVariables, boolean resolveStrings, Mode missingPropertyMode) {
64  		Assert.noNulls(env, missingPropertyMode);
65  		this.env = env;
66  		this.missingPropertyMode = missingPropertyMode;
67  		this.checkEnvironmentVariables = checkEnvironmentVariables;
68  		this.resolveStrings = resolveStrings;
69  	}
70  
71  	@Override
72  	public boolean containsProperty(String key) {
73  		Assert.noBlanks(key);
74  		return env.containsProperty(key);
75  	}
76  
77  	@Override
78  	public <T> T getProperty(EnvContext<T> context) {
79  
80  		// If context is null, we have issues
81  		Assert.noNulls(context);
82  
83  		// Extract a value from Spring's Environment abstraction
84  		T springValue = getSpringValue(context.getKey(), context.getType());
85  
86  		// If that value is null, use whatever default value they gave us (this might also be null)
87  		T returnValue = (springValue == null) ? context.getDefaultValue() : springValue;
88  
89  		// If we could not locate a value, we may need to error out
90  		if (returnValue == null) {
91  			ModeUtils.validate(missingPropertyMode, getMissingPropertyMessage(context.getKey()));
92  		}
93  
94  		// Return the value we've located
95  		return returnValue;
96  	}
97  
98  	protected String getMissingPropertyMessage(String key) {
99  		if (checkEnvironmentVariables) {
100 			String envKey = EnvUtils.getEnvironmentVariableKey(key);
101 			return "No value for [" + key + "] or [" + envKey + "]";
102 		} else {
103 			return "No value for [" + key + "]";
104 		}
105 	}
106 
107 	protected <T> T getSpringValue(String key, Class<T> type) {
108 		T value = env.getProperty(key, type);
109 		if (value == null && checkEnvironmentVariables) {
110 			String envKey = EnvUtils.getEnvironmentVariableKey(key);
111 			return env.getProperty(envKey, type);
112 		} else {
113 			return value;
114 		}
115 	}
116 
117 	protected <T> Class<T> getSpringValueAsClass(String key, Class<T> type) {
118 		Class<T> value = env.getPropertyAsClass(key, type);
119 		if (value == null && checkEnvironmentVariables) {
120 			String envKey = EnvUtils.getEnvironmentVariableKey(key);
121 			return env.getPropertyAsClass(envKey, type);
122 		} else {
123 			return value;
124 		}
125 	}
126 
127 	@Override
128 	public <T> Class<T> getClass(String key, Class<T> type) {
129 		return getClass(key, type, null);
130 	}
131 
132 	@Override
133 	public <T> Class<T> getClass(String key, Class<T> type, Class<T> defaultValue) {
134 		Class<T> springValue = getSpringValueAsClass(key, type);
135 		Class<T> returnValue = (springValue == null) ? defaultValue : springValue;
136 
137 		// If we could not locate a value, we may need to error out
138 		if (returnValue == null) {
139 			ModeUtils.validate(missingPropertyMode, getMissingPropertyMessage(key));
140 		}
141 
142 		// Return what we've got
143 		return returnValue;
144 	}
145 
146 	@Override
147 	public String getString(String key) {
148 		return getString(key, null);
149 	}
150 
151 	@Override
152 	public String getString(String key, String defaultValue) {
153 		String string = getProperty(EnvContext.newString(key, defaultValue));
154 		if (resolveStrings) {
155 			return env.resolveRequiredPlaceholders(string);
156 		} else {
157 			return string;
158 		}
159 	}
160 
161 	@Override
162 	public Boolean getBoolean(String key) {
163 		return getBoolean(key, null);
164 	}
165 
166 	@Override
167 	public Boolean getBoolean(String key, Boolean defaultValue) {
168 		return getProperty(EnvContext.newBoolean(key, defaultValue));
169 	}
170 
171 	@Override
172 	public File getFile(String key) {
173 		return getFile(key, null);
174 	}
175 
176 	@Override
177 	public File getFile(String key, File defaultValue) {
178 		return getProperty(EnvContext.newFile(key, defaultValue));
179 	}
180 
181 	@Override
182 	public Integer getInteger(String key, Integer defaultValue) {
183 		return getProperty(EnvContext.newInteger(key, defaultValue));
184 	}
185 
186 	@Override
187 	public Integer getInteger(String key) {
188 		return getInteger(key, null);
189 	}
190 
191 	public boolean isCheckEnvironmentVariables() {
192 		return checkEnvironmentVariables;
193 	}
194 
195 	public boolean isResolveStrings() {
196 		return resolveStrings;
197 	}
198 
199 	public Environment getEnv() {
200 		return env;
201 	}
202 
203 	public Mode getMissingPropertyMode() {
204 		return missingPropertyMode;
205 	}
206 
207 	@Override
208 	public <T> T getProperty(String key, Class<T> type, T provided) {
209 		return getProperty(EnvContext.newCtx(key, type, provided));
210 	}
211 
212 	@Override
213 	public <T> T getProperty(String key, Class<T> type) {
214 		return getProperty(EnvContext.newCtx(key, type, null));
215 	}
216 
217 }