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 {
52 /** The name of the file the configuration is stored to. */
53 private String filename;
55 /** The name of the lock file. */
56 private String lockFilename;
58 /** The root node of the configuration. */
59 private SimpleXML rootNode;
62 * Creates a new configuration with the default name of the configuration
65 public Configuration() {
66 this(System.getProperty("user.home") + "/.jSite/config7");
70 * Creates a new configuration that is read from the given file.
73 * The name of the configuration file
75 public Configuration(String filename) {
76 this(filename, filename + ".lock");
80 * Creates a new configuration that is read from the given file and uses the
84 * The name of the configuration file
86 * The name of the lock file
88 public Configuration(String filename, String lockFilename) {
89 this.filename = filename;
90 this.lockFilename = lockFilename;
95 * Creates the directory of the configuration file.
97 * @return <code>true</code> if the directory exists, or if it could be
98 * created, <code>false</code> otherwise
100 private boolean createConfigDirectory() {
101 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
102 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
106 * Creates the lock file.
108 * @return <code>true</code> if the lock file did not already exist and
109 * could be created, <code>false</code> otherwise
111 public boolean createLockFile() {
112 if (!createConfigDirectory()) {
115 File lockFile = new File(lockFilename);
116 lockFile.deleteOnExit();
118 return lockFile.createNewFile();
119 } catch (IOException e) {
126 * Reads the configuration from the file.
128 private void readConfiguration() {
129 File configurationFile = new File(filename);
130 if (configurationFile.exists()) {
131 ByteArrayOutputStream fileByteOutputStream = null;
132 FileInputStream fileInputStream = null;
134 fileByteOutputStream = new ByteArrayOutputStream();
135 fileInputStream = new FileInputStream(configurationFile);
136 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
137 fileByteOutputStream.close();
138 byte[] fileBytes = fileByteOutputStream.toByteArray();
139 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
141 } catch (FileNotFoundException e) {
143 } catch (IOException e) {
146 Closer.close(fileInputStream);
147 Closer.close(fileByteOutputStream);
150 rootNode = new SimpleXML("configuration");
154 * Saves the configuration.
156 * @return <code>true</code> if the configuration could be saved,
157 * <code>false</code> otherwise
159 public boolean save() {
160 File configurationFile = new File(filename);
161 if (!configurationFile.exists()) {
162 File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
163 if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
167 FileOutputStream fileOutputStream = null;
168 ByteArrayInputStream configurationInputStream = null;
170 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
171 configurationInputStream = new ByteArrayInputStream(configurationBytes);
172 fileOutputStream = new FileOutputStream(configurationFile);
173 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
175 } catch (IOException ioe1) {
178 Closer.close(configurationInputStream);
179 Closer.close(fileOutputStream);
185 * Returns the value of a node.
188 * The name of all nodes in the chain
189 * @param defaultValue
190 * The default value to return if the node could not be found
191 * @return The value of the node, or the default value if the node could not
194 private String getNodeValue(String[] nodeNames, String defaultValue) {
195 SimpleXML node = rootNode;
197 while ((node != null) && (nodeIndex < nodeNames.length)) {
198 node = node.getNode(nodeNames[nodeIndex++]);
203 return node.getValue();
207 * Returns the integer value of a node.
210 * The names of all nodes in the chain
211 * @param defaultValue
212 * The default value to return if the node can not be found
213 * @return The parsed integer value, or the default value if the node can
214 * not be found or the value can not be parsed into an integer
216 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
218 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
219 } catch (NumberFormatException nfe1) {
226 * Returns the boolean value of a node.
229 * The names of all nodes in the chain
230 * @param defaultValue
231 * The default value to return if the node can not be found
232 * @return The parsed boolean value, or the default value if the node can
235 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
236 String nodeValue = getNodeValue(nodeNames, null);
237 if (nodeValue == null) {
240 return Boolean.parseBoolean(nodeValue);
244 * Returns the hostname of the node.
246 * @return The hostname of the node
247 * @deprecated Use {@link #getSelectedNode()} instead
250 public String getNodeAddress() {
251 return getNodeValue(new String[] { "node-address" }, "localhost");
255 * Sets the hostname of the node.
258 * The hostname of the node
259 * @deprecated Use {@link #setSelectedNode(Node)} instead
262 public void setNodeAddress(String nodeAddress) {
263 rootNode.replace("node-address", nodeAddress);
267 * The port number of the node
269 * @return The port number of the node
270 * @deprecated Use {@link #getSelectedNode()} instead.
273 public int getNodePort() {
274 return getNodeIntValue(new String[] { "node-port" }, 9481);
278 * Sets the port number of the node.
281 * The port number of the node
282 * @deprecated Use {@link #setSelectedNode(Node)} instead
285 public void setNodePort(int nodePort) {
286 rootNode.replace("node-port", String.valueOf(nodePort));
290 * Returns whether the node configuration page should be skipped on startup.
292 * @return <code>true</code> to skip the node configuration page on
293 * startup, <code>false</code> to show it
295 public boolean isSkipNodePage() {
296 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
300 * Sets whether the node configuration page should be skipped on startup.
302 * @param skipNodePage
303 * <code>true</code> to skip the node configuration page on
304 * startup, <code>false</code> to show it
306 public void setSkipNodePage(boolean skipNodePage) {
307 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
311 * Returns all configured projects.
313 * @return A list of all projects
315 public Project[] getProjects() {
316 List<Project> projects = new ArrayList<Project>();
317 SimpleXML projectsNode = rootNode.getNode("project-list");
318 if (projectsNode != null) {
319 SimpleXML[] projectNodes = projectsNode.getNodes("project");
320 for (SimpleXML projectNode : projectNodes) {
322 Project project = new Project();
323 projects.add(project);
324 project.setDescription(projectNode.getNode("description").getValue());
325 project.setIndexFile(projectNode.getNode("index-file").getValue());
326 project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
327 project.setLocalPath(projectNode.getNode("local-path").getValue());
328 project.setName(projectNode.getNode("name").getValue());
329 project.setPath(projectNode.getNode("path").getValue());
330 if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
331 project.setPath(project.getPath().replaceAll("/", ""));
333 project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
334 project.setInsertURI(projectNode.getNode("insert-uri").getValue());
335 project.setRequestURI(projectNode.getNode("request-uri").getValue());
336 SimpleXML fileOptionsNode = projectNode.getNode("file-options");
337 Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
338 if (fileOptionsNode != null) {
339 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
340 for (SimpleXML fileOptionNode : fileOptionNodes) {
341 String filename = fileOptionNode.getNode("filename").getValue();
342 FileOption fileOption = project.getFileOption(filename);
343 fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
344 fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
345 fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
346 fileOption.setContainer(fileOptionNode.getNode("container").getValue());
347 if (fileOptionNode.getNode("replace-edition") != null) {
348 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
349 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
351 fileOptions.put(filename, fileOption);
354 project.setFileOptions(fileOptions);
355 } catch (NumberFormatException nfe1) {
356 nfe1.printStackTrace();
360 return projects.toArray(new Project[projects.size()]);
364 * Sets the list of all projects.
367 * The list of all projects
369 public void setProjects(Project[] projects) {
370 SimpleXML projectsNode = new SimpleXML("project-list");
371 for (Project project : projects) {
372 SimpleXML projectNode = projectsNode.append("project");
373 projectNode.append("edition", String.valueOf(project.getEdition()));
374 projectNode.append("description", project.getDescription());
375 projectNode.append("index-file", project.getIndexFile());
376 projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
377 projectNode.append("local-path", project.getLocalPath());
378 projectNode.append("name", project.getName());
379 projectNode.append("path", project.getPath());
380 projectNode.append("insert-uri", project.getInsertURI());
381 projectNode.append("request-uri", project.getRequestURI());
382 SimpleXML fileOptionsNode = projectNode.append("file-options");
383 Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
384 while (entries.hasNext()) {
385 Entry<String, FileOption> entry = entries.next();
386 FileOption fileOption = entry.getValue();
387 if (fileOption.isCustom()) {
388 SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
389 fileOptionNode.append("filename", entry.getKey());
390 fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
391 fileOptionNode.append("custom-key", fileOption.getCustomKey());
392 fileOptionNode.append("mime-type", fileOption.getMimeType());
393 fileOptionNode.append("container", fileOption.getContainer());
394 fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
395 fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
399 rootNode.replace(projectsNode);
403 * Returns the stored locale.
405 * @return The stored locale
407 public Locale getLocale() {
408 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
409 String country = getNodeValue(new String[] { "i18n", "country" }, null);
410 if (country != null) {
411 return new Locale(language, country);
413 return new Locale(language);
417 * Sets the locale to store.
420 * The locale to store
422 public void setLocale(Locale locale) {
423 SimpleXML i18nNode = new SimpleXML("i18n");
424 if (locale.getCountry().length() != 0) {
425 i18nNode.append("country", locale.getCountry());
427 i18nNode.append("language", locale.getLanguage());
428 rootNode.replace(i18nNode);
433 * Returns a list of configured nodes.
435 * @return The list of the configured nodes
437 public Node[] getNodes() {
438 SimpleXML nodesNode = rootNode.getNode("nodes");
439 if (nodesNode == null) {
440 String hostname = getNodeAddress();
441 int port = getNodePort();
442 if (hostname == null) {
443 hostname = "127.0.0.1";
446 return new Node[] { new Node(hostname, port, "Node") };
448 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
449 Node[] returnNodes = new Node[nodeNodes.length];
451 for (SimpleXML nodeNode : nodeNodes) {
452 String name = nodeNode.getNode("name").getValue();
453 String hostname = nodeNode.getNode("hostname").getValue();
454 int port = Integer.parseInt(nodeNode.getNode("port").getValue());
455 Node node = new Node(hostname, port, name);
456 returnNodes[nodeIndex++] = node;
462 * Sets the list of configured nodes.
465 * The list of configured nodes
467 public void setNodes(Node[] nodes) {
468 SimpleXML nodesNode = new SimpleXML("nodes");
469 for (Node node : nodes) {
470 SimpleXML nodeNode = nodesNode.append("node");
471 nodeNode.append("name", node.getName());
472 nodeNode.append("hostname", node.getHostname());
473 nodeNode.append("port", String.valueOf(node.getPort()));
475 rootNode.replace(nodesNode);
476 rootNode.remove("node-address");
477 rootNode.remove("node-port");
481 * Sets the selected node.
483 * @param selectedNode
486 public void setSelectedNode(Node selectedNode) {
487 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
488 selectedNodeNode.append("name", selectedNode.getName());
489 selectedNodeNode.append("hostname", selectedNode.getHostname());
490 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
491 rootNode.replace(selectedNodeNode);
495 * Returns the selected node.
497 * @return The selected node
499 public Node getSelectedNode() {
500 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
501 if (selectedNodeNode == null) {
502 String hostname = getNodeAddress();
503 int port = getNodePort();
504 if (hostname == null) {
505 hostname = "127.0.0.1";
508 return new Node(hostname, port, "Node");
510 String name = selectedNodeNode.getNode("name").getValue();
511 String hostname = selectedNodeNode.getNode("hostname").getValue();
512 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
513 return new Node(hostname, port, name);