commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r833927 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/interpol/ src/test/java/org/apache/commons/configuration2/ src/test/java/org/apache/commons/configuration2/inte...
Date Sun, 08 Nov 2009 21:12:54 GMT
Author: oheger
Date: Sun Nov  8 21:12:52 2009
New Revision: 833927

URL: http://svn.apache.org/viewvc?rev=833927&view=rev
Log:
[CONFIGURATION-399] Ported changes to configuration2 branch.

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java
  (with props)
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java
  (with props)
Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
    commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
    commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_basicfeatures.xml

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java?rev=833927&r1=833926&r2=833927&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
Sun Nov  8 21:12:52 2009
@@ -116,6 +116,12 @@
      */
     public static final String PREFIX_CONSTANTS = "const";
 
+    /**
+     * Constant for the prefix of the standard lookup object for resolving
+     * environment properties.
+     */
+    public static final String PREFIX_ENVIRONMENT = "env";
+
     /** Constant for the prefix separator. */
     private static final char PREFIX_SEPARATOR = ':';
 
@@ -127,7 +133,7 @@
 
     /** Stores the default lookup object. */
     private StrLookup defaultLookup;
-    
+
     /** Stores a parent interpolator objects if the interpolator is nested hierarchically.
*/
     private ConfigurationInterpolator parentInterpolator;
 
@@ -271,6 +277,7 @@
      * @return the value of this variable or <b>null</b> if it cannot be
      * resolved
      */
+    @Override
     public String lookup(String var)
     {
         if (var == null)
@@ -290,7 +297,7 @@
             if (value != null)
             {
                 return value;
-            } 
+            }
         }
         String value = fetchNoPrefixLookup().lookup(var);
         if (value == null && getParentInterpolator() != null) {
@@ -330,10 +337,10 @@
         }
         return lookup;
     }
-    
+
     /**
      * Registers the local lookup instances for the given interpolator.
-     * 
+     *
      * @param interpolator the instance receiving the local lookups
      * @since upcoming
      */
@@ -343,8 +350,8 @@
 
     /**
      * Sets the parent interpolator. This object is used if the interpolation is nested hierarchically
-     * and the current interpolation object cannot resolve a variable. 
-     * 
+     * and the current interpolation object cannot resolve a variable.
+     *
      * @param parentInterpolator the parent interpolator object or <code>null</code>
      * @since upcoming
      */
@@ -355,7 +362,7 @@
     /**
      * Requests the parent interpolator. This object is used if the interpolation is nested
hierarchically
      * and the current interpolation
-     * 
+     *
      * @return the parent interpolator or <code>null</code>
      * @since upcoming
      */
@@ -369,5 +376,6 @@
         globalLookups = new HashMap<String, StrLookup>();
         globalLookups.put(PREFIX_SYSPROPERTIES, StrLookup.systemPropertiesLookup());
         globalLookups.put(PREFIX_CONSTANTS, new ConstantLookup());
