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.resolver;
017
018import java.util.Properties;
019
020import org.kuali.common.util.Assert;
021import org.kuali.common.util.PropertyUtils;
022import org.springframework.util.PropertyPlaceholderHelper;
023
024public class PropertiesValueResolver implements ValueResolver {
025
026        private static final String PREFIX = "${";
027        private static final String SUFFIX = "}";
028        private static final String SEPARATOR = ":";
029
030        public static final boolean DEFAULT_IGNORE_UNRESOLVABLE = false;
031
032        private static final PropertyPlaceholderHelper DEFAULT_HELPER = new PropertyPlaceholderHelper(PREFIX, SUFFIX, SEPARATOR, DEFAULT_IGNORE_UNRESOLVABLE);
033
034        private final Properties properties;
035        private final PropertyPlaceholderHelper helper;
036
037        public PropertiesValueResolver() {
038                this(PropertyUtils.EMPTY);
039        }
040
041        public PropertiesValueResolver(Properties properties) {
042                this(properties, DEFAULT_HELPER);
043        }
044
045        public PropertiesValueResolver(Properties properties, boolean ignoreUnresolvable) {
046                this(properties, new PropertyPlaceholderHelper(PREFIX, SUFFIX, SEPARATOR, ignoreUnresolvable));
047        }
048
049        public PropertiesValueResolver(Properties properties, PropertyPlaceholderHelper helper) {
050                Assert.noNulls(properties, helper);
051                this.properties = PropertyUtils.toImmutable(properties);
052                this.helper = helper;
053        }
054
055        @Override
056        public String resolve(String value) {
057                return helper.replacePlaceholders(value, properties);
058        }
059
060        public Properties getProperties() {
061                return properties;
062        }
063
064        public PropertyPlaceholderHelper getHelper() {
065                return helper;
066        }
067
068}