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.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;
46 * @author David Roden <droden@gmail.com>
49 public class Configuration {
51 private String filename;
52 private String lockFilename;
53 private SimpleXML rootNode;
55 public Configuration() {
56 this(System.getProperty("user.home") + "/.jSite/config7");
59 public Configuration(String filename) {
60 this(filename, filename + ".lock");
63 public Configuration(String filename, String lockFilename) {
64 this.filename = filename;
65 this.lockFilename = lockFilename;
69 private boolean createConfigDirectory() {
70 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
71 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
74 public boolean createLockFile() {
75 if (!createConfigDirectory()) {
78 File lockFile = new File(lockFilename);
79 lockFile.deleteOnExit();
81 return lockFile.createNewFile();
82 } catch (IOException e) {
87 private void readConfiguration() {
88 File configurationFile = new File(filename);
89 if (configurationFile.exists()) {
90 ByteArrayOutputStream fileByteOutputStream = null;
91 FileInputStream fileInputStream = null;
93 fileByteOutputStream = new ByteArrayOutputStream();
94 fileInputStream = new FileInputStream(configurationFile);
95 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
96 fileByteOutputStream.close();
97 byte[] fileBytes = fileByteOutputStream.toByteArray();
98 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
100 } catch (FileNotFoundException e) {
101 } catch (IOException e) {
103 if (fileInputStream != null) {
105 fileInputStream.close();
106 } catch (IOException ioe1) {
109 if (fileByteOutputStream != null) {
111 fileByteOutputStream.close();
112 } catch (IOException ioe1) {
117 rootNode = new SimpleXML("configuration");
120 public boolean save() {
121 File configurationFile = new File(filename);
122 if (!configurationFile.exists()) {
123 File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
124 if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
128 FileOutputStream fileOutputStream = null;
129 ByteArrayInputStream configurationInputStream = null;
131 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
132 configurationInputStream = new ByteArrayInputStream(configurationBytes);
133 fileOutputStream = new FileOutputStream(configurationFile);
134 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
136 } catch (IOException ioe1) {
138 if (configurationInputStream != null) {
140 configurationInputStream.close();
141 } catch (IOException ioe1) {
144 if (fileOutputStream != null) {
146 fileOutputStream.close();
147 } catch (IOException ioe1) {
154 private String getNodeValue(String[] nodeNames, String defaultValue) {
155 SimpleXML node = rootNode;
157 while ((node != null) && (nodeIndex < nodeNames.length)) {
158 node = node.getNode(nodeNames[nodeIndex++]);
163 return node.getValue();
166 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
168 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
169 } catch (NumberFormatException nfe1) {
174 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
175 String nodeValue = getNodeValue(nodeNames, null);
176 if (nodeValue == null) {
179 return Boolean.parseBoolean(nodeValue);
183 * Returns the hostname of the node.
184 * @return The hostname of the node
185 * @deprecated Use {@link #getSelectedNode()} instead
188 public String getNodeAddress() {
189 return getNodeValue(new String[] { "node-address" }, "localhost");
193 * Sets the hostname of the node.
194 * @param nodeAddress The hostname of the node
195 * @deprecated Use {@link #setSelectedNode(Node)} instead
198 public void setNodeAddress(String nodeAddress) {
199 rootNode.replace("node-address", nodeAddress);
203 * The port number of the node
204 * @return The port number of the node
205 * @deprecated Use {@link #getSelectedNode()} instead.
208 public int getNodePort() {
209 return getNodeIntValue(new String[] { "node-port" }, 9481);
213 * Sets the port number of the node.
214 * @param nodePort The port number of the node
215 * @deprecated Use {@link #setSelectedNode(Node)} instead
218 public void setNodePort(int nodePort) {
219 rootNode.replace("node-port", String.valueOf(nodePort));
222 public boolean isSkipNodePage() {
223 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
226 public void setSkipNodePage(boolean skipNodePage) {
227 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
230 public Project[] getProjects() {
231 List<Project> projects = new ArrayList<Project>();
232 SimpleXML projectsNode = rootNode.getNode("project-list");
233 if (projectsNode != null) {
234 SimpleXML[] projectNodes = projectsNode.getNodes("project");
235 for (SimpleXML projectNode: projectNodes) {
237 Project project = null;
238 SimpleXML typeNode = projectNode.getNode("type");
239 if ("edition".equals(typeNode.getValue())) {
240 EditionProject editionProject = new EditionProject();
241 project = editionProject;
242 editionProject.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
244 projects.add(project);
245 project.setDescription(projectNode.getNode("description").getValue());
246 project.setIndexFile(projectNode.getNode("index-file").getValue());
247 project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
248 project.setLocalPath(projectNode.getNode("local-path").getValue());
249 project.setName(projectNode.getNode("name").getValue());
250 project.setPath(projectNode.getNode("path").getValue());
251 project.setInsertURI(projectNode.getNode("insert-uri").getValue());
252 project.setRequestURI(projectNode.getNode("request-uri").getValue());
253 SimpleXML fileOptionsNode = projectNode.getNode("file-options");
254 Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
255 if (fileOptionsNode != null) {
256 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
257 for (SimpleXML fileOptionNode: fileOptionNodes) {
258 String filename = fileOptionNode.getNode("filename").getValue();
259 FileOption fileOption = project.getFileOption(filename);
260 fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
261 fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
262 fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
263 fileOption.setContainer(fileOptionNode.getNode("container").getValue());
264 if (fileOptionNode.getNode("replace-edition") != null) {
265 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
266 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
268 fileOptions.put(filename, fileOption);
271 project.setFileOptions(fileOptions);
272 } catch (NumberFormatException nfe1) {
273 nfe1.printStackTrace();
277 return projects.toArray(new Project[projects.size()]);
280 public void setProjects(Project[] projects) {
281 SimpleXML projectsNode = new SimpleXML("project-list");
282 for (Project project: projects) {
283 SimpleXML projectNode = projectsNode.append("project");
284 if (project instanceof EditionProject) {
285 projectNode.append("type", "edition");
286 projectNode.append("edition", String.valueOf(((EditionProject) project).getEdition()));
288 projectNode.append("description", project.getDescription());
289 projectNode.append("index-file", project.getIndexFile());
290 projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
291 projectNode.append("local-path", project.getLocalPath());
292 projectNode.append("name", project.getName());
293 projectNode.append("path", project.getPath());
294 projectNode.append("insert-uri", project.getInsertURI());
295 projectNode.append("request-uri", project.getRequestURI());
296 SimpleXML fileOptionsNode = projectNode.append("file-options");
297 Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
298 while (entries.hasNext()) {
299 Entry<String, FileOption> entry = entries.next();
300 FileOption fileOption = entry.getValue();
301 if (fileOption.isCustom()) {
302 SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
303 fileOptionNode.append("filename", entry.getKey());
304 fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
305 fileOptionNode.append("custom-key", fileOption.getCustomKey());
306 fileOptionNode.append("mime-type", fileOption.getMimeType());
307 fileOptionNode.append("container", fileOption.getContainer());
308 fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
309 fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
313 rootNode.replace(projectsNode);
316 public Locale getLocale() {
317 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
318 String country = getNodeValue(new String[] { "i18n", "country" }, null);
319 if (country != null) {
320 return new Locale(language, country);
322 return new Locale(language);
325 public void setLocale(Locale locale) {
326 SimpleXML i18nNode = new SimpleXML("i18n");
327 if (locale.getCountry().length() != 0) {
328 i18nNode.append("country", locale.getCountry());
330 i18nNode.append("language", locale.getLanguage());
331 rootNode.replace(i18nNode);
335 public Node[] getNodes() {
336 SimpleXML nodesNode = rootNode.getNode("nodes");
337 if (nodesNode == null) {
338 String hostname = getNodeAddress();
339 int port = getNodePort();
340 if (hostname == null) {
341 hostname = "127.0.0.1";
344 return new Node[] { new Node(hostname, port, "Node") };
346 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
347 Node[] returnNodes = new Node[nodeNodes.length];
349 for (SimpleXML nodeNode: nodeNodes) {
350 String name = nodeNode.getNode("name").getValue();
351 String hostname = nodeNode.getNode("hostname").getValue();
352 int port = Integer.parseInt(nodeNode.getNode("port").getValue());
353 Node node = new Node(hostname, port, name);
354 returnNodes[nodeIndex++] = node;
359 public void setNodes(Node[] nodes) {
360 SimpleXML nodesNode = new SimpleXML("nodes");
361 for (Node node: nodes) {
362 SimpleXML nodeNode = nodesNode.append("node");
363 nodeNode.append("name", node.getName());
364 nodeNode.append("hostname", node.getHostname());
365 nodeNode.append("port", String.valueOf(node.getPort()));
367 rootNode.replace(nodesNode);
368 rootNode.remove("node-address");
369 rootNode.remove("node-port");
372 public void setSelectedNode(Node selectedNode) {
373 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
374 selectedNodeNode.append("name", selectedNode.getName());
375 selectedNodeNode.append("hostname", selectedNode.getHostname());
376 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
377 rootNode.replace(selectedNodeNode);
380 public Node getSelectedNode() {
381 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
382 if (selectedNodeNode == null) {
383 String hostname = getNodeAddress();
384 int port = getNodePort();
385 if (hostname == null) {
386 hostname = "127.0.0.1";
389 return new Node(hostname, port, "Node");
391 String name = selectedNodeNode.getNode("name").getValue();
392 String hostname = selectedNodeNode.getNode("hostname").getValue();
393 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
394 return new Node(hostname, port, name);