You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2018/06/05 11:58:28 UTC

[2/2] deltaspike git commit: DELTASPIKE-1335 provide helper methods to diff configs

DELTASPIKE-1335 provide helper methods to diff configs


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/bb5ee1e6
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/bb5ee1e6
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/bb5ee1e6

Branch: refs/heads/master
Commit: bb5ee1e699dbaab8e18af3659a71d72166bd67cc
Parents: b7a3779
Author: Mark Struberg <st...@apache.org>
Authored: Tue Jun 5 13:54:21 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Tue Jun 5 13:54:21 2018 +0200

----------------------------------------------------------------------
 .../core/api/config/ConfigResolver.java         | 20 ++++-
 .../core/impl/config/ConfigHelperImpl.java      | 78 ++++++++++++++++++++
 .../core/impl/config/ConfigProviderImpl.java    |  6 ++
 .../test/core/api/config/ConfigHelperTest.java  | 48 ++++++++++++
 4 files changed, 151 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/bb5ee1e6/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
index 86a1674..50c4ce3 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
@@ -26,6 +26,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.ServiceLoader;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
 import javax.enterprise.inject.Typed;
@@ -613,7 +614,7 @@ public final class ConfigResolver
     }
 
 
-    private static ConfigProvider getConfigProvider()
+    public static ConfigProvider getConfigProvider()
     {
         if (configProvider == null)
         {
@@ -670,6 +671,23 @@ public final class ConfigResolver
          *   It will automatically get invoked in BeforeShutdown via our ConfigExtension internally.
          */
         void releaseConfig(ClassLoader cl);
+
+        /**
+         * Provide access to the ConfigHelper
+         */
+        ConfigHelper getHelper();
+    }
+
+    /**
+     * Some utility functions which are useful for implementing own ConfigSources, etc.
+     */
+    public interface ConfigHelper
+    {
+        /**
+         * @return A Set of all the attributes which differ between the old and new config Map
+         *         or an empty Set if there is no difference.
+         */
+        Set<String> diffConfig(Map<String, String> oldValues, Map<String, String> newValues);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/bb5ee1e6/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigHelperImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigHelperImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigHelperImpl.java
new file mode 100644
index 0000000..1ee4b49
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigHelperImpl.java
@@ -0,0 +1,78 @@
+/*
+ * 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.deltaspike.core.impl.config;
+
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+
+public class ConfigHelperImpl implements ConfigResolver.ConfigHelper
+{
+
+    @Override
+    public Set<String> diffConfig(Map<String, String> oldValues, Map<String, String> newValues)
+    {
+        if (oldValues == null)
+        {
+            oldValues = Collections.emptyMap();
+        }
+        if (newValues == null)
+        {
+            newValues = Collections.emptyMap();
+        }
+        Set<String> changedAttribs = new HashSet<>();
+        Set<String> oldKeys = new HashSet<>(oldValues.keySet());
+        for (Map.Entry<String, String> newPropEntry : newValues.entrySet())
+        {
+            String key = newPropEntry.getKey();
+            if (oldValues.containsKey(key))
+            {
+                if (compare(oldValues.get(key), newPropEntry.getValue()) != 0)
+                {
+                    changedAttribs.add(key);
+                }
+                oldKeys.remove(key);
+            }
+            else
+            {
+                changedAttribs.add(key);
+            }
+        }
+        changedAttribs.addAll(oldKeys);
+
+        return changedAttribs;
+    }
+
+    private int compare(String a, String b)
+    {
+        if (a == null && b == null)
+        {
+            return 0;
+        }
+        if (a != null)
+        {
+            return a.compareTo(b);
+        }
+        return 1;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/bb5ee1e6/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java
index ea1b32f..a741363 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java
@@ -83,6 +83,12 @@ public class ConfigProviderImpl implements ConfigResolver.ConfigProvider
         }
     }
 
+    @Override
+    public ConfigResolver.ConfigHelper getHelper()
+    {
+        return new ConfigHelperImpl();
+    }
+
     private boolean isChildClassLoader(ClassLoader configClassLoader, ClassLoader suspect)
     {
         ClassLoader suspectParentCl = suspect.getParent();

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/bb5ee1e6/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigHelperTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigHelperTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigHelperTest.java
new file mode 100644
index 0000000..fa4687e
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigHelperTest.java
@@ -0,0 +1,48 @@
+package org.apache.deltaspike.test.core.api.config;
+
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Mark Struberg
+ */
+public class ConfigHelperTest {
+
+    @Test
+    public void testDiffConfig() {
+        ConfigResolver.ConfigHelper cfgHelper = ConfigResolver.getConfigProvider().getHelper();
+
+        Map<String, String> oldVal = new HashMap<>();
+        Map<String, String> newVal = new HashMap<>();
+
+        oldVal.put("a", "1");
+
+        newVal.put("b", "2");
+        newVal.put("a", "1");
+
+        assertAll(cfgHelper.diffConfig(null, newVal), "a", "b");
+        assertAll(cfgHelper.diffConfig(oldVal, null), "a");
+        assertAll(cfgHelper.diffConfig(oldVal, newVal), "b");
+        assertAll(cfgHelper.diffConfig(oldVal, oldVal));
+        assertAll(cfgHelper.diffConfig(newVal, newVal));
+
+        newVal.put("a", "5");
+        assertAll(cfgHelper.diffConfig(oldVal, newVal), "a", "b");
+
+    }
+
+    private void assertAll(Set<String> actualVals, String... expectedVals) {
+        Assert.assertNotNull(actualVals);
+        Assert.assertEquals(expectedVals.length, actualVals.size());
+
+        for (String expectedVal : expectedVals) {
+            Assert.assertTrue(actualVals.contains(expectedVal));
+        }
+    }
+
+}