HomeDirectoryLocationStrategy.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.io;

  18. import java.io.File;
  19. import java.net.URL;

  20. import org.apache.commons.lang3.StringUtils;
  21. import org.apache.commons.lang3.SystemProperties;

  22. /**
  23.  * <p>
  24.  * A specialized implementation of {@code FileLocationStrategy} which searches for files in the user's home directory or
  25.  * another special configurable directory.
  26.  * </p>
  27.  * <p>
  28.  * This strategy implementation ignores the URL stored in the passed in {@link FileLocator}. It constructs a file path
  29.  * from the configured home directory (which is the user's home directory per default, but can be changed to another
  30.  * path), optionally the base path, and the file name. If the resulting path points to an existing file, its URL is
  31.  * returned.
  32.  * </p>
  33.  * <p>
  34.  * When constructing an instance it can be configured whether the base path should be taken into account. If this option
  35.  * is set, the base path is appended to the home directory if it is not <strong>null</strong>. This is useful for instance to
  36.  * select a specific sub directory of the user's home directory. If this option is set to <strong>false</strong>, the base path is
  37.  * always ignored, and only the file name is evaluated.
  38.  * </p>
  39.  */
  40. public class HomeDirectoryLocationStrategy implements FileLocationStrategy {

  41.     /**
  42.      * Obtains the home directory to be used by a new instance. If a directory name is provided, it is used. Otherwise, the
  43.      * user's home directory is looked up.
  44.      *
  45.      * @param homeDir the passed in home directory
  46.      * @return the directory to be used
  47.      */
  48.     private static String fetchHomeDirectory(final String homeDir) {
  49.         return homeDir != null ? homeDir : SystemProperties.getUserHome();
  50.     }

  51.     /** The home directory to be searched for the requested file. */
  52.     private final String homeDirectory;

  53.     /** The flag whether the base path is to be taken into account. */
  54.     private final boolean evaluateBasePath;

  55.     /**
  56.      * Creates a new instance of {@code HomeDirectoryLocationStrategy} with default settings. The home directory is set to
  57.      * the user's home directory. The base path flag is set to <strong>false</strong> (which means that the base path is ignored).
  58.      */
  59.     public HomeDirectoryLocationStrategy() {
  60.         this(false);
  61.     }

  62.     /**
  63.      * Creates a new instance of {@code HomeDirectoryLocationStrategy} and initializes the base path flag. The home
  64.      * directory is set to the user's home directory.
  65.      *
  66.      * @param withBasePath a flag whether the base path should be evaluated
  67.      */
  68.     public HomeDirectoryLocationStrategy(final boolean withBasePath) {
  69.         this(null, withBasePath);
  70.     }

  71.     /**
  72.      * Creates a new instance of {@code HomeDirectoryLocationStrategy} and initializes it with the specified settings.
  73.      *
  74.      * @param homeDir the path to the home directory (can be <strong>null</strong>)
  75.      * @param withBasePath a flag whether the base path should be evaluated
  76.      */
  77.     public HomeDirectoryLocationStrategy(final String homeDir, final boolean withBasePath) {
  78.         homeDirectory = fetchHomeDirectory(homeDir);
  79.         evaluateBasePath = withBasePath;
  80.     }

  81.     /**
  82.      * Determines the base path to be used for the current locate() operation.
  83.      *
  84.      * @param locator the {@code FileLocator}
  85.      * @return the base path to be used
  86.      */
  87.     private String fetchBasePath(final FileLocator locator) {
  88.         if (isEvaluateBasePath() && StringUtils.isNotEmpty(locator.getBasePath())) {
  89.             return FileLocatorUtils.appendPath(getHomeDirectory(), locator.getBasePath());
  90.         }
  91.         return getHomeDirectory();
  92.     }

  93.     /**
  94.      * Gets the home directory. In this directory the strategy searches for files.
  95.      *
  96.      * @return the home directory used by this object
  97.      */
  98.     public String getHomeDirectory() {
  99.         return homeDirectory;
  100.     }

  101.     /**
  102.      * Returns a flag whether the base path is to be taken into account when searching for a file.
  103.      *
  104.      * @return the flag whether the base path is evaluated
  105.      */
  106.     public boolean isEvaluateBasePath() {
  107.         return evaluateBasePath;
  108.     }

  109.     /**
  110.      * {@inheritDoc} This implementation searches in the home directory for a file described by the passed in
  111.      * {@code FileLocator}. If the locator defines a base path and the {@code evaluateBasePath} property is <strong>true</strong>, a
  112.      * sub directory of the home directory is searched.
  113.      */
  114.     @Override
  115.     public URL locate(final FileSystem fileSystem, final FileLocator locator) {
  116.         if (StringUtils.isNotEmpty(locator.getFileName())) {
  117.             final String basePath = fetchBasePath(locator);
  118.             final File file = FileLocatorUtils.constructFile(basePath, locator.getFileName());
  119.             if (file.isFile()) {
  120.                 return FileLocatorUtils.convertFileToURL(file);
  121.             }
  122.         }

  123.         return null;
  124.     }
  125. }