You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by st...@apache.org on 2018/05/08 12:36:50 UTC

svn commit: r1831166 - in /geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config: ConfigSnapshotImpl.java ConfigValueImpl.java

Author: struberg
Date: Tue May  8 12:36:50 2018
New Revision: 1831166

URL: http://svn.apache.org/viewvc?rev=1831166&view=rev
Log:
update to new ConfigValue API (taken from Apache DeltaSpike)

Added:
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigSnapshotImpl.java   (with props)
Modified:
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java

Added: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigSnapshotImpl.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigSnapshotImpl.java?rev=1831166&view=auto
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigSnapshotImpl.java (added)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigSnapshotImpl.java Tue May  8 12:36:50 2018
@@ -0,0 +1,40 @@
+/*
+ * 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.geronimo.config;
+
+import java.util.Map;
+
+import javax.config.ConfigSnapshot;
+import javax.config.ConfigValue;
+
+
+public class ConfigSnapshotImpl implements ConfigSnapshot
+{
+    private final Map<ConfigValue<?>, Object> configValues;
+
+    public ConfigSnapshotImpl(Map<ConfigValue<?>, Object> configValues)
+    {
+        this.configValues = configValues;
+    }
+
+    public Map<ConfigValue<?>, Object> getConfigValues()
+    {
+        return configValues;
+    }
+}

Propchange: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigSnapshotImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1831166&r1=1831165&r2=1831166&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 12:36:50 2018
@@ -16,13 +16,16 @@
  */
 package org.apache.geronimo.config;
 
+import javax.config.ConfigSnapshot;
 import javax.config.ConfigValue;
 import javax.config.spi.Converter;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Optional;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.Logger;
 
@@ -51,6 +54,16 @@ public class ConfigValueImpl<T> implemen
     private volatile long reloadAfter = -1;
     private T lastValue = null;
     private ConfigChanged valueChangeListener;
+    private boolean isList;
+    private boolean isSet;
+
+    private T defaultValue;
+    private boolean withDefault;
+
+    /**
+     * Alternative Converter to be used instead of the default converter
+     */
+    private Converter<T> converter;
 
     public ConfigValueImpl(ConfigImpl config, String key) {
         this.config = config;
@@ -63,6 +76,68 @@ public class ConfigValueImpl<T> implemen
         return (ConfigValueImpl<N>) this;
     }
 
+    @Override
+    public ConfigValue<List<T>> asList() {
+        isList = true;
+        ConfigValue<List<T>> listTypedResolver = (ConfigValue<List<T>>) this;
+
+        if (defaultValue == null)
+        {
+            // the default for lists is an empty list instead of null
+            return listTypedResolver.withDefault(Collections.<T>emptyList());
+        }
+
+        return listTypedResolver;
+    }
+
+    @Override
+    public ConfigValue<Set<T>> asSet() {
+        isSet = true;
+        ConfigValue<Set<T>> listTypedResolver = (ConfigValue<Set<T>>) this;
+
+        if (defaultValue == null)
+        {
+            // the default for lists is an empty list instead of null
+            return listTypedResolver.withDefault(Collections.<T>emptySet());
+        }
+
+        return listTypedResolver;
+    }
+
+    @Override
+    public ConfigValue<T> withDefault(T value) {
+        defaultValue = value;
+        withDefault = true;
+        return this;
+    }
+
+    @Override
+    public ConfigValue<T> withStringDefault(String value) {
+        if (value == null || value.isEmpty()) {
+            throw new RuntimeException("Empty String or null supplied as string-default value for property "
+                    + keyOriginal);
+        }
+
+        if (isList) {
+            defaultValue = splitAndConvertListValue(value);
+        }
+        else {
+            defaultValue = convert(value);
+        }
+        withDefault = true;
+        return this;
+    }
+
+    @Override
+    public T getDefaultValue() {
+        return defaultValue;
+    }
+
+    @Override
+    public ConfigValue<T> useConverter(Converter<T> converter) {
+        this.converter = converter;
+        return this;
+    }
 
     @Override
     public ConfigValueImpl<T> cacheFor(long value, TimeUnit timeUnit) {
@@ -92,8 +167,6 @@ public class ConfigValueImpl<T> implemen
         return this;
     }
 
-    
-
     //X @Override
     public List<T> getValueList() {
         String rawList = (String) get(false);
@@ -147,6 +220,21 @@ public class ConfigValueImpl<T> implemen
         return val;
     }
 
+    @Override
+    public T getValue(ConfigSnapshot configSnapshot) {
+        ConfigSnapshotImpl snapshotImpl = (ConfigSnapshotImpl) configSnapshot;
+
+        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);
+    }
+
+
+
     private T get(boolean convert) {
         long now = -1;
         if (cacheTimeNs > 0)
@@ -218,6 +306,10 @@ public class ConfigValueImpl<T> implemen
     }
 
     private T convert(String value) {
+        if (converter != null) {
+            return converter.convert(value);
+        }
+
         if (String.class == configEntryType) {
             return (T) value;
         }
@@ -230,4 +322,43 @@ public class ConfigValueImpl<T> implemen
         return (T) converter.convert(value);
     }
 
+
+    private T splitAndConvertListValue(String valueStr) {
+        if (valueStr == null) {
+            return null;
+        }
+
+        List list = new ArrayList();
+        StringBuilder currentValue = new StringBuilder();
+        int length = valueStr.length();
+        for (int i = 0; i < length; i++) {
+            char c = valueStr.charAt(i);
+            if (c == '\\') {
+                if (i < length - 1) {
+                    char nextC = valueStr.charAt(i + 1);
+                    currentValue.append(nextC);
+                    i++;
+                }
+            }
+            else if (c == ',') {
+                String trimedVal = currentValue.toString().trim();
+                if (trimedVal.length() > 0) {
+                    list.add(convert(trimedVal));
+                }
+
+                currentValue.setLength(0);
+            }
+            else {
+                currentValue.append(c);
+            }
+        }
+
+        String trimedVal = currentValue.toString().trim();
+        if (trimedVal.length() > 0) {
+            list.add(convert(trimedVal));
+        }
+
+        return (T) list;
+    }
+
 }