+        globalLookups.put(PREFIX_ENVIRONMENT, new EnvironmentLookup());
     }
 }

Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java?rev=833927&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java
(added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java
Sun Nov  8 21:12:52 2009
@@ -0,0 +1,64 @@
+/*
+ * 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.configuration2.interpol;
+
+import org.apache.commons.configuration2.EnvironmentConfiguration;
+import org.apache.commons.lang.text.StrLookup;
+
+/**
+ * <p>
+ * A specialized lookup implementation that allows access to environment
+ * variables.
+ * </p>
+ * <p>
+ * This implementation relies on {@link EnvironmentConfiguration} to resolve
+ * environment variables. It can be used for referencing environment variables
+ * in configuration files in an easy way, for instance:
+ *
+ * <pre>
+ * java.home = ${env:JAVA_HOME}
+ * </pre>
+ *
+ * </p>
+ * <p>
+ * {@code EnvironmentLookup} is one of the standard lookups that is
+ * registered per default for each configuration.
+ * </p>
+ *
+ * @author <a
+ *         href="http://commons.apache.org/configuration/team-list.html">Commons
+ *         Configuration team</a>
+ * @version $Id$
+ */
+public class EnvironmentLookup extends StrLookup
+{
+    /** Stores the underlying {@code EnvironmentConfiguration}. */
+    private final EnvironmentConfiguration environmentConfig = new EnvironmentConfiguration();
+
+    /**
+     * Performs a lookup for the specified variable. This implementation
+     * directly delegates to a {@code EnvironmentConfiguration}.
+     *
+     * @param key the key to lookup
+     * @return the value of this key or <b>null</b> if it cannot be resolved
+     */
+    @Override
+    public String lookup(String key)
+    {
+        return environmentConfig.getString(key);
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/EnvironmentLookup.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java?rev=833927&r1=833926&r2=833927&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
Sun Nov  8 21:12:52 2009
@@ -27,6 +27,7 @@
 import java.util.Set;
 
 import junit.framework.TestCase;
+
 import org.apache.commons.configuration2.event.ConfigurationEvent;
 import org.apache.commons.configuration2.event.ConfigurationListener;
 import org.apache.commons.configuration2.flat.BaseConfiguration;
@@ -55,6 +56,7 @@
                 new BaseConfiguration())
         {
             // return an iterator that does not support remove operations
+            @Override
             public Iterator<String> getKeys()
             {
                 Collection<String> keyCol = new ArrayList<String>();
@@ -306,6 +308,30 @@
     }
 
     /**
+     * Tests whether environment variables can be interpolated.
+     */
+    public void testInterpolateEnvironmentVariables()
+    {
+        AbstractConfiguration config = new TestConfigurationImpl(
+                new PropertiesConfiguration());
+        EnvironmentConfiguration envConfig = new EnvironmentConfiguration();
+        Map<String, String> env = new HashMap<String, String>();
+        for (Iterator<String> it = envConfig.getKeys(); it.hasNext();)
+        {
+            String key =  it.next();
+            String propKey = "envtest." + key;
+            env.put(propKey, envConfig.getString(key));
+            config.addProperty(propKey, "${env:" + key + "}");
+        }
+        assertFalse("No environment properties", env.isEmpty());
+        for (Map.Entry<String, String> e : env.entrySet())
+        {
+            assertEquals("Wrong value for " + e.getKey(), e.getValue(), config
+                    .getString(e.getKey()));
+        }
+    }
+
+    /**
      * Creates the source configuration for testing the copy() and append()
      * methods. This configuration contains keys with an odd index and values
      * starting with the prefix "src". There are also some list properties.
@@ -416,6 +442,7 @@
             config = wrappedConfig;
         }
 
+        @Override
         protected void addPropertyDirect(String key, Object value)
         {
             config.addPropertyDirect(key, value);
@@ -441,6 +468,7 @@
             return config.isEmpty();
         }
 
+        @Override
         protected void clearPropertyDirect(String key)
         {
             config.clearPropertyDirect(key);

Added: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java?rev=833927&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java
(added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java
Sun Nov  8 21:12:52 2009
@@ -0,0 +1,71 @@
+/*
+ * 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.configuration2.interpol;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Iterator;
+
+import org.apache.commons.configuration2.EnvironmentConfiguration;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for EnvironmentLookup.
+ *
+ * @author <a
+ *         href="http://commons.apache.org/configuration/team-list.html">Commons
+ *         Configuration team</a>
+ * @version $Id$
+ */
+public class TestEnvironmentLookup
+{
+    /** The lookup to be tested. */
+    private EnvironmentLookup lookup;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        lookup = new EnvironmentLookup();
+    }
+
+    /**
+     * Tests whether environment variables can be queried.
+     */
+    @Test
+    public void testLookup()
+    {
+        EnvironmentConfiguration envConf = new EnvironmentConfiguration();
+        for (Iterator<String> it = envConf.getKeys(); it.hasNext();)
+        {
+            String var = (String) it.next();
+            assertEquals("Wrong value for " + var, envConf.getString(var),
+                    lookup.lookup(var));
+        }
+    }
+
+    /**
+     * Tries to lookup a non existing property.
+     */
+    @Test
+    public void testLookupNonExisting()
+    {
+        assertNull("Got result for non existing environment variable", lookup
+                .lookup("a non existing variable!"));
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/interpol/TestEnvironmentLookup.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=833927&r1=833926&r2=833927&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Sun
Nov  8 21:12:52 2009
@@ -79,6 +79,10 @@
     </release>
 
     <release version="1.7" date="in SVN" description="">
+      <action dev="oheger" type="add" issue="CONFIGURATION-399">
+        Default variable interpolation now supports the env: prefix for
+        referencing environment variables.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-393">
         BaseConfiguration.clone() now also clones collections stored in the
         internal map. This causes list properties to be handled correctly.

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_basicfeatures.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_basicfeatures.xml?rev=833927&r1=833926&r2=833927&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_basicfeatures.xml
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_basicfeatures.xml
Sun Nov  8 21:12:52 2009
@@ -190,6 +190,11 @@
         specified class will be loaded and the value of this field will be
         obtained.</td>
       </tr>
+      <tr>
+        <td valign="top">env</td>
+        <td>Variables can also reference OS-specific environment properties.
+        This is indicated by the <code>env</code> prefix.</td>
+      </tr>
       </table>
       Here are some examples (again using properties syntax):
     </p>
@@ -197,6 +202,8 @@
 user.file = ${sys:user.home}/settings.xml
 
 action.key = ${const:java.awt.event.KeyEvent.VK_CANCEL}
+
+java.home = ${env:JAVA_HOME}
 ]]></source>
     <p>
       If a variable cannot be resolved, e.g. because the name is invalid or an



Mime
View raw message