geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1831191 - in /geronimo/components/config/branches/ConfigJSR/impl: ./ src/main/java/org/apache/geronimo/config/ src/main/java/org/apache/geronimo/config/configsource/
Date Tue, 08 May 2018 14:37:16 GMT
Author: struberg
Date: Tue May  8 14:37:16 2018
New Revision: 1831191

URL: http://svn.apache.org/viewvc?rev=1831191&view=rev
Log:
implement ConfigValue and lastChanged behaviour

Modified:
    geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml
    geronimo/components/config/branches/ConfigJSR/impl/pom.xml
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java

Modified: geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml?rev=1831191&r1=1831190&r2=1831191&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml (original)
+++ geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml Tue May  8 14:37:16
2018
@@ -23,7 +23,7 @@
 
     <classes>
         <!-- Issues in the spec -->
-        <class name="org.eclipse.configjsr.CustomConfigSourceTest">
+        <class name="org.eclipse.configjsr.ConfigValueTest">
             <methods>
                 <include name=".*"/>
             </methods>

Modified: geronimo/components/config/branches/ConfigJSR/impl/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/pom.xml?rev=1831191&r1=1831190&r2=1831191&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/pom.xml (original)
+++ geronimo/components/config/branches/ConfigJSR/impl/pom.xml Tue May  8 14:37:16 2018
@@ -98,6 +98,10 @@
                         <HISTTIMEFORMAT>value-for-tck-since-we-ignore-empty</HISTTIMEFORMAT>
                         <INSTANCE>value-for-tck-since-we-ignore-empty</INSTANCE>
                         <UPSTART_INSTANCE>value-for-tck-since-we-ignore-empty</UPSTART_INSTANCE>
+                        <envconfig_my_int_property>45</envconfig_my_int_property>
+                        <ENVCONFIG_MY_BOOLEAN_PROPERTY>true</ENVCONFIG_MY_BOOLEAN_PROPERTY>
+                        <ENVCONFIG_MY_STRING_PROPERTY>woohoo</ENVCONFIG_MY_STRING_PROPERTY>
+                        <envconfig_my_string_property>haha</envconfig_my_string_property>
                     </environmentVariables>
                     <systemPropertyVariables>
                         <!-- TCKs assume it but this is not really a good option for prod
-->

Modified: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java?rev=1831191&r1=1831190&r2=1831191&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
(original)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
Tue May  8 14:37:16 2018
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Optional;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -42,6 +43,8 @@ import org.apache.geronimo.config.conver
 import org.apache.geronimo.config.converters.StringConverter;
 import org.apache.geronimo.config.converters.URLConverter;
 import javax.config.Config;
+import javax.config.ConfigSnapshot;
+import javax.config.ConfigValue;
 import javax.config.spi.ConfigSource;
 import javax.config.spi.Converter;
 
@@ -62,6 +65,9 @@ public class ConfigImpl implements Confi
     protected Map<Type, Converter> converters = new HashMap<>();
     protected Map<Type, Converter> implicitConverters = new ConcurrentHashMap<>();
 
+    // volatile to a.) make the read/write behave atomic and b.) guarantee multi-thread safety
+    private volatile long lastChanged = 0;
+
 
     public ConfigImpl() {
         registerDefaultConverter();
@@ -97,6 +103,30 @@ public class ConfigImpl implements Confi
     }
 
     @Override
