49f107b8281707ca05c7ed6505aeb2b79d3fc380
[jSite.git] / 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                 lockFile.deleteOnExit();
117                 try {
118                         return lockFile.createNewFile();
119                 } catch (IOException e) {
120                         /* ignore. */
121                 }
122                 return false;
123         }
124
125         /**
126          * Reads the configuration from the file.
127          */
128         private void readConfiguration() {
129                 File configurationFile = new File(filename);
130                 if (configurationFile.exists()) {
131                         ByteArrayOutputStream fileByteOutputStream = null;
132                         FileInputStream fileInputStream = null;
133                         try {
134                                 fileByteOutputStream = new ByteArrayOutputStream();
135                                 fileInputStream = new FileInputStream(configurationFile);
136                                 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
137                                 fileByteOutputStream.close();
138                                 byte[] fileBytes = fileByteOutputStream.toByteArray();
139                                 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
140                                 return;
141                         } catch (FileNotFoundException e) {
142                                 /* ignore. */
143                         } catch (IOException e) {
144                                 /* ignore. */
145                         } finally {
146                                 Closer.close(fileInputStream);
147                                 Closer.close(fileByteOutputStream);
148                         }
149                 }
150                 rootNode = new SimpleXML("configuration");
151         }
152
153         /**
154          * Saves the configuration.
155          * 
156          * @return <code>true</code> if the configuration could be saved,
157          *         <code>false</code> otherwise
158          */
159         public boolean save() {
160                 File configurationFile = new File(filename);
161                 if (!configurationFile.exists()) {
162                         File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
163                         if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
164                                 return false;
165                         }
166                 }
167                 FileOutputStream fileOutputStream = null;
168                 ByteArrayInputStream configurationInputStream = null;
169                 try {
170                         byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
171                         configurationInputStream = new ByteArrayInputStream(configurationBytes);
172                         fileOutputStream = new FileOutputStream(configurationFile);
173                         StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
174                         return true;
175                 } catch (IOException ioe1) {
176                         /* ignore. */
177                 } finally {
178                         Closer.close(configurationInputStream);
179                         Closer.close(fileOutputStream);
180                 }
181                 return false;
182         }
183
184         /**
185          * Returns the value of a node.
186          * 
187          * @param nodeNames
188          *            The name of all nodes in the chain
189          * @param defaultValue
190          *            The default value to return if the node could not be found
191          * @return The value of the node, or the default value if the node could not
192          *         be found
193          */
194         private String getNodeValue(String[] nodeNames, String defaultValue) {
195                 SimpleXML node = rootNode;
196                 int nodeIndex = 0;
197                 while ((node != null) && (nodeIndex < nodeNames.length)) {
198                         node = node.getNode(nodeNames[nodeIndex++]);
199                 }
200                 if (node == null) {
201                         return defaultValue;
202                 }
203                 return node.getValue();
204         }
205
206         /**
207          * Returns the integer value of a node.
208          * 
209          * @param nodeNames
210          *            The names of all nodes in the chain
211          * @param defaultValue
212          *            The default value to return if the node can not be found
213          * @return The parsed integer value, or the default value if the node can
214          *         not be found or the value can not be parsed into an integer
215          */
216         private int getNodeIntValue(String[] nodeNames, int defaultValue) {
217                 try {
218                         return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
219                 } catch (NumberFormatException nfe1) {
220                         /* ignore. */
221                 }
222                 return defaultValue;
223         }
224
225         /**
226          * Returns the boolean value of a node.
227          * 
228          * @param nodeNames
229          *            The names of all nodes in the chain
230          * @param defaultValue
231          *            The default value to return if the node can not be found
232          * @return The parsed boolean value, or the default value if the node can
233          *         not be found
234          */
235         private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
236                 String nodeValue = getNodeValue(nodeNames, null);
237                 if (nodeValue == null) {
238                         return defaultValue;
239                 }
240                 return Boolean.parseBoolean(nodeValue);
241         }
242
243         /**
244          * Returns the hostname of the node.
245          * 
246          * @return The hostname of the node
247          * @deprecated Use {@link #getSelectedNode()} instead
248          */
249         @Deprecated
250         public String getNodeAddress() {
251                 return getNodeValue(new String[] { "node-address" }, "localhost");
252         }
253
254         /**
255          * Sets the hostname of the node.
256          * 
257          * @param nodeAddress
258          *            The hostname of the node
259          * @deprecated Use {@link #setSelectedNode(Node)} instead
260          */
261         @Deprecated
262         public void setNodeAddress(String nodeAddress) {
263                 rootNode.replace("node-address", nodeAddress);
264         }
265
266         /**
267          * The port number of the node
268          * 
269          * @return The port number of the node
270          * @deprecated Use {@link #getSelectedNode()} instead.
271          */
272         @Deprecated
273         public int getNodePort() {
274                 return getNodeIntValue(new String[] { "node-port" }, 9481);
275         }
276
277         /**
278          * Sets the port number of the node.
279          * 
280          * @param nodePort
281          *            The port number of the node
282          * @deprecated Use {@link #setSelectedNode(Node)} instead
283          */
284         @Deprecated
285         public void setNodePort(int nodePort) {
286                 rootNode.replace("node-port", String.valueOf(nodePort));
287         }
288
289         /**
290          * Returns whether the node configuration page should be skipped on startup.
291          * 
292          * @return <code>true</code> to skip the node configuration page on
293          *         startup, <code>false</code> to show it
294          */
295         public boolean isSkipNodePage() {
296                 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
297         }
298
299         /**
300          * Sets whether the node configuration page should be skipped on startup.
301          * 
302          * @param skipNodePage
303          *            <code>true</code> to skip the node configuration page on
304          *            startup, <code>false</code> to show it
305          */
306         public void setSkipNodePage(boolean skipNodePage) {
307                 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
308         }
309
310         /**
311          * Returns all configured projects.
312          * 
313          * @return A list of all projects
314          */
315         public Project[] getProjects() {
316                 List<Project> projects = new ArrayList<Project>();
317                 SimpleXML projectsNode = rootNode.getNode("project-list");
318                 if (projectsNode != null) {
319                         SimpleXML[] projectNodes = projectsNode.getNodes("project");
320                         for (SimpleXML projectNode : projectNodes) {
321                                 try {
322                                         Project project = new Project();
323                                         projects.add(project);
324                                         project.setDescription(projectNode.getNode("description").getValue());
325                                         project.setIndexFile(projectNode.getNode("index-file").getValue());
326                                         project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
327                                         project.setLocalPath(projectNode.getNode("local-path").getValue());
328                                         project.setName(projectNode.getNode("name").getValue());
329                                         project.setPath(projectNode.getNode("path").getValue());
330                                         if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
331                                                 project.setPath(project.getPath().replaceAll("/", ""));
332                                         }
333                                         project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
334                                         project.setInsertURI(projectNode.getNode("insert-uri").getValue());
335                                         project.setRequestURI(projectNode.getNode("request-uri").getValue());
336                                         SimpleXML fileOptionsNode = projectNode.getNode("file-options");
337                                         Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
338                                         if (fileOptionsNode != null) {
339                                                 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
340                                                 for (SimpleXML fileOptionNode : fileOptionNodes) {
341                                                         String filename = fileOptionNode.getNode("filename").getValue();
342                                                         FileOption fileOption = project.getFileOption(filename);
343                                                         fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
344                                                         fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
345                                                         fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
346                                                         fileOption.setContainer(fileOptionNode.getNode("container").getValue());
347                                                         if (fileOptionNode.getNode("replace-edition") != null) {
348                                                                 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
349                                                                 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
350                                                         }
351                                                         fileOptions.put(filename, fileOption);
352                                                 }
353                                         }
354                                         project.setFileOptions(fileOptions);
355                                 } catch (NumberFormatException nfe1) {
356                                         nfe1.printStackTrace();
357                                 }
358                         }
359                 }
360                 return projects.toArray(new Project[projects.size()]);
361         }
362
363         /**
364          * Sets the list of all projects.
365          * 
366          * @param projects
367          *            The list of all projects
368          */
369         public void setProjects(Project[] projects) {
370                 SimpleXML projectsNode = new SimpleXML("project-list");
371                 for (Project project : projects) {
372                         SimpleXML projectNode = projectsNode.append("project");
373                         projectNode.append("edition", String.valueOf(project.getEdition()));
374                         projectNode.append("description", project.getDescription());
375                         projectNode.append("index-file", project.getIndexFile());
376                         projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
377                         projectNode.append("local-path", project.getLocalPath());
378                         projectNode.append("name", project.getName());
379                         projectNode.append("path", project.getPath());
380                         projectNode.append("insert-uri", project.getInsertURI());
381                         projectNode.append("request-uri", project.getRequestURI());
382                         SimpleXML fileOptionsNode = projectNode.append("file-options");
383                         Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
384                         while (entries.hasNext()) {
385                                 Entry<String, FileOption> entry = entries.next();
386                                 FileOption fileOption = entry.getValue();
387                                 if (fileOption.isCustom()) {
388                                         SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
389                                         fileOptionNode.append("filename", entry.getKey());
390                                         fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
391                                         fileOptionNode.append("custom-key", fileOption.getCustomKey());
392                                         fileOptionNode.append("mime-type", fileOption.getMimeType());
393                                         fileOptionNode.append("container", fileOption.getContainer());
394                                         fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
395                                         fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
396                                 }
397                         }
398                 }
399                 rootNode.replace(projectsNode);
400         }
401
402         /**
403          * Returns the stored locale.
404          * 
405          * @return The stored locale
406          */
407         public Locale getLocale() {
408                 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
409                 String country = getNodeValue(new String[] { "i18n", "country" }, null);
410                 if (country != null) {
411                         return new Locale(language, country);
412                 }
413                 return new Locale(language);
414         }
415
416         /**
417          * Sets the locale to store.
418          * 
419          * @param locale
420          *            The locale to store
421          */
422         public void setLocale(Locale locale) {
423                 SimpleXML i18nNode = new SimpleXML("i18n");
424                 if (locale.getCountry().length() != 0) {
425                         i18nNode.append("country", locale.getCountry());
426                 }
427                 i18nNode.append("language", locale.getLanguage());
428                 rootNode.replace(i18nNode);
429                 return;
430         }
431
432         /**
433          * Returns a list of configured nodes.
434          * 
435          * @return The list of the configured nodes
436          */
437         public Node[] getNodes() {
438                 SimpleXML nodesNode = rootNode.getNode("nodes");
439                 if (nodesNode == null) {
440                         String hostname = getNodeAddress();
441                         int port = getNodePort();
442                         if (hostname == null) {
443                                 hostname = "127.0.0.1";
444                                 port = 9481;
445                         }
446                         return new Node[] { new Node(hostname, port, "Node") };
447                 }
448                 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
449                 Node[] returnNodes = new Node[nodeNodes.length];
450                 int nodeIndex = 0;
451                 for (SimpleXML nodeNode : nodeNodes) {
452                         String name = nodeNode.getNode("name").getValue();
453                         String hostname = nodeNode.getNode("hostname").getValue();
454                         int port = Integer.parseInt(nodeNode.getNode("port").getValue());
455                         Node node = new Node(hostname, port, name);
456                         returnNodes[nodeIndex++] = node;
457                 }
458                 return returnNodes;
459         }
460
461         /**
462          * Sets the list of configured nodes.
463          * 
464          * @param nodes
465          *            The list of configured nodes
466          */
467         public void setNodes(Node[] nodes) {
468                 SimpleXML nodesNode = new SimpleXML("nodes");
469                 for (Node node : nodes) {
470                         SimpleXML nodeNode = nodesNode.append("node");
471                         nodeNode.append("name", node.getName());
472                         nodeNode.append("hostname", node.getHostname());
473                         nodeNode.append("port", String.valueOf(node.getPort()));
474                 }
475                 rootNode.replace(nodesNode);
476                 rootNode.remove("node-address");
477                 rootNode.remove("node-port");
478         }
479
480         /**
481          * Sets the selected node.
482          * 
483          * @param selectedNode
484          *            The selected node
485          */
486         public void setSelectedNode(Node selectedNode) {
487                 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
488                 selectedNodeNode.append("name", selectedNode.getName());
489                 selectedNodeNode.append("hostname", selectedNode.getHostname());
490                 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
491                 rootNode.replace(selectedNodeNode);
492         }
493
494         /**
495          * Returns the selected node.
496          * 
497          * @return The selected node
498          */
499         public Node getSelectedNode() {
500                 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
501                 if (selectedNodeNode == null) {
502                         String hostname = getNodeAddress();
503                         int port = getNodePort();
504                         if (hostname == null) {
505                                 hostname = "127.0.0.1";
506                                 port = 9481;
507                         }
508                         return new Node(hostname, port, "Node");
509                 }
510                 String name = selectedNodeNode.getNode("name").getValue();
511                 String hostname = selectedNodeNode.getNode("hostname").getValue();
512                 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
513                 return new Node(hostname, port, name);
514         }
515
516 }