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 */
047public class HomeDirectoryLocationStrategy implements FileLocationStrategy
048{
049    /** Constant for the system property with the user's home directory. */
050    private static final String PROP_HOME = "user.home";
051
052    /** The home directory to be searched for the requested file. */
053    private final String homeDirectory;
054
055    /** The flag whether the base path is to be taken into account. */
056    private final boolean evaluateBasePath;
057
058    /**
059     * Creates a new instance of {@code HomeDirectoryLocationStrategy} and
060     * initializes it with the specified settings.
061     *
062     * @param homeDir the path to the home directory (can be <b>null</b>)
063     * @param withBasePath a flag whether the base path should be evaluated
064     */
065    public HomeDirectoryLocationStrategy(final String homeDir, final boolean withBasePath)
066    {
067        homeDirectory = fetchHomeDirectory(homeDir);
068        evaluateBasePath = withBasePath;
069    }
070
071    /**
072     * Creates a new instance of {@code HomeDirectoryLocationStrategy} and
073     * initializes the base path flag. The home directory is set to the user's
074     * home directory.
075     *
076     * @param withBasePath a flag whether the base path should be evaluated
077     */
078    public HomeDirectoryLocationStrategy(final boolean withBasePath)
079    {
080        this(null, withBasePath);
081    }
082
083    /**
084     * Creates a new instance of {@code HomeDirectoryLocationStrategy} with
085     * default settings. The home directory is set to the user's home directory.
086     * The base path flag is set to <b>false</b> (which means that the base path
087     * is ignored).
088     */
089    public HomeDirectoryLocationStrategy()
090    {
091        this(false);
092    }
093
094    /**
095     * Returns the home directory. In this directory the strategy searches for
096     * files.
097     *
098     * @return the home directory used by this object
099     */
100    public String getHomeDirectory()
101    {
102        return homeDirectory;
103    }
104
105    /**
106     * Returns a flag whether the base path is to be taken into account when
107     * searching for a file.
108     *
109     * @return the flag whether the base path is evaluated
110     */
111    public boolean isEvaluateBasePath()
112    {
113        return evaluateBasePath;
114    }
115
116    /**
117     * {@inheritDoc} This implementation searches in the home directory for a
118     * file described by the passed in {@code FileLocator}. If the locator
119     * defines a base path and the {@code evaluateBasePath} property is
120     * <b>true</b>, a sub directory of the home directory is searched.
121     */
122    @Override
123    public URL locate(final FileSystem fileSystem, final FileLocator locator)
124    {
125        if (StringUtils.isNotEmpty(locator.getFileName()))
126        {
127            final String basePath = fetchBasePath(locator);
128            final File file =
129                    FileLocatorUtils.constructFile(basePath,
130                            locator.getFileName());
131            if (file.isFile())
132            {
133                return FileLocatorUtils.convertFileToURL(file);
134            }
135        }
136
137        return null;
138    }
139
140    /**
141     * Determines the base path to be used for the current locate() operation.
142     *
143     * @param locator the {@code FileLocator}
144     * @return the base path to be used
145     */
146    private String fetchBasePath(final FileLocator locator)
147    {
148        if (isEvaluateBasePath()
149                && StringUtils.isNotEmpty(locator.getBasePath()))
150        {
151            return FileLocatorUtils.appendPath(getHomeDirectory(),
152                    locator.getBasePath());
153        }
154        return getHomeDirectory();
155    }
156
157    /**
158     * Obtains the home directory to be used by a new instance. If a directory
159     * name is provided, it is used. Otherwise, the user's home directory is
160     * looked up.
161     *
162     * @param homeDir the passed in home directory
163     * @return the directory to be used
164     */
165    private static String fetchHomeDirectory(final String homeDir)
166    {
167        return homeDir != null ? homeDir : System.getProperty(PROP_HOME);
168    }
169}