+    public ConfigSnapshot snapshotFor(ConfigValue<?>... configValues) {
+        // 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<ConfigValue<?>, Object> resolved = new HashMap<>();
+            long startReadLastChanged = lastChanged;
+            for (ConfigValue configValue : configValues)
+            {
+                resolved.put(configValue, configValue.getValue());
+            }
+
+            if (startReadLastChanged == lastChanged)
+            {
+                return new ConfigSnapshotImpl(resolved);
+            }
+        }
+
+        throw new IllegalStateException(
+                "Could not resolve ConfigTransaction as underlying values are permanently
changing!");
+    }
+
+    @Override
     public <T> T getValue(String propertyName, Class<T> propertyType) {
         String value = getValue(propertyName);
         if (value == null) {
@@ -188,7 +218,10 @@ public class ConfigImpl implements Confi
 
     public synchronized void addConfigSources(List<ConfigSource> configSourcesToAdd)
{
         List<ConfigSource> allConfigSources = new ArrayList<>(configSources);
-        allConfigSources.addAll(configSourcesToAdd);
+        for (ConfigSource configSource : configSourcesToAdd) {
+            configSource.setOnAttributeChange(this::onAttributeChange);
+            allConfigSources.add(configSource);
+        }
 
         // finally put all the configSources back into the map
         configSources = sortDescending(allConfigSources);
@@ -305,4 +338,20 @@ public class ConfigImpl implements Confi
         return getTypeOfConverter(clazz.getSuperclass());
     }
 
+    public void onAttributeChange(Set<String> attributesChanged)
+    {
+        // this is to force an incremented lastChanged even on time glitches and fast updates
+        long newLastChanged = System.nanoTime();
+        lastChanged = lastChanged >= newLastChanged ? lastChanged++ : newLastChanged;
+    }
+
+    /**
+     * @return the nanoTime when the last change got reported by a ConfigSource
+     */
+    public long getLastChanged()
+    {
+        return lastChanged;
+    }
+
+
 }
\ No newline at end of file

Modified: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java?rev=1831191&r1=1831190&r2=1831191&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java
(original)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java
Tue May  8 14:37:16 2018
@@ -52,8 +52,10 @@ public class ConfigValueImpl<T> implemen
 
     private long cacheTimeNs = -1;
     private volatile long reloadAfter = -1;
+    private long lastReloadedAt = -1;
+
     private T lastValue = null;
-    private ConfigChanged valueChangeListener;
+    //X will later get added again private ConfigChanged valueChangeListener;
     private boolean isList;
     private boolean isSet;
 
@@ -161,11 +163,13 @@ public class ConfigValueImpl<T> implemen
         return Optional.ofNullable(get());
     }
 
-    @Override
+    //X will later get added again @Override
+    /*X
     public ConfigValueImpl<T> onChange(ConfigChanged valueChangeListener) {
         this.valueChangeListener = valueChangeListener;
         return this;
     }
+    */
 
     //X @Override
     public List<T> getValueList() {
@@ -242,23 +246,32 @@ public class ConfigValueImpl<T> implemen
             now = System.nanoTime();
             if (now <= reloadAfter)
             {
-                return lastValue;
+                // now check if anything in the underlying Config got changed
+                long lastCfgChange = config.getLastChanged();
+                if (lastCfgChange < lastReloadedAt)
+                {
+                    return lastValue;
+                }
             }
         }
 
         String valueStr = resolveStringValue();
         T value = convert ? convert(valueStr) : (T) valueStr;
 
+        //X will later get added again
+        /*X
         if (valueChangeListener != null && (value != null && !value.equals(lastValue)
|| (value == null && lastValue != null)) )
         {
             valueChangeListener.onValueChange(keyOriginal, lastValue, value);
         }
+        */
 
         lastValue = value;
 
         if (cacheTimeNs > 0)
         {
             reloadAfter = now + cacheTimeNs;
+            lastReloadedAt = now;
         }
 
         return value;

Modified: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java?rev=1831191&r1=1831190&r2=1831191&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java
(original)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java
Tue May  8 14:37:16 2018
@@ -58,9 +58,13 @@ public class SystemEnvConfigSource exten
     public String getValue(String key) {
         String val = configValues.get(key);
         if (val == null) {
-            val = configValues.get(key.replace('.', '_'));
+            key = key.replace('.', '_');
+            val = configValues.get(key);
+        }
+        if (val == null) {
+           key = key.toUpperCase();
+           val = configValues.get(key);
         }
-
         return val;
     }
 }



Mime
View raw message