Verify that the default location is valid.
[jSite.git] / src / de / todesbaum / jsite / main / ConfigurationLocator.java
1 /*
2  * jSite - ConfigurationLocator.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package de.todesbaum.jsite.main;
20
21 import java.io.File;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 /**
26  * Locator for configuration files in different places.
27  *
28  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
29  */
30 public class ConfigurationLocator {
31
32         /**
33          * The location of the configuration directory.
34          *
35          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36          */
37         public enum ConfigurationLocation {
38
39                 /** The configuration is in the same directory as the JAR file. */
40                 NEXT_TO_JAR_FILE,
41
42                 /**
43                  * The configuration is in the user’s home directory. This is the
44                  * pre-0.9.3 default.
45                  */
46                 HOME_DIRECTORY,
47
48                 /** Custom location. */
49                 CUSTOM,
50
51         }
52
53         /** The possible configuration locations. */
54         private final Map<ConfigurationLocation, String> configurationFiles = new HashMap<ConfigurationLocation, String>();
55
56         /**
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
59          * list 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)}.
63          */
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());
72                 }
73                 File homeDirectoryFile = new File(System.getProperty("user.home"), ".jSite/config7");
74                 configurationFiles.put(ConfigurationLocation.HOME_DIRECTORY, homeDirectoryFile.getPath());
75         }
76
77         //
78         // ACCESSORS
79         //
80
81         /**
82          * Sets the location of the custom configuration file.
83          *
84          * @param customFile
85          *            The custom location of the configuration file
86          */
87         public void setCustomLocation(String customFile) {
88                 configurationFiles.put(ConfigurationLocation.CUSTOM, customFile);
89         }
90
91         /**
92          * Returns whether the given location is valid. Certain locations (such as
93          * {@link ConfigurationLocation#NEXT_TO_JAR_FILE}) may be invalid in
94          * certain circumstances (such as the application not being run from a JAR
95          * file). A location being valid does not imply that a configuration file
96          * does exist at the given location, use
97          * {@link #hasFile(ConfigurationLocation)} to check for a configuration
98          * file at the desired location.
99          *
100          * @param configurationLocation
101          *            The configuration location
102          * @return {@code true} if the location is valid, {@code false} otherwise
103          */
104         public boolean isValidLocation(ConfigurationLocation configurationLocation) {
105                 return configurationFiles.containsKey(configurationLocation);
106         }
107
108         /**
109          * Checks whether a configuration file exists at the given location.
110          *
111          * @param configurationLocation
112          *            The configuration location
113          * @return {@code true} if a configuration file exists at the given
114          *         location, {@code false} otherwise
115          */
116         public boolean hasFile(ConfigurationLocation configurationLocation) {
117                 if (!isValidLocation(configurationLocation)) {
118                         return false;
119                 }
120                 return new File(configurationFiles.get(configurationLocation)).exists();
121         }
122
123         /**
124          * Returns the configuration file for the given location.
125          *
126          * @param configurationLocation
127          *            The location to get the file for
128          * @return The name of the configuration file at the given location, or
129          *         {@code null} if the given location is invalid
130          */
131         public String getFile(ConfigurationLocation configurationLocation) {
132                 return configurationFiles.get(configurationLocation);
133         }
134
135         //
136         // ACTIONS
137         //
138
139         /**
140          * Finds the preferred location of the configuration file.
141          *
142          * @see #findPreferredLocation(ConfigurationLocation)
143          * @return The preferred location of the configuration file
144          */
145         public ConfigurationLocation findPreferredLocation() {
146                 return findPreferredLocation(ConfigurationLocation.NEXT_TO_JAR_FILE);
147         }
148
149         /**
150          * Finds the preferred location of the configuration file. The following
151          * checks are performed: if a custom configuration location has been defined
152          * (by calling {@link #setCustomLocation(String)})
153          * {@link ConfigurationLocation#CUSTOM} is returned. If the application is
154          * run from a JAR file and a configuration file is found next to the JAR
155          * file (i.e. in the same directory),
156          * {@link ConfigurationLocation#NEXT_TO_JAR_FILE} is returned. If a
157          * configuration file exists in the user’s home directory,
158          * {@link ConfigurationLocation#HOME_DIRECTORY} is returned. Otherwise, the
159          * given {@code defaultLocation} is returned.
160          *
161          * @param defaultLocation
162          *            The default location to return if no other configuration file
163          *            is found
164          * @return The configuration location to load the configuration from
165          */
166         public ConfigurationLocation findPreferredLocation(ConfigurationLocation defaultLocation) {
167                 if (hasFile(ConfigurationLocation.CUSTOM)) {
168                         return ConfigurationLocation.CUSTOM;
169                 }
170                 if (hasFile(ConfigurationLocation.NEXT_TO_JAR_FILE)) {
171                         return ConfigurationLocation.NEXT_TO_JAR_FILE;
172                 }
173                 if (hasFile(ConfigurationLocation.HOME_DIRECTORY)) {
174                         return ConfigurationLocation.HOME_DIRECTORY;
175                 }
176                 if (isValidLocation(defaultLocation)) {
177                         return defaultLocation;
178                 }
179                 return ConfigurationLocation.HOME_DIRECTORY;
180         }
181
182 }