View Javadoc
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    *     https://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  
19  import java.io.File;
20  import java.net.URL;
21  
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.commons.lang3.SystemProperties;
24  
25  /**
26   * <p>
27   * A specialized implementation of {@code FileLocationStrategy} which searches for files in the user's home directory or
28   * another special configurable directory.
29   * </p>
30   * <p>
31   * This strategy implementation ignores the URL stored in the passed in {@link FileLocator}. It constructs a file path
32   * from the configured home directory (which is the user's home directory per default, but can be changed to another
33   * path), optionally the base path, and the file name. If the resulting path points to an existing file, its URL is
34   * returned.
35   * </p>
36   * <p>
37   * When constructing an instance it can be configured whether the base path should be taken into account. If this option
38   * is set, the base path is appended to the home directory if it is not <strong>null</strong>. This is useful for instance to
39   * select a specific sub directory of the user's home directory. If this option is set to <strong>false</strong>, the base path is
40   * always ignored, and only the file name is evaluated.
41   * </p>
42   */
43  public class HomeDirectoryLocationStrategy implements FileLocationStrategy {
44  
45      /**
46       * Obtains the home directory to be used by a new instance. If a directory name is provided, it is used. Otherwise, the
47       * user's home directory is looked up.
48       *
49       * @param homeDir the passed in home directory
50       * @return the directory to be used
51       */
52      private static String fetchHomeDirectory(final String homeDir) {
53          return homeDir != null ? homeDir : SystemProperties.getUserHome();
54      }
55  
56      /** The home directory to be searched for the requested file. */
57      private final String homeDirectory;
58  
59      /** The flag whether the base path is to be taken into account. */
60      private final boolean evaluateBasePath;
61  
62      /**
63       * Creates a new instance of {@code HomeDirectoryLocationStrategy} with default settings. The home directory is set to
64       * the user's home directory. The base path flag is set to <strong>false</strong> (which means that the base path is ignored).
65       */
66      public HomeDirectoryLocationStrategy() {
67          this(false);
68      }
69  
70      /**
71       * Creates a new instance of {@code HomeDirectoryLocationStrategy} and initializes the base path flag. The home
72       * directory is set to the user's home directory.
73       *
74       * @param withBasePath a flag whether the base path should be evaluated
75       */
76      public HomeDirectoryLocationStrategy(final boolean withBasePath) {
77          this(null, withBasePath);
78      }
79  
80      /**
81       * Creates a new instance of {@code HomeDirectoryLocationStrategy} and initializes it with the specified settings.
82       *
83       * @param homeDir the path to the home directory (can be <strong>null</strong>)
84       * @param withBasePath a flag whether the base path should be evaluated
85       */
86      public HomeDirectoryLocationStrategy(final String homeDir, final boolean withBasePath) {
87          homeDirectory = fetchHomeDirectory(homeDir);
88          evaluateBasePath = withBasePath;
89      }
90  
91      /**
92       * Determines the base path to be used for the current locate() operation.
93       *
94       * @param locator the {@code FileLocator}
95       * @return the base path to be used
96       */
97      private String fetchBasePath(final FileLocator locator) {
98          if (isEvaluateBasePath() && StringUtils.isNotEmpty(locator.getBasePath())) {
99              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 <strong>true</strong>, 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 }