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/05 12:16:04 UTC

[1/5] deltaspike git commit: remove the @author tag for myself

Repository: deltaspike
Updated Branches:
  refs/heads/master 1e5003f92 -> 0ec1c1e5b


remove the @author tag for myself

There is not code ownership at the ASF...


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

Branch: refs/heads/master
Commit: 4ee2fc9ee93d35c85dcb6f90695ee2fc341a5d77
Parents: a101bab
Author: Mark Struberg <st...@apache.org>
Authored: Wed Apr 4 17:10:54 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Wed Apr 4 17:31:39 2018 +0200

----------------------------------------------------------------------
 .../apache/deltaspike/core/impl/config/ConfigTransactionImpl.java | 3 ---
 .../org/apache/deltaspike/core/impl/crypto/CdiCipherService.java  | 3 ---
 2 files changed, 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4ee2fc9e/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
index 4a5108a..6a51343 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
@@ -23,9 +23,6 @@ import org.apache.deltaspike.core.api.config.ConfigTransaction;
 
 import java.util.Map;
 
-/**
- * @author Mark Struberg
- */
 public class ConfigTransactionImpl implements ConfigTransaction
 {
     private final ConfigImpl config;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4ee2fc9e/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/crypto/CdiCipherService.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/crypto/CdiCipherService.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/crypto/CdiCipherService.java
index 6efd8cc..381fe5d 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/crypto/CdiCipherService.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/crypto/CdiCipherService.java
@@ -25,9 +25,6 @@ import java.io.IOException;
 
 import org.apache.deltaspike.core.api.crypto.CipherService;
 
-/**
- * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
- */
 @ApplicationScoped
 public class CdiCipherService implements CipherService
 {


[4/5] deltaspike git commit: DELTASPIKE-1335 improve docs and tst handling

Posted by st...@apache.org.
DELTASPIKE-1335 improve docs and tst handling

hacked together with Alexander Falb (elexx)


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

Branch: refs/heads/master
Commit: a6296773d14db2339289179cf80ddb0339519229
Parents: 4ee2fc9
Author: Mark Struberg <st...@apache.org>
Authored: Wed Apr 4 17:55:57 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Wed Apr 4 17:55:57 2018 +0200

----------------------------------------------------------------------
 .../deltaspike/core/api/config/ConfigTransaction.java     |  8 +++++++-
 .../apache/deltaspike/core/impl/config/ConfigImpl.java    | 10 ++++++----
 .../core/impl/config/ConfigTransactionImpl.java           |  4 +---
 3 files changed, 14 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


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

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a6296773/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
index b6502bf..9784c23 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
@@ -138,15 +138,15 @@ public class ConfigImpl implements Config
         for (int tries = 1; tries < 5; tries++)
         {
             Map<ConfigResolver.TypedResolver<?>, Object> configValues = new HashMap<>();
-            long startReadCfgTst = lastChanged;
+            long startReadLastChanged = lastChanged;
             for (ConfigResolver.TypedResolver<?> typedResolver : typedResolvers)
             {
                 configValues.put(typedResolver, typedResolver.getValue());
             }
 
-            if (startReadCfgTst == lastChanged)
+            if (startReadLastChanged == lastChanged)
             {
-                return new ConfigTransactionImpl(this, configValues);
+                return new ConfigTransactionImpl(configValues);
             }
         }
 
@@ -229,7 +229,9 @@ public class ConfigImpl implements Config
 
     public void onAttributeChange(Set<String> attributesChanged)
     {
-        lastChanged = System.nanoTime();
+        // this is to force an incremented lastChanged even on time glitches and fast updates
+        long newLastChanged = System.nanoTime();
+        lastChanged = lastChanged >= newLastChanged ? lastChanged++ : newLastChanged;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a6296773/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
index 6a51343..2671cd1 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
@@ -25,12 +25,10 @@ import java.util.Map;
 
 public class ConfigTransactionImpl implements ConfigTransaction
 {
-    private final ConfigImpl config;
     private final Map<ConfigResolver.TypedResolver<?>, Object> configValues;
 
-    public ConfigTransactionImpl(ConfigImpl config, Map<ConfigResolver.TypedResolver<?>, Object> configValues)
+    public ConfigTransactionImpl(Map<ConfigResolver.TypedResolver<?>, Object> configValues)
     {
-        this.config = config;
         this.configValues = configValues;
     }
 


[2/5] deltaspike git commit: DELTASPIKE-1335 atomic access to configured values

Posted by st...@apache.org.
DELTASPIKE-1335 atomic access to configured values


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

Branch: refs/heads/master
Commit: a101bab996d6cfad8369dc96626bb7d016318351
Parents: fdd1e3d
Author: Mark Struberg <st...@apache.org>
Authored: Wed Apr 4 17:09:52 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Wed Apr 4 17:31:39 2018 +0200

----------------------------------------------------------------------
 .../deltaspike/core/api/config/Config.java      |  4 +-
 .../deltaspike/core/impl/config/ConfigImpl.java | 28 ++++++
 .../core/impl/config/ConfigTransactionImpl.java | 51 +++++++++++
 .../core/api/config/ConfigTransactionTest.java  | 88 +++++++++++++++++++
 .../config/ConfigurableTestConfigSource.java    | 91 ++++++++++++++++++++
 ...ache.deltaspike.core.spi.config.ConfigSource |  1 +
 6 files changed, 261 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a101bab9/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
index f1fc368..6a1bf54 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
@@ -66,7 +66,7 @@ public interface Config
      * To consistently access n different config values we can start a {@link ConfigTransaction} for those values.
      *
      * <pre>
-     *     ConfigTransaction cfgTx = config.startTx(hostCfg, portCfg);
+     *     ConfigTransaction cfgTx = config.startTransaction(hostCfg, portCfg);
      *
      *     String host = cfgTx.getValue(hostCfg);
      *     Integer port = cfgTx.getValue(portCfg);
@@ -80,7 +80,7 @@ public interface Config
      *
      * @return a new {@link ConfigTransaction} which holds the resolved values of all the {@param typedResolvers}.
      */
-    ConfigTransaction startTx(ConfigResolver.TypedResolver<?>... typedResolvers);
+    ConfigTransaction startTransaction(ConfigResolver.TypedResolver<?>... typedResolvers);
 
     /**
      * @return all the current ConfigSources for this Config

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a101bab9/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
index eefc612..b6502bf 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
@@ -20,6 +20,7 @@ package org.apache.deltaspike.core.impl.config;
 
 import org.apache.deltaspike.core.api.config.Config;
 import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.config.ConfigTransaction;
 import org.apache.deltaspike.core.spi.config.ConfigFilter;
 import org.apache.deltaspike.core.spi.config.ConfigSource;
 import org.apache.deltaspike.core.spi.config.ConfigSourceProvider;
@@ -28,7 +29,9 @@ import org.apache.deltaspike.core.util.ServiceUtils;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.logging.Level;
@@ -127,6 +130,31 @@ public class ConfigImpl implements Config
     }
 
     @Override
+    public ConfigTransaction startTransaction(ConfigResolver.TypedResolver<?>[] typedResolvers)
+    {
+        // we implement kind of optimistic Locking
+        // Means we try multiple time to resolve all the given values
+        // until the config didn't change inbetween.
+        for (int tries = 1; tries < 5; tries++)
+        {
+            Map<ConfigResolver.TypedResolver<?>, Object> configValues = new HashMap<>();
+            long startReadCfgTst = lastChanged;
+            for (ConfigResolver.TypedResolver<?> typedResolver : typedResolvers)
+            {
+                configValues.put(typedResolver, typedResolver.getValue());
+            }
+
+            if (startReadCfgTst == lastChanged)
+            {
+                return new ConfigTransactionImpl(this, configValues);
+            }
+        }
+
+        throw new IllegalStateException(
+                "Could not resolve ConfigTransaction as underlying values are permanently changing!");
+    }
+
+    @Override
     public void addConfigSources(List<ConfigSource> configSourcesToAdd)
     {
         List<ConfigSource> allConfigSources = new ArrayList<>();

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a101bab9/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
new file mode 100644
index 0000000..4a5108a
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigTransactionImpl.java
@@ -0,0 +1,51 @@
+/*
+ * 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 org.apache.deltaspike.core.api.config.ConfigTransaction;
+
+import java.util.Map;
+
+/**
+ * @author Mark Struberg
+ */
+public class ConfigTransactionImpl implements ConfigTransaction
+{
+    private final ConfigImpl config;
+    private final Map<ConfigResolver.TypedResolver<?>, Object> configValues;
+
+    public ConfigTransactionImpl(ConfigImpl config, Map<ConfigResolver.TypedResolver<?>, Object> configValues)
+    {
+        this.config = config;
+        this.configValues = configValues;
+    }
+
+    @Override
+    public <T> T getValue(ConfigResolver.TypedResolver<T> typedResolver)
+    {
+        if (!configValues.containsKey(typedResolver))
+        {
+            throw new IllegalArgumentException("The TypedResolver for key " + typedResolver.getKey() +
+                    " does not belong to this ConfigTransaction!");
+        }
+
+        return (T) configValues.get(typedResolver);
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a101bab9/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
new file mode 100644
index 0000000..e448ca6
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigTransactionTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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 org.apache.deltaspike.core.api.config.Config;
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.config.ConfigTransaction;
+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()
+    {
+        ConfigurableTestConfigSource configSource = ConfigurableTestConfigSource.instance();
+        try
+        {
+            configSource.set(HOST_KEY, "host1");
+            configSource.set(PORT1_KEY, "1");
+            configSource.set(PORT2_KEY, "1");
+
+            Config cfg = ConfigResolver.getConfig();
+            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());
+
+            ConfigTransaction configTransaction = cfg.startTransaction(hostCfg, port1Cfg, port2Cfg);
+            assertNotNull(configTransaction);
+
+            assertEquals("host1", configTransaction.getValue(hostCfg));
+            assertEquals(Integer.valueOf(1), configTransaction.getValue(port1Cfg));
+            assertEquals(Integer.valueOf(1), configTransaction.getValue(port2Cfg));
+
+            // and those values don't change, even if we modify the underlying ConfigSource!
+            configSource.set(HOST_KEY, "host2");
+            configSource.set(PORT1_KEY, "2");
+            configSource.set(PORT2_KEY, "2");
+
+            assertEquals("host1", configTransaction.getValue(hostCfg));
+            assertEquals(Integer.valueOf(1), configTransaction.getValue(port1Cfg));
+            assertEquals(Integer.valueOf(1), configTransaction.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();
+        }
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a101bab9/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java
new file mode 100644
index 0000000..2c3a9db
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java
@@ -0,0 +1,91 @@
+/*
+ * 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 org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.spi.config.ConfigSource;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A ConfigSource which is backed by a ThreadLocal.
+ * So it can be dynamically configured even for parallel tests.
+ *
+ * Note that you MUST call the {@link #clear()} method at the end of a method which uses this ConfigSource.
+ */
+public class ConfigurableTestConfigSource implements ConfigSource
+{
+    private static ThreadLocal<Map<String, String>> props = new ThreadLocal<>();
+
+    @Override
+    public int getOrdinal()
+    {
+        return 500;
+    }
+
+    public static ConfigurableTestConfigSource instance() {
+        return (ConfigurableTestConfigSource) Arrays.stream(ConfigResolver.getConfig().getConfigSources())
+                .filter(cs -> cs instanceof ConfigurableTestConfigSource)
+                .findFirst()
+                .get();
+    }
+
+    @Override
+    public Map<String, String> getProperties()
+    {
+        Map<String, String> propMap = props.get();
+        if (propMap == null)
+        {
+            propMap = new ConcurrentHashMap<>();
+            props.set(propMap);
+        }
+        return propMap;
+    }
+
+    @Override
+    public String getPropertyValue(String key)
+    {
+        return getProperties().get(key);
+    }
+
+    @Override
+    public String getConfigName()
+    {
+        return this.getClass().getSimpleName();
+    }
+
+    @Override
+    public boolean isScannable()
+    {
+        return true;
+    }
+
+    public void clear()
+    {
+        props.set(null);
+        props.remove();
+    }
+
+    public void set(String key, String value)
+    {
+        getProperties().put(key, value);
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a101bab9/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource b/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource
index 833d367..956fb04 100644
--- a/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource
+++ b/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource
@@ -18,3 +18,4 @@
 #####################################################################################
 
 org.apache.deltaspike.test.core.api.config.TestConfigSource
+org.apache.deltaspike.test.core.api.config.ConfigurableTestConfigSource
\ No newline at end of file


[5/5] deltaspike git commit: DELTASPIKE-1335 DELTASPIKE-1277 fix reportChangeListener

Posted by st...@apache.org.
DELTASPIKE-1335 DELTASPIKE-1277 fix reportChangeListener

Also implement the config change callback in the new ConfigurableTestConfigSource


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

Branch: refs/heads/master
Commit: 0ec1c1e5bca85141ddbe164d50743c13b572e73b
Parents: a629677
Author: Mark Struberg <st...@apache.org>
Authored: Thu Apr 5 10:35:52 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Thu Apr 5 10:35:52 2018 +0200

----------------------------------------------------------------------
 .../deltaspike/core/impl/config/ConfigImpl.java | 13 ++++++++++--
 .../core/api/config/ConfigTransactionTest.java  | 21 +++++++++++++-------
 .../config/ConfigurableTestConfigSource.java    | 17 ++++++++++++++--
 3 files changed, 40 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0ec1c1e5/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
index 9784c23..0296475 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java
@@ -157,17 +157,26 @@ public class ConfigImpl implements Config
     @Override
     public void addConfigSources(List<ConfigSource> configSourcesToAdd)
     {
+        if (configSourcesToAdd == null || configSourcesToAdd.isEmpty())
+        {
+            return;
+        }
+
         List<ConfigSource> allConfigSources = new ArrayList<>();
+        // start with all existing ConfigSources
         if (this.configSources != null)
         {
             for (ConfigSource configSource : this.configSources)
             {
-                configSource.setOnAttributeChange(this::onAttributeChange);
                 allConfigSources.add(configSource);
             }
         }
 
-        allConfigSources.addAll(configSourcesToAdd);
+        for (ConfigSource configSourceToAdd : configSourcesToAdd)
+        {
+            configSourceToAdd.setOnAttributeChange(this::onAttributeChange);
+            allConfigSources.add(configSourceToAdd);
+        }
 
         this.configSources = sortDescending(allConfigSources);
     }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0ec1c1e5/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
index e448ca6..57c7468 100644
--- 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
@@ -18,6 +18,9 @@
  */
 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.ConfigTransaction;
@@ -41,14 +44,16 @@ public class ConfigTransactionTest
     @Test
     public void testConfigTx()
     {
+        Config cfg = ConfigResolver.getConfig();
         ConfigurableTestConfigSource configSource = ConfigurableTestConfigSource.instance();
         try
         {
-            configSource.set(HOST_KEY, "host1");
-            configSource.set(PORT1_KEY, "1");
-            configSource.set(PORT2_KEY, "1");
+            Map<String, String> newVals = new HashMap<>();
+            newVals.put(HOST_KEY, "host1");
+            newVals.put(PORT1_KEY, "1");
+            newVals.put(PORT2_KEY, "1");
+            configSource.setValues(newVals);
 
-            Config cfg = ConfigResolver.getConfig();
             hostCfg = cfg.resolve(HOST_KEY);
             port1Cfg = cfg.resolve(PORT1_KEY).as(Integer.class);
             port2Cfg = cfg.resolve(PORT2_KEY).as(Integer.class);
@@ -65,9 +70,11 @@ public class ConfigTransactionTest
             assertEquals(Integer.valueOf(1), configTransaction.getValue(port2Cfg));
 
             // and those values don't change, even if we modify the underlying ConfigSource!
-            configSource.set(HOST_KEY, "host2");
-            configSource.set(PORT1_KEY, "2");
-            configSource.set(PORT2_KEY, "2");
+            newVals.clear();
+            newVals.put(HOST_KEY, "host2");
+            newVals.put(PORT1_KEY, "2");
+            newVals.put(PORT2_KEY, "2");
+            configSource.setValues(newVals);
 
             assertEquals("host1", configTransaction.getValue(hostCfg));
             assertEquals(Integer.valueOf(1), configTransaction.getValue(port1Cfg));

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0ec1c1e5/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java
index 2c3a9db..c605dc9 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigurableTestConfigSource.java
@@ -23,7 +23,9 @@ import org.apache.deltaspike.core.spi.config.ConfigSource;
 
 import java.util.Arrays;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Consumer;
 
 /**
  * A ConfigSource which is backed by a ThreadLocal.
@@ -35,6 +37,8 @@ public class ConfigurableTestConfigSource implements ConfigSource
 {
     private static ThreadLocal<Map<String, String>> props = new ThreadLocal<>();
 
+    private Consumer<Set<String>> reportAttributeChange;
+
     @Override
     public int getOrdinal()
     {
@@ -84,8 +88,17 @@ public class ConfigurableTestConfigSource implements ConfigSource
         props.remove();
     }
 
-    public void set(String key, String value)
+    public void setValues(Map<String, String> values)
+    {
+        getProperties().putAll(values);
+
+        // now notify our Config that some values got changed
+        reportAttributeChange.accept(values.keySet());
+    }
+
+    @Override
+    public void setOnAttributeChange(Consumer<Set<String>> reportAttributeChange)
     {
-        getProperties().put(key, value);
+        this.reportAttributeChange = reportAttributeChange;
     }
 }


[3/5] deltaspike git commit: DELTASPIKE-1335 proposal for atomic config access

Posted by st...@apache.org.
DELTASPIKE-1335 proposal for atomic config access

crafted together with Manfred Huber


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

Branch: refs/heads/master
Commit: fdd1e3dcd9a12ceed831dd7460492b6dd788721c
Parents: 1e5003f
Author: Mark Struberg <st...@apache.org>
Authored: Wed Apr 4 13:56:03 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Wed Apr 4 17:31:39 2018 +0200

----------------------------------------------------------------------
 .../deltaspike/core/api/config/Config.java      | 41 ++++++++++++++++++++
 .../core/api/config/ConfigTransaction.java      | 27 +++++++++++++
 2 files changed, 68 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fdd1e3dc/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
index 47d2c50..f1fc368 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/Config.java
@@ -42,6 +42,47 @@ public interface Config
     ConfigResolver.UntypedResolver<String> resolve(String name);
 
     /**
+     * <p>This method can be used to access multiple
+     * {@link ConfigResolver.TypedResolver} which must be consistent.
+     *
+     * <p>An example would be to access some {@code 'myapp.host'} and {@code 'myapp.port'}:
+     * The underlying values are {@code 'oldserver'} and {@code '8080'}.
+     *
+     * <pre>
+     *     // get the current host value
+     *     TypedResolver&lt;String&gt; hostCfg config.resolve("myapp.host")
+     *              .cacheFor(TimeUnit.MINUTES, 60);
+     *
+     *     // and right inbetween the underlying values get changed to 'newserver' and port 8082
+     *
+     *     // get the current port for the host
+     *     TypedResolver&lt;Integer&gt; portCfg config.resolve("myapp.port")
+     *              .cacheFor(TimeUnit.MINUTES, 60);
+     * </pre>
+     *
+     * In ths above code we would get the combination of {@code 'oldserver'} but with the new port {@code 8081}.
+     * And this will obviously blow up because that host+port combination doesn't exist.
+     *
+     * To consistently access n different config values we can start a {@link ConfigTransaction} for those values.
+     *
+     * <pre>
+     *     ConfigTransaction cfgTx = config.startTx(hostCfg, portCfg);
+     *
+     *     String host = cfgTx.getValue(hostCfg);
+     *     Integer port = cfgTx.getValue(portCfg);
+     * </pre>
+     *
+     * Note that there is no <em>close</em> on the transaction.
+     * They should be used as local variables inside a method.
+     * Values will not be reloaded for an open {@link ConfigTransaction}.
+     *
+     * @param typedResolvers the list of {@link ConfigResolver.TypedResolver} to be accessed in an atomic way
+     *
+     * @return a new {@link ConfigTransaction} which holds the resolved values of all the {@param typedResolvers}.
+     */
+    ConfigTransaction startTx(ConfigResolver.TypedResolver<?>... typedResolvers);
+
+    /**
      * @return all the current ConfigSources for this Config
      */
     ConfigSource[] getConfigSources();

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fdd1e3dc/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigTransaction.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigTransaction.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigTransaction.java
new file mode 100644
index 0000000..22afbac
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigTransaction.java
@@ -0,0 +1,27 @@
+/*
+ * 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.api.config;
+
+/**
+ * A value holder for
+ */
+public interface ConfigTransaction
+{
+    <T> T getValue(ConfigResolver.TypedResolver<T> typedResolver);
+}