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;
37 import de.todesbaum.jsite.application.FileOption;
38 import de.todesbaum.jsite.application.Node;
39 import de.todesbaum.jsite.application.Project;
40 import de.todesbaum.util.io.Closer;
41 import de.todesbaum.util.io.StreamCopier;
42 import de.todesbaum.util.xml.SimpleXML;
43 import de.todesbaum.util.xml.XML;
48 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
50 public class Configuration {
53 * The location of the configuration directory.
55 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
57 public enum ConfigurationDirectory {
59 /** The configuration is in the same directory as the JAR file. */
63 * The configuration is in the user’s home directory. This is the
70 /** The configuration directory. */
71 private ConfigurationDirectory configurationDirectory = ConfigurationDirectory.HOME_DIRECTORY;
73 /** The name of the file the configuration is stored to. */
74 private String filename;
76 /** The name of the lock file. */
77 private String lockFilename;
79 /** The root node of the configuration. */
80 private SimpleXML rootNode;
83 * Creates a new configuration with the default name of the configuration
86 public Configuration() {
87 this(System.getProperty("user.home") + "/.jSite/config7");
91 * Creates a new configuration that is read from the given file.
94 * The name of the configuration file
96 public Configuration(String filename) {
97 this(filename, filename + ".lock");
101 * Creates a new configuration that is read from the given file and uses the
105 * The name of the configuration file
106 * @param lockFilename
107 * The name of the lock file
109 public Configuration(String filename, String lockFilename) {
110 this.filename = filename;
111 this.lockFilename = lockFilename;
116 * Returns the configuration directory.
118 * @return The configuration directory
120 public ConfigurationDirectory getConfigurationDirectory() {
121 return configurationDirectory;
125 * Sets the configuration directory.
127 * @param configurationDirectory
128 * The configuration directory
130 public void setConfigurationDirectory(ConfigurationDirectory configurationDirectory) {
131 this.configurationDirectory = configurationDirectory;
135 * Creates the directory of the configuration file.
137 * @return <code>true</code> if the directory exists, or if it could be
138 * created, <code>false</code> otherwise
140 private boolean createConfigDirectory() {
141 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
142 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
146 * Creates the lock file.
148 * @return <code>true</code> if the lock file did not already exist and
149 * could be created, <code>false</code> otherwise
151 public boolean createLockFile() {
152 if (!createConfigDirectory()) {
155 File lockFile = new File(lockFilename);
157 boolean fileLocked = lockFile.createNewFile();
159 lockFile.deleteOnExit();
162 } catch (IOException e) {
169 * Tells the VM to remove the lock file on program exit.
171 public void removeLockfileOnExit() {
172 new File(lockFilename).deleteOnExit();
176 * Reads the configuration from the file.
178 private void readConfiguration() {
179 File configurationFile = new File(filename);
180 if (configurationFile.exists()) {
181 ByteArrayOutputStream fileByteOutputStream = null;
182 FileInputStream fileInputStream = null;
184 fileByteOutputStream = new ByteArrayOutputStream();
185 fileInputStream = new FileInputStream(configurationFile);
186 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
187 fileByteOutputStream.close();
188 byte[] fileBytes = fileByteOutputStream.toByteArray();
189 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
191 } catch (FileNotFoundException e) {
193 } catch (IOException e) {
196 Closer.close(fileInputStream);
197 Closer.close(fileByteOutputStream);
200 rootNode = new SimpleXML("configuration");
204 * Saves the configuration.
206 * @return <code>true</code> if the configuration could be saved,
207 * <code>false</code> otherwise
209 public boolean save() {
210 File configurationFile = new File(filename);
211 if (!configurationFile.exists()) {
212 File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
213 if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
217 FileOutputStream fileOutputStream = null;
218 ByteArrayInputStream configurationInputStream = null;
220 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
221 configurationInputStream = new ByteArrayInputStream(configurationBytes);
222 fileOutputStream = new FileOutputStream(configurationFile);
223 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
225 } catch (IOException ioe1) {
228 Closer.close(configurationInputStream);
229 Closer.close(fileOutputStream);
235 * Returns the value of a node.
238 * The name of all nodes in the chain
239 * @param defaultValue
240 * The default value to return if the node could not be found
241 * @return The value of the node, or the default value if the node could not
244 private String getNodeValue(String[] nodeNames, String defaultValue) {
245 SimpleXML node = rootNode;
247 while ((node != null) && (nodeIndex < nodeNames.length)) {
248 node = node.getNode(nodeNames[nodeIndex++]);
253 return node.getValue();
257 * Returns the integer value of a node.
260 * The names of all nodes in the chain
261 * @param defaultValue
262 * The default value to return if the node can not be found
263 * @return The parsed integer value, or the default value if the node can
264 * not be found or the value can not be parsed into an integer
266 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
268 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
269 } catch (NumberFormatException nfe1) {
276 * Returns the boolean value of a node.
279 * The names of all nodes in the chain
280 * @param defaultValue
281 * The default value to return if the node can not be found
282 * @return The parsed boolean value, or the default value if the node can
285 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
286 String nodeValue = getNodeValue(nodeNames, null);
287 if (nodeValue == null) {
290 return Boolean.parseBoolean(nodeValue);
294 * Returns the hostname of the node.
296 * @return The hostname of the node
297 * @deprecated Use {@link #getSelectedNode()} instead
300 public String getNodeAddress() {
301 return getNodeValue(new String[] { "node-address" }, "localhost");
305 * Sets the hostname of the node.
308 * The hostname of the node
309 * @deprecated Use {@link #setSelectedNode(Node)} instead
312 public void setNodeAddress(String nodeAddress) {
313 rootNode.replace("node-address", nodeAddress);
317 * The port number of the node
319 * @return The port number of the node
320 * @deprecated Use {@link #getSelectedNode()} instead.
323 public int getNodePort() {
324 return getNodeIntValue(new String[] { "node-port" }, 9481);
328 * Sets the port number of the node.
331 * The port number of the node
332 * @deprecated Use {@link #setSelectedNode(Node)} instead
335 public void setNodePort(int nodePort) {
336 rootNode.replace("node-port", String.valueOf(nodePort));
340 * Returns whether the node configuration page should be skipped on startup.
342 * @return <code>true</code> to skip the node configuration page on startup,
343 * <code>false</code> to show it
345 public boolean isSkipNodePage() {
346 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
350 * Sets whether the node configuration page should be skipped on startup.
352 * @param skipNodePage
353 * <code>true</code> to skip the node configuration page on
354 * startup, <code>false</code> to show it
356 public void setSkipNodePage(boolean skipNodePage) {
357 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
361 * Returns all configured projects.
363 * @return A list of all projects
365 public Project[] getProjects() {
366 List<Project> projects = new ArrayList<Project>();
367 SimpleXML projectsNode = rootNode.getNode("project-list");
368 if (projectsNode != null) {
369 SimpleXML[] projectNodes = projectsNode.getNodes("project");
370 for (SimpleXML projectNode : projectNodes) {
372 Project project = new Project();
373 projects.add(project);
374 project.setDescription(projectNode.getNode("description").getValue(""));
375 String indexFile = projectNode.getNode("index-file").getValue("");
376 if (indexFile.indexOf('/') > -1) {
379 project.setIndexFile(indexFile);
380 project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0")));
381 project.setLocalPath(projectNode.getNode("local-path").getValue(""));
382 project.setName(projectNode.getNode("name").getValue(""));
383 project.setPath(projectNode.getNode("path").getValue(""));
384 if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
385 project.setPath(project.getPath().replaceAll("/", ""));
387 project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0")));
388 project.setInsertURI(projectNode.getNode("insert-uri").getValue(""));
389 project.setRequestURI(projectNode.getNode("request-uri").getValue(""));
390 if (projectNode.getNode("ignore-hidden-files") != null) {
391 project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true")));
393 project.setIgnoreHiddenFiles(true);
395 SimpleXML fileOptionsNode = projectNode.getNode("file-options");
396 Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
397 if (fileOptionsNode != null) {
398 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
399 for (SimpleXML fileOptionNode : fileOptionNodes) {
400 String filename = fileOptionNode.getNode("filename").getValue();
401 FileOption fileOption = project.getFileOption(filename);
402 fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
403 if (fileOptionNode.getNode("insert-redirect") != null) {
404 fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
406 fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue(""));
407 if (fileOptionNode.getNode("changed-name") != null) {
408 fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
410 fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue(""));
411 fileOption.setContainer(fileOptionNode.getNode("container").getValue());
412 if (fileOptionNode.getNode("replace-edition") != null) {
413 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
414 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
416 fileOptions.put(filename, fileOption);
419 project.setFileOptions(fileOptions);
420 } catch (NumberFormatException nfe1) {
421 nfe1.printStackTrace();
425 return projects.toArray(new Project[projects.size()]);
429 * Sets the list of all projects.
432 * The list of all projects
434 public void setProjects(Project[] projects) {
435 SimpleXML projectsNode = new SimpleXML("project-list");
436 for (Project project : projects) {
437 SimpleXML projectNode = projectsNode.append("project");
438 projectNode.append("edition", String.valueOf(project.getEdition()));
439 projectNode.append("description", project.getDescription());
440 projectNode.append("index-file", project.getIndexFile());
441 projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
442 projectNode.append("local-path", project.getLocalPath());
443 projectNode.append("name", project.getName());
444 projectNode.append("path", project.getPath());
445 projectNode.append("insert-uri", project.getInsertURI());
446 projectNode.append("request-uri", project.getRequestURI());
447 projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles()));
448 SimpleXML fileOptionsNode = projectNode.append("file-options");
449 Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
450 while (entries.hasNext()) {
451 Entry<String, FileOption> entry = entries.next();
452 FileOption fileOption = entry.getValue();
453 if (fileOption.isCustom()) {
454 SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
455 fileOptionNode.append("filename", entry.getKey());
456 fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
457 fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect()));
458 fileOptionNode.append("custom-key", fileOption.getCustomKey());
459 fileOptionNode.append("changed-name", fileOption.getChangedName());
460 fileOptionNode.append("mime-type", fileOption.getMimeType());
461 fileOptionNode.append("container", fileOption.getContainer());
462 fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
463 fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
467 rootNode.replace(projectsNode);
471 * Returns the stored locale.
473 * @return The stored locale
475 public Locale getLocale() {
476 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
477 String country = getNodeValue(new String[] { "i18n", "country" }, null);
478 if (country != null) {
479 return new Locale(language, country);
481 return new Locale(language);
485 * Sets the locale to store.
488 * The locale to store
490 public void setLocale(Locale locale) {
491 SimpleXML i18nNode = new SimpleXML("i18n");
492 if (locale.getCountry().length() != 0) {
493 i18nNode.append("country", locale.getCountry());
495 i18nNode.append("language", locale.getLanguage());
496 rootNode.replace(i18nNode);
501 * Returns a list of configured nodes.
503 * @return The list of the configured nodes
505 public Node[] getNodes() {
506 SimpleXML nodesNode = rootNode.getNode("nodes");
507 if (nodesNode == null) {
508 String hostname = getNodeAddress();
509 int port = getNodePort();
510 if (hostname == null) {
511 hostname = "127.0.0.1";
514 return new Node[] { new Node(hostname, port, "Node") };
516 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
517 Node[] returnNodes = new Node[nodeNodes.length];
519 for (SimpleXML nodeNode : nodeNodes) {
520 String name = nodeNode.getNode("name").getValue();
521 String hostname = nodeNode.getNode("hostname").getValue();
522 int port = Integer.parseInt(nodeNode.getNode("port").getValue());
523 Node node = new Node(hostname, port, name);
524 returnNodes[nodeIndex++] = node;
530 * Sets the list of configured nodes.
533 * The list of configured nodes
535 public void setNodes(Node[] nodes) {
536 SimpleXML nodesNode = new SimpleXML("nodes");
537 for (Node node : nodes) {
538 SimpleXML nodeNode = nodesNode.append("node");
539 nodeNode.append("name", node.getName());
540 nodeNode.append("hostname", node.getHostname());
541 nodeNode.append("port", String.valueOf(node.getPort()));
543 rootNode.replace(nodesNode);
544 rootNode.remove("node-address");
545 rootNode.remove("node-port");
549 * Sets the selected node.
551 * @param selectedNode
554 public void setSelectedNode(Node selectedNode) {
555 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
556 selectedNodeNode.append("name", selectedNode.getName());
557 selectedNodeNode.append("hostname", selectedNode.getHostname());
558 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
559 rootNode.replace(selectedNodeNode);
563 * Returns the selected node.
565 * @return The selected node
567 public Node getSelectedNode() {
568 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
569 if (selectedNodeNode == null) {
570 String hostname = getNodeAddress();
571 int port = getNodePort();
572 if (hostname == null) {
573 hostname = "127.0.0.1";
576 return new Node(hostname, port, "Node");
578 String name = selectedNodeNode.getNode("name").getValue();
579 String hostname = selectedNodeNode.getNode("hostname").getValue();
580 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
581 return new Node(hostname, port, name);
585 * Returns the temp directory to use.
587 * @return The temp directoy, or {@code null} to use the default temp
590 public String getTempDirectory() {
591 return getNodeValue(new String[] { "temp-directory" }, null);
595 * Sets the temp directory to use.
597 * @param tempDirectory
598 * The temp directory to use, or {@code null} to use the default
601 public void setTempDirectory(String tempDirectory) {
602 if (tempDirectory != null) {
603 SimpleXML tempDirectoryNode = new SimpleXML("temp-directory");
604 tempDirectoryNode.setValue(tempDirectory);
605 rootNode.replace(tempDirectoryNode);
607 rootNode.remove("temp-directory");