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