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