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.Closer;
41 import de.todesbaum.util.io.StreamCopier;
42 import de.todesbaum.util.xml.SimpleXML;
43 import de.todesbaum.util.xml.XML;
46 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
48 public class Configuration {
50 private String filename;
51 private String lockFilename;
52 private SimpleXML rootNode;
54 public Configuration() {
55 this(System.getProperty("user.home") + "/.jSite/config7");
58 public Configuration(String filename) {
59 this(filename, filename + ".lock");
62 public Configuration(String filename, String lockFilename) {
63 this.filename = filename;
64 this.lockFilename = lockFilename;
68 private boolean createConfigDirectory() {
69 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
70 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
73 public boolean createLockFile() {
74 if (!createConfigDirectory()) {
77 File lockFile = new File(lockFilename);
78 lockFile.deleteOnExit();
80 return lockFile.createNewFile();
81 } catch (IOException e) {
86 private void readConfiguration() {
87 File configurationFile = new File(filename);
88 if (configurationFile.exists()) {
89 ByteArrayOutputStream fileByteOutputStream = null;
90 FileInputStream fileInputStream = null;
92 fileByteOutputStream = new ByteArrayOutputStream();
93 fileInputStream = new FileInputStream(configurationFile);
94 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
95 fileByteOutputStream.close();
96 byte[] fileBytes = fileByteOutputStream.toByteArray();
97 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
99 } catch (FileNotFoundException e) {
100 } catch (IOException e) {
102 Closer.close(fileInputStream);
103 Closer.close(fileByteOutputStream);
106 rootNode = new SimpleXML("configuration");
109 public boolean save() {
110 File configurationFile = new File(filename);
111 if (!configurationFile.exists()) {
112 File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
113 if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
117 FileOutputStream fileOutputStream = null;
118 ByteArrayInputStream configurationInputStream = null;
120 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
121 configurationInputStream = new ByteArrayInputStream(configurationBytes);
122 fileOutputStream = new FileOutputStream(configurationFile);
123 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
125 } catch (IOException ioe1) {
127 Closer.close(configurationInputStream);
128 Closer.close(fileOutputStream);
133 private String getNodeValue(String[] nodeNames, String defaultValue) {
134 SimpleXML node = rootNode;
136 while ((node != null) && (nodeIndex < nodeNames.length)) {
137 node = node.getNode(nodeNames[nodeIndex++]);
142 return node.getValue();
145 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
147 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
148 } catch (NumberFormatException nfe1) {
153 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
154 String nodeValue = getNodeValue(nodeNames, null);
155 if (nodeValue == null) {
158 return Boolean.parseBoolean(nodeValue);
162 * Returns the hostname of the node.
163 * @return The hostname of the node
164 * @deprecated Use {@link #getSelectedNode()} instead
167 public String getNodeAddress() {
168 return getNodeValue(new String[] { "node-address" }, "localhost");
172 * Sets the hostname of the node.
173 * @param nodeAddress The hostname of the node
174 * @deprecated Use {@link #setSelectedNode(Node)} instead
177 public void setNodeAddress(String nodeAddress) {
178 rootNode.replace("node-address", nodeAddress);
182 * The port number of the node
183 * @return The port number of the node
184 * @deprecated Use {@link #getSelectedNode()} instead.
187 public int getNodePort() {
188 return getNodeIntValue(new String[] { "node-port" }, 9481);
192 * Sets the port number of the node.
193 * @param nodePort The port number of the node
194 * @deprecated Use {@link #setSelectedNode(Node)} instead
197 public void setNodePort(int nodePort) {
198 rootNode.replace("node-port", String.valueOf(nodePort));
201 public boolean isSkipNodePage() {
202 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
205 public void setSkipNodePage(boolean skipNodePage) {
206 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
209 public Project[] getProjects() {
210 List<Project> projects = new ArrayList<Project>();
211 SimpleXML projectsNode = rootNode.getNode("project-list");
212 if (projectsNode != null) {
213 SimpleXML[] projectNodes = projectsNode.getNodes("project");
214 for (SimpleXML projectNode: projectNodes) {
216 Project project = new Project();
217 projects.add(project);
218 project.setDescription(projectNode.getNode("description").getValue());
219 project.setIndexFile(projectNode.getNode("index-file").getValue());
220 project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
221 project.setLocalPath(projectNode.getNode("local-path").getValue());
222 project.setName(projectNode.getNode("name").getValue());
223 project.setPath(projectNode.getNode("path").getValue());
224 if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
225 project.setPath(project.getPath().replaceAll("/", ""));
227 project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
228 project.setInsertURI(projectNode.getNode("insert-uri").getValue());
229 project.setRequestURI(projectNode.getNode("request-uri").getValue());
230 SimpleXML fileOptionsNode = projectNode.getNode("file-options");
231 Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
232 if (fileOptionsNode != null) {
233 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
234 for (SimpleXML fileOptionNode: fileOptionNodes) {
235 String filename = fileOptionNode.getNode("filename").getValue();
236 FileOption fileOption = project.getFileOption(filename);
237 fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
238 fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
239 fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
240 fileOption.setContainer(fileOptionNode.getNode("container").getValue());
241 if (fileOptionNode.getNode("replace-edition") != null) {
242 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
243 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
245 fileOptions.put(filename, fileOption);
248 project.setFileOptions(fileOptions);
249 } catch (NumberFormatException nfe1) {
250 nfe1.printStackTrace();
254 return projects.toArray(new Project[projects.size()]);
257 public void setProjects(Project[] projects) {
258 SimpleXML projectsNode = new SimpleXML("project-list");
259 for (Project project: projects) {
260 SimpleXML projectNode = projectsNode.append("project");
261 projectNode.append("edition", String.valueOf(project.getEdition()));
262 projectNode.append("description", project.getDescription());
263 projectNode.append("index-file", project.getIndexFile());
264 projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
265 projectNode.append("local-path", project.getLocalPath());
266 projectNode.append("name", project.getName());
267 projectNode.append("path", project.getPath());
268 projectNode.append("insert-uri", project.getInsertURI());
269 projectNode.append("request-uri", project.getRequestURI());
270 SimpleXML fileOptionsNode = projectNode.append("file-options");
271 Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
272 while (entries.hasNext()) {
273 Entry<String, FileOption> entry = entries.next();
274 FileOption fileOption = entry.getValue();
275 if (fileOption.isCustom()) {
276 SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
277 fileOptionNode.append("filename", entry.getKey());
278 fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
279 fileOptionNode.append("custom-key", fileOption.getCustomKey());
280 fileOptionNode.append("mime-type", fileOption.getMimeType());
281 fileOptionNode.append("container", fileOption.getContainer());
282 fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
283 fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
287 rootNode.replace(projectsNode);
290 public Locale getLocale() {
291 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
292 String country = getNodeValue(new String[] { "i18n", "country" }, null);
293 if (country != null) {
294 return new Locale(language, country);
296 return new Locale(language);
299 public void setLocale(Locale locale) {
300 SimpleXML i18nNode = new SimpleXML("i18n");
301 if (locale.getCountry().length() != 0) {
302 i18nNode.append("country", locale.getCountry());
304 i18nNode.append("language", locale.getLanguage());
305 rootNode.replace(i18nNode);
309 public Node[] getNodes() {
310 SimpleXML nodesNode = rootNode.getNode("nodes");
311 if (nodesNode == null) {
312 String hostname = getNodeAddress();
313 int port = getNodePort();
314 if (hostname == null) {
315 hostname = "127.0.0.1";
318 return new Node[] { new Node(hostname, port, "Node") };
320 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
321 Node[] returnNodes = new Node[nodeNodes.length];
323 for (SimpleXML nodeNode: nodeNodes) {
324 String name = nodeNode.getNode("name").getValue();
325 String hostname = nodeNode.getNode("hostname").getValue();
326 int port = Integer.parseInt(nodeNode.getNode("port").getValue());
327 Node node = new Node(hostname, port, name);
328 returnNodes[nodeIndex++] = node;
333 public void setNodes(Node[] nodes) {
334 SimpleXML nodesNode = new SimpleXML("nodes");
335 for (Node node: nodes) {
336 SimpleXML nodeNode = nodesNode.append("node");
337 nodeNode.append("name", node.getName());
338 nodeNode.append("hostname", node.getHostname());
339 nodeNode.append("port", String.valueOf(node.getPort()));
341 rootNode.replace(nodesNode);
342 rootNode.remove("node-address");
343 rootNode.remove("node-port");
346 public void setSelectedNode(Node selectedNode) {
347 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
348 selectedNodeNode.append("name", selectedNode.getName());
349 selectedNodeNode.append("hostname", selectedNode.getHostname());
350 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
351 rootNode.replace(selectedNodeNode);
354 public Node getSelectedNode() {
355 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
356 if (selectedNodeNode == null) {
357 String hostname = getNodeAddress();
358 int port = getNodePort();
359 if (hostname == null) {
360 hostname = "127.0.0.1";
363 return new Node(hostname, port, "Node");
365 String name = selectedNodeNode.getNode("name").getValue();
366 String hostname = selectedNodeNode.getNode("hostname").getValue();
367 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
368 return new Node(hostname, port, name);