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