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