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.property.processor;
017
018import java.util.Properties;
019
020import org.kuali.common.util.Assert;
021import org.kuali.common.util.PropertyUtils;
022import org.kuali.common.util.log.LoggerUtils;
023import org.slf4j.Logger;
024import org.springframework.util.PropertyPlaceholderHelper;
025
026public class ResolvingProcessor implements PropertyProcessor {
027
028        private static final Logger logger = LoggerUtils.make();
029
030        public static final boolean DEFAULT_IGNORE_UNRESOLVABLE = false;
031        public static final String DEFAULT_RESOLVE_KEY = "properties.resolve";
032
033        public ResolvingProcessor() {
034                this(DEFAULT_IGNORE_UNRESOLVABLE, DEFAULT_RESOLVE_KEY);
035        }
036
037        public ResolvingProcessor(boolean ignoreUnresolvable, String resolveKey) {
038                Assert.noBlanks(resolveKey);
039                this.ignoreUnresolvable = ignoreUnresolvable;
040                this.resolveKey = resolveKey;
041                this.helper = new PropertyPlaceholderHelper("${", "}", ":", ignoreUnresolvable);
042        }
043
044        private final boolean ignoreUnresolvable;
045        private final String resolveKey;
046        private final PropertyPlaceholderHelper helper;
047
048        @Override
049        public void process(Properties properties) {
050                boolean resolve = PropertyUtils.getBoolean(resolveKey, properties, true);
051                if (resolve) {
052                        logger.info("Performing placeholder resolution on {} properties", properties.size());
053                        PropertyUtils.resolve(properties, helper);
054                } else {
055                        logger.info("Skipping placeholder resolution for {} properties", properties.size());
056                }
057        }
058
059        public boolean isIgnoreUnresolvable() {
060                return ignoreUnresolvable;
061        }
062
063        public String getResolveKey() {
064                return resolveKey;
065        }
066
067        public PropertyPlaceholderHelper getHelper() {
068                return helper;
069        }
070
071}