001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.util;
018    
019    import java.util.regex.Matcher;
020    import java.util.regex.Pattern;
021    
022    /**
023     * A resolver for file paths that supports resolving with system and environment properties.
024     */
025    public final class FilePathResolver {
026    
027        // must be non greedy patterns
028        private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{env:(.*?)\\}", Pattern.DOTALL);
029        private static final Pattern SYS_PATTERN = Pattern.compile("\\$\\{(.*?)\\}", Pattern.DOTALL);
030    
031        private FilePathResolver() {
032        }
033    
034        /**
035         * Resolves the path.
036         * <p/>
037         * The pattern is:
038         * <ul>
039         *   <li><tt>${env.key}</tt> for environment variables.</li>
040         *   <li><tt>${key}</tt> for JVM system properties.</li>
041         * </ul>
042         * For example: <tt>${env.KARAF_HOME}/data/logs</tt>
043         *
044         * @param path  the path
045         * @return the resolved path
046         * @throws IllegalArgumentException is thrown if system property / environment not found
047         */
048        public static String resolvePath(String path) throws IllegalArgumentException {
049            Matcher matcher = ENV_PATTERN.matcher(path);
050            while (matcher.find()) {
051                String key = matcher.group(1);
052                String value = System.getenv(key);
053                if (ObjectHelper.isEmpty(value)) {
054                    throw new IllegalArgumentException("Cannot find system environment with key: " + key);
055                }
056                // must quote the replacement to have it work as literal replacement
057                value = Matcher.quoteReplacement(value);
058                path = matcher.replaceFirst(value);
059                // must match again as location is changed
060                matcher = ENV_PATTERN.matcher(path);
061            }
062    
063            matcher = SYS_PATTERN.matcher(path);
064            while (matcher.find()) {
065                String key = matcher.group(1);
066                String value = System.getProperty(key);
067                if (ObjectHelper.isEmpty(value)) {
068                    throw new IllegalArgumentException("Cannot find JVM system property with key: " + key);
069                }
070                // must quote the replacement to have it work as literal replacement
071                value = Matcher.quoteReplacement(value);
072                path = matcher.replaceFirst(value);
073                // must match again as location is changed
074                matcher = SYS_PATTERN.matcher(path);
075            }
076    
077            return path;
078        }
079    
080    }