X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fmain%2FConfiguration.java;h=75f2204849562c605be69fa188c16fe1a6e084ee;hb=a7d2ac9e58d1fffbb8a9c63c7d4081e8717a6f7f;hp=49f107b8281707ca05c7ed6505aeb2b79d3fc380;hpb=ea1f1cf3741c2e21edcbc6127e6e171b125cff2b;p=jSite.git diff --git a/src/de/todesbaum/jsite/main/Configuration.java b/src/de/todesbaum/jsite/main/Configuration.java index 49f107b..75f2204 100644 --- a/src/de/todesbaum/jsite/main/Configuration.java +++ b/src/de/todesbaum/jsite/main/Configuration.java @@ -33,10 +33,13 @@ 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.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; @@ -44,107 +47,97 @@ import de.todesbaum.util.xml.XML; /** * The configuration. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class Configuration { - /** The name of the file the configuration is stored to. */ - private String filename; - - /** The name of the lock file. */ - private String lockFilename; - /** The root node of the configuration. */ private SimpleXML rootNode; - /** - * Creates a new configuration with the default name of the configuration - * file. - */ - public Configuration() { - this(System.getProperty("user.home") + "/.jSite/config7"); - } + /** 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 filename - * The name of the configuration file + * + * @param configurationLocator + * The configuration locator + * @param configurationLocation + * The configuration directory */ - public Configuration(String filename) { - this(filename, filename + ".lock"); + public Configuration(ConfigurationLocator configurationLocator, ConfigurationLocation configurationLocation) { + this.configurationLocator = configurationLocator; + this.configurationLocation = configurationLocation; + readConfiguration(configurationLocator.getFile(configurationLocation)); } + // + // ACCESSORS + // + /** - * Creates a new configuration that is read from the given file and uses the - * given lock file. - * - * @param filename - * The name of the configuration file - * @param lockFilename - * The name of the lock file + * Returns the configuration locator. + * + * @return The configuration locator */ - public Configuration(String filename, String lockFilename) { - this.filename = filename; - this.lockFilename = lockFilename; - readConfiguration(); + public ConfigurationLocator getConfigurationLocator() { + return configurationLocator; } /** - * Creates the directory of the configuration file. - * - * @return true if the directory exists, or if it could be - * created, false otherwise + * Returns the location the configuration will be written to when calling + * {@link #save()}. + * + * @return The location the configuration will be written to */ - private boolean createConfigDirectory() { - File configDirectory = new File(filename).getAbsoluteFile().getParentFile(); - return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs(); + public ConfigurationLocation getConfigurationDirectory() { + return configurationLocation; } /** - * Creates the lock file. - * - * @return true if the lock file did not already exist and - * could be created, false otherwise + * Sets the location the configuration will be written to when calling + * {@link #save()}. + * + * @param configurationLocation + * The location to write the configuration to */ - public boolean createLockFile() { - if (!createConfigDirectory()) { - return false; - } - File lockFile = new File(lockFilename); - lockFile.deleteOnExit(); - try { - return lockFile.createNewFile(); - } catch (IOException e) { - /* ignore. */ - } - return false; + 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() { - 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); + 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"); @@ -152,12 +145,13 @@ public class 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.getAbsoluteFile().getParentFile(); if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) { @@ -183,7 +177,7 @@ public class Configuration { /** * Returns the value of a node. - * + * * @param nodeNames * The name of all nodes in the chain * @param defaultValue @@ -205,7 +199,7 @@ public class Configuration { /** * Returns the integer value of a node. - * + * * @param nodeNames * The names of all nodes in the chain * @param defaultValue @@ -224,7 +218,7 @@ public class Configuration { /** * Returns the boolean value of a node. - * + * * @param nodeNames * The names of all nodes in the chain * @param defaultValue @@ -242,7 +236,7 @@ public class Configuration { /** * Returns the hostname of the node. - * + * * @return The hostname of the node * @deprecated Use {@link #getSelectedNode()} instead */ @@ -253,7 +247,7 @@ public class Configuration { /** * Sets the hostname of the node. - * + * * @param nodeAddress * The hostname of the node * @deprecated Use {@link #setSelectedNode(Node)} instead @@ -265,7 +259,7 @@ public class Configuration { /** * The port number of the node - * + * * @return The port number of the node * @deprecated Use {@link #getSelectedNode()} instead. */ @@ -276,7 +270,7 @@ public class Configuration { /** * Sets the port number of the node. - * + * * @param nodePort * The port number of the node * @deprecated Use {@link #setSelectedNode(Node)} instead @@ -288,9 +282,9 @@ public class Configuration { /** * 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 + * + * @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); @@ -298,7 +292,7 @@ public class Configuration { /** * 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 @@ -309,7 +303,7 @@ public class Configuration { /** * Returns all configured projects. - * + * * @return A list of all projects */ public Project[] getProjects() { @@ -321,18 +315,27 @@ public class Configuration { try { 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.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())); - project.setInsertURI(projectNode.getNode("insert-uri").getValue()); - project.setRequestURI(projectNode.getNode("request-uri").getValue()); + 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) { @@ -341,8 +344,14 @@ public class Configuration { 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())); @@ -362,7 +371,7 @@ public class Configuration { /** * Sets the list of all projects. - * + * * @param projects * The list of all projects */ @@ -379,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()) { @@ -388,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())); @@ -401,7 +413,7 @@ public class Configuration { /** * Returns the stored locale. - * + * * @return The stored locale */ public Locale getLocale() { @@ -415,7 +427,7 @@ public class Configuration { /** * Sets the locale to store. - * + * * @param locale * The locale to store */ @@ -431,7 +443,7 @@ public class Configuration { /** * Returns a list of configured nodes. - * + * * @return The list of the configured nodes */ public Node[] getNodes() { @@ -460,7 +472,7 @@ public class Configuration { /** * Sets the list of configured nodes. - * + * * @param nodes * The list of configured nodes */ @@ -479,7 +491,7 @@ public class Configuration { /** * Sets the selected node. - * + * * @param selectedNode * The selected node */ @@ -493,7 +505,7 @@ public class Configuration { /** * Returns the selected node. - * + * * @return The selected node */ public Node getSelectedNode() { @@ -513,4 +525,31 @@ public class Configuration { 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"); + } + } + }