e98a18dc51d7e445199bfc5a3d049cfde17cc388
[jSite.git] / src / de / todesbaum / jsite / main / Configuration.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package de.todesbaum.jsite.main;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
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;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
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;
44
45 /**
46  * The configuration.
47  *
48  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
49  */
50 public class Configuration {
51
52         /** The name of the file the configuration is stored to. */
53         private String filename;
54
55         /** The name of the lock file. */
56         private String lockFilename;
57
58         /** The root node of the configuration. */
59         private SimpleXML rootNode;
60
61         /**
62          * Creates a new configuration with the default name of the configuration
63          * file.
64          */
65         public Configuration() {
66                 this(System.getProperty("user.home") + "/.jSite/config7");
67         }
68
69         /**
70          * Creates a new configuration that is read from the given file.
71          *
72          * @param filename
73          *            The name of the configuration file
74          */
75         public Configuration(String filename) {
76                 this(filename, filename + ".lock");
77         }
78
79         /**
80          * Creates a new configuration that is read from the given file and uses the
81          * given lock file.
82          *
83          * @param filename
84          *            The name of the configuration file
85          * @param lockFilename
86          *            The name of the lock file
87          */
88         public Configuration(String filename, String lockFilename) {
89                 this.filename = filename;
90                 this.lockFilename = lockFilename;
91                 readConfiguration();
92         }
93
94         /**
95          * Creates the directory of the configuration file.
96          *
97          * @return <code>true</code> if the directory exists, or if it could be
98          *         created, <code>false</code> otherwise
99          */
100         private boolean createConfigDirectory() {
101                 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
102                 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
103         }
104
105         /**
106          * Creates the lock file.
107          *
108          * @return <code>true</code> if the lock file did not already exist and
109          *         could be created, <code>false</code> otherwise
110          */
111         public boolean createLockFile() {
112                 if (!createConfigDirectory()) {
113                         return false;
114                 }
115                 File lockFile = new File(lockFilename);
116                 try {
117                         boolean fileLocked = lockFile.createNewFile();
118                         if (fileLocked) {
119                                 lockFile.deleteOnExit();
120                         }
121                         return fileLocked;
122                 } catch (IOException e) {
123                         /* ignore. */
124                 }
125                 return false;
126         }
127
128         /**
129          * Tells the VM to remove the lock file on program exit.
130          */
131         public void removeLockfileOnExit() {
132                 new File(lockFilename).deleteOnExit();
133         }
134
135         /**
136          * Reads the configuration from the file.
137          */
138         private void readConfiguration() {
139                 File configurationFile = new File(filename);
140                 if (configurationFile.exists()) {
141                         ByteArrayOutputStream fileByteOutputStream = null;
142                         FileInputStream fileInputStream = null;
143                         try {
144                                 fileByteOutputStream = new ByteArrayOutputStream();
145                                 fileInputStream = new FileInputStream(configurationFile);
146                                 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
147                                 fileByteOutputStream.close();
148                                 byte[] fileBytes = fileByteOutputStream.toByteArray();
149                                 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
150                                 return;
151                         } catch (FileNotFoundException e) {
152                                 /* ignore. */
153                         } catch (IOException e) {
154                                 /* ignore. */
155                         } finally {
156                                 Closer.close(fileInputStream);
157                                 Closer.close(fileByteOutputStream);
158                         }
159                 }
160                 rootNode = new SimpleXML("configuration");
161         }
162
163         /**
164          * Saves the configuration.
165          *
166          * @return <code>true</code> if the configuration could be saved,
167          *         <code>false</code> otherwise
168          */
169         public boolean save() {
170                 File configurationFile = new File(filename);
171                 if (!configurationFile.exists()) {
172                         File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
173                         if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
174                                 return false;
175                         }
176                 }
177                 FileOutputStream fileOutputStream = null;
178                 ByteArrayInputStream configurationInputStream = null;
179                 try {
180                         byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
181                         configurationInputStream = new ByteArrayInputStream(configurationBytes);
182                         fileOutputStream = new FileOutputStream(configurationFile);
183                         StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
184                         return true;
185                 } catch (IOException ioe1) {
186                         /* ignore. */
187                 } finally {
188                         Closer.close(configurationInputStream);
189                         Closer.close(fileOutputStream);
190                 }
191                 return false;
192         }
193
194         /**
195          * Returns the value of a node.
196          *
197          * @param nodeNames
198          *            The name of all nodes in the chain
199          * @param defaultValue
200          *            The default value to return if the node could not be found
201          * @return The value of the node, or the default value if the node could not
202          *         be found
203          */
204         private String getNodeValue(String[] nodeNames, String defaultValue) {
205                 SimpleXML node = rootNode;
206                 int nodeIndex = 0;
207                 while ((node != null) && (nodeIndex < nodeNames.length)) {
208                         node = node.getNode(nodeNames[nodeIndex++]);
209                 }
210                 if (node == null) {
211                         return defaultValue;
212                 }
213                 return node.getValue();
214         }
215
216         /**
217          * Returns the integer value of a node.
218          *
219          * @param nodeNames
220          *            The names of all nodes in the chain
221          * @param defaultValue
222          *            The default value to return if the node can not be found
223          * @return The parsed integer value, or the default value if the node can
224          *         not be found or the value can not be parsed into an integer
225          */
226         private int getNodeIntValue(String[] nodeNames, int defaultValue) {
227                 try {
228                         return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
229                 } catch (NumberFormatException nfe1) {
230                         /* ignore. */
231                 }
232                 return defaultValue;
233         }
234
235         /**
236          * Returns the boolean value of a node.
237          *
238          * @param nodeNames
239          *            The names of all nodes in the chain
240          * @param defaultValue
241          *            The default value to return if the node can not be found
242          * @return The parsed boolean value, or the default value if the node can
243          *         not be found
244          */
245         private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
246                 String nodeValue = getNodeValue(nodeNames, null);
247                 if (nodeValue == null) {
248                         return defaultValue;
249                 }
250                 return Boolean.parseBoolean(nodeValue);
251         }
252
253         /**
254          * Returns the hostname of the node.
255          *
256          * @return The hostname of the node
257          * @deprecated Use {@link #getSelectedNode()} instead
258          */
259         @Deprecated
260         public String getNodeAddress() {
261                 return getNodeValue(new String[] { "node-address" }, "localhost");
262         }
263
264         /**
265          * Sets the hostname of the node.
266          *
267          * @param nodeAddress
268          *            The hostname of the node
269          * @deprecated Use {@link #setSelectedNode(Node)} instead
270          */
271         @Deprecated
272         public void setNodeAddress(String nodeAddress) {
273                 rootNode.replace("node-address", nodeAddress);
274         }
275
276         /**
277          * The port number of the node
278          *
279          * @return The port number of the node
280          * @deprecated Use {@link #getSelectedNode()} instead.
281          */
282         @Deprecated
283         public int getNodePort() {
284                 return getNodeIntValue(new String[] { "node-port" }, 9481);
285         }
286
287         /**
288          * Sets the port number of the node.
289          *
290          * @param nodePort
291          *            The port number of the node
292          * @deprecated Use {@link #setSelectedNode(Node)} instead
293          */
294         @Deprecated
295         public void setNodePort(int nodePort) {
296                 rootNode.replace("node-port", String.valueOf(nodePort));
297         }
298
299         /**
300          * Returns whether the node configuration page should be skipped on startup.
301          *
302          * @return <code>true</code> to skip the node configuration page on startup,
303          *         <code>false</code> to show it
304          */
305         public boolean isSkipNodePage() {
306                 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
307         }
308
309         /**
310          * Sets whether the node configuration page should be skipped on startup.
311          *
312          * @param skipNodePage
313          *            <code>true</code> to skip the node configuration page on
314          *            startup, <code>false</code> to show it
315          */
316         public void setSkipNodePage(boolean skipNodePage) {
317                 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
318         }
319
320         /**
321          * Returns all configured projects.
322          *
323          * @return A list of all projects
324          */
325         public Project[] getProjects() {
326                 List<Project> projects = new ArrayList<Project>();
327                 SimpleXML projectsNode = rootNode.getNode("project-list");
328                 if (projectsNode != null) {
329                         SimpleXML[] projectNodes = projectsNode.getNodes("project");
330                         for (SimpleXML projectNode : projectNodes) {
331                                 try {
332                                         Project project = new Project();
333                                         projects.add(project);
334                                         project.setDescription(projectNode.getNode("description").getValue(""));
335                                         project.setIndexFile(projectNode.getNode("index-file").getValue(""));
336                                         project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0")));
337                                         project.setLocalPath(projectNode.getNode("local-path").getValue(""));
338                                         project.setName(projectNode.getNode("name").getValue(""));
339                                         project.setPath(projectNode.getNode("path").getValue(""));
340                                         if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
341                                                 project.setPath(project.getPath().replaceAll("/", ""));
342                                         }
343                                         project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0")));
344                                         project.setInsertURI(projectNode.getNode("insert-uri").getValue(""));
345                                         project.setRequestURI(projectNode.getNode("request-uri").getValue(""));
346                                         if (projectNode.getNode("ignore-hidden-files") != null) {
347                                                 project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true")));
348                                         } else {
349                                                 project.setIgnoreHiddenFiles(true);
350                                         }
351                                         SimpleXML fileOptionsNode = projectNode.getNode("file-options");
352                                         Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
353                                         if (fileOptionsNode != null) {
354                                                 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
355                                                 for (SimpleXML fileOptionNode : fileOptionNodes) {
356                                                         String filename = fileOptionNode.getNode("filename").getValue();
357                                                         FileOption fileOption = project.getFileOption(filename);
358                                                         fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
359                                                         if (fileOptionNode.getNode("insert-redirect") != null) {
360                                                                 fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
361                                                         }
362                                                         fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue(""));
363                                                         if (fileOptionNode.getNode("changed-name") != null) {
364                                                                 fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
365                                                         }
366                                                         fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue(""));
367                                                         fileOption.setContainer(fileOptionNode.getNode("container").getValue());
368                                                         if (fileOptionNode.getNode("replace-edition") != null) {
369                                                                 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
370                                                                 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
371                                                         }
372                                                         fileOptions.put(filename, fileOption);
373                                                 }
374                                         }
375                                         project.setFileOptions(fileOptions);
376                                 } catch (NumberFormatException nfe1) {
377                                         nfe1.printStackTrace();
378                                 }
379                         }
380                 }
381                 return projects.toArray(new Project[projects.size()]);
382         }
383
384         /**
385          * Sets the list of all projects.
386          *
387          * @param projects
388          *            The list of all projects
389          */
390         public void setProjects(Project[] projects) {
391                 SimpleXML projectsNode = new SimpleXML("project-list");
392                 for (Project project : projects) {
393                         SimpleXML projectNode = projectsNode.append("project");
394                         projectNode.append("edition", String.valueOf(project.getEdition()));
395                         projectNode.append("description", project.getDescription());
396                         projectNode.append("index-file", project.getIndexFile());
397                         projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
398                         projectNode.append("local-path", project.getLocalPath());
399                         projectNode.append("name", project.getName());
400                         projectNode.append("path", project.getPath());
401                         projectNode.append("insert-uri", project.getInsertURI());
402                         projectNode.append("request-uri", project.getRequestURI());
403                         projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles()));
404                         SimpleXML fileOptionsNode = projectNode.append("file-options");
405                         Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
406                         while (entries.hasNext()) {
407                                 Entry<String, FileOption> entry = entries.next();
408                                 FileOption fileOption = entry.getValue();
409                                 if (fileOption.isCustom()) {
410                                         SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
411                                         fileOptionNode.append("filename", entry.getKey());
412                                         fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
413                                         fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect()));
414                                         fileOptionNode.append("custom-key", fileOption.getCustomKey());
415                                         fileOptionNode.append("changed-name", fileOption.getChangedName());
416                                         fileOptionNode.append("mime-type", fileOption.getMimeType());
417                                         fileOptionNode.append("container", fileOption.getContainer());
418                                         fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
419                                         fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
420                                 }
421                         }
422                 }
423                 rootNode.replace(projectsNode);
424         }
425
426         /**
427          * Returns the stored locale.
428          *
429          * @return The stored locale
430          */
431         public Locale getLocale() {
432                 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
433                 String country = getNodeValue(new String[] { "i18n", "country" }, null);
434                 if (country != null) {
435                         return new Locale(language, country);
436                 }
437                 return new Locale(language);
438         }
439
440         /**
441          * Sets the locale to store.
442          *
443          * @param locale
444          *            The locale to store
445          */
446         public void setLocale(Locale locale) {
447                 SimpleXML i18nNode = new SimpleXML("i18n");
448                 if (locale.getCountry().length() != 0) {
449                         i18nNode.append("country", locale.getCountry());
450                 }
451                 i18nNode.append("language", locale.getLanguage());
452                 rootNode.replace(i18nNode);
453                 return;
454         }
455
456         /**
457          * Returns a list of configured nodes.
458          *
459          * @return The list of the configured nodes
460          */
461         public Node[] getNodes() {
462                 SimpleXML nodesNode = rootNode.getNode("nodes");
463                 if (nodesNode == null) {
464                         String hostname = getNodeAddress();
465                         int port = getNodePort();
466                         if (hostname == null) {
467                                 hostname = "127.0.0.1";
468                                 port = 9481;
469                         }
470                         return new Node[] { new Node(hostname, port, "Node") };
471                 }
472                 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
473                 Node[] returnNodes = new Node[nodeNodes.length];
474                 int nodeIndex = 0;
475                 for (SimpleXML nodeNode : nodeNodes) {
476                         String name = nodeNode.getNode("name").getValue();
477                         String hostname = nodeNode.getNode("hostname").getValue();
478                         int port = Integer.parseInt(nodeNode.getNode("port").getValue());
479                         Node node = new Node(hostname, port, name);
480                         returnNodes[nodeIndex++] = node;
481                 }
482                 return returnNodes;
483         }
484
485         /**
486          * Sets the list of configured nodes.
487          *
488          * @param nodes
489          *            The list of configured nodes
490          */
491         public void setNodes(Node[] nodes) {
492                 SimpleXML nodesNode = new SimpleXML("nodes");
493                 for (Node node : nodes) {
494                         SimpleXML nodeNode = nodesNode.append("node");
495                         nodeNode.append("name", node.getName());
496                         nodeNode.append("hostname", node.getHostname());
497                         nodeNode.append("port", String.valueOf(node.getPort()));
498                 }
499                 rootNode.replace(nodesNode);
500                 rootNode.remove("node-address");
501                 rootNode.remove("node-port");
502         }
503
504         /**
505          * Sets the selected node.
506          *
507          * @param selectedNode
508          *            The selected node
509          */
510         public void setSelectedNode(Node selectedNode) {
511                 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
512                 selectedNodeNode.append("name", selectedNode.getName());
513                 selectedNodeNode.append("hostname", selectedNode.getHostname());
514                 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
515                 rootNode.replace(selectedNodeNode);
516         }
517
518         /**
519          * Returns the selected node.
520          *
521          * @return The selected node
522          */
523         public Node getSelectedNode() {
524                 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
525                 if (selectedNodeNode == null) {
526                         String hostname = getNodeAddress();
527                         int port = getNodePort();
528                         if (hostname == null) {
529                                 hostname = "127.0.0.1";
530                                 port = 9481;
531                         }
532                         return new Node(hostname, port, "Node");
533                 }
534                 String name = selectedNodeNode.getNode("name").getValue();
535                 String hostname = selectedNodeNode.getNode("hostname").getValue();
536                 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
537                 return new Node(hostname, port, name);
538         }
539
540         /**
541          * Returns the temp directory to use.
542          *
543          * @return The temp directoy, or {@code null} to use the default temp
544          *         directory
545          */
546         public String getTempDirectory() {
547                 return getNodeValue(new String[] { "temp-directory" }, null);
548         }
549
550         /**
551          * Sets the temp directory to use.
552          *
553          * @param tempDirectory
554          *            The temp directory to use, or {@code null} to use the default
555          *            temp directory
556          */
557         public void setTempDirectory(String tempDirectory) {
558                 if (tempDirectory != null) {
559                         SimpleXML tempDirectoryNode = new SimpleXML("temp-directory");
560                         tempDirectoryNode.setValue(tempDirectory);
561                         rootNode.replace(tempDirectoryNode);
562                 } else {
563                         rootNode.remove("temp-directory");
564                 }
565         }
566
567 }