2 * jSite - ConfigurationLocator.java - Copyright © 2011–2014 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.main;
22 import java.util.HashMap;
24 import java.util.Optional;
27 * Locator for configuration files in different places.
29 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31 public class ConfigurationLocator {
34 * The location of the configuration directory.
36 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
38 public enum ConfigurationLocation {
40 /** The configuration is in the same directory as the JAR file. */
44 * The configuration is in the user’s home directory. This is the
49 /** Custom location. */
54 /** The possible configuration locations. */
55 private final Map<ConfigurationLocation, String> configurationFiles = new HashMap<ConfigurationLocation, String>();
58 * Creates a new configuration locator. If this class is loaded from a JAR
59 * file, {@link ConfigurationLocation#NEXT_TO_JAR_FILE} is added to the list
60 * of possible configuration file locations.
61 * {@link ConfigurationLocation#HOME_DIRECTORY} is always added to this
62 * list, {@link ConfigurationLocation#CUSTOM} has to be enabled by calling
63 * {@link #setCustomLocation(String)}.
65 public ConfigurationLocator(JarFileLocator jarFileLocator) {
66 /* are we executed from a JAR file? */
67 Optional<File> jarFile = jarFileLocator.locateJarFile();
68 if (jarFile.isPresent()) {
69 File configurationFile = new File(jarFile.get().getParent(), "jSite.conf");
70 configurationFiles.put(ConfigurationLocation.NEXT_TO_JAR_FILE, configurationFile.getPath());
72 File homeDirectoryFile = new File(System.getProperty("user.home"), ".jSite/config7");
73 configurationFiles.put(ConfigurationLocation.HOME_DIRECTORY, homeDirectoryFile.getPath());
81 * Sets the location of the custom configuration file.
84 * The custom location of the configuration file
86 public void setCustomLocation(String customFile) {
87 configurationFiles.put(ConfigurationLocation.CUSTOM, customFile);
91 * Returns whether the given location is valid. Certain locations (such as
92 * {@link ConfigurationLocation#NEXT_TO_JAR_FILE}) may be invalid in certain
93 * circumstances (such as the application not being run from a JAR file). A
94 * location being valid does not imply that a configuration file does exist
95 * at the given location, use {@link #hasFile(ConfigurationLocation)} to
96 * check for a configuration file at the desired location.
98 * @param configurationLocation
99 * The configuration location
100 * @return {@code true} if the location is valid, {@code false} otherwise
102 public boolean isValidLocation(ConfigurationLocation configurationLocation) {
103 return configurationFiles.containsKey(configurationLocation);
107 * Checks whether a configuration file exists at the given location.
109 * @param configurationLocation
110 * The configuration location
111 * @return {@code true} if a configuration file exists at the given
112 * location, {@code false} otherwise
114 public boolean hasFile(ConfigurationLocation configurationLocation) {
115 if (!isValidLocation(configurationLocation)) {
118 return new File(configurationFiles.get(configurationLocation)).exists();
122 * Returns the configuration file for the given location.
124 * @param configurationLocation
125 * The location to get the file for
126 * @return The name of the configuration file at the given location, or
127 * {@code null} if the given location is invalid
129 public String getFile(ConfigurationLocation configurationLocation) {
130 return configurationFiles.get(configurationLocation);
138 * Finds the preferred location of the configuration file.
140 * @see #findPreferredLocation(ConfigurationLocation)
141 * @return The preferred location of the configuration file
143 public ConfigurationLocation findPreferredLocation() {
144 return findPreferredLocation(ConfigurationLocation.NEXT_TO_JAR_FILE);
148 * Finds the preferred location of the configuration file. The following
149 * checks are performed: if a custom configuration location has been defined
150 * (by calling {@link #setCustomLocation(String)})
151 * {@link ConfigurationLocation#CUSTOM} is returned. If the application is
152 * run from a JAR file and a configuration file is found next to the JAR
153 * file (i.e. in the same directory),
154 * {@link ConfigurationLocation#NEXT_TO_JAR_FILE} is returned. If a
155 * configuration file exists in the user’s home directory,
156 * {@link ConfigurationLocation#HOME_DIRECTORY} is returned. Otherwise, the
157 * given {@code defaultLocation} is returned.
159 * @param defaultLocation
160 * The default location to return if no other configuration file
162 * @return The configuration location to load the configuration from
164 public ConfigurationLocation findPreferredLocation(ConfigurationLocation defaultLocation) {
165 if (hasFile(ConfigurationLocation.CUSTOM)) {
166 return ConfigurationLocation.CUSTOM;
168 if (hasFile(ConfigurationLocation.NEXT_TO_JAR_FILE)) {
169 return ConfigurationLocation.NEXT_TO_JAR_FILE;
171 if (hasFile(ConfigurationLocation.HOME_DIRECTORY)) {
172 return ConfigurationLocation.HOME_DIRECTORY;
174 if (isValidLocation(defaultLocation)) {
175 return defaultLocation;
177 return ConfigurationLocation.HOME_DIRECTORY;