X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fmain%2FConfiguration.java;h=75f2204849562c605be69fa188c16fe1a6e084ee;hb=a7d2ac9e58d1fffbb8a9c63c7d4081e8717a6f7f;hp=9b4378491819abb161a2a16ab26d76a3897dc0cf;hpb=e4f461213da0e30faf9e9eb2e97626abff320618;p=jSite.git diff --git a/src/de/todesbaum/jsite/main/Configuration.java b/src/de/todesbaum/jsite/main/Configuration.java index 9b43784..75f2204 100644 --- a/src/de/todesbaum/jsite/main/Configuration.java +++ b/src/de/todesbaum/jsite/main/Configuration.java @@ -33,86 +33,127 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.logging.Level; +import java.util.logging.Logger; -import de.todesbaum.jsite.application.EditionProject; import de.todesbaum.jsite.application.FileOption; import de.todesbaum.jsite.application.Node; import de.todesbaum.jsite.application.Project; +import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation; +import de.todesbaum.util.io.Closer; import de.todesbaum.util.io.StreamCopier; import de.todesbaum.util.xml.SimpleXML; import de.todesbaum.util.xml.XML; /** - * @author David Roden <droden@gmail.com> - * @version $Id$ + * The configuration. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class Configuration { - private String filename; - private String lockFilename; + /** The root node of the configuration. */ private SimpleXML rootNode; - public Configuration() { - filename = System.getProperty("user.home") + "/.jSite/config7"; - lockFilename = System.getProperty("user.home") + "/.jSite/lock7"; - readConfiguration(); + /** The configuration locator. */ + private final ConfigurationLocator configurationLocator; + + /** Where the configuration resides. */ + private ConfigurationLocation configurationLocation; + + /** + * Creates a new configuration that is read from the given file. + * + * @param configurationLocator + * The configuration locator + * @param configurationLocation + * The configuration directory + */ + public Configuration(ConfigurationLocator configurationLocator, ConfigurationLocation configurationLocation) { + this.configurationLocator = configurationLocator; + this.configurationLocation = configurationLocation; + readConfiguration(configurationLocator.getFile(configurationLocation)); } - - private boolean createConfigDirectory() { - File configDirectory = new File(System.getProperty("user.home"), ".jSite"); - return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs(); + + // + // ACCESSORS + // + + /** + * Returns the configuration locator. + * + * @return The configuration locator + */ + public ConfigurationLocator getConfigurationLocator() { + return configurationLocator; } - public boolean createLockFile() { - if (!createConfigDirectory()) { - return false; - } - File lockFile = new File(lockFilename); - lockFile.deleteOnExit(); - try { - return lockFile.createNewFile(); - } catch (IOException e) { - } - return false; + /** + * Returns the location the configuration will be written to when calling + * {@link #save()}. + * + * @return The location the configuration will be written to + */ + public ConfigurationLocation getConfigurationDirectory() { + return configurationLocation; } - private void readConfiguration() { - File configurationFile = new File(filename); - if (configurationFile.exists()) { - ByteArrayOutputStream fileByteOutputStream = null; - FileInputStream fileInputStream = null; - try { - fileByteOutputStream = new ByteArrayOutputStream(); - fileInputStream = new FileInputStream(configurationFile); - StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length()); - fileByteOutputStream.close(); - byte[] fileBytes = fileByteOutputStream.toByteArray(); - rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes)); - return; - } catch (FileNotFoundException e) { - } catch (IOException e) { - } finally { - if (fileInputStream != null) { - try { - fileInputStream.close(); - } catch (IOException ioe1) { - } - } - if (fileByteOutputStream != null) { - try { - fileByteOutputStream.close(); - } catch (IOException ioe1) { - } + /** + * Sets the location the configuration will be written to when calling + * {@link #save()}. + * + * @param configurationLocation + * The location to write the configuration to + */ + public void setConfigurationLocation(ConfigurationLocation configurationLocation) { + this.configurationLocation = configurationLocation; + } + + /** + * Reads the configuration from the file. + * + * @param filename + * The name of the file to read the configuration from + */ + private void readConfiguration(String filename) { + Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Reading configuration from: " + filename); + if (filename != null) { + File configurationFile = new File(filename); + if (configurationFile.exists()) { + ByteArrayOutputStream fileByteOutputStream = null; + FileInputStream fileInputStream = null; + try { + fileByteOutputStream = new ByteArrayOutputStream(); + fileInputStream = new FileInputStream(configurationFile); + StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length()); + fileByteOutputStream.close(); + byte[] fileBytes = fileByteOutputStream.toByteArray(); + rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes)); + return; + } catch (FileNotFoundException e) { + /* ignore. */ + } catch (IOException e) { + /* ignore. */ + } finally { + Closer.close(fileInputStream); + Closer.close(fileByteOutputStream); } } } rootNode = new SimpleXML("configuration"); } + /** + * Saves the configuration. + * + * @return true if the configuration could be saved, + * false otherwise + */ public boolean save() { - File configurationFile = new File(filename); + Logger.getLogger(Configuration.class.getName()).log(Level.CONFIG, "Trying to save configuration to: " + configurationLocation); + File configurationFile = new File(configurationLocator.getFile(configurationLocation)); if (!configurationFile.exists()) { - File configurationFilePath = configurationFile.getParentFile(); + File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile(); if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) { return false; } @@ -126,23 +167,24 @@ public class Configuration { StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length); return true; } catch (IOException ioe1) { + /* ignore. */ } finally { - if (configurationInputStream != null) { - try { - configurationInputStream.close(); - } catch (IOException ioe1) { - } - } - if (fileOutputStream != null) { - try { - fileOutputStream.close(); - } catch (IOException ioe1) { - } - } + Closer.close(configurationInputStream); + Closer.close(fileOutputStream); } return false; } + /** + * Returns the value of a node. + * + * @param nodeNames + * The name of all nodes in the chain + * @param defaultValue + * The default value to return if the node could not be found + * @return The value of the node, or the default value if the node could not + * be found + */ private String getNodeValue(String[] nodeNames, String defaultValue) { SimpleXML node = rootNode; int nodeIndex = 0; @@ -155,14 +197,35 @@ public class Configuration { return node.getValue(); } + /** + * Returns the integer value of a node. + * + * @param nodeNames + * The names of all nodes in the chain + * @param defaultValue + * The default value to return if the node can not be found + * @return The parsed integer value, or the default value if the node can + * not be found or the value can not be parsed into an integer + */ private int getNodeIntValue(String[] nodeNames, int defaultValue) { try { return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue))); } catch (NumberFormatException nfe1) { + /* ignore. */ } return defaultValue; } + /** + * Returns the boolean value of a node. + * + * @param nodeNames + * The names of all nodes in the chain + * @param defaultValue + * The default value to return if the node can not be found + * @return The parsed boolean value, or the default value if the node can + * not be found + */ private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) { String nodeValue = getNodeValue(nodeNames, null); if (nodeValue == null) { @@ -173,6 +236,7 @@ public class Configuration { /** * Returns the hostname of the node. + * * @return The hostname of the node * @deprecated Use {@link #getSelectedNode()} instead */ @@ -183,7 +247,9 @@ public class Configuration { /** * Sets the hostname of the node. - * @param nodeAddress The hostname of the node + * + * @param nodeAddress + * The hostname of the node * @deprecated Use {@link #setSelectedNode(Node)} instead */ @Deprecated @@ -193,8 +259,9 @@ public class Configuration { /** * The port number of the node + * * @return The port number of the node - * @deprecated Use {@link #getSelectedNode()} instead. + * @deprecated Use {@link #getSelectedNode()} instead. */ @Deprecated public int getNodePort() { @@ -203,7 +270,9 @@ public class Configuration { /** * Sets the port number of the node. - * @param nodePort The port number of the node + * + * @param nodePort + * The port number of the node * @deprecated Use {@link #setSelectedNode(Node)} instead */ @Deprecated @@ -211,47 +280,78 @@ public class Configuration { rootNode.replace("node-port", String.valueOf(nodePort)); } + /** + * Returns whether the node configuration page should be skipped on startup. + * + * @return true to skip the node configuration page on startup, + * false to show it + */ public boolean isSkipNodePage() { return getNodeBooleanValue(new String[] { "skip-node-page" }, false); } + /** + * Sets whether the node configuration page should be skipped on startup. + * + * @param skipNodePage + * true to skip the node configuration page on + * startup, false to show it + */ public void setSkipNodePage(boolean skipNodePage) { rootNode.replace("skip-node-page", String.valueOf(skipNodePage)); } + /** + * Returns all configured projects. + * + * @return A list of all projects + */ public Project[] getProjects() { List projects = new ArrayList(); SimpleXML projectsNode = rootNode.getNode("project-list"); if (projectsNode != null) { SimpleXML[] projectNodes = projectsNode.getNodes("project"); - for (SimpleXML projectNode: projectNodes) { + for (SimpleXML projectNode : projectNodes) { try { - Project project = null; - SimpleXML typeNode = projectNode.getNode("type"); - if ("edition".equals(typeNode.getValue())) { - EditionProject editionProject = new EditionProject(); - project = editionProject; - editionProject.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue())); - } + Project project = new Project(); projects.add(project); - project.setDescription(projectNode.getNode("description").getValue()); - project.setIndexFile(projectNode.getNode("index-file").getValue()); - project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue())); - project.setLocalPath(projectNode.getNode("local-path").getValue()); - project.setName(projectNode.getNode("name").getValue()); - project.setPath(projectNode.getNode("path").getValue()); - project.setInsertURI(projectNode.getNode("insert-uri").getValue()); - project.setRequestURI(projectNode.getNode("request-uri").getValue()); + project.setDescription(projectNode.getNode("description").getValue("")); + String indexFile = projectNode.getNode("index-file").getValue(""); + if (indexFile.indexOf('/') > -1) { + indexFile = ""; + } + project.setIndexFile(indexFile); + project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0"))); + project.setLocalPath(projectNode.getNode("local-path").getValue("")); + project.setName(projectNode.getNode("name").getValue("")); + project.setPath(projectNode.getNode("path").getValue("")); + if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) { + project.setPath(project.getPath().replaceAll("/", "")); + } + project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0"))); + project.setInsertURI(projectNode.getNode("insert-uri").getValue("")); + project.setRequestURI(projectNode.getNode("request-uri").getValue("")); + if (projectNode.getNode("ignore-hidden-files") != null) { + project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true"))); + } else { + project.setIgnoreHiddenFiles(true); + } SimpleXML fileOptionsNode = projectNode.getNode("file-options"); Map fileOptions = new HashMap(); if (fileOptionsNode != null) { SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option"); - for (SimpleXML fileOptionNode: fileOptionNodes) { + for (SimpleXML fileOptionNode : fileOptionNodes) { String filename = fileOptionNode.getNode("filename").getValue(); FileOption fileOption = project.getFileOption(filename); fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue())); - fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue()); - fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue()); + if (fileOptionNode.getNode("insert-redirect") != null) { + fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue())); + } + fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue("")); + if (fileOptionNode.getNode("changed-name") != null) { + fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue()); + } + fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue("")); fileOption.setContainer(fileOptionNode.getNode("container").getValue()); if (fileOptionNode.getNode("replace-edition") != null) { fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue())); @@ -269,14 +369,17 @@ public class Configuration { return projects.toArray(new Project[projects.size()]); } + /** + * Sets the list of all projects. + * + * @param projects + * The list of all projects + */ public void setProjects(Project[] projects) { SimpleXML projectsNode = new SimpleXML("project-list"); - for (Project project: projects) { + for (Project project : projects) { SimpleXML projectNode = projectsNode.append("project"); - if (project instanceof EditionProject) { - projectNode.append("type", "edition"); - projectNode.append("edition", String.valueOf(((EditionProject) project).getEdition())); - } + projectNode.append("edition", String.valueOf(project.getEdition())); projectNode.append("description", project.getDescription()); projectNode.append("index-file", project.getIndexFile()); projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime())); @@ -285,6 +388,7 @@ public class Configuration { projectNode.append("path", project.getPath()); projectNode.append("insert-uri", project.getInsertURI()); projectNode.append("request-uri", project.getRequestURI()); + projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles())); SimpleXML fileOptionsNode = projectNode.append("file-options"); Iterator> entries = project.getFileOptions().entrySet().iterator(); while (entries.hasNext()) { @@ -294,7 +398,9 @@ public class Configuration { SimpleXML fileOptionNode = fileOptionsNode.append("file-option"); fileOptionNode.append("filename", entry.getKey()); fileOptionNode.append("insert", String.valueOf(fileOption.isInsert())); + fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect())); fileOptionNode.append("custom-key", fileOption.getCustomKey()); + fileOptionNode.append("changed-name", fileOption.getChangedName()); fileOptionNode.append("mime-type", fileOption.getMimeType()); fileOptionNode.append("container", fileOption.getContainer()); fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition())); @@ -305,6 +411,11 @@ public class Configuration { rootNode.replace(projectsNode); } + /** + * Returns the stored locale. + * + * @return The stored locale + */ public Locale getLocale() { String language = getNodeValue(new String[] { "i18n", "language" }, "en"); String country = getNodeValue(new String[] { "i18n", "country" }, null); @@ -314,6 +425,12 @@ public class Configuration { return new Locale(language); } + /** + * Sets the locale to store. + * + * @param locale + * The locale to store + */ public void setLocale(Locale locale) { SimpleXML i18nNode = new SimpleXML("i18n"); if (locale.getCountry().length() != 0) { @@ -323,7 +440,12 @@ public class Configuration { rootNode.replace(i18nNode); return; } - + + /** + * Returns a list of configured nodes. + * + * @return The list of the configured nodes + */ public Node[] getNodes() { SimpleXML nodesNode = rootNode.getNode("nodes"); if (nodesNode == null) { @@ -338,7 +460,7 @@ public class Configuration { SimpleXML[] nodeNodes = nodesNode.getNodes("node"); Node[] returnNodes = new Node[nodeNodes.length]; int nodeIndex = 0; - for (SimpleXML nodeNode: nodeNodes) { + for (SimpleXML nodeNode : nodeNodes) { String name = nodeNode.getNode("name").getValue(); String hostname = nodeNode.getNode("hostname").getValue(); int port = Integer.parseInt(nodeNode.getNode("port").getValue()); @@ -347,10 +469,16 @@ public class Configuration { } return returnNodes; } - + + /** + * Sets the list of configured nodes. + * + * @param nodes + * The list of configured nodes + */ public void setNodes(Node[] nodes) { SimpleXML nodesNode = new SimpleXML("nodes"); - for (Node node: nodes) { + for (Node node : nodes) { SimpleXML nodeNode = nodesNode.append("node"); nodeNode.append("name", node.getName()); nodeNode.append("hostname", node.getHostname()); @@ -361,6 +489,12 @@ public class Configuration { rootNode.remove("node-port"); } + /** + * Sets the selected node. + * + * @param selectedNode + * The selected node + */ public void setSelectedNode(Node selectedNode) { SimpleXML selectedNodeNode = new SimpleXML("selected-node"); selectedNodeNode.append("name", selectedNode.getName()); @@ -368,7 +502,12 @@ public class Configuration { selectedNodeNode.append("port", String.valueOf(selectedNode.getPort())); rootNode.replace(selectedNodeNode); } - + + /** + * Returns the selected node. + * + * @return The selected node + */ public Node getSelectedNode() { SimpleXML selectedNodeNode = rootNode.getNode("selected-node"); if (selectedNodeNode == null) { @@ -385,5 +524,32 @@ public class Configuration { int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue()); return new Node(hostname, port, name); } - + + /** + * Returns the temp directory to use. + * + * @return The temp directoy, or {@code null} to use the default temp + * directory + */ + public String getTempDirectory() { + return getNodeValue(new String[] { "temp-directory" }, null); + } + + /** + * Sets the temp directory to use. + * + * @param tempDirectory + * The temp directory to use, or {@code null} to use the default + * temp directory + */ + public void setTempDirectory(String tempDirectory) { + if (tempDirectory != null) { + SimpleXML tempDirectoryNode = new SimpleXML("temp-directory"); + tempDirectoryNode.setValue(tempDirectory); + rootNode.replace(tempDirectoryNode); + } else { + rootNode.remove("temp-directory"); + } + } + }