1240f329468e62c28adefa1b155e2b1ba03b96d2
[jSite.git] / src / main / java / de / todesbaum / jsite / main / Configuration.java
1 /*
2  * jSite - Configuration.java - Copyright © 2006–2012 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.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
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;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37
38 import net.pterodactylus.util.io.Closer;
39 import net.pterodactylus.util.io.StreamCopier;
40 import net.pterodactylus.util.xml.SimpleXML;
41 import net.pterodactylus.util.xml.XML;
42 import de.todesbaum.jsite.application.FileOption;
43 import de.todesbaum.jsite.application.Node;
44 import de.todesbaum.jsite.application.Project;
45 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
46 import de.todesbaum.util.freenet.fcp2.ClientPutDir.ManifestPutter;
47 import de.todesbaum.util.freenet.fcp2.PriorityClass;
48
49 /**
50  * The configuration.
51  *
52  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
53  */
54 public class Configuration {
55
56         /** The root node of the configuration. */
57         private SimpleXML rootNode;
58
59         /** The configuration locator. */
60         private final ConfigurationLocator configurationLocator;
61
62         /** Where the configuration resides. */
63         private ConfigurationLocation configurationLocation;
64
65         /**
66          * Creates a new configuration that is read from the given file.
67          *
68          * @param configurationLocator
69          *            The configuration locator
70          * @param configurationLocation
71          *            The configuration directory
72          */
73         public Configuration(ConfigurationLocator configurationLocator, ConfigurationLocation configurationLocation) {
74                 this.configurationLocator = configurationLocator;
75                 this.configurationLocation = configurationLocation;
76                 readConfiguration(configurationLocator.getFile(configurationLocation));
77         }
78
79         //
80         // ACCESSORS
81         //
82
83         /**
84          * Returns the configuration locator.
85          *
86          * @return The configuration locator
87          */
88         public ConfigurationLocator getConfigurationLocator() {
89                 return configurationLocator;
90         }
91
92         /**
93          * Returns the location the configuration will be written to when calling
94          * {@link #save()}.
95          *
96          * @return The location the configuration will be written to
97          */
98         public ConfigurationLocation getConfigurationDirectory() {
99                 return configurationLocation;
100         }
101
102         /**
103          * Sets the location the configuration will be written to when calling
104          * {@link #save()}.
105          *
106          * @param configurationLocation
107          *            The location to write the configuration to
108          */
109         public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
110                 this.configurationLocation = configurationLocation;
111         }
112
113         /**
114          * Reads the configuration from the file.
115          *
116          * @param filename
117          *            The name of the file to read the configuration from
118          */
119         private void readConfiguration(String filename) {
120                 Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Reading configuration from: " + filename);
121                 if (filename != null) {
122                         File configurationFile = new File(filename);
123                         if (configurationFile.exists()) {
124                                 ByteArrayOutputStream fileByteOutputStream = null;
125                                 FileInputStream fileInputStream = null;
126                                 try {
127                                         fileByteOutputStream = new ByteArrayOutputStream();
128                                         fileInputStream = new FileInputStream(configurationFile);
129                                         StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
130                                         fileByteOutputStream.close();
131                                         byte[] fileBytes = fileByteOutputStream.toByteArray();
132                                         rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
133                                         return;
134                                 } catch (FileNotFoundException e) {
135                                         /* ignore. */
136                                 } catch (IOException e) {
137                                         /* ignore. */
138                                 } finally {
139                                         Closer.close(fileInputStream);
140                                         Closer.close(fileByteOutputStream);
141                                 }
142                         }
143                 }
144                 rootNode = new SimpleXML("configuration");
145         }
146
147         /**
148          * Saves the configuration.
149          *
150          * @return <code>true</code> if the configuration could be saved,
151          *         <code>false</code> otherwise
152          */
153         public boolean save() {
154                 Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Trying to save configuration to: " + configurationLocation);
155                 File configurationFile = new File(configurationLocator.getFile(configurationLocation));
156                 if (!configurationFile.exists()) {
157                         File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
158                         if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
159                                 return false;
160                         }
161                 }
162                 FileOutputStream fileOutputStream = null;
163                 ByteArrayInputStream configurationInputStream = null;
164                 try {
165                         byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
166                         configurationInputStream = new ByteArrayInputStream(configurationBytes);
167                         fileOutputStream = new FileOutputStream(configurationFile);
168                         StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
169                         return true;
170                 } catch (IOException ioe1) {
171                         /* ignore. */
172                 } finally {
173                         Closer.close(configurationInputStream);
174                         Closer.close(fileOutputStream);
175                 }
176                 return false;
177         }
178
179         /**
180          * Returns the value of a node.
181          *
182          * @param nodeNames
183          *            The name of all nodes in the chain
184          * @param defaultValue
185          *            The default value to return if the node could not be found
186          * @return The value of the node, or the default value if the node could not
187          *         be found
188          */
189         private String getNodeValue(String[] nodeNames, String defaultValue) {
190                 SimpleXML node = rootNode;
191                 int nodeIndex = 0;
192                 while ((node != null) && (nodeIndex < nodeNames.length)) {
193                         node = node.getNode(nodeNames[nodeIndex++]);
194                 }
195                 if (node == null) {
196                         return defaultValue;
197                 }
198                 return node.getValue();
199         }
200
201         /**
202          * Returns the integer value of a node.
203          *
204          * @param nodeNames
205          *            The names of all nodes in the chain
206          * @param defaultValue
207          *            The default value to return if the node can not be found
208          * @return The parsed integer value, or the default value if the node can
209          *         not be found or the value can not be parsed into an integer
210          */
211         private int getNodeIntValue(String[] nodeNames, int defaultValue) {
212                 try {
213                         return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
214                 } catch (NumberFormatException nfe1) {
215                         /* ignore. */
216                 }
217                 return defaultValue;
218         }
219
220         /**
221          * Returns the boolean value of a node.
222          *
223          * @param nodeNames
224          *            The names of all nodes in the chain
225          * @param defaultValue
226          *            The default value to return if the node can not be found
227          * @return The parsed boolean value, or the default value if the node can
228          *         not be found
229          */
230         private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
231                 String nodeValue = getNodeValue(nodeNames, null);
232                 if (nodeValue == null) {
233                         return defaultValue;
234                 }
235                 return Boolean.parseBoolean(nodeValue);
236         }
237
238         /**
239          * Returns the hostname of the node.
240          *
241          * @return The hostname of the node
242          * @deprecated Use {@link #getSelectedNode()} instead
243          */
244         @Deprecated
245         public String getNodeAddress() {
246                 return getNodeValue(new String[] { "node-address" }, "localhost");
247         }
248
249         /**
250          * Sets the hostname of the node.
251          *
252          * @param nodeAddress
253          *            The hostname of the node
254          * @deprecated Use {@link #setSelectedNode(Node)} instead
255          */
256         @Deprecated
257         public void setNodeAddress(String nodeAddress) {
258                 rootNode.replace("node-address", nodeAddress);
259         }
260
261         /**
262          * The port number of the node
263          *
264          * @return The port number of the node
265          * @deprecated Use {@link #getSelectedNode()} instead.
266          */
267         @Deprecated
268         public int getNodePort() {
269                 return getNodeIntValue(new String[] { "node-port" }, 9481);
270         }
271
272         /**
273          * Sets the port number of the node.
274          *
275          * @param nodePort
276          *            The port number of the node
277          * @deprecated Use {@link #setSelectedNode(Node)} instead
278          */
279         @Deprecated
280         public void setNodePort(int nodePort) {
281                 rootNode.replace("node-port", String.valueOf(nodePort));
282         }
283
284         /**
285          * Returns whether the node configuration page should be skipped on startup.
286          *
287          * @return <code>true</code> to skip the node configuration page on startup,
288          *         <code>false</code> to show it
289          */
290         public boolean isSkipNodePage() {
291                 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
292         }
293
294         /**
295          * Sets whether the node configuration page should be skipped on startup.
296          *
297          * @param skipNodePage
298          *            <code>true</code> to skip the node configuration page on
299          *            startup, <code>false</code> to show it
300          */
301         public void setSkipNodePage(boolean skipNodePage) {
302                 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
303         }
304
305         /**
306          * Returns all configured projects.
307          *
308          * @return A list of all projects
309          */
310         public List<Project> getProjects() {
311                 List<Project> projects = new ArrayList<Project>();
312                 SimpleXML projectsNode = rootNode.getNode("project-list");
313                 if (projectsNode != null) {
314                         SimpleXML[] projectNodes = projectsNode.getNodes("project");
315                         for (SimpleXML projectNode : projectNodes) {
316                                 try {
317                                         Project project = new Project();
318                                         projects.add(project);
319                                         project.setDescription(projectNode.getValue("description", ""));
320                                         String indexFile = projectNode.getValue("index-file", "");
321                                         if (indexFile.indexOf('/') > -1) {
322                                                 indexFile = "";
323                                         }
324                                         project.setIndexFile(indexFile);
325                                         project.setLastInsertionTime(Long.parseLong(projectNode.getValue("last-insertion-time", "0")));
326                                         project.setLocalPath(projectNode.getValue("local-path", ""));
327                                         project.setName(projectNode.getValue("name", ""));
328                                         project.setPath(projectNode.getValue("path", ""));
329                                         if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
330                                                 project.setPath(project.getPath().replaceAll("/", ""));
331                                         }
332                                         project.setEdition(Integer.parseInt(projectNode.getValue("edition", "0")));
333                                         project.setInsertURI(projectNode.getValue("insert-uri", ""));
334                                         project.setRequestURI(projectNode.getValue("request-uri", ""));
335                                         if (projectNode.getNode("ignore-hidden-files") != null) {
336                                                 project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getValue("ignore-hidden-files", "true")));
337                                         } else {
338                                                 project.setIgnoreHiddenFiles(true);
339                                         }
340                                         project.setAlwaysForceInsert(Boolean.parseBoolean(projectNode.getValue("always-force-insert", "false")));
341
342                                         /* load last insert hashes. */
343                                         Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
344                                         SimpleXML lastInsertHashesNode = projectNode.getNode("last-insert-hashes");
345                                         if (lastInsertHashesNode != null) {
346                                                 for (SimpleXML fileNode : lastInsertHashesNode.getNodes("file")) {
347                                                         String filename = fileNode.getNode("filename").getValue();
348                                                         String lastInsertHash = fileNode.getNode("last-insert-hash").getValue();
349                                                         int lastInsertEdition = Integer.valueOf(fileNode.getNode("last-insert-edition").getValue());
350                                                         String lastInsertFilename = filename;
351                                                         if (fileNode.getNode("last-insert-filename") != null) {
352                                                                 lastInsertFilename = fileNode.getNode("last-insert-filename").getValue();
353                                                         }
354                                                         FileOption fileOption = project.getFileOption(filename);
355                                                         fileOption.setLastInsertHash(lastInsertHash).setLastInsertEdition(lastInsertEdition).setLastInsertFilename(lastInsertFilename);
356                                                         fileOptions.put(filename, fileOption);
357                                                 }
358                                         }
359
360                                         SimpleXML fileOptionsNode = projectNode.getNode("file-options");
361                                         if (fileOptionsNode != null) {
362                                                 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
363                                                 for (SimpleXML fileOptionNode : fileOptionNodes) {
364                                                         String filename = fileOptionNode.getNode("filename").getValue();
365                                                         FileOption fileOption = project.getFileOption(filename);
366                                                         fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
367                                                         if (fileOptionNode.getNode("insert-redirect") != null) {
368                                                                 fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
369                                                         }
370                                                         fileOption.setCustomKey(fileOptionNode.getValue("custom-key", ""));
371                                                         if (fileOptionNode.getNode("changed-name") != null) {
372                                                                 fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
373                                                         }
374                                                         fileOption.setMimeType(fileOptionNode.getValue("mime-type", ""));
375                                                         fileOptions.put(filename, fileOption);
376                                                 }
377                                         }
378                                         project.setFileOptions(fileOptions);
379                                 } catch (NumberFormatException nfe1) {
380                                         nfe1.printStackTrace();
381                                 }
382                         }
383                 }
384                 return projects;
385         }
386
387         /**
388          * Sets the list of all projects.
389          *
390          * @param projects
391          *            The list of all projects
392          */
393         public void setProjects(List<Project> projects) {
394                 SimpleXML projectsNode = new SimpleXML("project-list");
395                 for (Project project : projects) {
396                         SimpleXML projectNode = projectsNode.append("project");
397                         projectNode.append("edition", String.valueOf(project.getEdition()));
398                         projectNode.append("description", project.getDescription());
399                         projectNode.append("index-file", project.getIndexFile());
400                         projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
401                         projectNode.append("local-path", project.getLocalPath());
402                         projectNode.append("name", project.getName());
403                         projectNode.append("path", project.getPath());
404                         projectNode.append("insert-uri", project.getInsertURI());
405                         projectNode.append("request-uri", project.getRequestURI());
406                         projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles()));
407                         projectNode.append("always-force-insert", String.valueOf(project.isAlwaysForceInsert()));
408
409                         /* store last insert hashes. */
410                         SimpleXML lastInsertHashesNode = projectNode.append("last-insert-hashes");
411                         for (Entry<String, FileOption> fileOption : project.getFileOptions().entrySet()) {
412                                 if ((fileOption.getValue().getLastInsertHash() == null) || (fileOption.getValue().getLastInsertHash().length() == 0)) {
413                                         continue;
414                                 }
415                                 SimpleXML fileNode = lastInsertHashesNode.append("file");
416                                 fileNode.append("filename", fileOption.getKey());
417                                 fileNode.append("last-insert-hash", fileOption.getValue().getLastInsertHash());
418                                 fileNode.append("last-insert-edition", String.valueOf(fileOption.getValue().getLastInsertEdition()));
419                                 fileNode.append("last-insert-filename", fileOption.getValue().getLastInsertFilename());
420                         }
421
422                         SimpleXML fileOptionsNode = projectNode.append("file-options");
423                         Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
424                         while (entries.hasNext()) {
425                                 Entry<String, FileOption> entry = entries.next();
426                                 FileOption fileOption = entry.getValue();
427                                 if (fileOption.isCustom()) {
428                                         SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
429                                         fileOptionNode.append("filename", entry.getKey());
430                                         fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
431                                         fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect()));
432                                         fileOptionNode.append("custom-key", fileOption.getCustomKey());
433                                         fileOptionNode.append("changed-name", fileOption.getChangedName().orNull());
434                                         fileOptionNode.append("mime-type", fileOption.getMimeType());
435                                 }
436                         }
437                 }
438                 rootNode.replace(projectsNode);
439         }
440
441         /**
442          * Returns the stored locale.
443          *
444          * @return The stored locale
445          */
446         public Locale getLocale() {
447                 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
448                 String country = getNodeValue(new String[] { "i18n", "country" }, null);
449                 if (country != null) {
450                         return new Locale(language, country);
451                 }
452                 return new Locale(language);
453         }
454
455         /**
456          * Sets the locale to store.
457          *
458          * @param locale
459          *            The locale to store
460          */
461         public void setLocale(Locale locale) {
462                 SimpleXML i18nNode = new SimpleXML("i18n");
463                 if (locale.getCountry().length() != 0) {
464                         i18nNode.append("country", locale.getCountry());
465                 }
466                 i18nNode.append("language", locale.getLanguage());
467                 rootNode.replace(i18nNode);
468                 return;
469         }
470
471         /**
472          * Returns a list of configured nodes.
473          *
474          * @return The list of the configured nodes
475          */
476         public Node[] getNodes() {
477                 SimpleXML nodesNode = rootNode.getNode("nodes");
478                 if (nodesNode == null) {
479                         String hostname = getNodeAddress();
480                         int port = getNodePort();
481                         if (hostname == null) {
482                                 hostname = "127.0.0.1";
483                                 port = 9481;
484                         }
485                         return new Node[] { new Node(hostname, port, "Node") };
486                 }
487                 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
488                 Node[] returnNodes = new Node[nodeNodes.length];
489                 int nodeIndex = 0;
490                 for (SimpleXML nodeNode : nodeNodes) {
491                         String name = nodeNode.getNode("name").getValue();
492                         String hostname = nodeNode.getNode("hostname").getValue();
493                         int port = Integer.parseInt(nodeNode.getNode("port").getValue());
494                         Node node = new Node(hostname, port, name);
495                         returnNodes[nodeIndex++] = node;
496                 }
497                 return returnNodes;
498         }
499
500         /**
501          * Sets the list of configured nodes.
502          *
503          * @param nodes
504          *            The list of configured nodes
505          */
506         public void setNodes(Node[] nodes) {
507                 SimpleXML nodesNode = new SimpleXML("nodes");
508                 for (Node node : nodes) {
509                         SimpleXML nodeNode = nodesNode.append("node");
510                         nodeNode.append("name", node.getName());
511                         nodeNode.append("hostname", node.getHostname());
512                         nodeNode.append("port", String.valueOf(node.getPort()));
513                 }
514                 rootNode.replace(nodesNode);
515                 rootNode.remove("node-address");
516                 rootNode.remove("node-port");
517         }
518
519         /**
520          * Sets the selected node.
521          *
522          * @param selectedNode
523          *            The selected node
524          */
525         public void setSelectedNode(Node selectedNode) {
526                 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
527                 selectedNodeNode.append("name", selectedNode.getName());
528                 selectedNodeNode.append("hostname", selectedNode.getHostname());
529                 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
530                 rootNode.replace(selectedNodeNode);
531         }
532
533         /**
534          * Returns the selected node.
535          *
536          * @return The selected node
537          */
538         public Node getSelectedNode() {
539                 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
540                 if (selectedNodeNode == null) {
541                         String hostname = getNodeAddress();
542                         int port = getNodePort();
543                         if (hostname == null) {
544                                 hostname = "127.0.0.1";
545                                 port = 9481;
546                         }
547                         return new Node(hostname, port, "Node");
548                 }
549                 String name = selectedNodeNode.getNode("name").getValue();
550                 String hostname = selectedNodeNode.getNode("hostname").getValue();
551                 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
552                 return new Node(hostname, port, name);
553         }
554
555         /**
556          * Returns the temp directory to use.
557          *
558          * @return The temp directoy, or {@code null} to use the default temp
559          *         directory
560          */
561         public String getTempDirectory() {
562                 return getNodeValue(new String[] { "temp-directory" }, null);
563         }
564
565         /**
566          * Sets the temp directory to use.
567          *
568          * @param tempDirectory
569          *            The temp directory to use, or {@code null} to use the default
570          *            temp directory
571          */
572         public void setTempDirectory(String tempDirectory) {
573                 if (tempDirectory != null) {
574                         SimpleXML tempDirectoryNode = new SimpleXML("temp-directory");
575                         tempDirectoryNode.setValue(tempDirectory);
576                         rootNode.replace(tempDirectoryNode);
577                 } else {
578                         rootNode.remove("temp-directory");
579                 }
580         }
581
582         /**
583          * Returns whether to use the “early encode“ flag for the insert.
584          *
585          * @return {@code true} to set the “early encode” flag for the insert,
586          *         {@code false} otherwise
587          */
588         public boolean useEarlyEncode() {
589                 return getNodeBooleanValue(new String[] { "use-early-encode" }, false);
590         }
591
592         /**
593          * Sets whether to use the “early encode“ flag for the insert.
594          *
595          * @param useEarlyEncode
596          *            {@code true} to set the “early encode” flag for the insert,
597          *            {@code false} otherwise
598          * @return This configuration
599          */
600         public Configuration setUseEarlyEncode(boolean useEarlyEncode) {
601                 rootNode.replace("use-early-encode", String.valueOf(useEarlyEncode));
602                 return this;
603         }
604
605         /**
606          * Returns the insert priority.
607          *
608          * @return The insert priority
609          */
610         public PriorityClass getPriority() {
611                 return PriorityClass.valueOf(getNodeValue(new String[] { "insert-priority" }, "interactive"));
612         }
613
614         /**
615          * Sets the insert priority.
616          *
617          * @param priority
618          *            The insert priority
619          * @return This configuration
620          */
621         public Configuration setPriority(PriorityClass priority) {
622                 rootNode.replace("insert-priority", priority.toString());
623                 return this;
624         }
625
626         /**
627          * Returns the manifest putter.
628          *
629          * @return The manifest putter
630          */
631         public ManifestPutter getManifestPutter() {
632                 return ManifestPutter.valueOf(getNodeValue(new String[] { "manifest-putter" }, "simple").toUpperCase());
633         }
634
635         /**
636          * Sets the manifest putter.
637          *
638          * @param manifestPutter
639          *            The manifest putter
640          * @return This configuration
641          */
642         public Configuration setManifestPutter(ManifestPutter manifestPutter) {
643                 rootNode.replace("manifest-putter", manifestPutter.name().toLowerCase());
644                 return this;
645         }
646
647 }