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.env; 017 018import java.io.File; 019 020import org.kuali.common.util.Assert; 021import org.kuali.common.util.Mode; 022import org.kuali.common.util.ModeUtils; 023import org.springframework.core.env.Environment; 024 025/** 026 * <p> 027 * By default, an exception is thrown if a value cannot be located (unless a default value has been supplied). 028 * </p> 029 * 030 * <p> 031 * By default, an exception is thrown if any placeholders cannot be resolved in any string values. 032 * </p> 033 * 034 * <p> 035 * By default, environment variables are automatically checked if a normal property value cannot be found. 036 * 037 * For example, given the key <code>db.vendor</code> the service will also automatically check <code>env.DB_VENDOR</code> 038 * </p> 039 * 040 * @deprecated Use BasicEnvironmentService instead 041 */ 042@Deprecated 043public class DefaultEnvironmentService implements EnvironmentService { 044 045 public static final boolean DEFAULT_CHECK_ENVIRONMENT_VARIABLES = true; 046 public static final boolean DEFAULT_RESOLVE_STRINGS = true; 047 public static final Mode DEFAULT_MISSING_PROPERTY_MODE = Mode.ERROR; 048 public static final String ENV_PREFIX = "env"; 049 050 private final boolean checkEnvironmentVariables; 051 private final boolean resolveStrings; 052 private final Environment env; 053 private final Mode missingPropertyMode; 054 055 public DefaultEnvironmentService(Environment env) { 056 this(env, DEFAULT_CHECK_ENVIRONMENT_VARIABLES, DEFAULT_RESOLVE_STRINGS, DEFAULT_MISSING_PROPERTY_MODE); 057 } 058 059 public DefaultEnvironmentService(Environment env, boolean checkEnvironmentVariables) { 060 this(env, checkEnvironmentVariables, DEFAULT_RESOLVE_STRINGS, DEFAULT_MISSING_PROPERTY_MODE); 061 } 062 063 public DefaultEnvironmentService(Environment env, boolean checkEnvironmentVariables, boolean resolveStrings, Mode missingPropertyMode) { 064 Assert.noNulls(env, missingPropertyMode); 065 this.env = env; 066 this.missingPropertyMode = missingPropertyMode; 067 this.checkEnvironmentVariables = checkEnvironmentVariables; 068 this.resolveStrings = resolveStrings; 069 } 070 071 @Override 072 public boolean containsProperty(String key) { 073 Assert.noBlanks(key); 074 return env.containsProperty(key); 075 } 076 077 @Override 078 public <T> T getProperty(EnvContext<T> context) { 079 080 // If context is null, we have issues 081 Assert.noNulls(context); 082 083 // Extract a value from Spring's Environment abstraction 084 T springValue = getSpringValue(context.getKey(), context.getType()); 085 086 // If that value is null, use whatever default value they gave us (this might also be null) 087 T returnValue = (springValue == null) ? context.getDefaultValue() : springValue; 088 089 // If we could not locate a value, we may need to error out 090 if (returnValue == null) { 091 ModeUtils.validate(missingPropertyMode, getMissingPropertyMessage(context.getKey())); 092 } 093 094 // Return the value we've located 095 return returnValue; 096 } 097 098 protected String getMissingPropertyMessage(String key) { 099 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}