| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.krad.service.impl; |
| 18 | |
|
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.apache.commons.logging.Log; |
| 21 | |
import org.apache.commons.logging.LogFactory; |
| 22 | |
import org.apache.log4j.Logger; |
| 23 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 24 | |
import org.kuali.rice.core.api.config.property.ConfigurationService; |
| 25 | |
import org.kuali.rice.core.api.util.Truth; |
| 26 | |
import org.kuali.rice.kns.web.struts.action.KualiPropertyMessageResources; |
| 27 | |
import org.kuali.rice.kns.web.struts.action.KualiPropertyMessageResourcesFactory; |
| 28 | |
import org.kuali.rice.krad.exception.DuplicateKeyException; |
| 29 | |
import org.kuali.rice.krad.exception.PropertiesException; |
| 30 | |
|
| 31 | |
import java.io.IOException; |
| 32 | |
import java.io.InputStream; |
| 33 | |
import java.net.URL; |
| 34 | |
import java.util.Collections; |
| 35 | |
import java.util.Iterator; |
| 36 | |
import java.util.Map; |
| 37 | |
import java.util.Properties; |
| 38 | |
|
| 39 | |
public class ConfigurationServiceImpl implements ConfigurationService { |
| 40 | 0 | private final PropertyHolder propertyHolder = new PropertyHolder(); |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public ConfigurationServiceImpl() { |
| 46 | 0 | this.propertyHolder.getHeldProperties().putAll(ConfigContext.getCurrentContextConfig().getProperties()); |
| 47 | |
|
| 48 | 0 | KualiPropertyMessageResourcesFactory propertyMessageFactory = new KualiPropertyMessageResourcesFactory(); |
| 49 | |
|
| 50 | |
|
| 51 | 0 | KualiPropertyMessageResources messageResources = |
| 52 | |
(KualiPropertyMessageResources) propertyMessageFactory.createResources(""); |
| 53 | |
|
| 54 | |
|
| 55 | 0 | this.propertyHolder.getHeldProperties().putAll(messageResources.getKualiProperties(null)); |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
@Override |
| 62 | |
public String getPropertyValueAsString(String key) { |
| 63 | 0 | if (key == null) { |
| 64 | 0 | throw new IllegalArgumentException("invalid (null) key"); |
| 65 | |
} |
| 66 | |
|
| 67 | 0 | return this.propertyHolder.getProperty(key); |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
@Override |
| 74 | |
public boolean getPropertyValueAsBoolean(String key) { |
| 75 | 0 | if (key == null) { |
| 76 | 0 | throw new IllegalArgumentException("invalid (null) key"); |
| 77 | |
} |
| 78 | 0 | String property = this.propertyHolder.getProperty(key); |
| 79 | 0 | Boolean b = Truth.strToBooleanIgnoreCase(property); |
| 80 | 0 | if (b == null) { |
| 81 | 0 | return false; |
| 82 | |
} |
| 83 | 0 | return b; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
@Override |
| 90 | |
public Map<String, String> getAllProperties() { |
| 91 | 0 | return (Map) Collections.unmodifiableMap(propertyHolder.getHeldProperties()); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
static interface PropertySource { |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public Properties loadProperties(); |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
static class PropertyHolder { |
| 113 | 1 | private static Logger LOG = Logger.getLogger(PropertyHolder.class); |
| 114 | |
|
| 115 | |
Properties heldProperties; |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | 24 | public PropertyHolder() { |
| 121 | 24 | this.heldProperties = new Properties(); |
| 122 | 24 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public boolean isEmpty() { |
| 129 | 8 | return this.heldProperties.isEmpty(); |
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
public boolean containsKey(String key) { |
| 138 | 56 | validateKey(key); |
| 139 | |
|
| 140 | 55 | return this.heldProperties.containsKey(key); |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public String getProperty(String key) { |
| 149 | 11 | validateKey(key); |
| 150 | |
|
| 151 | 10 | return this.heldProperties.getProperty(key); |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public void setProperty(String key, String value) { |
| 165 | 37 | setProperty(null, key, value); |
| 166 | 34 | } |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public void setProperty(PropertySource source, String key, String value) { |
| 179 | 37 | validateKey(key); |
| 180 | 36 | validateValue(value); |
| 181 | |
|
| 182 | 35 | if (containsKey(key)) { |
| 183 | 1 | if (source != null && source instanceof FilePropertySource && ((FilePropertySource)source).isAllowOverrides()) { |
| 184 | 0 | LOG.info("Duplicate Key: Override is enabled [key=" + key + ", new value=" + value + ", old value=" + this.heldProperties.getProperty(key) + "]"); |
| 185 | |
} else { |
| 186 | 1 | throw new DuplicateKeyException("duplicate key '" + key + "'"); |
| 187 | |
} |
| 188 | |
} |
| 189 | 34 | this.heldProperties.setProperty(key, value); |
| 190 | 34 | } |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public void clearProperty(String key) { |
| 199 | 3 | validateKey(key); |
| 200 | |
|
| 201 | 2 | this.heldProperties.remove(key); |
| 202 | 2 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
public void loadProperties(PropertySource source) { |
| 214 | 3 | if (source == null) { |
| 215 | 1 | throw new IllegalArgumentException("invalid (null) source"); |
| 216 | |
} |
| 217 | |
|
| 218 | 2 | Properties newProperties = source.loadProperties(); |
| 219 | |
|
| 220 | 0 | for (Iterator i = newProperties.keySet().iterator(); i.hasNext();) { |
| 221 | 0 | String key = (String) i.next(); |
| 222 | 0 | setProperty(source, key, newProperties.getProperty(key)); |
| 223 | 0 | } |
| 224 | 0 | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public void clearProperties() { |
| 230 | 2 | this.heldProperties.clear(); |
| 231 | 2 | } |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public Iterator getKeys() { |
| 238 | 2 | return this.heldProperties.keySet().iterator(); |
| 239 | |
} |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
private void validateKey(String key) { |
| 247 | 107 | if (key == null) { |
| 248 | 4 | throw new IllegalArgumentException("invalid (null) key"); |
| 249 | |
} |
| 250 | 103 | } |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
private void validateValue(String value) { |
| 257 | 36 | if (value == null) { |
| 258 | 1 | throw new IllegalArgumentException("invalid (null) value"); |
| 259 | |
} |
| 260 | 35 | } |
| 261 | |
|
| 262 | |
|
| 263 | |
public Properties getHeldProperties() { |
| 264 | 0 | return heldProperties; |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | |
public void setHeldProperties(Properties heldProperties) { |
| 269 | 0 | this.heldProperties = heldProperties; |
| 270 | 0 | } |
| 271 | |
} |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | 6 | static class FilePropertySource implements PropertySource { |
| 279 | 1 | private static Log log = LogFactory.getLog(FilePropertySource.class); |
| 280 | |
|
| 281 | |
|
| 282 | |
private String fileName; |
| 283 | |
private boolean allowOverrides; |
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
public void setFileName(String fileName) { |
| 291 | 4 | this.fileName = fileName; |
| 292 | 4 | } |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
public String getFileName() { |
| 298 | 12 | return this.fileName; |
| 299 | |
} |
| 300 | |
|
| 301 | |
public boolean isAllowOverrides() { |
| 302 | 0 | return this.allowOverrides; |
| 303 | |
} |
| 304 | |
|
| 305 | |
public void setAllowOverrides(boolean allowOverrides) { |
| 306 | 0 | this.allowOverrides = allowOverrides; |
| 307 | 0 | } |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
public Properties loadProperties() { |
| 316 | 6 | if (StringUtils.isBlank(getFileName())) { |
| 317 | 3 | throw new IllegalStateException("invalid (blank) fileName"); |
| 318 | |
} |
| 319 | |
|
| 320 | 3 | Properties properties = new Properties(); |
| 321 | |
|
| 322 | 3 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 323 | 3 | URL url = loader.getResource(getFileName()); |
| 324 | 3 | if (url == null) { |
| 325 | 3 | throw new PropertiesException("unable to locate properties file '" + getFileName() + "'"); |
| 326 | |
} |
| 327 | |
|
| 328 | 0 | InputStream in = null; |
| 329 | |
|
| 330 | |
try { |
| 331 | 0 | in = url.openStream(); |
| 332 | 0 | properties.load(in); |
| 333 | |
} |
| 334 | 0 | catch (IOException e) { |
| 335 | 0 | throw new PropertiesException("error loading from properties file '" + getFileName() + "'", e); |
| 336 | |
} |
| 337 | |
finally { |
| 338 | 0 | if (in != null) { |
| 339 | |
try { |
| 340 | 0 | in.close(); |
| 341 | |
} |
| 342 | 0 | catch (IOException e) { |
| 343 | 0 | log.error("caught exception closing InputStream: " + e); |
| 344 | 0 | } |
| 345 | |
|
| 346 | |
} |
| 347 | |
} |
| 348 | |
|
| 349 | 0 | return properties; |
| 350 | |
} |
| 351 | |
|
| 352 | |
} |
| 353 | |
} |