2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.main;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
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;
35 import java.util.Map.Entry;
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.StreamCopier;
41 import de.todesbaum.util.xml.SimpleXML;
42 import de.todesbaum.util.xml.XML;
45 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
47 public class Configuration {
49 private String filename;
50 private String lockFilename;
51 private SimpleXML rootNode;
53 public Configuration() {
54 this(System.getProperty("user.home") + "/.jSite/config7");
57 public Configuration(String filename) {
58 this(filename, filename + ".lock");
61 public Configuration(String filename, String lockFilename) {
62 this.filename = filename;
63 this.lockFilename = lockFilename;
67 private boolean createConfigDirectory() {
68 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
69 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
72 public boolean createLockFile() {
73 if (!createConfigDirectory()) {
76 File lockFile = new File(lockFilename);
77 lockFile.deleteOnExit();
79 return lockFile.createNewFile();
80 } catch (IOException e) {
85 private void readConfiguration() {
86 File configurationFile = new File(filename);
87 if (configurationFile.exists()) {
88 ByteArrayOutputStream fileByteOutputStream = null;
89 FileInputStream fileInputStream = null;
91 fileByteOutputStream = new ByteArrayOutputStream();
92 fileInputStream = new FileInputStream(configurationFile);
93 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
94 fileByteOutputStream.close();
95 byte[] fileBytes = fileByteOutputStream.toByteArray();
96 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
98 } catch (FileNotFoundException e) {
99 } catch (IOException e) {
101 if (fileInputStream != null) {
103 fileInputStream.close();
104 } catch (IOException ioe1) {
107 if (fileByteOutputStream != null) {
109 fileByteOutputStream.close();
110 } catch (IOException ioe1) {
115 rootNode = new SimpleXML("configuration");
118 public boolean save() {
119 File configurationFile = new File(filename);
120 if (!configurationFile.exists()) {
121 File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
122 if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
126 FileOutputStream fileOutputStream = null;
127 ByteArrayInputStream configurationInputStream = null;
129 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
130 configurationInputStream = new ByteArrayInputStream(configurationBytes);
131 fileOutputStream = new FileOutputStream(configurationFile);
132 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
134 } catch (IOException ioe1) {
136 if (configurationInputStream != null) {
138 configurationInputStream.close();
139 } catch (IOException ioe1) {
142 if (fileOutputStream != null) {
144 fileOutputStream.close();
145 } catch (IOException ioe1) {
152 private String getNodeValue(String[] nodeNames, String defaultValue) {
153 SimpleXML node = rootNode;
155 while ((node != null) && (nodeIndex < nodeNames.length)) {
156 node = node.getNode(nodeNames[nodeIndex++]);
161 return node.getValue();
164 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
166 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
167 } catch (NumberFormatException nfe1) {
172 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
173 String nodeValue = getNodeValue(nodeNames, null);
174 if (nodeValue == null) {
177 return Boolean.parseBoolean(nodeValue);
181 * Returns the hostname of the node.
182 * @return The hostname of the node
183 * @deprecated Use {@link #getSelectedNode()} instead
186 public String getNodeAddress() {
187 return getNodeValue(new String[] { "node-address" }, "localhost");
191 * Sets the hostname of the node.
192 * @param nodeAddress The hostname of the node
193 * @deprecated Use {@link #setSelectedNode(Node)} instead
196 public void setNodeAddress(String nodeAddress) {
197 rootNode.replace("node-address", nodeAddress);
201 * The port number of the node
202 * @return The port number of the node
203 * @deprecated Use {@link #getSelectedNode()} instead.
206 public int getNodePort() {
207 return getNodeIntValue(new String[] { "node-port" }, 9481);
211 * Sets the port number of the node.
212 * @param nodePort The port number of the node
213 * @deprecated Use {@link #setSelectedNode(Node)} instead
216 public void setNodePort(int nodePort) {
217 rootNode.replace("node-port", String.valueOf(nodePort));
220 public boolean isSkipNodePage() {
221 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
224 public void setSkipNodePage(boolean skipNodePage) {
225 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
228 public Project[] getProjects() {
229 List<Project> projects = new ArrayList<Project>();
230 SimpleXML projectsNode = rootNode.getNode("project-list");
231 if (projectsNode != null) {
232 SimpleXML[] projectNodes = projectsNode.getNodes("project");
233 for (SimpleXML projectNode: projectNodes) {
235 Project project = new Project();
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 if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
244 project.setPath(project.getPath().replaceAll("/", ""));
246 project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
247 project.setInsertURI(projectNode.getNode("insert-uri").getValue());
248 project.setRequestURI(projectNode.getNode("request-uri").getValue());
249 SimpleXML fileOptionsNode = projectNode.getNode("file-options");
250 Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
251 if (fileOptionsNode != null) {
252 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
253 for (SimpleXML fileOptionNode: fileOptionNodes) {
254 String filename = fileOptionNode.getNode("filename").getValue();
255 FileOption fileOption = project.getFileOption(filename);
256 fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
257 fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
258 fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
259 fileOption.setContainer(fileOptionNode.getNode("container").getValue());
260 if (fileOptionNode.getNode("replace-edition") != null) {
261 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
262 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
264 fileOptions.put(filename, fileOption);
267 project.setFileOptions(fileOptions);
268 } catch (NumberFormatException nfe1) {
269 nfe1.printStackTrace();
273 return projects.toArray(new Project[projects.size()]);
276 public void setProjects(Project[] projects) {
277 SimpleXML projectsNode = new SimpleXML("project-list");
278 for (Project project: projects) {
279 SimpleXML projectNode = projectsNode.append("project");
280 projectNode.append("edition", String.valueOf(project.getEdition()));
281 projectNode.append("description", project.getDescription());
282 projectNode.append("index-file", project.getIndexFile());
283 projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
284 projectNode.append("local-path", project.getLocalPath());
285 projectNode.append("name", project.getName());
286 projectNode.append("path", project.getPath());
287 projectNode.append("insert-uri", project.getInsertURI());
288 projectNode.append("request-uri", project.getRequestURI());
289 SimpleXML fileOptionsNode = projectNode.append("file-options");
290 Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
291 while (entries.hasNext()) {
292 Entry<String, FileOption> entry = entries.next();
293 FileOption fileOption = entry.getValue();
294 if (fileOption.isCustom()) {
295 SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
296 fileOptionNode.append("filename", entry.getKey());
297 fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
298 fileOptionNode.append("custom-key", fileOption.getCustomKey());
299 fileOptionNode.append("mime-type", fileOption.getMimeType());
300 fileOptionNode.append("container", fileOption.getContainer());
301 fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
302 fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
306 rootNode.replace(projectsNode);
309 public Locale getLocale() {
310 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
311 String country = getNodeValue(new String[] { "i18n", "country" }, null);
312 if (country != null) {
313 return new Locale(language, country);
315 return new Locale(language);
318 public void setLocale(Locale locale) {
319 SimpleXML i18nNode = new SimpleXML("i18n");
320 if (locale.getCountry().length() != 0) {
321 i18nNode.append("country", locale.getCountry());
323 i18nNode.append("language", locale.getLanguage());
324 rootNode.replace(i18nNode);
328 public Node[] getNodes() {
329 SimpleXML nodesNode = rootNode.getNode("nodes");
330 if (nodesNode == null) {
331 String hostname = getNodeAddress();
332 int port = getNodePort();
333 if (hostname == null) {
334 hostname = "127.0.0.1";
337 return new Node[] { new Node(hostname, port, "Node") };
339 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
340 Node[] returnNodes = new Node[nodeNodes.length];
342 for (SimpleXML nodeNode: nodeNodes) {
343 String name = nodeNode.getNode("name").getValue();
344 String hostname = nodeNode.getNode("hostname").getValue();
345 int port = Integer.parseInt(nodeNode.getNode("port").getValue());
346 Node node = new Node(hostname, port, name);
347 returnNodes[nodeIndex++] = node;
352 public void setNodes(Node[] nodes) {
353 SimpleXML nodesNode = new SimpleXML("nodes");
354 for (Node node: nodes) {
355 SimpleXML nodeNode = nodesNode.append("node");
356 nodeNode.append("name", node.getName());
357 nodeNode.append("hostname", node.getHostname());
358 nodeNode.append("port", String.valueOf(node.getPort()));
360 rootNode.replace(nodesNode);
361 rootNode.remove("node-address");
362 rootNode.remove("node-port");
365 public void setSelectedNode(Node selectedNode) {
366 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
367 selectedNodeNode.append("name", selectedNode.getName());
368 selectedNodeNode.append("hostname", selectedNode.getHostname());
369 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
370 rootNode.replace(selectedNodeNode);
373 public Node getSelectedNode() {
374 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
375 if (selectedNodeNode == null) {
376 String hostname = getNodeAddress();
377 int port = getNodePort();
378 if (hostname == null) {
379 hostname = "127.0.0.1";
382 return new Node(hostname, port, "Node");
384 String name = selectedNodeNode.getNode("name").getValue();
385 String hostname = selectedNodeNode.getNode("hostname").getValue();
386 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
387 return new Node(hostname, port, name);