001package org.kuali.common.util.resolver;
002
003import java.util.Properties;
004
005import org.kuali.common.util.Assert;
006import org.kuali.common.util.PropertyUtils;
007import org.springframework.util.PropertyPlaceholderHelper;
008
009public class PropertiesValueResolver implements ValueResolver {
010
011        private static final String PREFIX = "${";
012        private static final String SUFFIX = "}";
013        private static final String SEPARATOR = ":";
014
015        public static final boolean DEFAULT_IGNORE_UNRESOLVABLE = false;
016
017        private static final PropertyPlaceholderHelper DEFAULT_HELPER = new PropertyPlaceholderHelper(PREFIX, SUFFIX, SEPARATOR, DEFAULT_IGNORE_UNRESOLVABLE);
018
019        private final Properties properties;
020        private final PropertyPlaceholderHelper helper;
021
022        public PropertiesValueResolver() {
023                this(PropertyUtils.EMPTY);
024        }
025
026        public PropertiesValueResolver(Properties properties) {
027                this(properties, DEFAULT_HELPER);
028        }
029
030        public PropertiesValueResolver(Properties properties, boolean ignoreUnresolvable) {
031                this(properties, new PropertyPlaceholderHelper(PREFIX, SUFFIX, SEPARATOR, ignoreUnresolvable));
032        }
033
034        public PropertiesValueResolver(Properties properties, PropertyPlaceholderHelper helper) {
035                Assert.noNulls(properties, helper);
036                this.properties = PropertyUtils.toImmutable(properties);
037                this.helper = helper;
038        }
039
040        @Override
041        public String resolve(String value) {
042                return helper.replacePlaceholders(value, properties);
043        }
044
045        public Properties getProperties() {
046                return properties;
047        }
048
049        public PropertyPlaceholderHelper getHelper() {
050                return helper;
051        }
052
053}