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