2 * jSite - ConfigurationLocator.java - Copyright © 2011 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;
26 * Locator for configuration files in different places.
28 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30 public class ConfigurationLocator {
33 * The location of the configuration directory.
35 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
37 public enum ConfigurationLocation {
39 /** The configuration is in the same directory as the JAR file. */
43 * The configuration is in the user’s home directory. This is the
48 /** Custom location. */
53 /** The possible configuration locations. */
54 private final Map<ConfigurationLocation, String> configurationFiles = new HashMap<ConfigurationLocation, String>();
57 * Creates a new configuration locator. If this class is loaded from a JAR
58 * file, {@link ConfigurationLocation#NEXT_TO_JAR_FILE} is added to the list
59 * of possible configuration file locations.
60 * {@link ConfigurationLocation#HOME_DIRECTORY} is always added to this
61 * list, {@link ConfigurationLocation#CUSTOM} has to be enabled by calling
62 * {@link #setCustomLocation(String)}.
64 public ConfigurationLocator() {
65 /* are we executed from a JAR file? */
66 String resource = getClass().getResource("/" + getClass().getName().replace(".", "/") + ".class").toString();
67 if (resource.startsWith("jar:")) {
68 String jarFileLocation = resource.substring(9, resource.indexOf(".jar!") + 4);
69 String jarFileDirectory = new File(jarFileLocation).getParent();
70 File configurationFile = new File(jarFileDirectory, "jSite.conf");
71 configurationFiles.put(ConfigurationLocation.NEXT_TO_JAR_FILE, configurationFile.getPath());
73 File homeDirectoryFile = new File(System.getProperty("user.home"), ".jSite/config7");
74 configurationFiles.put(ConfigurationLocation.HOME_DIRECTORY, homeDirectoryFile.getPath());
82 * Sets the location of the custom configuration file.
85 * The custom location of the configuration file
87 public void setCustomLocation(String customFile) {
88 configurationFiles.put(ConfigurationLocation.CUSTOM, customFile);
92 * Returns whether the given location is valid. Certain locations (such as
93 * {@link ConfigurationLocation#NEXT_TO_JAR_FILE}) may be invalid in certain
94 * circumstances (such as the application not being run from a JAR file). A
95 * location being valid does not imply that a configuration file does exist
96 * at the given location, use {@link #hasFile(ConfigurationLocation)} to
97 * check for a configuration file at the desired location.
99 * @param configurationLocation
100 * The configuration location
101 * @return {@code true} if the location is valid, {@code false} otherwise
103 public boolean isValidLocation(ConfigurationLocation configurationLocation) {
104 return configurationFiles.containsKey(configurationLocation);
108 * Checks whether a configuration file exists at the given location.
110 * @param configurationLocation
111 * The configuration location
112 * @return {@code true} if a configuration file exists at the given
113 * location, {@code false} otherwise
115 public boolean hasFile(ConfigurationLocation configurationLocation) {
116 if (!isValidLocation(configurationLocation)) {
119 return new File(configurationFiles.get(configurationLocation)).exists();
123 * Returns the configuration file for the given location.
125 * @param configurationLocation
126 * The location to get the file for
127 * @return The name of the configuration file at the given location, or
128 * {@code null} if the given location is invalid
130 public String getFile(ConfigurationLocation configurationLocation) {
131 return configurationFiles.get(configurationLocation);
139 * Finds the preferred location of the configuration file.
141 * @see #findPreferredLocation(ConfigurationLocation)
142 * @return The preferred location of the configuration file
144 public ConfigurationLocation findPreferredLocation() {
145 return findPreferredLocation(ConfigurationLocation.NEXT_TO_JAR_FILE);
149 * Finds the preferred location of the configuration file. The following
150 * checks are performed: if a custom configuration location has been defined
151 * (by calling {@link #setCustomLocation(String)})
152 * {@link ConfigurationLocation#CUSTOM} is returned. If the application is
153 * run from a JAR file and a configuration file is found next to the JAR
154 * file (i.e. in the same directory),
155 * {@link ConfigurationLocation#NEXT_TO_JAR_FILE} is returned. If a
156 * configuration file exists in the user’s home directory,
157 * {@link ConfigurationLocation#HOME_DIRECTORY} is returned. Otherwise, the
158 * given {@code defaultLocation} is returned.
160 * @param defaultLocation
161 * The default location to return if no other configuration file
163 * @return The configuration location to load the configuration from
165 public ConfigurationLocation findPreferredLocation(ConfigurationLocation defaultLocation) {
166 if (hasFile(ConfigurationLocation.CUSTOM)) {
167 return ConfigurationLocation.CUSTOM;
169 if (hasFile(ConfigurationLocation.NEXT_TO_JAR_FILE)) {
170 return ConfigurationLocation.NEXT_TO_JAR_FILE;
172 if (hasFile(ConfigurationLocation.HOME_DIRECTORY)) {
173 return ConfigurationLocation.HOME_DIRECTORY;
175 if (isValidLocation(defaultLocation)) {
176 return defaultLocation;
178 return ConfigurationLocation.HOME_DIRECTORY;