2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.main;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Locale;
35 import java.util.Map.Entry;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
39 import de.todesbaum.jsite.application.FileOption;
40 import de.todesbaum.jsite.application.Node;
41 import de.todesbaum.jsite.application.Project;
42 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
43 import de.todesbaum.util.io.Closer;
44 import de.todesbaum.util.io.StreamCopier;
45 import de.todesbaum.util.xml.SimpleXML;
46 import de.todesbaum.util.xml.XML;
51 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
53 public class Configuration {
55 /** The root node of the configuration. */
56 private SimpleXML rootNode;
58 /** The configuration locator. */
59 private final ConfigurationLocator configurationLocator;
61 /** Where the configuration resides. */
62 private ConfigurationLocation configurationLocation;
65 * Creates a new configuration that is read from the given file.
67 * @param configurationLocator
68 * The configuration locator
69 * @param configurationLocation
70 * The configuration directory
72 public Configuration(ConfigurationLocator configurationLocator, ConfigurationLocation configurationLocation) {
73 this.configurationLocator = configurationLocator;
74 this.configurationLocation = configurationLocation;
75 readConfiguration(configurationLocator.getFile(configurationLocation));
83 * Returns the configuration locator.
85 * @return The configuration locator
87 public ConfigurationLocator getConfigurationLocator() {
88 return configurationLocator;
92 * Returns the location the configuration will be written to when calling
95 * @return The location the configuration will be written to
97 public ConfigurationLocation getConfigurationDirectory() {
98 return configurationLocation;
102 * Sets the location the configuration will be written to when calling
105 * @param configurationLocation
106 * The location to write the configuration to
108 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
109 this.configurationLocation = configurationLocation;
113 * Reads the configuration from the file.
116 * The name of the file to read the configuration from
118 private void readConfiguration(String filename) {
119 Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Reading configuration from: " + filename);
120 if (filename != null) {
121 File configurationFile = new File(filename);
122 if (configurationFile.exists()) {
123 ByteArrayOutputStream fileByteOutputStream = null;
124 FileInputStream fileInputStream = null;
126 fileByteOutputStream = new ByteArrayOutputStream();
127 fileInputStream = new FileInputStream(configurationFile);
128 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
129 fileByteOutputStream.close();
130 byte[] fileBytes = fileByteOutputStream.toByteArray();
131 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
133 } catch (FileNotFoundException e) {
135 } catch (IOException e) {
138 Closer.close(fileInputStream);
139 Closer.close(fileByteOutputStream);
143 rootNode = new SimpleXML("configuration");
147 * Saves the configuration.
149 * @return <code>true</code> if the configuration could be saved,
150 * <code>false</code> otherwise
152 public boolean save() {
153 Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Trying to save configuration to: " + configurationLocation);
154 File configurationFile = new File(configurationLocator.getFile(configurationLocation));
155 if (!configurationFile.exists()) {
156 File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
157 if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
161 FileOutputStream fileOutputStream = null;
162 ByteArrayInputStream configurationInputStream = null;
164 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
165 configurationInputStream = new ByteArrayInputStream(configurationBytes);
166 fileOutputStream = new FileOutputStream(configurationFile);
167 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
169 } catch (IOException ioe1) {
172 Closer.close(configurationInputStream);
173 Closer.close(fileOutputStream);
179 * Returns the value of a node.
182 * The name of all nodes in the chain
183 * @param defaultValue
184 * The default value to return if the node could not be found
185 * @return The value of the node, or the default value if the node could not
188 private String getNodeValue(String[] nodeNames, String defaultValue) {
189 SimpleXML node = rootNode;
191 while ((node != null) && (nodeIndex < nodeNames.length)) {
192 node = node.getNode(nodeNames[nodeIndex++]);
197 return node.getValue();
201 * Returns the integer value of a node.
204 * The names of all nodes in the chain
205 * @param defaultValue
206 * The default value to return if the node can not be found
207 * @return The parsed integer value, or the default value if the node can
208 * not be found or the value can not be parsed into an integer
210 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
212 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
213 } catch (NumberFormatException nfe1) {
220 * Returns the boolean value of a node.
223 * The names of all nodes in the chain
224 * @param defaultValue
225 * The default value to return if the node can not be found
226 * @return The parsed boolean value, or the default value if the node can
229 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
230 String nodeValue = getNodeValue(nodeNames, null);
231 if (nodeValue == null) {
234 return Boolean.parseBoolean(nodeValue);
238 * Returns the hostname of the node.
240 * @return The hostname of the node
241 * @deprecated Use {@link #getSelectedNode()} instead
244 public String getNodeAddress() {
245 return getNodeValue(new String[] { "node-address" }, "localhost");
249 * Sets the hostname of the node.
252 * The hostname of the node
253 * @deprecated Use {@link #setSelectedNode(Node)} instead
256 public void setNodeAddress(String nodeAddress) {
257 rootNode.replace("node-address", nodeAddress);
261 * The port number of the node
263 * @return The port number of the node
264 * @deprecated Use {@link #getSelectedNode()} instead.
267 public int getNodePort() {
268 return getNodeIntValue(new String[] { "node-port" }, 9481);
272 * Sets the port number of the node.
275 * The port number of the node
276 * @deprecated Use {@link #setSelectedNode(Node)} instead
279 public void setNodePort(int nodePort) {
280 rootNode.replace("node-port", String.valueOf(nodePort));
284 * Returns whether the node configuration page should be skipped on startup.
286 * @return <code>true</code> to skip the node configuration page on startup,
287 * <code>false</code> to show it
289 public boolean isSkipNodePage() {
290 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
294 * Sets whether the node configuration page should be skipped on startup.
296 * @param skipNodePage
297 * <code>true</code> to skip the node configuration page on
298 * startup, <code>false</code> to show it
300 public void setSkipNodePage(boolean skipNodePage) {
301 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
305 * Returns all configured projects.
307 * @return A list of all projects
309 public Project[] getProjects() {
310 List<Project> projects = new ArrayList<Project>();
311 SimpleXML projectsNode = rootNode.getNode("project-list");
312 if (projectsNode != null) {
313 SimpleXML[] projectNodes = projectsNode.getNodes("project");
314 for (SimpleXML projectNode : projectNodes) {
316 Project project = new Project();
317 projects.add(project);
318 project.setDescription(projectNode.getNode("description").getValue(""));
319 String indexFile = projectNode.getNode("index-file").getValue("");
320 if (indexFile.indexOf('/') > -1) {
323 project.setIndexFile(indexFile);
324 project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0")));
325 project.setLocalPath(projectNode.getNode("local-path").getValue(""));
326 project.setName(projectNode.getNode("name").getValue(""));
327 project.setPath(projectNode.getNode("path").getValue(""));
328 if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
329 project.setPath(project.getPath().replaceAll("/", ""));
331 project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0")));
332 project.setInsertURI(projectNode.getNode("insert-uri").getValue(""));
333 project.setRequestURI(projectNode.getNode("request-uri").getValue(""));
334 if (projectNode.getNode("ignore-hidden-files") != null) {
335 project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true")));
337 project.setIgnoreHiddenFiles(true);
339 SimpleXML fileOptionsNode = projectNode.getNode("file-options");
340 Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
341 if (fileOptionsNode != null) {
342 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
343 for (SimpleXML fileOptionNode : fileOptionNodes) {
344 String filename = fileOptionNode.getNode("filename").getValue();
345 FileOption fileOption = project.getFileOption(filename);
346 fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
347 if (fileOptionNode.getNode("insert-redirect") != null) {
348 fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
350 fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue(""));
351 if (fileOptionNode.getNode("changed-name") != null) {
352 fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
354 fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue(""));
355 fileOption.setContainer(fileOptionNode.getNode("container").getValue());
356 if (fileOptionNode.getNode("replace-edition") != null) {
357 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
358 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
360 fileOptions.put(filename, fileOption);
363 project.setFileOptions(fileOptions);
364 } catch (NumberFormatException nfe1) {
365 nfe1.printStackTrace();
369 return projects.toArray(new Project[projects.size()]);
373 * Sets the list of all projects.
376 * The list of all projects
378 public void setProjects(Project[] projects) {
379 SimpleXML projectsNode = new SimpleXML("project-list");
380 for (Project project : projects) {
381 SimpleXML projectNode = projectsNode.append("project");
382 projectNode.append("edition", String.valueOf(project.getEdition()));
383 projectNode.append("description", project.getDescription());
384 projectNode.append("index-file", project.getIndexFile());
385 projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
386 projectNode.append("local-path", project.getLocalPath());
387 projectNode.append("name", project.getName());
388 projectNode.append("path", project.getPath());
389 projectNode.append("insert-uri", project.getInsertURI());
390 projectNode.append("request-uri", project.getRequestURI());
391 projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles()));
392 SimpleXML fileOptionsNode = projectNode.append("file-options");
393 Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
394 while (entries.hasNext()) {
395 Entry<String, FileOption> entry = entries.next();
396 FileOption fileOption = entry.getValue();
397 if (fileOption.isCustom()) {
398 SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
399 fileOptionNode.append("filename", entry.getKey());
400 fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
401 fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect()));
402 fileOptionNode.append("custom-key", fileOption.getCustomKey());
403 fileOptionNode.append("changed-name", fileOption.getChangedName());
404 fileOptionNode.append("mime-type", fileOption.getMimeType());
405 fileOptionNode.append("container", fileOption.getContainer());
406 fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
407 fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
411 rootNode.replace(projectsNode);
415 * Returns the stored locale.
417 * @return The stored locale
419 public Locale getLocale() {
420 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
421 String country = getNodeValue(new String[] { "i18n", "country" }, null);
422 if (country != null) {
423 return new Locale(language, country);
425 return new Locale(language);
429 * Sets the locale to store.
432 * The locale to store
434 public void setLocale(Locale locale) {
435 SimpleXML i18nNode = new SimpleXML("i18n");
436 if (locale.getCountry().length() != 0) {
437 i18nNode.append("country", locale.getCountry());
439 i18nNode.append("language", locale.getLanguage());
440 rootNode.replace(i18nNode);
445 * Returns a list of configured nodes.
447 * @return The list of the configured nodes
449 public Node[] getNodes() {
450 SimpleXML nodesNode = rootNode.getNode("nodes");
451 if (nodesNode == null) {
452 String hostname = getNodeAddress();
453 int port = getNodePort();
454 if (hostname == null) {
455 hostname = "127.0.0.1";
458 return new Node[] { new Node(hostname, port, "Node") };
460 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
461 Node[] returnNodes = new Node[nodeNodes.length];
463 for (SimpleXML nodeNode : nodeNodes) {
464 String name = nodeNode.getNode("name").getValue();
465 String hostname = nodeNode.getNode("hostname").getValue();
466 int port = Integer.parseInt(nodeNode.getNode("port").getValue());
467 Node node = new Node(hostname, port, name);
468 returnNodes[nodeIndex++] = node;
474 * Sets the list of configured nodes.
477 * The list of configured nodes
479 public void setNodes(Node[] nodes) {
480 SimpleXML nodesNode = new SimpleXML("nodes");
481 for (Node node : nodes) {
482 SimpleXML nodeNode = nodesNode.append("node");
483 nodeNode.append("name", node.getName());
484 nodeNode.append("hostname", node.getHostname());
485 nodeNode.append("port", String.valueOf(node.getPort()));
487 rootNode.replace(nodesNode);
488 rootNode.remove("node-address");
489 rootNode.remove("node-port");
493 * Sets the selected node.
495 * @param selectedNode
498 public void setSelectedNode(Node selectedNode) {
499 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
500 selectedNodeNode.append("name", selectedNode.getName());
501 selectedNodeNode.append("hostname", selectedNode.getHostname());
502 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
503 rootNode.replace(selectedNodeNode);
507 * Returns the selected node.
509 * @return The selected node
511 public Node getSelectedNode() {
512 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
513 if (selectedNodeNode == null) {
514 String hostname = getNodeAddress();
515 int port = getNodePort();
516 if (hostname == null) {
517 hostname = "127.0.0.1";
520 return new Node(hostname, port, "Node");
522 String name = selectedNodeNode.getNode("name").getValue();
523 String hostname = selectedNodeNode.getNode("hostname").getValue();
524 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
525 return new Node(hostname, port, name);
529 * Returns the temp directory to use.
531 * @return The temp directoy, or {@code null} to use the default temp
534 public String getTempDirectory() {
535 return getNodeValue(new String[] { "temp-directory" }, null);
539 * Sets the temp directory to use.
541 * @param tempDirectory
542 * The temp directory to use, or {@code null} to use the default
545 public void setTempDirectory(String tempDirectory) {
546 if (tempDirectory != null) {
547 SimpleXML tempDirectoryNode = new SimpleXML("temp-directory");
548 tempDirectoryNode.setValue(tempDirectory);
549 rootNode.replace(tempDirectoryNode);
551 rootNode.remove("temp-directory");