You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/05/21 21:45:22 UTC

[commons-configuration] branch master updated: [CONFIGURATION-745] Add the special key "includeoptional" for properties files. (#27)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git


The following commit(s) were added to refs/heads/master by this push:
     new 76abecc  [CONFIGURATION-745] Add the special key "includeoptional" for properties files. (#27)
76abecc is described below

commit 76abecc4ac6a6e89d58d5809247df8f3d4e03bcf
Author: Gary Gregory <ga...@users.noreply.github.com>
AuthorDate: Tue May 21 17:45:18 2019 -0400

    [CONFIGURATION-745] Add the special key "includeoptional" for properties files. (#27)
    
    * [CONFIGURATION-745] Add the special key "includeoptional" for properties
    files.
---
 src/changes/changes.xml                            |  3 +
 .../configuration2/PropertiesConfiguration.java    | 66 +++++++++++++++++++++-
 .../TestPropertiesConfiguration.java               | 12 ++++
 .../resources/includeoptional-target.properties    | 15 +++++
 src/test/resources/includeoptional.properties      | 16 ++++++
 5 files changed, 109 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9de4708..e5c1768 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,9 @@
       <action dev="ggregory" type="update" issue="CONFIGURATION-747" due-to="Gary Gregory">
         Update Spring from 4.3.22 to 4.3.24.
       </action>
+      <action dev="ggregory" type="update" issue="CONFIGURATION-745" due-to="Gary Gregory">
+        Add the special key "includeoptional" for properties files.
+      </action>
     </release>
     <release version="2.4" date="2018-10-23"
              description="Minor release with new features and updated dependencies.">
diff --git a/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java b/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
index f1ac50c..751e99f 100644
--- a/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
@@ -243,6 +243,15 @@ public class PropertiesConfiguration extends BaseConfiguration
      */
     private static String include = "include";
 
+    /**
+     * This is the name of the property that can point to other
+     * properties file for including other properties files.
+     * <p>
+     * If the file is absent, processing continues normally.
+     * </p>
+     */
+    private static String includeOptional = "includeoptional";
+
     /** The list of possible key/value separators */
     private static final char[] SEPARATORS = new char[] {'=', ':'};
 
@@ -292,6 +301,21 @@ public class PropertiesConfiguration extends BaseConfiguration
     }
 
     /**
+     * Gets the property value for including other properties files.
+     * By default it is "include".
+     * <p>
+     * If the file is absent, processing continues normally.
+     * </p>
+     *
+     * @return A String.
+     * @since 2.5
+     */
+    public static String getIncludeOptional()
+    {
+        return PropertiesConfiguration.includeOptional;
+    }
+
+    /**
      * Sets the property value for including other properties files.
      * By default it is "include".
      *
@@ -303,6 +327,21 @@ public class PropertiesConfiguration extends BaseConfiguration
     }
 
     /**
+     * Sets the property value for including other properties files.
+     * By default it is "include".
+     * <p>
+     * If the file is absent, processing continues normally.
+     * </p>
+     *
+     * @param inc A String.
+     * @since 2.5
+     */
+    public static void setIncludeOptional(final String inc)
+    {
+        PropertiesConfiguration.includeOptional = inc;
+    }
+
+    /**
      * Controls whether additional files can be loaded by the {@code include = <xxx>}
      * statement or not. This is <b>true</b> per default.
      *
@@ -586,7 +625,22 @@ public class PropertiesConfiguration extends BaseConfiguration
                         getListDelimiterHandler().split(value, true);
                 for (final String f : files)
                 {
-                    loadIncludeFile(interpolate(f));
+                    loadIncludeFile(interpolate(f), false);
+                }
+            }
+            result = false;
+        }
+
+        else if (StringUtils.isNotEmpty(getIncludeOptional())
+            && key.equalsIgnoreCase(getIncludeOptional()))
+        {
+            if (isIncludesAllowed())
+            {
+                final Collection<String> files =
+                        getListDelimiterHandler().split(value, true);
+                for (final String f : files)
+                {
+                    loadIncludeFile(interpolate(f), true);
                 }
             }
             result = false;
@@ -1538,7 +1592,7 @@ public class PropertiesConfiguration extends BaseConfiguration
             initialMap.put("\f", "\\f");
             initialMap.put("\r", "\\r");
             JUP_CHARS_ESCAPE = Collections.unmodifiableMap(initialMap);
-        };
+        }
 
         /**
          * Creates a new instance of {@code JupPropertiesWriter}.
@@ -1740,9 +1794,10 @@ public class PropertiesConfiguration extends BaseConfiguration
      * this properties file is tried.
      *
      * @param fileName the name of the file to load
+     * @param optional whether or not the {@code fileName} is optional
      * @throws ConfigurationException if loading fails
      */
-    private void loadIncludeFile(final String fileName) throws ConfigurationException
+    private void loadIncludeFile(final String fileName, boolean optional) throws ConfigurationException
     {
         if (locator == null)
         {
@@ -1761,6 +1816,11 @@ public class PropertiesConfiguration extends BaseConfiguration
             }
         }
 
+        if (optional && url == null)
+        {
+            return;
+        }
+
         if (url == null)
         {
             throw new ConfigurationException("Cannot resolve include file "
diff --git a/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
index 1d6b4f2..89295ce 100644
--- a/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
@@ -1073,6 +1073,18 @@ public class TestPropertiesConfiguration
     }
 
     @Test
+    public void testLoadIncludeOptional() throws Exception
+    {
+        final PropertiesConfiguration pc = new PropertiesConfiguration();
+        final FileHandler handler = new FileHandler(pc);
+        handler.setBasePath(testBasePath);
+        handler.setFileName("includeoptional.properties");
+        handler.load();
+
+        assertTrue("Make sure we have multiple keys", pc.getBoolean("includeoptional.loaded"));
+    }
+
+    @Test
     public void testLoadViaPropertyWithBasePath2() throws Exception
     {
         final PropertiesConfiguration pc = new PropertiesConfiguration();
diff --git a/src/test/resources/includeoptional-target.properties b/src/test/resources/includeoptional-target.properties
new file mode 100644
index 0000000..ccd93ca
--- /dev/null
+++ b/src/test/resources/includeoptional-target.properties
@@ -0,0 +1,15 @@
+#   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.
+includeoptional.loaded = true
diff --git a/src/test/resources/includeoptional.properties b/src/test/resources/includeoptional.properties
new file mode 100644
index 0000000..12f8720
--- /dev/null
+++ b/src/test/resources/includeoptional.properties
@@ -0,0 +1,16 @@
+#   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.
+includeoptional = this-file-does-not-and-should-not-exist.txt
+includeoptional = includeoptional-target.properties