2 * jSite - Configuration.java - Copyright © 2006–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;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Locale;
34 import java.util.Map.Entry;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import de.todesbaum.jsite.application.FileOption;
39 import de.todesbaum.jsite.application.Node;
40 import de.todesbaum.jsite.application.Project;
41 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
42 import de.todesbaum.util.io.Closer;
43 import de.todesbaum.util.io.StreamCopier;
44 import de.todesbaum.util.xml.SimpleXML;
45 import de.todesbaum.util.xml.XML;
50 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
52 public class Configuration {
54 /** The root node of the configuration. */
55 private SimpleXML rootNode;
57 /** The configuration locator. */
58 private final ConfigurationLocator configurationLocator;
60 /** Where the configuration resides. */
61 private ConfigurationLocation configurationLocation;
64 * Creates a new configuration that is read from the given file.
66 * @param configurationLocator
67 * The configuration locator
68 * @param configurationLocation
69 * The configuration directory
71 public Configuration(ConfigurationLocator configurationLocator, ConfigurationLocation configurationLocation) {
72 this.configurationLocator = configurationLocator;
73 this.configurationLocation = configurationLocation;
74 readConfiguration(configurationLocator.getFile(configurationLocation));
82 * Returns the configuration locator.
84 * @return The configuration locator
86 public ConfigurationLocator getConfigurationLocator() {
87 return configurationLocator;
91 * Returns the location the configuration will be written to when calling
94 * @return The location the configuration will be written to
96 public ConfigurationLocation getConfigurationDirectory() {
97 return configurationLocation;
101 * Sets the location the configuration will be written to when calling
104 * @param configurationLocation
105 * The location to write the configuration to
107 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
108 this.configurationLocation = configurationLocation;
112 * Reads the configuration from the file.
115 * The name of the file to read the configuration from
117 private void readConfiguration(String filename) {
118 Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Reading configuration from: " + filename);
119 if (filename != null) {
120 File configurationFile = new File(filename);
121 if (configurationFile.exists()) {
122 ByteArrayOutputStream fileByteOutputStream = null;
123 FileInputStream fileInputStream = null;
125 fileByteOutputStream = new ByteArrayOutputStream();
126 fileInputStream = new FileInputStream(configurationFile);
127 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
128 fileByteOutputStream.close();
129 byte[] fileBytes = fileByteOutputStream.toByteArray();
130 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
132 } catch (FileNotFoundException e) {
134 } catch (IOException e) {
137 Closer.close(fileInputStream);
138 Closer.close(fileByteOutputStream);
142 rootNode = new SimpleXML("configuration");
146 * Saves the configuration.
148 * @return <code>true</code> if the configuration could be saved,
149 * <code>false</code> otherwise
151 public boolean save() {
152 Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Trying to save configuration to: " + configurationLocation);
153 File configurationFile = new File(configurationLocator.getFile(configurationLocation));
154 if (!configurationFile.exists()) {
155 File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
156 if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
160 FileOutputStream fileOutputStream = null;
161 ByteArrayInputStream configurationInputStream = null;
163 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
164 configurationInputStream = new ByteArrayInputStream(configurationBytes);
165 fileOutputStream = new FileOutputStream(configurationFile);
166 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
168 } catch (IOException ioe1) {
171 Closer.close(configurationInputStream);
172 Closer.close(fileOutputStream);
178 * Returns the value of a node.
181 * The name of all nodes in the chain
182 * @param defaultValue
183 * The default value to return if the node could not be found
184 * @return The value of the node, or the default value if the node could not
187 private String getNodeValue(String[] nodeNames, String defaultValue) {
188 SimpleXML node = rootNode;
190 while ((node != null) && (nodeIndex < nodeNames.length)) {
191 node = node.getNode(nodeNames[nodeIndex++]);
196 return node.getValue();
200 * Returns the integer value of a node.
203 * The names of all nodes in the chain
204 * @param defaultValue
205 * The default value to return if the node can not be found
206 * @return The parsed integer value, or the default value if the node can
207 * not be found or the value can not be parsed into an integer
209 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
211 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
212 } catch (NumberFormatException nfe1) {
219 * Returns the boolean value of a node.
222 * The names of all nodes in the chain
223 * @param defaultValue
224 * The default value to return if the node can not be found
225 * @return The parsed boolean value, or the default value if the node can
228 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
229 String nodeValue = getNodeValue(nodeNames, null);
230 if (nodeValue == null) {
233 return Boolean.parseBoolean(nodeValue);
237 * Returns the hostname of the node.
239 * @return The hostname of the node
240 * @deprecated Use {@link #getSelectedNode()} instead
243 public String getNodeAddress() {
244 return getNodeValue(new String[] { "node-address" }, "localhost");
248 * Sets the hostname of the node.
251 * The hostname of the node
252 * @deprecated Use {@link #setSelectedNode(Node)} instead
255 public void setNodeAddress(String nodeAddress) {
256 rootNode.replace("node-address", nodeAddress);
260 * The port number of the node
262 * @return The port number of the node
263 * @deprecated Use {@link #getSelectedNode()} instead.
266 public int getNodePort() {
267 return getNodeIntValue(new String[] { "node-port" }, 9481);
271 * Sets the port number of the node.
274 * The port number of the node
275 * @deprecated Use {@link #setSelectedNode(Node)} instead
278 public void setNodePort(int nodePort) {
279 rootNode.replace("node-port", String.valueOf(nodePort));
283 * Returns whether the node configuration page should be skipped on startup.
285 * @return <code>true</code> to skip the node configuration page on startup,
286 * <code>false</code> to show it
288 public boolean isSkipNodePage() {
289 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
293 * Sets whether the node configuration page should be skipped on startup.
295 * @param skipNodePage
296 * <code>true</code> to skip the node configuration page on
297 * startup, <code>false</code> to show it
299 public void setSkipNodePage(boolean skipNodePage) {
300 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
304 * Returns all configured projects.
306 * @return A list of all projects
308 public Project[] getProjects() {
309 List<Project> projects = new ArrayList<Project>();
310 SimpleXML projectsNode = rootNode.getNode("project-list");
311 if (projectsNode != null) {
312 SimpleXML[] projectNodes = projectsNode.getNodes("project");
313 for (SimpleXML projectNode : projectNodes) {
315 Project project = new Project();
316 projects.add(project);
317 project.setDescription(projectNode.getNode("description").getValue(""));
318 String indexFile = projectNode.getNode("index-file").getValue("");
319 if (indexFile.indexOf('/') > -1) {
322 project.setIndexFile(indexFile);
323 project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0")));
324 project.setLocalPath(projectNode.getNode("local-path").getValue(""));
325 project.setName(projectNode.getNode("name").getValue(""));
326 project.setPath(projectNode.getNode("path").getValue(""));
327 if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
328 project.setPath(project.getPath().replaceAll("/", ""));
330 project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0")));
331 project.setInsertURI(projectNode.getNode("insert-uri").getValue(""));
332 project.setRequestURI(projectNode.getNode("request-uri").getValue(""));
333 if (projectNode.getNode("ignore-hidden-files") != null) {
334 project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true")));
336 project.setIgnoreHiddenFiles(true);
338 SimpleXML fileOptionsNode = projectNode.getNode("file-options");
339 Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
340 if (fileOptionsNode != null) {
341 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
342 for (SimpleXML fileOptionNode : fileOptionNodes) {
343 String filename = fileOptionNode.getNode("filename").getValue();
344 FileOption fileOption = project.getFileOption(filename);
345 fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
346 if (fileOptionNode.getNode("insert-redirect") != null) {
347 fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
349 fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue(""));
350 if (fileOptionNode.getNode("changed-name") != null) {
351 fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
353 fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue(""));
354 fileOptions.put(filename, fileOption);
357 project.setFileOptions(fileOptions);
358 } catch (NumberFormatException nfe1) {
359 nfe1.printStackTrace();
363 return projects.toArray(new Project[projects.size()]);
367 * Sets the list of all projects.
370 * The list of all projects
372 public void setProjects(Project[] projects) {
373 SimpleXML projectsNode = new SimpleXML("project-list");
374 for (Project project : projects) {
375 SimpleXML projectNode = projectsNode.append("project");
376 projectNode.append("edition", String.valueOf(project.getEdition()));
377 projectNode.append("description", project.getDescription());
378 projectNode.append("index-file", project.getIndexFile());
379 projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
380 projectNode.append("local-path", project.getLocalPath());
381 projectNode.append("name", project.getName());
382 projectNode.append("path", project.getPath());
383 projectNode.append("insert-uri", project.getInsertURI());
384 projectNode.append("request-uri", project.getRequestURI());
385 projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles()));
386 SimpleXML fileOptionsNode = projectNode.append("file-options");
387 Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
388 while (entries.hasNext()) {
389 Entry<String, FileOption> entry = entries.next();
390 FileOption fileOption = entry.getValue();
391 if (fileOption.isCustom()) {
392 SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
393 fileOptionNode.append("filename", entry.getKey());
394 fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
395 fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect()));
396 fileOptionNode.append("custom-key", fileOption.getCustomKey());
397 fileOptionNode.append("changed-name", fileOption.getChangedName());
398 fileOptionNode.append("mime-type", fileOption.getMimeType());
402 rootNode.replace(projectsNode);
406 * Returns the stored locale.
408 * @return The stored locale
410 public Locale getLocale() {
411 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
412 String country = getNodeValue(new String[] { "i18n", "country" }, null);
413 if (country != null) {
414 return new Locale(language, country);
416 return new Locale(language);
420 * Sets the locale to store.
423 * The locale to store
425 public void setLocale(Locale locale) {
426 SimpleXML i18nNode = new SimpleXML("i18n");
427 if (locale.getCountry().length() != 0) {
428 i18nNode.append("country", locale.getCountry());
430 i18nNode.append("language", locale.getLanguage());
431 rootNode.replace(i18nNode);
436 * Returns a list of configured nodes.
438 * @return The list of the configured nodes
440 public Node[] getNodes() {
441 SimpleXML nodesNode = rootNode.getNode("nodes");
442 if (nodesNode == null) {
443 String hostname = getNodeAddress();
444 int port = getNodePort();
445 if (hostname == null) {
446 hostname = "127.0.0.1";
449 return new Node[] { new Node(hostname, port, "Node") };
451 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
452 Node[] returnNodes = new Node[nodeNodes.length];
454 for (SimpleXML nodeNode : nodeNodes) {
455 String name = nodeNode.getNode("name").getValue();
456 String hostname = nodeNode.getNode("hostname").getValue();
457 int port = Integer.parseInt(nodeNode.getNode("port").getValue());
458 Node node = new Node(hostname, port, name);
459 returnNodes[nodeIndex++] = node;
465 * Sets the list of configured nodes.
468 * The list of configured nodes
470 public void setNodes(Node[] nodes) {
471 SimpleXML nodesNode = new SimpleXML("nodes");
472 for (Node node : nodes) {
473 SimpleXML nodeNode = nodesNode.append("node");
474 nodeNode.append("name", node.getName());
475 nodeNode.append("hostname", node.getHostname());
476 nodeNode.append("port", String.valueOf(node.getPort()));
478 rootNode.replace(nodesNode);
479 rootNode.remove("node-address");
480 rootNode.remove("node-port");
484 * Sets the selected node.
486 * @param selectedNode
489 public void setSelectedNode(Node selectedNode) {
490 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
491 selectedNodeNode.append("name", selectedNode.getName());
492 selectedNodeNode.append("hostname", selectedNode.getHostname());
493 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
494 rootNode.replace(selectedNodeNode);
498 * Returns the selected node.
500 * @return The selected node
502 public Node getSelectedNode() {
503 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
504 if (selectedNodeNode == null) {
505 String hostname = getNodeAddress();
506 int port = getNodePort();
507 if (hostname == null) {
508 hostname = "127.0.0.1";
511 return new Node(hostname, port, "Node");
513 String name = selectedNodeNode.getNode("name").getValue();
514 String hostname = selectedNodeNode.getNode("hostname").getValue();
515 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
516 return new Node(hostname, port, name);
520 * Returns the temp directory to use.
522 * @return The temp directoy, or {@code null} to use the default temp
525 public String getTempDirectory() {
526 return getNodeValue(new String[] { "temp-directory" }, null);
530 * Sets the temp directory to use.
532 * @param tempDirectory
533 * The temp directory to use, or {@code null} to use the default
536 public void setTempDirectory(String tempDirectory) {
537 if (tempDirectory != null) {
538 SimpleXML tempDirectoryNode = new SimpleXML("temp-directory");
539 tempDirectoryNode.setValue(tempDirectory);
540 rootNode.replace(tempDirectoryNode);
542 rootNode.remove("temp-directory");