You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2005/05/04 19:42:34 UTC
svn commit: r168157 - in /jakarta/commons/proper/configuration/trunk/src:
java/org/apache/commons/configuration/AbstractFileConfiguration.java
test/org/apache/commons/configuration/TestFileConfiguration.java
Author: oheger
Date: Wed May 4 10:42:34 2005
New Revision: 168157
URL: http://svn.apache.org/viewcvs?rev=168157&view=rev
Log:
Patch for issue 34362, contributed by Jamie Guillemette; the URL of the configuration file is stored in the load() method and reused by save()
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?rev=168157&r1=168156&r2=168157&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java Wed May 4 10:42:34 2005
@@ -86,6 +86,8 @@
private Object reloadLock = new Object();
private String encoding;
+
+ private URL sourceURL = null;
/**
* Default constructor
@@ -163,7 +165,14 @@
*/
public void load() throws ConfigurationException
{
- load(getFileName());
+ if (sourceURL != null)
+ {
+ load(sourceURL);
+ }
+ else
+ {
+ load(getFileName());
+ }
}
/**
@@ -178,6 +187,7 @@
try
{
URL url = ConfigurationUtils.locate(basePath, fileName);
+
if (url == null)
{
throw new ConfigurationException("Cannot locate configuration source " + fileName);
@@ -226,6 +236,10 @@
*/
public void load(URL url) throws ConfigurationException
{
+ if (sourceURL == null)
+ {
+ sourceURL = url;
+ }
InputStream in = null;
try
@@ -312,7 +326,14 @@
*/
public void save() throws ConfigurationException
{
- save(fileName);
+ if (sourceURL != null)
+ {
+ save(sourceURL);
+ }
+ else
+ {
+ save(fileName);
+ }
strategy.init();
}
@@ -471,6 +492,7 @@
*/
public void setFileName(String fileName)
{
+ sourceURL = null;
this.fileName = fileName;
}
@@ -483,19 +505,20 @@
}
/**
- * Set the base path. Relative configurations are loaded from this path.
- * The base path can be either a path to a directory or a URL.
- *
+ * Set the base path. Relative configurations are loaded from this path. The
+ * base path can be either a path to a directory or a URL.
+ *
* @param basePath the base path.
*/
public void setBasePath(String basePath)
{
+ sourceURL = null;
this.basePath = basePath;
}
/**
- * Return the file where the configuration is stored. If the base path is
- * a URL with a protocol different than "file", the return value
+ * Return the file where the configuration is stored. If the base path is a
+ * URL with a protocol different than "file", the return value
* will not point to a valid file object.
*
* @return the file where the configuration is stored
@@ -509,13 +532,15 @@
* Set the file where the configuration is stored. The passed in file is
* made absolute if it is not yet. Then the file's path component becomes
* the base path and its name component becomes the file name.
- *
+ *
* @param file the file where the configuration is stored
*/
public void setFile(File file)
{
+ sourceURL = null;
setFileName(file.getName());
- setBasePath((file.getParentFile() != null) ? file.getParentFile().getAbsolutePath() : null);
+ setBasePath((file.getParentFile() != null) ? file.getParentFile()
+ .getAbsolutePath() : null);
}
/**
@@ -548,7 +573,8 @@
*/
public URL getURL()
{
- return ConfigurationUtils.locate(getBasePath(), getFileName());
+ return (sourceURL != null) ? sourceURL :
+ ConfigurationUtils.locate(getBasePath(), getFileName());
}
/**
Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?rev=168157&r1=168156&r2=168157&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java Wed May 4 10:42:34 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
@@ -17,7 +17,11 @@
package org.apache.commons.configuration;
import java.net.URL;
+import java.util.Properties;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
import junit.framework.TestCase;
@@ -191,6 +195,69 @@
catch (ConfigurationException cex)
{
//fine
+ }
+ }
+
+ /**
+ * Tests if the URL used by the load() method is also used by save().
+ */
+ public void testFileOverwrite() throws Exception
+ {
+ FileOutputStream out = null;
+ FileInputStream in = null;
+ File tempFile = null;
+ try
+ {
+ String path = System.getProperties().getProperty("user.home");
+ File homeDir = new File(path);
+ tempFile = File.createTempFile("CONF", null, homeDir);
+ String fileName = tempFile.getName();
+ Properties props = new Properties();
+ props.setProperty("1", "one");
+ out = new FileOutputStream(tempFile);
+ props.store(out, "TestFileOverwrite");
+ out.close();
+ out = null;
+ FileConfiguration config = new PropertiesConfiguration(fileName);
+ config.load();
+ String value = config.getString("1");
+ assertTrue("one".equals(value));
+ config.setProperty("1", "two");
+ config.save();
+ props = new Properties();
+ in = new FileInputStream(tempFile);
+ props.load(in);
+ String value2 = props.getProperty("1");
+ assertTrue("two".equals(value2));
+ }
+ finally
+ {
+ if (out != null)
+ {
+ try
+ {
+ out.close();
+ }
+ catch (IOException ioex)
+ {
+ ioex.printStackTrace();
+ }
+ }
+ if (in != null)
+ {
+ try
+ {
+ in.close();
+ }
+ catch (IOException ioex)
+ {
+ ioex.printStackTrace();
+ }
+ }
+ if (tempFile.exists())
+ {
+ assertTrue(tempFile.delete());
+ }
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org