Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AF7889840 for ; Fri, 21 Oct 2011 15:23:26 +0000 (UTC) Received: (qmail 25025 invoked by uid 500); 21 Oct 2011 15:23:26 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 24867 invoked by uid 500); 21 Oct 2011 15:23:26 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 24860 invoked by uid 99); 21 Oct 2011 15:23:26 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Oct 2011 15:23:26 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Oct 2011 15:23:22 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 55CB0238897D for ; Fri, 21 Oct 2011 15:23:00 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1187398 - in /commons/proper/configuration/trunk: ./ src/main/java/org/apache/commons/configuration/ src/test/java/org/apache/commons/configuration/ Date: Fri, 21 Oct 2011 15:23:00 -0000 To: commits@commons.apache.org From: ebourg@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111021152300.55CB0238897D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ebourg Date: Fri Oct 21 15:22:59 2011 New Revision: 1187398 URL: http://svn.apache.org/viewvc?rev=1187398&view=rev Log: Removed the dependency on Ant for EnvironmentConfiguration Modified: commons/proper/configuration/trunk/pom.xml commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/EnvironmentConfiguration.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestEnvironmentConfiguration.java Modified: commons/proper/configuration/trunk/pom.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/pom.xml?rev=1187398&r1=1187397&r2=1187398&view=diff ============================================================================== --- commons/proper/configuration/trunk/pom.xml (original) +++ commons/proper/configuration/trunk/pom.xml Fri Oct 21 15:22:59 2011 @@ -303,13 +303,6 @@ provided - - org.apache.ant - ant - 1.8.2 - true - - xerces xercesImpl Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/EnvironmentConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/EnvironmentConfiguration.java?rev=1187398&r1=1187397&r2=1187398&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/EnvironmentConfiguration.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/EnvironmentConfiguration.java Fri Oct 21 15:22:59 2011 @@ -14,68 +14,40 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.commons.configuration; - -import java.lang.reflect.Method; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import org.apache.commons.lang.SystemUtils; -import org.apache.tools.ant.taskdefs.Execute; +package org.apache.commons.configuration; /** - *

- * A Configuration implementation that reads the platform specific environment - * variables. On pre java5 JRE it uses Ant Execute task to read the environment. - * (in this case ant must be present in classpath). On java >= 5 JRE it uses - * java.lang.System#getenv() and ant is not required. - *

- *

- * This configuration implementation is read-only. It allows read access to the - * defined OS environment variables, but their values cannot be changed. - *

- *

- * Usage of this class is easy: After an instance has been created the get + *

