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/04/20 12:13:41 UTC

deltaspike git commit: DELTASPIKE-1335 move access to configured value to TppedResolver

Repository: deltaspike
Updated Branches:
  refs/heads/master 463a8c9b8 -> aabbc879e


DELTASPIKE-1335 move access to configured value to TppedResolver

Feedback from Jeff Mesnil and Tomas Langer.
It will be easier to understand if the access to the effective value
is done via TypedResolver and ConfigSnapshot is merely a value holder.


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

Branch: refs/heads/master
Commit: aabbc879e389bfa3538cbdfa52a19fbfae904616
Parents: 463a8c9
Author: Mark Struberg <st...@apache.org>
Authored: Fri Apr 20 14:08:04 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Fri Apr 20 14:10:40 2018 +0200

----------------------------------------------------------------------
 .../core/api/config/ConfigResolver.java         |  8 ++
 .../core/api/config/ConfigSnapshot.java         |  6 +-
 .../core/impl/config/ConfigSnapshotImpl.java    | 11 +--
 .../core/impl/config/TypedResolverImpl.java     | 15 ++++
 .../core/api/config/ConfigSnapshotTest.java     | 95 ++++++++++++++++++++
 .../core/api/config/ConfigTransactionTest.java  | 95 --------------------
 6 files changed, 121 insertions(+), 109 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/aabbc879/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 1d92163..fe05771 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
@@ -525,6 +525,14 @@ public final class ConfigResolver
         T getValue();
 
         /**
+         * Returns the value from a previously taken {@link ConfigSnapshot}.
+         *
+         * @return the resolved Value
+         * @see Config#snapshotFor(TypedResolver[])
+         */
+        T getValue(ConfigSnapshot configSnapshot);
+
+        /**
          * Returns the key given in {@link #resolve(String)}.
          * @return the original key
          */

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/aabbc879/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigSnapshot.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigSnapshot.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigSnapshot.java
index 8491567..64368d0 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigSnapshot.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigSnapshot.java
@@ -22,12 +22,8 @@ package org.apache.deltaspike.core.api.config;
  * A value holder for TypedResolver values which all got resolved in a guaranteed atomic way.
  *
  * @see Config#snapshotFor(ConfigResolver.TypedResolver[])
+ * @see ConfigResolver.TypedResolver#getValue(ConfigSnapshot)
  */
 public interface ConfigSnapshot
 {
-    /**
-     * Access the value as it was at the time when {@link Config#snapshotFor(ConfigResolver.TypedResolver[])}
-     * got called.
-     */
-    <T> T getValue(ConfigResolver.TypedResolver<T> typedResolver);
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/aabbc879/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigSnapshotImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigSnapshotImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigSnapshotImpl.java
index f41f255..8970309 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigSnapshotImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigSnapshotImpl.java
@@ -32,15 +32,8 @@ public class ConfigSnapshotImpl implements ConfigSnapshot
         this.configValues = configValues;
     }
 
-    @Override
-    public <T> T getValue(ConfigResolver.TypedResolver<T> typedResolver)
+    public Map<ConfigResolver.TypedResolver<?>, Object> getConfigValues()
     {
-        if (!configValues.containsKey(typedResolver))
-        {
-            throw new IllegalArgumentException("The TypedResolver for key " + typedResolver.getKey() +
-                    " does not belong to this ConfigTransaction!");
-        }
-
-        return (T) configValues.get(typedResolver);
+        return configValues;
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/aabbc879/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/TypedResolverImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/TypedResolverImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/TypedResolverImpl.java
index f532f0e..e45b4b6 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/TypedResolverImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/TypedResolverImpl.java
@@ -19,6 +19,7 @@
 package org.apache.deltaspike.core.impl.config;
 
 import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.config.ConfigSnapshot;
 import org.apache.deltaspike.core.api.projectstage.ProjectStage;
 import org.apache.deltaspike.core.spi.config.ConfigSource;
 import org.apache.deltaspike.core.util.ClassUtils;
@@ -209,6 +210,20 @@ public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
     }
 
     @Override
