version 0.5: show usk keys only, update usk on insert completion
[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.EditionProject;
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.util.io.StreamCopier;
42 import de.todesbaum.util.xml.SimpleXML;
43 import de.todesbaum.util.xml.XML;
44
45 /**
46  * @author David Roden <droden@gmail.com>
47  * @version $Id$
48  */
49 public class Configuration {
50
51         private String filename;
52         private String lockFilename;
53         private SimpleXML rootNode;
54
55         public Configuration() {
56                 filename = System.getProperty("user.home") + "/.jSite/config7";
57                 lockFilename = System.getProperty("user.home") + "/.jSite/lock7";
58                 readConfiguration();
59         }
60         
61         private boolean createConfigDirectory() {
62                 File configDirectory = new File(System.getProperty("user.home"), ".jSite");
63                 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
64         }
65
66         public boolean createLockFile() {
67                 if (!createConfigDirectory()) {
68                         return false;
69                 }
70                 File lockFile = new File(lockFilename);
71                 lockFile.deleteOnExit();
72                 try {
73                         return lockFile.createNewFile();
74                 } catch (IOException e) {
75                 }
76                 return false;
77         }
78
79         private void readConfiguration() {
80                 File configurationFile = new File(filename);
81                 if (configurationFile.exists()) {
82                         ByteArrayOutputStream fileByteOutputStream = null;
83                         FileInputStream fileInputStream = null;
84                         try {
85                                 fileByteOutputStream = new ByteArrayOutputStream();
86                                 fileInputStream = new FileInputStream(configurationFile);
87                                 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
88                                 fileByteOutputStream.close();
89                                 byte[] fileBytes = fileByteOutputStream.toByteArray();
90                                 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
91                                 return;
92                         } catch (FileNotFoundException e) {
93                         } catch (IOException e) {
94                         } finally {
95                                 if (fileInputStream != null) {
96                                         try {
97                                                 fileInputStream.close();
98                                         } catch (IOException ioe1) {
99                                         }
100                                 }
101                                 if (fileByteOutputStream != null) {
102                                         try {
103                                                 fileByteOutputStream.close();
104                                         } catch (IOException ioe1) {
105                                         }
106                                 }
107                         }
108                 }
109                 rootNode = new SimpleXML("configuration");
110         }
111
112         public boolean save() {
113                 File configurationFile = new File(filename);
114                 if (!configurationFile.exists()) {
115                         File configurationFilePath = configurationFile.getParentFile();
116                         if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
117                                 return false;
118                         }
119                 }
120                 FileOutputStream fileOutputStream = null;
121                 ByteArrayInputStream configurationInputStream = null;
122                 try {
123                         byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
124                         configurationInputStream = new ByteArrayInputStream(configurationBytes);
125                         fileOutputStream = new FileOutputStream(configurationFile);
126                         StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
127                         return true;
128                 } catch (IOException ioe1) {
129                 } finally {
130                         if (configurationInputStream != null) {
131                                 try {
132                                         configurationInputStream.close();
133                                 } catch (IOException ioe1) {
134                                 }
135                         }
136                         if (fileOutputStream != null) {
137                                 try {
138                                         fileOutputStream.close();
139                                 } catch (IOException ioe1) {
140                                 }
141                         }
142                 }
143                 return false;
144         }
145
146         private String getNodeValue(String[] nodeNames, String defaultValue) {
147                 SimpleXML node = rootNode;
148                 int nodeIndex = 0;
149                 while ((node != null) && (nodeIndex < nodeNames.length)) {
150                         node = node.getNode(nodeNames[nodeIndex++]);
151                 }
152                 if (node == null) {
153                         return defaultValue;
154                 }
155                 return node.getValue();
156         }
157
158         private int getNodeIntValue(String[] nodeNames, int defaultValue) {
159                 try {
160                         return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
161                 } catch (NumberFormatException nfe1) {
162                 }
163                 return defaultValue;
164         }
165
166         private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
167                 String nodeValue = getNodeValue(nodeNames, null);
168                 if (nodeValue == null) {
169                         return defaultValue;
170                 }
171                 return Boolean.parseBoolean(nodeValue);
172         }
173
174         /**
175          * Returns the hostname of the node.
176          * @return The hostname of the node
177          * @deprecated Use {@link #getSelectedNode()} instead
178          */
179         @Deprecated
180         public String getNodeAddress() {
181                 return getNodeValue(new String[] { "node-address" }, "localhost");
182         }
183
184         /**
185          * Sets the hostname of the node.
186          * @param nodeAddress The hostname of the node
187          * @deprecated Use {@link #setSelectedNode(Node)} instead
188          */
189         @Deprecated
190         public void setNodeAddress(String nodeAddress) {
191                 rootNode.replace("node-address", nodeAddress);
192         }
193
194         /**
195          * The port number of the node
196          * @return The port number of the node
197          * @deprecated Use {@link #getSelectedNode()} instead. 
198          */
199         @Deprecated
200         public int getNodePort() {
201                 return getNodeIntValue(new String[] { "node-port" }, 9481);
202         }
203
204         /**
205          * Sets the port number of the node.
206          * @param nodePort The port number of the node
207          * @deprecated Use {@link #setSelectedNode(Node)} instead
208          */
209         @Deprecated
210         public void setNodePort(int nodePort) {
211                 rootNode.replace("node-port", String.valueOf(nodePort));
212         }
213
214         public boolean isSkipNodePage() {
215                 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
216         }
217
218         public void setSkipNodePage(boolean skipNodePage) {
219                 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
220         }
221
222         public Project[] getProjects() {
223                 List<Project> projects = new ArrayList<Project>();
224                 SimpleXML projectsNode = rootNode.getNode("project-list");
225                 if (projectsNode != null) {
226                         SimpleXML[] projectNodes = projectsNode.getNodes("project");
227                         for (SimpleXML projectNode: projectNodes) {
228                                 try {
229                                         Project project = null;
230                                         SimpleXML typeNode = projectNode.getNode("type");
231                                         if ("edition".equals(typeNode.getValue())) {
232                                                 EditionProject editionProject = new EditionProject();
233                                                 project = editionProject;
234                                                 editionProject.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
235                                         }
236                                         projects.add(project);
237                                         project.setDescription(projectNode.getNode("description").getValue());
238                                         project.setIndexFile(projectNode.getNode("index-file").getValue());
239                                         project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
240                                         project.setLocalPath(projectNode.getNode("local-path").getValue());
241                                         project.setName(projectNode.getNode("name").getValue());
242                                         project.setPath(projectNode.getNode("path").getValue());
243                                         project.setInsertURI(projectNode.getNode("insert-uri").getValue());
244                                         project.setRequestURI(projectNode.getNode("request-uri").getValue());
245                                         SimpleXML fileOptionsNode = projectNode.getNode("file-options");
246                                         Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
247                                         if (fileOptionsNode != null) {
248                                                 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
249                                                 for (SimpleXML fileOptionNode: fileOptionNodes) {
250                                                         String filename = fileOptionNode.getNode("filename").getValue();
251                                                         FileOption fileOption = project.getFileOption(filename);
252                                                         fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
253                                                         fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
254                                                         fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
255                                                         fileOption.setContainer(fileOptionNode.getNode("container").getValue());
256                                                         if (fileOptionNode.getNode("replace-edition") != null) {
257                                                                 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
258                                                                 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
259                                                         }
260                                                         fileOptions.put(filename, fileOption);
261                                                 }
262                                         }
263                                         project.setFileOptions(fileOptions);
264                                 } catch (NumberFormatException nfe1) {
265                                         nfe1.printStackTrace();
266                                 }
267                         }
268                 }
269                 return projects.toArray(new Project[projects.size()]);
270         }
271
272         public void setProjects(Project[] projects) {
273                 SimpleXML projectsNode = new SimpleXML("project-list");
274                 for (Project project: projects) {
275                         SimpleXML projectNode = projectsNode.append("project");
276                         if (project instanceof EditionProject) {
277                                 projectNode.append("type", "edition");
278                                 projectNode.append("edition", String.valueOf(((EditionProject) project).getEdition()));
279                         }
280                         projectNode.append("description", project.getDescription());
281                         projectNode.append("index-file", project.getIndexFile());
282                         projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
283                         projectNode.append("local-path", project.getLocalPath());
284                         projectNode.append("name", project.getName());
285                         projectNode.append("path", project.getPath());
286                         projectNode.append("insert-uri", project.getInsertURI());
287                         projectNode.append("request-uri", project.getRequestURI());
288                         SimpleXML fileOptionsNode = projectNode.append("file-options");
289                         Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
290                         while (entries.hasNext()) {
291                                 Entry<String, FileOption> entry = entries.next();
292                                 FileOption fileOption = entry.getValue();
293                                 if (fileOption.isCustom()) {
294                                         SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
295                                         fileOptionNode.append("filename", entry.getKey());
296                                         fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
297                                         fileOptionNode.append("custom-key", fileOption.getCustomKey());
298                                         fileOptionNode.append("mime-type", fileOption.getMimeType());
299                                         fileOptionNode.append("container", fileOption.getContainer());
300                                         fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
301                                         fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
302                                 }
303                         }
304                 }
305                 rootNode.replace(projectsNode);
306         }
307
308         public Locale getLocale() {
309                 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
310                 String country = getNodeValue(new String[] { "i18n", "country" }, null);
311                 if (country != null) {
312                         return new Locale(language, country);
313                 }
314                 return new Locale(language);
315         }
316
317         public void setLocale(Locale locale) {
318                 SimpleXML i18nNode = new SimpleXML("i18n");
319                 if (locale.getCountry().length() != 0) {
320                         i18nNode.append("country", locale.getCountry());
321                 }
322                 i18nNode.append("language", locale.getLanguage());
323                 rootNode.replace(i18nNode);
324                 return;
325         }
326         
327         public Node[] getNodes() {
328                 SimpleXML nodesNode = rootNode.getNode("nodes");
329                 if (nodesNode == null) {
330                         String hostname = getNodeAddress();
331                         int port = getNodePort();
332                         if (hostname == null) {
333                                 hostname = "127.0.0.1";
334                                 port = 9481;
335                         }
336                         return new Node[] { new Node(hostname, port, "Node") };
337                 }
338                 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
339                 Node[] returnNodes = new Node[nodeNodes.length];
340                 int nodeIndex = 0;
341                 for (SimpleXML nodeNode: nodeNodes) {
342                         String name = nodeNode.getNode("name").getValue();
343                         String hostname = nodeNode.getNode("hostname").getValue();
344                         int port = Integer.parseInt(nodeNode.getNode("port").getValue());
345                         Node node = new Node(hostname, port, name);
346                         returnNodes[nodeIndex++] = node;
347                 }
348                 return returnNodes;
349         }
350         
351         public void setNodes(Node[] nodes) {
352                 SimpleXML nodesNode = new SimpleXML("nodes");
353                 for (Node node: nodes) {
354                         SimpleXML nodeNode = nodesNode.append("node");
355                         nodeNode.append("name", node.getName());
356                         nodeNode.append("hostname", node.getHostname());
357                         nodeNode.append("port", String.valueOf(node.getPort()));
358                 }
359                 rootNode.replace(nodesNode);
360                 rootNode.remove("node-address");
361                 rootNode.remove("node-port");
362         }
363
364         public void setSelectedNode(Node selectedNode) {
365                 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
366                 selectedNodeNode.append("name", selectedNode.getName());
367                 selectedNodeNode.append("hostname", selectedNode.getHostname());
368                 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
369                 rootNode.replace(selectedNodeNode);
370         }
371         
372         public Node getSelectedNode() {
373                 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
374                 if (selectedNodeNode == null) {
375                         String hostname = getNodeAddress();
376                         int port = getNodePort();
377                         if (hostname == null) {
378                                 hostname = "127.0.0.1";
379                                 port = 9481;
380                         }
381                         return new Node(hostname, port, "Node");
382                 }
383                 String name = selectedNodeNode.getNode("name").getValue();
384                 String hostname = selectedNodeNode.getNode("hostname").getValue();
385                 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
386                 return new Node(hostname, port, name);
387         }
388         
389 }