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 */
017package org.apache.camel.util;
018
019/**
020 * A resolver for file paths that supports resolving with system and environment properties.
021 */
022public final class FilePathResolver {
023
024    private FilePathResolver() {
025    }
026
027    /**
028     * Resolves the path.
029     * <p/>
030     * The pattern is:
031     * <ul>
032     * <li><tt>${env:key}</tt> or <tt>${env.key}</tt> for environment variables.</li>
033     * <li><tt>${key}</tt> for JVM system properties.</li>
034     * </ul>
035     * For example: <tt>${env.KARAF_HOME}/data/logs</tt>
036     *
037     * @param  path                     the path
038     * @return                          the resolved path
039     * @throws IllegalArgumentException is thrown if system property / environment not found
040     */
041    public static String resolvePath(String path) throws IllegalArgumentException {
042        int count = StringHelper.countChar(path, '}') + 1;
043        if (count <= 1) {
044            return path;
045        }
046
047        String[] functions = StringHelper.splitOnCharacter(path, "}", count);
048        for (String fun : functions) {
049            int pos = fun.indexOf("${env:");
050            if (pos != -1) {
051                String key = fun.substring(pos + 6);
052                String value = System.getenv(key);
053                if (value != null) {
054                    path = path.replace("${env:" + key + "}", value);
055                }
056            }
057            pos = fun.indexOf("${env.");
058            if (pos != -1) {
059                String key = fun.substring(pos + 6);
060                String value = System.getenv(key);
061                if (value != null) {
062                    path = path.replace("${env." + key + "}", value);
063                }
064            }
065        }
066
067        count = StringHelper.countChar(path, '}') + 1;
068        if (count <= 1) {
069            return path;
070        }
071        functions = StringHelper.splitOnCharacter(path, "}", count);
072        for (String fun : functions) {
073            int pos = fun.indexOf("${");
074            if (pos != -1) {
075                String key = fun.substring(pos + 2);
076                String value = System.getProperty(key);
077                if (value != null) {
078                    path = path.replace("${" + key + "}", value);
079                }
080            }
081        }
082
083        return path;
084    }
085
086}