+    public T getValue(ConfigSnapshot snapshot)
+    {
+        ConfigSnapshotImpl snapshotImpl = (ConfigSnapshotImpl) snapshot;
+
+        if (!snapshotImpl.getConfigValues().containsKey(this))
+        {
+            throw new IllegalArgumentException("The TypedResolver for key " + getKey() +
+                " does not belong the given ConfigSnapshot!");
+        }
+
+        return (T) snapshotImpl.getConfigValues().get(this);
+    }
+
+    @Override
     public T getValue()
     {
         long now = -1;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/aabbc879/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSnapshotTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSnapshotTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSnapshotTest.java
new file mode 100644
index 0000000..19119b8
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSnapshotTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.test.core.api.config;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.deltaspike.core.api.config.Config;
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.config.ConfigSnapshot;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+
+public class ConfigSnapshotTest
+{
+    private static final String HOST_KEY = "ds.test.myapp.host";
+    private static final String PORT1_KEY = "ds.test.myapp.port1";
+    private static final String PORT2_KEY = "ds.test.myapp.port2";
+
+    private ConfigResolver.TypedResolver<String> hostCfg;
+    private ConfigResolver.TypedResolver<Integer> port1Cfg;
+    private ConfigResolver.TypedResolver<Integer> port2Cfg;
+
+
+    @Test
+    public void testConfigTx()
+    {
+        Config cfg = ConfigResolver.getConfig();
+        ConfigurableTestConfigSource configSource = ConfigurableTestConfigSource.instance();
+        try
+        {
+            Map<String, String> newVals = new HashMap<>();
+            newVals.put(HOST_KEY, "host1");
+            newVals.put(PORT1_KEY, "1");
+            newVals.put(PORT2_KEY, "1");
+            configSource.setValues(newVals);
+
+            hostCfg = cfg.resolve(HOST_KEY);
+            port1Cfg = cfg.resolve(PORT1_KEY).as(Integer.class);
+            port2Cfg = cfg.resolve(PORT2_KEY).as(Integer.class);
+
+            assertEquals("host1", hostCfg.getValue());
+            assertEquals(Integer.valueOf(1), port1Cfg.getValue());
+            assertEquals(Integer.valueOf(1), port2Cfg.getValue());
+
+            ConfigSnapshot configSnapshot = cfg.snapshotFor(hostCfg, port1Cfg, port2Cfg);
+            assertNotNull(configSnapshot);
+
+            assertEquals("host1", hostCfg.getValue(configSnapshot));
+            assertEquals(Integer.valueOf(1), port1Cfg.getValue(configSnapshot));
+            assertEquals(Integer.valueOf(1), port2Cfg.getValue(configSnapshot));
+
+            // and those values don't change, even if we modify the underlying ConfigSource!
+            newVals.clear();
+            newVals.put(HOST_KEY, "host2");
+            newVals.put(PORT1_KEY, "2");
+            newVals.put(PORT2_KEY, "2");
+            configSource.setValues(newVals);
+
+            assertEquals("host1", hostCfg.getValue(configSnapshot));
+            assertEquals(Integer.valueOf(1), port1Cfg.getValue(configSnapshot));
+            assertEquals(Integer.valueOf(1), port2Cfg.getValue(configSnapshot));
+
+            // but the actual config did really change!
+            assertEquals("host2", hostCfg.getValue());
+            assertEquals(Integer.valueOf(2), port1Cfg.getValue());
+            assertEquals(Integer.valueOf(2), port2Cfg.getValue());
+        }
+        finally
+        {
+            configSource.clear();
+        }
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/aabbc879/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigTransactionTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigTransactionTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigTransactionTest.java
deleted file mode 100644
index b05aff0..0000000
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigTransactionTest.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * 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.test.core.api.config;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.deltaspike.core.api.config.Config;
-import org.apache.deltaspike.core.api.config.ConfigResolver;
-import org.apache.deltaspike.core.api.config.ConfigSnapshot;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-
-public class ConfigTransactionTest
-{
-    private static final String HOST_KEY = "ds.test.myapp.host";
-    private static final String PORT1_KEY = "ds.test.myapp.port1";
-    private static final String PORT2_KEY = "ds.test.myapp.port2";
-
-    private ConfigResolver.TypedResolver<String> hostCfg;
-    private ConfigResolver.TypedResolver<Integer> port1Cfg;
-    private ConfigResolver.TypedResolver<Integer> port2Cfg;
-
-
-    @Test
-    public void testConfigTx()
-    {
-        Config cfg = ConfigResolver.getConfig();
-        ConfigurableTestConfigSource configSource = ConfigurableTestConfigSource.instance();
-        try
-        {
-            Map<String, String> newVals = new HashMap<>();
-            newVals.put(HOST_KEY, "host1");
-            newVals.put(PORT1_KEY, "1");
-            newVals.put(PORT2_KEY, "1");
-            configSource.setValues(newVals);
-
-            hostCfg = cfg.resolve(HOST_KEY);
-            port1Cfg = cfg.resolve(PORT1_KEY).as(Integer.class);
-            port2Cfg = cfg.resolve(PORT2_KEY).as(Integer.class);
-
-            assertEquals("host1", hostCfg.getValue());
-            assertEquals(Integer.valueOf(1), port1Cfg.getValue());
-            assertEquals(Integer.valueOf(1), port2Cfg.getValue());
-
-            ConfigSnapshot configSnapshot = cfg.snapshotFor(hostCfg, port1Cfg, port2Cfg);
-            assertNotNull(configSnapshot);
-
-            assertEquals("host1", configSnapshot.getValue(hostCfg));
-            assertEquals(Integer.valueOf(1), configSnapshot.getValue(port1Cfg));
-            assertEquals(Integer.valueOf(1), configSnapshot.getValue(port2Cfg));
-
-            // and those values don't change, even if we modify the underlying ConfigSource!
-            newVals.clear();
-            newVals.put(HOST_KEY, "host2");
-            newVals.put(PORT1_KEY, "2");
-            newVals.put(PORT2_KEY, "2");
-            configSource.setValues(newVals);
-
-            assertEquals("host1", configSnapshot.getValue(hostCfg));
-            assertEquals(Integer.valueOf(1), configSnapshot.getValue(port1Cfg));
-            assertEquals(Integer.valueOf(1), configSnapshot.getValue(port2Cfg));
-
-            // but the actual config did really change!
-            assertEquals("host2", hostCfg.getValue());
-            assertEquals(Integer.valueOf(2), port1Cfg.getValue());
-            assertEquals(Integer.valueOf(2), port2Cfg.getValue());
-        }
-        finally
-        {
-            configSource.clear();
-        }
-    }
-
-
-}