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.commons.configuration2.io;
018
019import java.io.File;
020import java.net.URL;
021
022import org.apache.commons.lang3.StringUtils;
023
024/**
025 * <p>
026 * A specialized implementation of {@code FileLocationStrategy} which searches
027 * for files in the user's home directory or another special configurable
028 * directory.
029 * </p>
030 * <p>
031 * This strategy implementation ignores the URL stored in the passed in
032 * {@link FileLocator}. It constructs a file path from the configured home
033 * directory (which is the user's home directory per default, but can be changed
034 * to another path), optionally the base path, and the file name. If the
035 * resulting path points to an existing file, its URL is returned.
036 * </p>
037 * <p>
038 * When constructing an instance it can be configured whether the base path
039 * should be taken into account. If this option is set, the base path is
040 * appended to the home directory if it is not <b>null</b>. This is useful for
041 * instance to select a specific sub directory of the user's home directory. If
042 * this option is set to <b>false</b>, the base path is always ignored, and only
043 * the file name is evaluated.
044 * </p>
045 *
046 * @version $Id: HomeDirectoryLocationStrategy.java 1842194 2018-09-27 22:24:23Z ggregory $
047 */
048public class HomeDirectoryLocationStrategy implements FileLocationStrategy
049{
050    /** Constant for the system property with the user's home directory. */
051    private static final String PROP_HOME = "user.home";
052
053    /** The home directory to be searched for the requested file. */
054    private final String homeDirectory;
055
056    /** The flag whether the base path is to be taken into account. */
057    private final boolean evaluateBasePath;
058
059    /**
060     * Creates a new instance of {@code HomeDirectoryLocationStrategy} and
061     * initializes it with the specified settings.
062     *
063     * @param homeDir the path to the home directory (can be <b>null</b>)
064     * @param withBasePath a flag whether the base path should be evaluated
065     */
066    public HomeDirectoryLocationStrategy(final String homeDir, final boolean withBasePath)
067    {
068        homeDirectory = fetchHomeDirectory(homeDir);
069        evaluateBasePath = withBasePath;
070    }
071
072    /**
073     * Creates a new instance of {@code HomeDirectoryLocationStrategy} and
074     * initializes the base path flag. The home directory is set to the user's
075     * home directory.
076     *
077     * @param withBasePath a flag whether the base path should be evaluated
078     */
079    public HomeDirectoryLocationStrategy(final boolean withBasePath)
080    {
081        this(null, withBasePath);
082    }
083
084    /**
085     * Creates a new instance of {@code HomeDirectoryLocationStrategy} with
086     * default settings. The home directory is set to the user's home directory.
087     * The base path flag is set to <b>false</b> (which means that the base path
088     * is ignored).
089     */
090    public HomeDirectoryLocationStrategy()
091    {
092        this(false);
093    }
094
095    /**
096     * Returns the home directory. In this directory the strategy searches for
097     * files.
098     *
099     * @return the home directory used by this object
100     */
101    public String getHomeDirectory()
102    {
103        return homeDirectory;
104    }
105
106    /**
107     * Returns a flag whether the base path is to be taken into account when
108     * searching for a file.
109     *
110     * @return the flag whether the base path is evaluated
111     */
112    public boolean isEvaluateBasePath()
113    {
114        return evaluateBasePath;
115    }
116
117    /**
118     * {@inheritDoc} This implementation searches in the home directory for a
119     * file described by the passed in {@code FileLocator}. If the locator
120     * defines a base path and the {@code evaluateBasePath} property is
121     * <b>true</b>, a sub directory of the home directory is searched.
122     */
123    @Override
124    public URL locate(final FileSystem fileSystem, final FileLocator locator)
125    {
126        if (StringUtils.isNotEmpty(locator.getFileName()))
127        {
128            final String basePath = fetchBasePath(locator);
129            final File file =
130                    FileLocatorUtils.constructFile(basePath,
131                            locator.getFileName());
132            if (file.isFile())
133            {
134                return FileLocatorUtils.convertFileToURL(file);
135            }
136        }
137
138        return null;
139    }
140
141    /**
142     * Determines the base path to be used for the current locate() operation.
143     *
144     * @param locator the {@code FileLocator}
145     * @return the base path to be used
146     */
147    private String fetchBasePath(final FileLocator locator)
148    {
149        if (isEvaluateBasePath()
150                && StringUtils.isNotEmpty(locator.getBasePath()))
151        {
152            return FileLocatorUtils.appendPath(getHomeDirectory(),
153                    locator.getBasePath());
154        }
155        return getHomeDirectory();
156    }
157
158    /**
159     * Obtains the home directory to be used by a new instance. If a directory
160     * name is provided, it is used. Otherwise, the user's home directory is
161     * looked up.
162     *
163     * @param homeDir the passed in home directory
164     * @return the directory to be used
165     */
166    private static String fetchHomeDirectory(final String homeDir)
167    {
168        return (homeDir != null) ? homeDir : System.getProperty(PROP_HOME);
169    }
170}