001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache 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 *      https://www.apache.org/licenses/LICENSE-2.0
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 */
016
017package org.springframework.util;
018
019import org.springframework.lang.Nullable;
020
021/**
022 * Helper class for resolving placeholders in texts. Usually applied to file paths.
023 *
024 * <p>A text may contain {@code ${...}} placeholders, to be resolved as system properties:
025 * e.g. {@code ${user.dir}}. Default values can be supplied using the ":" separator
026 * between key and value.
027 *
028 * @author Juergen Hoeller
029 * @author Rob Harrop
030 * @author Dave Syer
031 * @since 1.2.5
032 * @see #PLACEHOLDER_PREFIX
033 * @see #PLACEHOLDER_SUFFIX
034 * @see System#getProperty(String)
035 */
036public abstract class SystemPropertyUtils {
037
038        /** Prefix for system property placeholders: "${". */
039        public static final String PLACEHOLDER_PREFIX = "${";
040
041        /** Suffix for system property placeholders: "}". */
042        public static final String PLACEHOLDER_SUFFIX = "}";
043
044        /** Value separator for system property placeholders: ":". */
045        public static final String VALUE_SEPARATOR = ":";
046
047
048        private static final PropertyPlaceholderHelper strictHelper =
049                        new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, false);
050
051        private static final PropertyPlaceholderHelper nonStrictHelper =
052                        new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true);
053
054
055        /**
056         * Resolve {@code ${...}} placeholders in the given text, replacing them with
057         * corresponding system property values.
058         * @param text the String to resolve
059         * @return the resolved String
060         * @throws IllegalArgumentException if there is an unresolvable placeholder
061         * @see #PLACEHOLDER_PREFIX
062         * @see #PLACEHOLDER_SUFFIX
063         */
064        public static String resolvePlaceholders(String text) {
065                return resolvePlaceholders(text, false);
066        }
067
068        /**
069         * Resolve {@code ${...}} placeholders in the given text, replacing them with
070         * corresponding system property values. Unresolvable placeholders with no default
071         * value are ignored and passed through unchanged if the flag is set to {@code true}.
072         * @param text the String to resolve
073         * @param ignoreUnresolvablePlaceholders whether unresolved placeholders are to be ignored
074         * @return the resolved String
075         * @throws IllegalArgumentException if there is an unresolvable placeholder
076         * @see #PLACEHOLDER_PREFIX
077         * @see #PLACEHOLDER_SUFFIX
078         * and the "ignoreUnresolvablePlaceholders" flag is {@code false}
079         */
080        public static String resolvePlaceholders(String text, boolean ignoreUnresolvablePlaceholders) {
081                if (text.isEmpty()) {
082                        return text;
083                }
084                PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
085                return helper.replacePlaceholders(text, new SystemPropertyPlaceholderResolver(text));
086        }
087
088
089        /**
090         * PlaceholderResolver implementation that resolves against system properties
091         * and system environment variables.
092         */
093        private static class SystemPropertyPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver {
094
095                private final String text;
096
097                public SystemPropertyPlaceholderResolver(String text) {
098                        this.text = text;
099                }
100
101                @Override
102                @Nullable
103                public String resolvePlaceholder(String placeholderName) {
104                        try {
105                                String propVal = System.getProperty(placeholderName);
106                                if (propVal == null) {
107                                        // Fall back to searching the system environment.
108                                        propVal = System.getenv(placeholderName);
109                                }
110                                return propVal;
111                        }
112                        catch (Throwable ex) {
113                                System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" +
114                                                this.text + "] as system property: " + ex);
115                                return null;
116                        }
117                }
118        }
119
120}