You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2007/10/23 22:03:48 UTC
svn commit: r587636 - in /commons/proper/configuration/trunk: ./
src/java/org/apache/commons/configuration/
src/test/org/apache/commons/configuration/ xdocs/
Author: oheger
Date: Tue Oct 23 13:03:47 2007
New Revision: 587636
URL: http://svn.apache.org/viewvc?rev=587636&view=rev
Log:
CONFIGURATION-284: New EnvironmentConfiguration class; thanks to Nicolas De Loof for the patch.
Added:
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java (with props)
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java (with props)
Modified:
commons/proper/configuration/trunk/build.xml
commons/proper/configuration/trunk/pom.xml
commons/proper/configuration/trunk/project.xml
commons/proper/configuration/trunk/xdocs/changes.xml
commons/proper/configuration/trunk/xdocs/dependencies.xml
Modified: commons/proper/configuration/trunk/build.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/build.xml?rev=587636&r1=587635&r2=587636&view=diff
==============================================================================
--- commons/proper/configuration/trunk/build.xml (original)
+++ commons/proper/configuration/trunk/build.xml Tue Oct 23 13:03:47 2007
@@ -276,6 +276,8 @@
</get>
<get dest="${libdir}/mail-1.4.jar" usetimestamp="true" ignoreerrors="true" src="http://repo1.maven.org/maven2/javax/mail/mail/1.4/mail-1.4.jar">
</get>
+ <get dest="${libdir}/ant-1.6.5.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/ant/jars/ant-1.6.5.jar">
+ </get>
</target>
<target name="install-maven">
<get dest="${user.home}/maven-install-latest.jar" usetimestamp="true" src="${repo}/maven/maven-install-latest.jar">
Modified: commons/proper/configuration/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/pom.xml?rev=587636&r1=587635&r2=587636&view=diff
==============================================================================
--- commons/proper/configuration/trunk/pom.xml (original)
+++ commons/proper/configuration/trunk/pom.xml Tue Oct 23 13:03:47 2007
@@ -260,6 +260,13 @@
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>ant</groupId>
+ <artifactId>ant</artifactId>
+ <version>1.6.5</version>
+ <optional>true</optional>
+ </dependency>
+
<!-- Needed for testing -->
<dependency>
Modified: commons/proper/configuration/trunk/project.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/project.xml?rev=587636&r1=587635&r2=587636&view=diff
==============================================================================
--- commons/proper/configuration/trunk/project.xml (original)
+++ commons/proper/configuration/trunk/project.xml Tue Oct 23 13:03:47 2007
@@ -343,6 +343,15 @@
</properties>
</dependency>
+ <dependency>
+ <groupId>ant</groupId>
+ <artifactId>ant</artifactId>
+ <version>1.6.5</version>
+ <properties>
+ <optional>true</optional>
+ </properties>
+ </dependency>
+
<!-- Needed for testing -->
<dependency>
Added: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java?rev=587636&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java (added)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java Tue Oct 23 13:03:47 2007
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * 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;
+
+/**
+ * <p>
+ * 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
+ * {@link java.lang.System#getenv()} and ant is not required.
+ * </p>
+ * <p>
+ * This configuration implementation is read-only. It allows read access to the
+ * defined OS environment variables, but their values cannot be changed.
+ * </p>
+ * <p>
+ * Usage of this class is easy: After an instance has been created the get
+ * methods provided by the <code>Configuration</code> interface can be used
+ * for querying environment variables, e.g.:
+ *
+ * <pre>
+ * Configuration envConfig = new EnvironmentConfiguration();
+ * System.out.println("JAVA_HOME=" + envConfig.getString("JAVA_HOME");
+ * </pre>
+ *
+ * </p>
+ *
+ * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
+ * @see org.apache.tools.ant.taskdefs.Execute#getProcEnvironment()
+ * @since 1.5
+ */
+public class EnvironmentConfiguration extends AbstractConfiguration
+{
+ /** 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.
+ */
+ public EnvironmentConfiguration()
+ {
+ if (SystemUtils.isJavaVersionAtLeast(VERSION_1_5))
+ {
+ extractProperties15();
+ }
+ else
+ {
+ extractProperties14();
+ }
+ }
+
+ /**
+ * Adds a property to this configuration. Because this configuration is
+ * read-only, this operation is not allowed and will cause an exception.
+ *
+ * @param key the key of the property to be added
+ * @param value the property value
+ */
+ 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();
+ }
+
+ /**
+ * Removes a property from this configuration. Because this configuration is
+ * read-only, this operation is not allowed and will cause an exception.
+ *
+ * @param key the key of the property to be removed
+ */
+ public void clearProperty(String key)
+ {
+ throw new UnsupportedOperationException("Configuration is read-only!");
+ }
+
+ /**
+ * Removes all properties from this configuration. Because this
+ * configuration is read-only, this operation is not allowed and will cause
+ * an exception.
+ */
+ 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
+ * <code>property=value</code>. 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);
+ }
+ }
+}
Propchange: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/EnvironmentConfiguration.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java?rev=587636&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java (added)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java Tue Oct 23 13:03:47 2007
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * 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;
+
+/**
+ * Test class for EnvironmentConfiguration.
+ *
+ * @author Oliver Heger
+ * @version $Id$
+ */
+public class TestEnvironmentConfiguration extends TestCase
+{
+ /** Stores the configuration to be tested. */
+ private EnvironmentConfiguration config;
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ config = new EnvironmentConfiguration();
+ }
+
+ /**
+ * Helper method for checking that the configuration contains some
+ * environment properties. (We expect that at least some properties are set
+ * in each environment.)
+ */
+ private void checkProperties()
+ {
+ boolean found = false;
+ assertFalse("No properties found", config.isEmpty());
+ for (Iterator it = config.getKeys(); it.hasNext();)
+ {
+ String key = (String) it.next();
+ assertTrue("Key not found: " + key, config.containsKey(key));
+ assertNotNull("No value for property " + key, config.getString(key));
+ found = true;
+ }
+ assertTrue("No property keys returned", found);
+ }
+
+ /**
+ * 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()
+ {
+ String key = (String) config.getKeys().next();
+ try
+ {
+ config.clearProperty(key);
+ fail("Could remove a property!");
+ }
+ catch (UnsupportedOperationException uoex)
+ {
+ // ok
+ }
+ }
+
+ /**
+ * Tests removing all properties. This should not be possible.
+ */
+ public void testClear()
+ {
+ try
+ {
+ config.clear();
+ fail("Could remove properties!");
+ }
+ catch (UnsupportedOperationException uoex)
+ {
+ // ok
+ }
+ }
+
+ /**
+ * Tries to add another property. This should cause an exception.
+ */
+ public void testAddProperty()
+ {
+ try
+ {
+ config.addProperty("JAVA_HOME", "C:\\java");
+ fail("Could add a property!");
+ }
+ catch (UnsupportedOperationException uoex)
+ {
+ // ok
+ }
+ }
+
+ /**
+ * Tries to set the value of a property. This should cause an exception.
+ */
+ public void testSetProperty()
+ {
+ try
+ {
+ config.setProperty("JAVA_HOME", "C:\\java");
+ fail("Could set a property!");
+ }
+ catch (UnsupportedOperationException uoex)
+ {
+ // ok
+ }
+ }
+}
Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestEnvironmentConfiguration.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=587636&r1=587635&r2=587636&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Tue Oct 23 13:03:47 2007
@@ -23,6 +23,12 @@
<body>
<release version="1.5-SNAPSHOT" date="in SVN" description="">
+ <action dev="oheger" type="add" issue="CONFIGURATION-284" due-to="Nicolas De Loof">
+ There is a new configuration implementation EnvironmentConfiguration,
+ which provides access to (OS) environment variables. On Java >= 1.5
+ this class can be directly used; on earlier versions a dependency to ant
+ is required.
+ </action>
<action dev="oheger" type="fix" issue="CONFIGURATION-296">
A bug in XMLConfiguration caused that attributes of the root element
could not be changed. This has been fixed.
Modified: commons/proper/configuration/trunk/xdocs/dependencies.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/dependencies.xml?rev=587636&r1=587635&r2=587636&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/dependencies.xml (original)
+++ commons/proper/configuration/trunk/xdocs/dependencies.xml Tue Oct 23 13:03:47 2007
@@ -99,6 +99,10 @@
<td>XPathExpressionEngine</td>
<td>commons-jxpath</td>
</tr>
+ <tr>
+ <td>EnvironmentConfiguration</td>
+ <td>Java 1.5 or ant 1.6.5</td>
+ </tr>
</tbody>
</table>