Index: pom.xml =================================================================== --- pom.xml (revision 507433) +++ pom.xml (working copy) @@ -227,6 +227,13 @@ commons-jxpath 1.2 + + + ant + ant + 1.6.5 + true + xerces Index: src/java/org/apache/commons/configuration/EnvironmentConfiguration.java =================================================================== --- src/java/org/apache/commons/configuration/EnvironmentConfiguration.java (revision 0) +++ src/java/org/apache/commons/configuration/EnvironmentConfiguration.java (revision 0) @@ -0,0 +1,129 @@ +/* + * 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.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +import org.apache.commons.configuration.AbstractConfiguration; +import org.apache.tools.ant.taskdefs.Execute; + +/** + * 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. + * + * @author Nicolas De Loof + * @see org.apache.tools.ant.taskdefs.Execute#getProcEnvironment() + */ +public class EnvironmentConfiguration + extends AbstractConfiguration +{ + private Map environment; + + public static void main( String[] args ) + { + System.out.print( new EnvironmentConfiguration().getProperty( "MAVEN_HOME" ) ); + } + + /** + * Constructor. + */ + public EnvironmentConfiguration() + { + super(); + String javaVersion = System.getProperty( "java.version", "." ); + if ( javaVersion.startsWith( "1.3" ) || javaVersion.startsWith( "1.4" ) ) + { + this.environment = new HashMap(); + Vector osEnv = Execute.getProcEnvironment(); + for ( Enumeration e = osEnv.elements(); e.hasMoreElements(); ) + { + String entry = (String) e.nextElement(); + int pos = entry.indexOf( '=' ); + if ( pos == -1 ) + { + getLogger().warn( "Ignoring: " + entry ); + } + else + { + environment.put( entry.substring( 0, pos ), entry.substring( pos + 1 ) ); + } + } + } + else + { + this.environment = System.getenv(); + } + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.configuration.AbstractConfiguration#addPropertyDirect(java.lang.String, + * java.lang.Object) + */ + protected void addPropertyDirect( String key, Object value ) + { + throw new UnsupportedOperationException(); + } + + /** + * {@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(); + } + +}