A Configuration implementation that reads the platform specific + * environment variables using the map returned by {@link System#getenv()}.

+ * + *

This configuration implementation is read-only. It allows read access to the + * defined OS environment variables, but their values cannot be changed. Any + * attempts to add or remove a property will throw an + * {@link UnsupportedOperationException}

+ * + *

Usage of this class is easy: After an instance has been created the get * methods provided by the Configuration interface can be used - * for querying environment variables, e.g.: + * for querying environment variables, e.g.:

* *
  * Configuration envConfig = new EnvironmentConfiguration();
  * System.out.println("JAVA_HOME=" + envConfig.getString("JAVA_HOME");
  * 
* - *

- * * @author Nicolas De Loof - * @see org.apache.tools.ant.taskdefs.Execute#getProcEnvironment() * @since 1.5 */ -public class EnvironmentConfiguration extends AbstractConfiguration +public class EnvironmentConfiguration extends MapConfiguration { - /** Constant for the name of the getenv() method. */ - private static final String METHOD_NAME = "getenv"; - - /** Constant for the Java version 1.5. */ - private static final int VERSION_1_5 = 150; - - /** Stores the environment properties. */ - private Map environment; - /** - * Constructor. + * Create a Configuration based on the environment variables. + * + * @see System#getenv() */ public EnvironmentConfiguration() { - if (SystemUtils.isJavaVersionAtLeast(VERSION_1_5)) - { - extractProperties15(); - } - else - { - extractProperties14(); - } + super(System.getenv()); } /** @@ -87,47 +59,7 @@ public class EnvironmentConfiguration ex */ protected void addPropertyDirect(String key, Object value) { - throw new UnsupportedOperationException("Configuration is read-only!"); - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.configuration.AbstractConfiguration#containsKey(java.lang.String) - */ - public boolean containsKey(String key) - { - return environment.containsKey(key); - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.configuration.AbstractConfiguration#getKeys() - */ - public Iterator getKeys() - { - return environment.keySet().iterator(); - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.configuration.AbstractConfiguration#getProperty(java.lang.String) - */ - public Object getProperty(String key) - { - return environment.get(key); - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.configuration.AbstractConfiguration#isEmpty() - */ - public boolean isEmpty() - { - return environment.isEmpty(); + throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); } /** @@ -138,7 +70,7 @@ public class EnvironmentConfiguration ex */ public void clearProperty(String key) { - throw new UnsupportedOperationException("Configuration is read-only!"); + throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); } /** @@ -148,62 +80,6 @@ public class EnvironmentConfiguration ex */ public void clear() { - throw new UnsupportedOperationException("Configuration is read-only!"); - } - - /** - * Extracts environment properties on a JRE < 1.5. This implementation - * uses ant for this purpose. - */ - void extractProperties14() - { - extractPropertiesFromCollection(Execute.getProcEnvironment()); - } - - /** - * An internally used method for processing a collection with environment - * entries. The collection must contain strings like - * property=value. Such a collection is returned by ant. - * - * @param env the collection with the properties - */ - void extractPropertiesFromCollection(Collection env) - { - environment = new HashMap(); - for (Iterator it = env.iterator(); it.hasNext();) - { - String entry = (String) it.next(); - int pos = entry.indexOf('='); - if (pos == -1) - { - getLogger().warn("Ignoring: " + entry); - } - else - { - environment.put(entry.substring(0, pos), entry - .substring(pos + 1)); - } - } - } - - /** - * Extracts environment properties on a JR >= 1.5. From this Java version - * on, there is an official way of doing this. However because the code - * should compile on lower Java versions, too, we have to invoke the method - * using reflection. - */ - void extractProperties15() - { - try - { - Method method = System.class.getMethod(METHOD_NAME, null); - environment = (Map) method.invoke(null, null); - } - catch (Exception ex) - { - // this should normally not happen on a JRE >= 1.5 - throw new ConfigurationRuntimeException( - "Error when accessing environment properties", ex); - } + throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); } } Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java?rev=1187398&r1=1187397&r2=1187398&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java Fri Oct 21 15:22:59 2011 @@ -267,7 +267,7 @@ public class TestAbstractConfigurationBa { String key = (String) it.next(); String propKey = "envtest." + key; - env.put(propKey, envConfig.getProperty(key)); + env.put(propKey, envConfig.getString(key)); config.addProperty(propKey, "${env:" + key + "}"); } assertFalse("No environment properties", env.isEmpty()); Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestEnvironmentConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestEnvironmentConfiguration.java?rev=1187398&r1=1187397&r2=1187398&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestEnvironmentConfiguration.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestEnvironmentConfiguration.java Fri Oct 21 15:22:59 2011 @@ -14,13 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.configuration; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; -import java.util.Map; import junit.framework.TestCase; @@ -42,11 +39,10 @@ public class TestEnvironmentConfiguratio } /** - * Helper method for checking that the configuration contains some - * environment properties. (We expect that at least some properties are set - * in each environment.) + * Tests whether a newly created configuration contains some properties. (We + * expect that at least some properties are set in each environment.) */ - private void checkProperties() + public void testInit() { boolean found = false; assertFalse("No properties found", config.isEmpty()); @@ -61,58 +57,6 @@ public class TestEnvironmentConfiguratio } /** - * Tests whether a newly created configuration contains some properties. (We - * expect that at least some properties are set in each environment.) - */ - public void testInit() - { - checkProperties(); - } - - /** - * Tests extracting properties for JDK before 1.5. This method should work - * on later JDKs, too, so we can test it always. - */ - public void testExtractProperties14() - { - config.extractProperties14(); - checkProperties(); - } - - /** - * Tests whether a collection with properties is correctly processed. - */ - public void testExtractPropertiesFromCollection() - { - final int count = 8; - final String prop = "property"; - final String value = "value"; - - Collection env = new ArrayList(count); - for (int i = 0; i < count; i++) - { - env.add(prop + i + "=" + value + i); - } - env.add("irregularProperty"); - config.extractPropertiesFromCollection(env); - - Map props = new HashMap(); - for (Iterator it = config.getKeys(); it.hasNext();) - { - String key = (String) it.next(); - props.put(key, config.getString(key)); - } - assertEquals("Wrong number of properties", count, props.size()); - for (int i = 0; i < count; i++) - { - assertEquals("Wrong value for property " + i, value + i, props - .get(prop + i)); - } - assertFalse("Irregular property found", config - .containsKey("irregularProperty")); - } - - /** * Tests removing properties. This should not be possible. */ public void testClearProperty()