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 2021/12/08 22:19:52 UTC

[deltaspike] branch master updated (f49e386 -> 941e371)

This is an automated email from the ASF dual-hosted git repository.

struberg pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/deltaspike.git.


    from f49e386  DELTASPIKE-1426 fix proxyfactory test
     new facacde  DELTASPIKE-1444 add POJO based Config
     new 422a78a  DELTASPIKE-1444 make BeanConverter a non-static factory
     new 941e371  DELTASPIKE-1445 implement Supplier<T> support

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../main/resources/deltaspike/default-checks.xml   |   2 +-
 .../deltaspike/core/api/config/ConfigProperty.java |  17 +
 .../deltaspike/core/api/config/ConfigResolver.java |  42 ++
 .../spi/config/BaseConfigPropertyProducer.java     |  43 +-
 .../deltaspike/core/impl/config/ConfigImpl.java    |  17 +-
 .../impl/config/DefaultConfigPropertyProducer.java |  40 ++
 .../core/impl/config/DeltaSpikeConfigInfo.java     | 504 ++++++++++-----------
 .../impl/config/DeltaSpikeConfigInfoMBean.java     |  66 +--
 .../core/impl/config/TypedResolverImpl.java        |  89 +++-
 .../config/converter/BeanConverterFactory.java     |  71 +++
 .../config/converter/CtInjectionBeanConverter.java |  87 ++++
 .../converter/FieldInjectionBeanConverter.java     |  88 ++++
 .../core/api/config/BeanConfigResolverTest.java    |  74 +++
 .../test/core/api/config/ConfigResolverTest.java   |   3 +
 .../ServerEndpointPojoWithCt.java}                 |  21 +-
 .../config/beans/ServerEndpointPojoWithFields.java |  62 +++
 .../injectable/InjectableConfigPropertyTest.java   |  38 +-
 .../core/api/config/injectable/SettingsBean.java   |  19 +
 .../META-INF/apache-deltaspike.properties          |   8 +
 19 files changed, 966 insertions(+), 325 deletions(-)
 create mode 100644 deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java
 create mode 100644 deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/CtInjectionBeanConverter.java
 create mode 100644 deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/FieldInjectionBeanConverter.java
 create mode 100644 deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java
 copy deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/{injectable/PrefixedConfigBean.java => beans/ServerEndpointPojoWithCt.java} (61%)
 create mode 100644 deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/beans/ServerEndpointPojoWithFields.java

[deltaspike] 03/03: DELTASPIKE-1445 implement Supplier support

Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/deltaspike.git

commit 941e3717c18196dc7bcee1de8dcaac86e6f874c4
Author: Mark Struberg <st...@apache.org>
AuthorDate: Wed Dec 8 22:29:29 2021 +0100

    DELTASPIKE-1445 implement Supplier<T> support
---
 .../deltaspike/core/api/config/ConfigProperty.java |  17 +
 .../spi/config/BaseConfigPropertyProducer.java     |  43 +-
 .../impl/config/DefaultConfigPropertyProducer.java |  40 ++
 .../core/impl/config/DeltaSpikeConfigInfo.java     | 504 ++++++++++-----------
 .../impl/config/DeltaSpikeConfigInfoMBean.java     |  66 +--
 .../config/converter/BeanConverterFactory.java     |   6 +-
 .../core/api/config/BeanConfigResolverTest.java    |  37 +-
 .../config/beans/ServerEndpointPojoWithCt.java}    |  70 +--
 .../config/beans/ServerEndpointPojoWithFields.java |  62 +++
 .../injectable/InjectableConfigPropertyTest.java   |  38 +-
 .../core/api/config/injectable/SettingsBean.java   |  19 +
 11 files changed, 539 insertions(+), 363 deletions(-)

diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigProperty.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigProperty.java
index a856ad7..586a21a 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigProperty.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigProperty.java
@@ -23,6 +23,7 @@ import javax.inject.Qualifier;
 import java.lang.annotation.Documented;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
+import java.util.concurrent.TimeUnit;
 
 import static java.lang.annotation.ElementType.CONSTRUCTOR;
 import static java.lang.annotation.ElementType.FIELD;
@@ -30,6 +31,7 @@ import static java.lang.annotation.ElementType.METHOD;
 import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.util.concurrent.TimeUnit.SECONDS;
 
 /**
  * This Qualifier allows simple injection of configuration properties through the DeltaSpike configuration 
@@ -151,4 +153,19 @@ public @interface ConfigProperty
      */
     @Nonbinding
     Class<? extends ConfigResolver.Converter> converter() default ConfigResolver.Converter.class;
+
+    /**
+     * This only applies when used with injecting a {@link java.util.function.Supplier} for the requested configuration.
+     * @return the duration while the value is not reloaded.
+     */
+    @Nonbinding
+    long cacheFor() default -1;
+
+    /**
+     * This only applies when used with injecting a {@link java.util.function.Supplier} for the requested configuration.
+     * @return the duration unit for {@see cacheFor()}.
+     */
+    @Nonbinding
+    TimeUnit cacheUnit() default SECONDS;
+
 }
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java
index 48517dc..f120736 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java
@@ -23,6 +23,7 @@ import javax.enterprise.inject.spi.InjectionPoint;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import org.apache.deltaspike.core.api.config.Config;
 import org.apache.deltaspike.core.api.config.ConfigResolver;
 import org.apache.deltaspike.core.api.config.ConfigProperty;
 import org.apache.deltaspike.core.api.provider.BeanProvider;
@@ -176,12 +177,26 @@ public abstract class BaseConfigPropertyProducer
                                                           final String parameterizedBy,
                                                           final boolean projectStageAware, final boolean evaluate)
     {
-        final ConfigResolver.UntypedResolver<String> untypedResolver = ConfigResolver.resolve(key);
-        final ConfigResolver.TypedResolver<T> resolver =
-                (ConfigResolver.Converter.class == converterType ?
-                        untypedResolver.as(Class.class.cast(ipCls)) :
-                        untypedResolver.as(ipCls, BeanProvider.getContextualReference(converterType)))
-                        .withCurrentProjectStage(projectStageAware);
+        final Config config = ConfigResolver.getConfig();
+        final ConfigResolver.UntypedResolver<String> untypedResolver = config.resolve(key);
+
+        final ConfigResolver.TypedResolver<T> resolver;
+
+        if (ConfigResolver.Converter.class != converterType)
+        {
+            resolver = untypedResolver.as(ipCls, BeanProvider.getContextualReference(converterType));
+        }
+        else if (converterExists(config, ipCls))
+        {
+            resolver = untypedResolver.as(Class.class.cast(ipCls));
+        }
+        else
+        {
+            resolver = untypedResolver.asBean(Class.class.cast(ipCls));
+        }
+
+        resolver.withCurrentProjectStage(projectStageAware);
+
         if (!ConfigProperty.NULL.equals(stringDefault))
         {
             resolver.withStringDefault(stringDefault);
@@ -192,4 +207,20 @@ public abstract class BaseConfigPropertyProducer
         }
         return resolver.evaluateVariables(evaluate);
     }
+
+    /**
+     * TODO we should make Converters for single attributes configurable.
+     * Until then the list is fixed.
+     * See TypedResolverImpl#convert
+     */
+    private boolean converterExists(Config config, Type ipCls)
+    {
+        return (String.class == ipCls ||
+                Integer.class == ipCls ||
+                Boolean.class == ipCls ||
+                Long.class == ipCls ||
+                Float.class == ipCls ||
+                Double.class == ipCls
+                );
+    }
 }
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigPropertyProducer.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigPropertyProducer.java
index 7e7efeb..0a00ece 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigPropertyProducer.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigPropertyProducer.java
@@ -23,10 +23,13 @@ import javax.enterprise.context.Dependent;
 import javax.enterprise.inject.Produces;
 import javax.enterprise.inject.spi.InjectionPoint;
 
+import org.apache.deltaspike.core.api.config.ConfigResolver;
 import org.apache.deltaspike.core.spi.config.BaseConfigPropertyProducer;
 import org.apache.deltaspike.core.api.config.ConfigProperty;
 
+import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.util.function.Supplier;
 
 /**
  * This class contains producer methods for injecting
@@ -92,7 +95,44 @@ public class DefaultConfigPropertyProducer extends BaseConfigPropertyProducer
     public Double produceDoubleConfiguration(InjectionPoint injectionPoint)
     {
         return getPropertyWithException(injectionPoint, Double.class);
+    }
+
+    @Produces
+    @Dependent
+    @ConfigProperty(name = "ignore")
+    public <C> Supplier<C> produceConfigSupplier(InjectionPoint injectionPoint)
+    {
+        ConfigProperty configProperty = getAnnotation(injectionPoint, ConfigProperty.class);
+
+        if (configProperty == null)
+        {
+            throw new IllegalStateException("producer method called without @ConfigProperty being present!");
+        }
+        final Type injectionPointType = injectionPoint.getType();
+        Type ipClass = null;
+        if (injectionPointType instanceof ParameterizedType && ((ParameterizedType) injectionPointType).getActualTypeArguments().length == 1)
+        {
+            ipClass = ((ParameterizedType) injectionPointType).getActualTypeArguments()[0];
+        }
+        else
+        {
+            throw new IllegalStateException("Supplier for Configuration must be a Parameterized Type");
+        }
+
+        ConfigResolver.TypedResolver<C> resolver = asResolver(configProperty.name(),
+            configProperty.defaultValue(),
+            ipClass,
+            configProperty.converter(),
+            configProperty.parameterizedBy(),
+            configProperty.projectStageAware(),
+            configProperty.evaluateVariables());
+
+        if (configProperty.cacheFor() > 0)
+        {
+            resolver.cacheFor(configProperty.cacheUnit(), configProperty.cacheFor());
+        }
 
+        return () -> resolver.getValue();
     }
 
     private <T> T getPropertyWithException(InjectionPoint ip, Type ipCls)
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfo.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfo.java
index 6efb1e7..6aa7b9e 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfo.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfo.java
@@ -1,252 +1,252 @@
-/*
- * 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 javax.management.openmbean.CompositeDataSupport;
-import javax.management.openmbean.CompositeType;
-import javax.management.openmbean.OpenDataException;
-import javax.management.openmbean.OpenType;
-import javax.management.openmbean.SimpleType;
-import javax.management.openmbean.TabularData;
-import javax.management.openmbean.TabularDataSupport;
-import javax.management.openmbean.TabularType;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.deltaspike.core.api.config.ConfigResolver;
-import org.apache.deltaspike.core.spi.config.ConfigSource;
-
-/**
- * JMX MBean for DeltaSpike
- */
-public class DeltaSpikeConfigInfo implements DeltaSpikeConfigInfoMBean
-{
-    private final ClassLoader appConfigClassLoader;
-
-    public DeltaSpikeConfigInfo(ClassLoader appConfigClassLoader)
-    {
-        this.appConfigClassLoader = appConfigClassLoader;
-    }
-
-    @Override
-    public String[] getConfigSourcesAsString()
-    {
-        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
-        try
-        {
-            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
-
-            ConfigSource[] configSources = ConfigResolver.getConfigSources();
-            List<String> configSourceInfo = new ArrayList<String>();
-            for (ConfigSource configSource : configSources)
-            {
-                configSourceInfo.add(Integer.toString(configSource.getOrdinal())
-                    + " - " + configSource.getConfigName());
-            }
-
-            return configSourceInfo.toArray(new String[configSourceInfo.size()]);
-        }
-        finally
-        {
-            // set back the original TCCL
-            Thread.currentThread().setContextClassLoader(originalCl);
-        }
-
-    }
-
-    @Override
-    public String[] getConfigEntriesAsString()
-    {
-        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
-        try
-        {
-            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
-
-            List<ConfigEntry> configEntries = calculateConfigEntries();
-
-            String[] configArray = new String[configEntries.size()];
-
-            for (int i = 0 ; i < configEntries.size(); i++)
-            {
-                ConfigEntry configEntry = configEntries.get(i);
-                configArray[i] = configEntry.getKey() + " = " + configEntry.getValue()
-                    + " - picked up from: " + configEntry.getFromConfigSource();
-            }
-
-            return configArray;
-
-        }
-        finally
-        {
-            // set back the original TCCL
-            Thread.currentThread().setContextClassLoader(originalCl);
-        }
-    }
-
-    @Override
-    public TabularData getConfigEntries()
-    {
-        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
-        try
-        {
-            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
-
-            List<ConfigEntry> configEntries = calculateConfigEntries();
-
-            String[] configArray = new String[configEntries.size()];
-
-            for (int i = 0 ; i < configEntries.size(); i++)
-            {
-                ConfigEntry configEntry = configEntries.get(i);
-                configArray[i] = configEntry.getKey() + " = " + configEntry.getValue()
-                    + " - picked up from: " + configEntry.getFromConfigSource();
-            }
-
-            String typeName = "ConfigEntries";
-            OpenType<?>[] types = new OpenType<?>[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING};
-            String[] keys = new String[]{"Key", "Value", "fromConfigSource"};
-
-            CompositeType ct = new CompositeType(typeName, typeName, keys, keys, types);
-            TabularType type = new TabularType(typeName, typeName, ct, keys);
-            TabularDataSupport configEntryInfo = new TabularDataSupport(type);
-
-            ConfigSource[] configSources = ConfigResolver.getConfigSources();
-            for (ConfigEntry configEntry : configEntries)
-            {
-                configEntryInfo.put(
-                    new CompositeDataSupport(ct, keys,
-                        new Object[]{configEntry.getKey(), configEntry.getValue(), configEntry.getFromConfigSource()}));
-            }
-
-            return configEntryInfo;
-        }
-        catch (OpenDataException e)
-        {
-            throw new RuntimeException(e);
-        }
-        finally
-        {
-            // set back the original TCCL
-            Thread.currentThread().setContextClassLoader(originalCl);
-        }
-    }
-
-    @Override
-    public TabularData getConfigSources()
-    {
-        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
-        try
-        {
-            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
-
-            String typeName = "ConfigSources";
-            OpenType<?>[] types = new OpenType<?>[]{SimpleType.INTEGER, SimpleType.STRING};
-            String[] keys = new String[]{"Ordinal", "ConfigSource"};
-
-            CompositeType ct = new CompositeType(typeName, typeName, keys, keys, types);
-            TabularType type = new TabularType(typeName, typeName, ct, keys);
-            TabularDataSupport configSourceInfo = new TabularDataSupport(type);
-
-            ConfigSource[] configSources = ConfigResolver.getConfigSources();
-            for (ConfigSource configSource : configSources)
-            {
-                configSourceInfo.put(
-                    new CompositeDataSupport(ct, keys,
-                            new Object[]{configSource.getOrdinal(), configSource.getConfigName()}));
-            }
-
-            return configSourceInfo;
-        }
-        catch (OpenDataException e)
-        {
-            throw new RuntimeException(e);
-        }
-        finally
-        {
-            // set back the original TCCL
-            Thread.currentThread().setContextClassLoader(originalCl);
-        }
-    }
-
-    private List<ConfigEntry> calculateConfigEntries()
-    {
-        Map<String, String> allProperties = ConfigResolver.getAllProperties();
-        List<ConfigEntry> configEntries = new ArrayList<ConfigEntry>(allProperties.size());
-        ConfigSource[] configSources = ConfigResolver.getConfigSources();
-
-        for (Map.Entry<String, String> configEntry : allProperties.entrySet())
-        {
-            String key = configEntry.getKey();
-            String value = ConfigResolver.filterConfigValueForLog(key,
-                                    ConfigResolver.getProjectStageAwarePropertyValue(key));
-
-            String fromConfigSource = getFromConfigSource(configSources, key);
-            configEntries.add(new ConfigEntry(key, value, fromConfigSource));
-        }
-
-        return configEntries;
-    }
-
-    private String getFromConfigSource(ConfigSource[] configSources, String key)
-    {
-        for (ConfigSource configSource : configSources)
-        {
-            if (configSource.getPropertyValue(key) != null)
-            {
-                return configSource.getConfigName();
-            }
-        }
-
-        return null;
-    }
-
-
-
-    private class ConfigEntry
-    {
-        private final String key;
-        private final String value;
-        private final String fromConfigSource;
-
-        ConfigEntry(String key, String value, String fromConfigSource)
-        {
-            this.key = key;
-            this.value = value;
-            this.fromConfigSource = fromConfigSource;
-        }
-
-        String getKey()
-        {
-            return key;
-        }
-
-        String getValue()
-        {
-            return value;
-        }
-
-        String getFromConfigSource()
-        {
-            return fromConfigSource;
-        }
-    }
-
-}
+/*
+ * 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 javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+import javax.management.openmbean.TabularType;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.spi.config.ConfigSource;
+
+/**
+ * JMX MBean for DeltaSpike
+ */
+public class DeltaSpikeConfigInfo implements DeltaSpikeConfigInfoMBean
+{
+    private final ClassLoader appConfigClassLoader;
+
+    public DeltaSpikeConfigInfo(ClassLoader appConfigClassLoader)
+    {
+        this.appConfigClassLoader = appConfigClassLoader;
+    }
+
+    @Override
+    public String[] getConfigSourcesAsString()
+    {
+        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
+        try
+        {
+            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
+
+            ConfigSource[] configSources = ConfigResolver.getConfigSources();
+            List<String> configSourceInfo = new ArrayList<String>();
+            for (ConfigSource configSource : configSources)
+            {
+                configSourceInfo.add(Integer.toString(configSource.getOrdinal())
+                    + " - " + configSource.getConfigName());
+            }
+
+            return configSourceInfo.toArray(new String[configSourceInfo.size()]);
+        }
+        finally
+        {
+            // set back the original TCCL
+            Thread.currentThread().setContextClassLoader(originalCl);
+        }
+
+    }
+
+    @Override
+    public String[] getConfigEntriesAsString()
+    {
+        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
+        try
+        {
+            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
+
+            List<ConfigEntry> configEntries = calculateConfigEntries();
+
+            String[] configArray = new String[configEntries.size()];
+
+            for (int i = 0 ; i < configEntries.size(); i++)
+            {
+                ConfigEntry configEntry = configEntries.get(i);
+                configArray[i] = configEntry.getKey() + " = " + configEntry.getValue()
+                    + " - picked up from: " + configEntry.getFromConfigSource();
+            }
+
+            return configArray;
+
+        }
+        finally
+        {
+            // set back the original TCCL
+            Thread.currentThread().setContextClassLoader(originalCl);
+        }
+    }
+
+    @Override
+    public TabularData getConfigEntries()
+    {
+        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
+        try
+        {
+            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
+
+            List<ConfigEntry> configEntries = calculateConfigEntries();
+
+            String[] configArray = new String[configEntries.size()];
+
+            for (int i = 0 ; i < configEntries.size(); i++)
+            {
+                ConfigEntry configEntry = configEntries.get(i);
+                configArray[i] = configEntry.getKey() + " = " + configEntry.getValue()
+                    + " - picked up from: " + configEntry.getFromConfigSource();
+            }
+
+            String typeName = "ConfigEntries";
+            OpenType<?>[] types = new OpenType<?>[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING};
+            String[] keys = new String[]{"Key", "Value", "fromConfigSource"};
+
+            CompositeType ct = new CompositeType(typeName, typeName, keys, keys, types);
+            TabularType type = new TabularType(typeName, typeName, ct, keys);
+            TabularDataSupport configEntryInfo = new TabularDataSupport(type);
+
+            ConfigSource[] configSources = ConfigResolver.getConfigSources();
+            for (ConfigEntry configEntry : configEntries)
+            {
+                configEntryInfo.put(
+                    new CompositeDataSupport(ct, keys,
+                        new Object[]{configEntry.getKey(), configEntry.getValue(), configEntry.getFromConfigSource()}));
+            }
+
+            return configEntryInfo;
+        }
+        catch (OpenDataException e)
+        {
+            throw new RuntimeException(e);
+        }
+        finally
+        {
+            // set back the original TCCL
+            Thread.currentThread().setContextClassLoader(originalCl);
+        }
+    }
+
+    @Override
+    public TabularData getConfigSources()
+    {
+        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
+        try
+        {
+            Thread.currentThread().setContextClassLoader(appConfigClassLoader);
+
+            String typeName = "ConfigSources";
+            OpenType<?>[] types = new OpenType<?>[]{SimpleType.INTEGER, SimpleType.STRING};
+            String[] keys = new String[]{"Ordinal", "ConfigSource"};
+
+            CompositeType ct = new CompositeType(typeName, typeName, keys, keys, types);
+            TabularType type = new TabularType(typeName, typeName, ct, keys);
+            TabularDataSupport configSourceInfo = new TabularDataSupport(type);
+
+            ConfigSource[] configSources = ConfigResolver.getConfigSources();
+            for (ConfigSource configSource : configSources)
+            {
+                configSourceInfo.put(
+                    new CompositeDataSupport(ct, keys,
+                            new Object[]{configSource.getOrdinal(), configSource.getConfigName()}));
+            }
+
+            return configSourceInfo;
+        }
+        catch (OpenDataException e)
+        {
+            throw new RuntimeException(e);
+        }
+        finally
+        {
+            // set back the original TCCL
+            Thread.currentThread().setContextClassLoader(originalCl);
+        }
+    }
+
+    private List<ConfigEntry> calculateConfigEntries()
+    {
+        Map<String, String> allProperties = ConfigResolver.getAllProperties();
+        List<ConfigEntry> configEntries = new ArrayList<ConfigEntry>(allProperties.size());
+        ConfigSource[] configSources = ConfigResolver.getConfigSources();
+
+        for (Map.Entry<String, String> configEntry : allProperties.entrySet())
+        {
+            String key = configEntry.getKey();
+            String value = ConfigResolver.filterConfigValueForLog(key,
+                                    ConfigResolver.getProjectStageAwarePropertyValue(key));
+
+            String fromConfigSource = getFromConfigSource(configSources, key);
+            configEntries.add(new ConfigEntry(key, value, fromConfigSource));
+        }
+
+        return configEntries;
+    }
+
+    private String getFromConfigSource(ConfigSource[] configSources, String key)
+    {
+        for (ConfigSource configSource : configSources)
+        {
+            if (configSource.getPropertyValue(key) != null)
+            {
+                return configSource.getConfigName();
+            }
+        }
+
+        return null;
+    }
+
+
+
+    private class ConfigEntry
+    {
+        private final String key;
+        private final String value;
+        private final String fromConfigSource;
+
+        ConfigEntry(String key, String value, String fromConfigSource)
+        {
+            this.key = key;
+            this.value = value;
+            this.fromConfigSource = fromConfigSource;
+        }
+
+        String getKey()
+        {
+            return key;
+        }
+
+        String getValue()
+        {
+            return value;
+        }
+
+        String getFromConfigSource()
+        {
+            return fromConfigSource;
+        }
+    }
+
+}
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfoMBean.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfoMBean.java
index d2d154f..277d66e 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfoMBean.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfoMBean.java
@@ -1,33 +1,33 @@
-/*
- * 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 javax.management.openmbean.TabularData;
-
-public interface DeltaSpikeConfigInfoMBean
-{
-    String[] getConfigSourcesAsString();
-
-    TabularData getConfigSources();
-
-    String[] getConfigEntriesAsString();
-
-    TabularData getConfigEntries();
-
-}
+/*
+ * 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 javax.management.openmbean.TabularData;
+
+public interface DeltaSpikeConfigInfoMBean
+{
+    String[] getConfigSourcesAsString();
+
+    TabularData getConfigSources();
+
+    String[] getConfigEntriesAsString();
+
+    TabularData getConfigEntries();
+
+}
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java
index de501f0..9aa3e3d 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java
@@ -33,7 +33,7 @@ import org.apache.deltaspike.core.api.config.Config;
  */
 public final class BeanConverterFactory
 {
-    private final ConcurrentMap<Class<?>, BiFunction<Config, String, ?>> BEAN_CONVERTER = new ConcurrentHashMap<>();
+    private final ConcurrentMap<Class<?>, BiFunction<Config, String, ?>> beanConverters = new ConcurrentHashMap<>();
 
     /**
      * Determine the bean converter function to be used according to the rules defined in
@@ -41,7 +41,7 @@ public final class BeanConverterFactory
      */
     public <N> BiFunction<Config, String, N> detectConverter(Class<N> clazz)
     {
-        BiFunction<Config, String, N> beanConverter = (BiFunction<Config, String, N>) BEAN_CONVERTER.get(clazz);
+        BiFunction<Config, String, N> beanConverter = (BiFunction<Config, String, N>) beanConverters.get(clazz);
         if (beanConverter == null)
         {
             // class with public param ct
@@ -62,7 +62,7 @@ public final class BeanConverterFactory
                 beanConverter = new CtInjectionBeanConverter(clazz, paramConstructors.get(0));
             }
 
-            BEAN_CONVERTER.putIfAbsent(clazz, beanConverter);
+            beanConverters.putIfAbsent(clazz, beanConverter);
         }
 
         return beanConverter;
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java
index 2b1899a..d038d62 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java
@@ -21,8 +21,9 @@ package org.apache.deltaspike.test.core.api.config;
 import java.util.function.BiFunction;
 
 import org.apache.deltaspike.core.api.config.Config;
-import org.apache.deltaspike.core.api.config.ConfigProperty;
 import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.test.core.api.config.beans.ServerEndpointPojoWithCt;
+import org.apache.deltaspike.test.core.api.config.beans.ServerEndpointPojoWithFields;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -70,38 +71,4 @@ public class BeanConfigResolverTest
     }
 
 
-    public static class ServerEndpointPojoWithCt extends ServerEndpointPojoWithFields
-    {
-        public ServerEndpointPojoWithCt(@ConfigProperty(name = "host") String host,
-                                        @ConfigProperty(name = "port") Integer port,
-                                        @ConfigProperty(name = "path") String path)
-        {
-            this.host = host;
-            this.port = port;
-            this.path = path;
-        }
-
-    }
-
-    public static class ServerEndpointPojoWithFields
-    {
-        protected String host;
-        protected Integer port;
-        protected String path;
-
-        @Override
-        public String toString()
-        {
-            StringBuilder sb = new StringBuilder();
-            if (host == null)
-            {
-                return "unknown";
-            }
-            sb.append((port != null && port.equals(443)) ? "https://" : "http://");
-            sb.append(host);
-            sb.append(port == null ? ":80" : ":" + port);
-            sb.append(path != null ? path : "");
-            return sb.toString();
-        }
-    }
 }
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfoMBean.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/beans/ServerEndpointPojoWithCt.java
similarity index 57%
copy from deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfoMBean.java
copy to deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/beans/ServerEndpointPojoWithCt.java
index d2d154f..2057124 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigInfoMBean.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/beans/ServerEndpointPojoWithCt.java
@@ -1,33 +1,37 @@
-/*
- * 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 javax.management.openmbean.TabularData;
-
-public interface DeltaSpikeConfigInfoMBean
-{
-    String[] getConfigSourcesAsString();
-
-    TabularData getConfigSources();
-
-    String[] getConfigEntriesAsString();
-
-    TabularData getConfigEntries();
-
-}
+/*
+ * 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.beans;
+
+import org.apache.deltaspike.core.api.config.ConfigProperty;
+
+/**
+ * @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
+ */
+public class ServerEndpointPojoWithCt extends ServerEndpointPojoWithFields
+{
+    public ServerEndpointPojoWithCt(@ConfigProperty(name = "host") String host,
+                                    @ConfigProperty(name = "port") Integer port,
+                                    @ConfigProperty(name = "path") String path)
+    {
+        this.host = host;
+        this.port = port;
+        this.path = path;
+    }
+
+}
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/beans/ServerEndpointPojoWithFields.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/beans/ServerEndpointPojoWithFields.java
new file mode 100644
index 0000000..0f7ec05
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/beans/ServerEndpointPojoWithFields.java
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+/*
+ * 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.beans;
+
+/**
+ * @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
+ */
+public class ServerEndpointPojoWithFields
+{
+    protected String host;
+    protected Integer port;
+    protected String path;
+
+    @Override
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        if (host == null)
+        {
+            return "unknown";
+        }
+        sb.append((port != null && port.equals(443)) ? "https://" : "http://");
+        sb.append(host);
+        sb.append(port == null ? ":80" : ":" + port);
+        sb.append(path != null ? path : "");
+        return sb.toString();
+    }
+}
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java
index 2b4c8be..f7f27c0 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java
@@ -18,12 +18,17 @@
  */
 package org.apache.deltaspike.test.core.api.config.injectable;
 
+import javax.inject.Inject;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.HashSet;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
 
+import org.apache.deltaspike.core.api.config.ConfigProperty;
 import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.core.api.config.beans.ServerEndpointPojoWithCt;
 import org.apache.deltaspike.test.util.ArchiveUtils;
 import org.apache.deltaspike.test.util.FileUtils;
 import org.jboss.arquillian.container.test.api.Deployment;
@@ -32,6 +37,7 @@ import org.jboss.shrinkwrap.api.ShrinkWrap;
 import org.jboss.shrinkwrap.api.asset.EmptyAsset;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
 import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Assert;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
@@ -41,6 +47,7 @@ import org.apache.deltaspike.test.core.api.config.injectable.numberconfig.Number
 import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
 
@@ -59,6 +66,7 @@ public class InjectableConfigPropertyTest
 
         JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "injectableConfigPropertyTest.jar")
                 .addPackage(SettingsBean.class.getPackage())
+                .addPackage(ServerEndpointPojoWithCt.class.getPackage())
                 .addPackage(NumberConfiguredBean.class.getPackage())
                 .addAsManifestResource(FileUtils.getFileForURL(fileUrl.toString()))
                 .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
@@ -69,6 +77,30 @@ public class InjectableConfigPropertyTest
                 .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
     }
 
+    @ConfigProperty(name = "myapp.some.server", cacheFor = 2, cacheUnit = TimeUnit.SECONDS)
+    private @Inject Supplier<ServerEndpointPojoWithCt> someServer;
+
+    @ConfigProperty(name = "myapp.other.server")
+    private @Inject Supplier<ServerEndpointPojoWithCt> otherServer;
+
+
+    @Test
+    public void injectBeanViaProvider() {
+        assertNotNull(someServer);
+        final ServerEndpointPojoWithCt server1 = someServer.get();
+        assertNotNull(server1);
+        Assert.assertEquals("http://myserver:80/myapp/endpoint1", server1.toString());
+
+        // once again
+        final ServerEndpointPojoWithCt server2 = someServer.get();
+        assertNotNull(server2);
+        Assert.assertEquals("http://myserver:80/myapp/endpoint1", server2.toString());
+        Assert.assertTrue(server1 == server2);
+
+        assertNotNull(otherServer.get());
+        Assert.assertEquals("https://otherserver:443/otherapp/endpoint2", otherServer.get().toString());
+    }
+
     @Test
     public void injectionViaConfigProperty()
     {
@@ -80,8 +112,12 @@ public class InjectableConfigPropertyTest
 
         // also check the ones with defaultValue
         assertEquals("14", settingsBean.getProperty3Filled());
+
         assertEquals("myDefaultValue", settingsBean.getProperty3Defaulted());
+        assertEquals("myDefaultValue", settingsBean.getSupplierStringProperty3Defaulted().get());
+
         assertEquals(42, settingsBean.getProperty4Defaulted());
+        assertEquals(Integer.valueOf(42), settingsBean.getSupplierIntProperty4Defaulted().get());
 
         assertEquals("some setting for prodDB", settingsBean.getDbConfig());
     }
@@ -103,7 +139,7 @@ public class InjectableConfigPropertyTest
     }
 
     @Test
-    public void testNmberConfigInjection()
+    public void testNumberConfigInjection()
     {
         NumberConfiguredBean numberBean = BeanProvider.getContextualReference(NumberConfiguredBean.class, false);
         assertNull(numberBean.getPropertyNonexisting());
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/SettingsBean.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/SettingsBean.java
index cd582d2..55da26f 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/SettingsBean.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/SettingsBean.java
@@ -27,6 +27,8 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
 
 @ApplicationScoped
 public class SettingsBean
@@ -51,6 +53,14 @@ public class SettingsBean
     @ConfigProperty(name = "nonexistingProperty", defaultValue = "42")
     private Integer intProperty4Defaulted;
 
+    @Inject
+    @ConfigProperty(name = "nonexistingProperty", defaultValue = "myDefaultValue", cacheFor = 10, cacheUnit = TimeUnit.MINUTES)
+    private Supplier<String> supplierStringProperty3Defaulted;
+
+    @Inject
+    @ConfigProperty(name = "nonexistingProperty", defaultValue = "42")
+    private Supplier<Integer> supplierIntProperty4Defaulted;
+
     private Long inverseProperty2;
 
     @Inject
@@ -159,6 +169,15 @@ public class SettingsBean
         return intProperty4Defaulted;
     }
 
+    public Supplier<String> getSupplierStringProperty3Defaulted()
+    {
+        return supplierStringProperty3Defaulted;
+    }
+
+    public Supplier<Integer> getSupplierIntProperty4Defaulted()
+    {
+        return supplierIntProperty4Defaulted;
+    }
 
     public Boolean getBooleanPropertyNull()
     {

[deltaspike] 01/03: DELTASPIKE-1444 add POJO based Config

Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/deltaspike.git

commit facacde41ded50f3f98499354680356283fb3a99
Author: Mark Struberg <st...@apache.org>
AuthorDate: Tue Dec 7 21:24:56 2021 +0100

    DELTASPIKE-1444 add POJO based Config
---
 .../main/resources/deltaspike/default-checks.xml   |   2 +-
 .../deltaspike/core/api/config/ConfigResolver.java |  42 ++++++++
 .../deltaspike/core/impl/config/ConfigImpl.java    |   8 +-
 .../core/impl/config/TypedResolverImpl.java        |  90 ++++++++++++-----
 .../core/impl/config/converter/BeanConverter.java  |  76 +++++++++++++++
 .../config/converter/CtInjectionBeanConverter.java |  87 +++++++++++++++++
 .../converter/FieldInjectionBeanConverter.java     |  88 +++++++++++++++++
 .../core/api/config/BeanConfigResolverTest.java    | 107 +++++++++++++++++++++
 .../test/core/api/config/ConfigResolverTest.java   |   3 +
 .../META-INF/apache-deltaspike.properties          |   8 ++
 10 files changed, 485 insertions(+), 26 deletions(-)

diff --git a/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml b/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
index 93e8d8f..219b706 100644
--- a/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
+++ b/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
@@ -114,7 +114,7 @@
 
 
         <module name="LineLength">
-          <property name="max" value="120" />
+          <property name="max" value="150" />
           <property name="ignorePattern" value="@version|@see"/>
         </module>
         <module name="MethodLength">
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 50c4ce3..e552405 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
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.ServiceLoader;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
+import java.util.function.BiFunction;
 
 import javax.enterprise.inject.Typed;
 
@@ -596,6 +597,47 @@ public final class ConfigResolver
          */
         <N> TypedResolver<N> as(Class<N> clazz, Converter<N> converter);
 
+        /**
+         * Use this method if the target type consists of information taken from multiple config keys.
+         * It is similar to {@link #asBean(Class, BiFunction)} but automatically chooses a converter which fits.
+         * The following Beans are supported:
+         * <ul>
+         *     <li>POJO with Constructor parameters. In this case this constructor will be used</li>
+         *     <li>Java14 Records</li>
+         *     <li>POJO with default ct and fields.
+         *          Those fields can use the {@link ConfigProperty} annotation on the fields for more control</li>
+         * </ul>
+         * @param clazz
+         * @param <N>
+         * @return
+         */
+        <N> TypedResolver<N> asBean(Class<N> clazz);
+
+        /**
+         * Set the required Target type and use a converter function which has access to multiple attributes in the configuration.
+         * Use this method if the target type consists of information taken from multiple config keys.
+         * If you consider the following type:
+         * <pre>
+         *  public record ServerEndpoint(String host, Integer port, String path)
+         *
+         *  config.properties:
+         *  myapp.some.server.host=myserver
+         *  myapp.some.server.port=80
+         *  myapp.some.server.path=/myapp/endpoint1
+         *  myapp.other.server.host=otherserver
+         *  myapp.other.server.port=443
+         *  myapp.other.server.path=/otherapp/endpoint2
+         * </pre>
+         *
+         * This function will have access to all the underlying properties by using the given Config.
+         * When resolving the target type the system makes sure that access to all requested properties is atomic.
+         *
+         * @param clazz
+         * @param beanConverter function which gets a starting point (prefix) propertyPath in the configuration.
+         *                      In the examples above it is e.g. "myapp.some.server." and "myapp.other.server."
+         * @return This builder as a TypedResolver
+         */
+        <N> TypedResolver<N> asBean(Class<N> clazz, BiFunction<Config /*this config*/, String /* propertyPath */, N> beanConverter);
     }
 
     /**
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 2e7c48c..93729d3 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
@@ -42,8 +42,14 @@ import java.util.logging.Logger;
  */
 public class ConfigImpl implements Config
 {
+    /**
+     * How many times should we at max retry to get multiple attributes in an atomic way.
+     */
+    public static final int MAX_CONFIG_RETRIES = 5;
+
     private static final Logger LOG = Logger.getLogger(ConfigImpl.class.getName());
 
+
     private final ClassLoader classLoader;
 
     private ConfigSource[] configSources;
@@ -135,7 +141,7 @@ public class ConfigImpl implements Config
         // 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++)
+        for (int tries = 1; tries < MAX_CONFIG_RETRIES; tries++)
         {
             Map<ConfigResolver.TypedResolver<?>, Object> configValues = new HashMap<>();
             long startReadLastChanged = lastChanged;
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 8997a1e..c4642b9 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
@@ -18,9 +18,11 @@
  */
 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.ConfigSnapshot;
 import org.apache.deltaspike.core.api.projectstage.ProjectStage;
+import org.apache.deltaspike.core.impl.config.converter.BeanConverter;
 import org.apache.deltaspike.core.spi.config.ConfigSource;
 import org.apache.deltaspike.core.util.ClassUtils;
 import org.apache.deltaspike.core.util.ExceptionUtils;
@@ -31,11 +33,11 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
+import java.util.function.BiFunction;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 
-
 public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
 {
     private static final Logger LOG = Logger.getLogger(TypedResolverImpl.class.getName());
@@ -74,6 +76,7 @@ public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
     private long lastReloadedAt = -1;
 
     private T lastValue = null;
+    private BiFunction<Config, String, ?> beanConverter = null;
 
 
     TypedResolverImpl(ConfigImpl config, String propertyName)
@@ -127,6 +130,20 @@ public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
     }
 
     @Override
+    public <N> ConfigResolver.TypedResolver<N> asBean(Class<N> clazz)
+    {
+        return asBean(clazz, BeanConverter.detectConverter(clazz));
+    }
+
+    @Override
+    public <N> ConfigResolver.TypedResolver<N> asBean(Class<N> clazz, BiFunction<Config, String, N> beanConverter)
+    {
+        configEntryType = clazz;
+        this.beanConverter = beanConverter;
+        return (ConfigResolver.TypedResolver<N>) this;
+    }
+
+    @Override
     public ConfigResolver.TypedResolver<T> withDefault(T value)
     {
         defaultValue = value;
@@ -218,6 +235,7 @@ public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
         return this;
     }
 
+
     @Override
     public T getValue(ConfigSnapshot snapshot)
     {
@@ -250,41 +268,49 @@ public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
             }
         }
 
-        String valueStr = resolveStringValue();
         T value;
-        if (isList)
+        if (beanConverter != null)
         {
-            value = splitAndConvertListValue(valueStr);
+            value = getValueByBeanConverter(beanConverter);
         }
         else
         {
-            value = convert(valueStr);
-        }
-
-        if (withDefault)
-        {
-            ConfigResolverContext configResolverContext = new ConfigResolverContext()
-                    .setEvaluateVariables(evaluateVariables)
-                    .setProjectStageAware(projectStageAware);
-            value = fallbackToDefaultIfEmpty(keyResolved, value, defaultValue, configResolverContext);
-            if (isList && String.class.isInstance(value))
+            String valueStr = resolveStringValue();
+            if (isList)
             {
-                value = splitAndConvertListValue(String.class.cast(value));
+                value = splitAndConvertListValue(valueStr);
+            }
+            else
+            {
+                value = convert(valueStr);
             }
-        }
 
-        if ((logChanges || valueChangedCallback != null)
-            && (value != null && !value.equals(lastValue) || (value == null && lastValue != null)))
-        {
-            if (logChanges)
+            if (withDefault)
             {
-                LOG.log(Level.INFO, "New value {0} for key {1}.",
-                    new Object[]{ConfigResolver.filterConfigValueForLog(keyOriginal, valueStr), keyOriginal});
+                ConfigResolverContext configResolverContext = new ConfigResolverContext()
+                    .setEvaluateVariables(evaluateVariables)
+                    .setProjectStageAware(projectStageAware);
+
+                value = fallbackToDefaultIfEmpty(keyResolved, value, defaultValue, configResolverContext);
+                if (isList && String.class.isInstance(value))
+                {
+                    value = splitAndConvertListValue(String.class.cast(value));
+                }
             }
 
-            if (valueChangedCallback != null)
+            if ((logChanges || valueChangedCallback != null)
+                && (value != null && !value.equals(lastValue) || (value == null && lastValue != null)))
             {
-                valueChangedCallback.onValueChange(keyOriginal, lastValue, value);
+                if (logChanges)
+                {
+                    LOG.log(Level.INFO, "New value {0} for key {1}.",
+                        new Object[]{ConfigResolver.filterConfigValueForLog(keyOriginal, valueStr), keyOriginal});
+                }
+
+                if (valueChangedCallback != null)
+                {
+                    valueChangedCallback.onValueChange(keyOriginal, lastValue, value);
+                }
             }
         }
 
@@ -299,6 +325,22 @@ public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
         return value;
     }
 
+    private T getValueByBeanConverter(BiFunction<Config, String, ?> beanConverter)
+    {
+        T value;
+        for (int tries = 1; tries < ConfigImpl.MAX_CONFIG_RETRIES; tries++)
+        {
+            long startReadLastChanged = config.getLastChanged();
+            value = (T) beanConverter.apply(config, keyOriginal + ".");
+            if (startReadLastChanged == config.getLastChanged())
+            {
+                return value;
+            }
+        }
+        throw new IllegalStateException(
+            "Could not resolve ConfigTransaction as underlying values are permanently changing!");
+    }
+
     private T splitAndConvertListValue(String valueStr)
     {
         if (valueStr == null)
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverter.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverter.java
new file mode 100644
index 0000000..6efb3d1
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverter.java
@@ -0,0 +1,76 @@
+/*
+ * 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.converter;
+
+import java.lang.reflect.Constructor;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.function.BiFunction;
+import java.util.stream.Collectors;
+
+import org.apache.deltaspike.core.api.config.Config;
+
+/**
+ * @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
+ */
+public final class BeanConverter
+{
+    private static final ConcurrentMap<Class<?>, BiFunction<Config, String, ?>> BEAN_CONVERTER = new ConcurrentHashMap<>();
+
+    private BeanConverter()
+    {
+        // private utility class ct
+    }
+
+    /**
+     * Determine the bean converter function to be used according to the rules defined in
+     * {@link org.apache.deltaspike.core.api.config.ConfigResolver.UntypedResolver#asBean(Class)}
+     */
+    public static <N> BiFunction<Config, String, N> detectConverter(Class<N> clazz)
+    {
+        BiFunction<Config, String, N> beanConverter = (BiFunction<Config, String, N>) BEAN_CONVERTER.get(clazz);
+        if (beanConverter == null)
+        {
+            // class with public param ct
+            final List<Constructor<?>> paramConstructors = Arrays.stream(clazz.getConstructors())
+                .filter(ct -> ct.getParameterTypes().length > 0)
+                .collect(Collectors.toList());
+            if (paramConstructors.size() > 1)
+            {
+                throw new IllegalStateException("Cannot handle beans with multiple non-default ct");
+            }
+            if (paramConstructors.size() == 0)
+            {
+                // use field config injection
+                beanConverter = new FieldInjectionBeanConverter(clazz);
+            }
+            else
+            {
+                beanConverter = new CtInjectionBeanConverter(clazz, paramConstructors.get(0));
+            }
+
+            BEAN_CONVERTER.putIfAbsent(clazz, beanConverter);
+        }
+
+        return beanConverter;
+    }
+
+}
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/CtInjectionBeanConverter.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/CtInjectionBeanConverter.java
new file mode 100644
index 0000000..5bff8fb
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/CtInjectionBeanConverter.java
@@ -0,0 +1,87 @@
+/*
+ * 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.converter;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Parameter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+
+import org.apache.deltaspike.core.api.config.Config;
+import org.apache.deltaspike.core.api.config.ConfigProperty;
+import org.apache.deltaspike.core.util.ExceptionUtils;
+
+/**
+ * @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
+ */
+public class CtInjectionBeanConverter<N> implements BiFunction<Config, String, N>
+{
+    private final Constructor<?> constructor;
+
+    public <N> CtInjectionBeanConverter(Class<N> clazz, Constructor<?> constructor)
+    {
+        this.constructor = constructor;
+    }
+
+    @Override
+    public N apply(Config config, String path)
+    {
+        List<Object> params = new ArrayList<>();
+        for (int i = 0; i < constructor.getParameters().length; i++)
+        {
+            Parameter p = constructor.getParameters()[i];
+            String paramName;
+            final ConfigProperty configProperty = p.getAnnotation(ConfigProperty.class);
+            if (configProperty != null)
+            {
+                paramName = configProperty.name();
+            }
+            else
+            {
+                paramName = p.getName();
+                if (paramName.equals("arg" + i))
+                {
+                    throw new IllegalStateException("Config POJO constructor pareameters must be annotated with @ConfigProperty if the " +
+                        "class is not compiled with the javac -parameters option!");
+                }
+            }
+
+            params.add(config.resolve(path + paramName)
+                .as(p.getType())
+                .getValue());
+        }
+
+        if (params.stream().allMatch(p -> p == null))
+        {
+            return null;
+        }
+
+        try
+        {
+            return (N) constructor.newInstance(params.toArray(new Object[params.size()]));
+        }
+        catch (InstantiationException | IllegalAccessException | InvocationTargetException  e)
+        {
+            throw ExceptionUtils.throwAsRuntimeException(e);
+        }
+    }
+}
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/FieldInjectionBeanConverter.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/FieldInjectionBeanConverter.java
new file mode 100644
index 0000000..4a62de4
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/FieldInjectionBeanConverter.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.core.impl.config.converter;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.BiFunction;
+
+import org.apache.deltaspike.core.api.config.Config;
+import org.apache.deltaspike.core.api.config.ConfigProperty;
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.util.ExceptionUtils;
+
+/**
+ * @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
+ */
+public class FieldInjectionBeanConverter<N> implements BiFunction<Config, String, N>
+{
+    private final Class<?> clazz;
+    private final List<Field> fields;
+
+    public <N> FieldInjectionBeanConverter(Class<N> clazz)
+    {
+        this.clazz = clazz;
+        this.fields = collectFields(clazz);
+    }
+
+    private <N> List<Field> collectFields(Class<N> clazz)
+    {
+        List<Field> fields = new ArrayList<>();
+        fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
+        if (clazz.getSuperclass() != Object.class)
+        {
+            fields.addAll(collectFields(clazz.getSuperclass()));
+        }
+        return fields;
+    }
+
+    @Override
+    public N apply(Config config, String path)
+    {
+        try
+        {
+            final Object o = clazz.getDeclaredConstructor().newInstance();
+            for (Field field : fields)
+            {
+                final ConfigProperty configProperty = field.getAnnotation(ConfigProperty.class);
+                String name = configProperty != null ? configProperty.name() : field.getName();
+                final ConfigResolver.UntypedResolver<String> resolver = config.resolve(path + name);
+                if (field.getType() != String.class)
+                {
+                    resolver.as(field.getType());
+                }
+                resolver.evaluateVariables(configProperty != null ? configProperty.evaluateVariables() :  true);
+                resolver.withCurrentProjectStage(configProperty != null ? configProperty.projectStageAware() :  true);
+                if (!field.isAccessible())
+                {
+                    field.setAccessible(true);
+                }
+                field.set(o, resolver.getValue());
+            }
+            return (N) o;
+        }
+        catch (Exception e)
+        {
+            throw ExceptionUtils.throwAsRuntimeException(e);
+        }
+    }
+}
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java
new file mode 100644
index 0000000..2b1899a
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/BeanConfigResolverTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.function.BiFunction;
+
+import org.apache.deltaspike.core.api.config.Config;
+import org.apache.deltaspike.core.api.config.ConfigProperty;
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class BeanConfigResolverTest
+{
+    @Test
+    public void testBeanConverterConfig()
+    {
+        final BiFunction<Config, String, ServerEndpointPojoWithCt> serverBeanConverter = (cfg, path) -> new ServerEndpointPojoWithCt(
+            cfg.resolve(path + "host").getValue(),
+            cfg.resolve(path + "port").as(Integer.class).getValue(),
+            cfg.resolve(path + "path").getValue());
+
+        final ServerEndpointPojoWithCt someServer = ConfigResolver.resolve("myapp.some.server")
+            .asBean(ServerEndpointPojoWithCt.class, serverBeanConverter)
+            .getValue();
+        Assert.assertNotNull(someServer);
+        Assert.assertEquals("http://myserver:80/myapp/endpoint1", someServer.toString());
+
+        final ServerEndpointPojoWithCt otherServer = ConfigResolver.resolve("myapp.other.server")
+            .asBean(ServerEndpointPojoWithCt.class, serverBeanConverter)
+            .getValue();
+        Assert.assertNotNull(otherServer);
+        Assert.assertEquals("https://otherserver:443/otherapp/endpoint2", otherServer.toString());
+    }
+
+    @Test
+    public void testConfigBeanWithCt()
+    {
+        final ServerEndpointPojoWithCt someServer = ConfigResolver.resolve("myapp.some.server")
+            .asBean(ServerEndpointPojoWithCt.class)
+            .getValue();
+        Assert.assertNotNull(someServer);
+        Assert.assertEquals("http://myserver:80/myapp/endpoint1", someServer.toString());
+    }
+
+    @Test
+    public void testConfigBeanWithFields()
+    {
+        final ServerEndpointPojoWithFields someServer = ConfigResolver.resolve("myapp.some.server")
+            .asBean(ServerEndpointPojoWithFields.class)
+            .getValue();
+        Assert.assertNotNull(someServer);
+        Assert.assertEquals("http://myserver:80/myapp/endpoint1", someServer.toString());
+    }
+
+
+    public static class ServerEndpointPojoWithCt extends ServerEndpointPojoWithFields
+    {
+        public ServerEndpointPojoWithCt(@ConfigProperty(name = "host") String host,
+                                        @ConfigProperty(name = "port") Integer port,
+                                        @ConfigProperty(name = "path") String path)
+        {
+            this.host = host;
+            this.port = port;
+            this.path = path;
+        }
+
+    }
+
+    public static class ServerEndpointPojoWithFields
+    {
+        protected String host;
+        protected Integer port;
+        protected String path;
+
+        @Override
+        public String toString()
+        {
+            StringBuilder sb = new StringBuilder();
+            if (host == null)
+            {
+                return "unknown";
+            }
+            sb.append((port != null && port.equals(443)) ? "https://" : "http://");
+            sb.append(host);
+            sb.append(port == null ? ":80" : ":" + port);
+            sb.append(path != null ? path : "");
+            return sb.toString();
+        }
+    }
+}
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigResolverTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigResolverTest.java
index 7bf004a..b580e59 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigResolverTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigResolverTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.deltaspike.test.core.api.config;
 
+import org.apache.deltaspike.core.api.config.Config;
+import org.apache.deltaspike.core.api.config.ConfigProperty;
 import org.apache.deltaspike.core.api.config.ConfigResolver;
 import org.apache.deltaspike.core.api.projectstage.ProjectStage;
 import org.apache.deltaspike.core.spi.config.ConfigFilter;
@@ -31,6 +33,7 @@ import org.junit.Test;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.BiFunction;
 
 @SuppressWarnings("Duplicates")
 public class ConfigResolverTest
diff --git a/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties b/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
index b098884..8e7c7b3 100644
--- a/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
+++ b/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
@@ -64,3 +64,11 @@ myapp.login.hostname.Production=https://myapp
 myapp.login.hostname.UnitTest=https://myapp
 myapp.login.url=${myapp.login.hostname}/login.xhtml
 
+# tree based configuration
+myapp.some.server.host=myserver
+myapp.some.server.port=80
+myapp.some.server.path=/myapp/endpoint1
+myapp.other.server.host=otherserver
+myapp.other.server.port=443
+myapp.other.server.path=/otherapp/endpoint2
+

[deltaspike] 02/03: DELTASPIKE-1444 make BeanConverter a non-static factory

Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/deltaspike.git

commit 422a78a3c1c25b8040fa4516dfa080da0baa7ed8
Author: Mark Struberg <st...@apache.org>
AuthorDate: Wed Dec 8 17:48:43 2021 +0100

    DELTASPIKE-1444 make BeanConverter a non-static factory
    
    otherwise we might create mem leaks, as rightfully noted by rmannibucau
---
 .../org/apache/deltaspike/core/impl/config/ConfigImpl.java  |  9 +++++++++
 .../deltaspike/core/impl/config/TypedResolverImpl.java      |  3 +--
 .../{BeanConverter.java => BeanConverterFactory.java}       | 13 ++++---------
 3 files changed, 14 insertions(+), 11 deletions(-)

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 93729d3..9b883d1 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
@@ -21,6 +21,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.ConfigSnapshot;
+import org.apache.deltaspike.core.impl.config.converter.BeanConverterFactory;
 import org.apache.deltaspike.core.spi.config.ConfigFilter;
 import org.apache.deltaspike.core.spi.config.ConfigSource;
 import org.apache.deltaspike.core.spi.config.ConfigSourceProvider;
@@ -58,9 +59,12 @@ public class ConfigImpl implements Config
     // volatile to a.) make the read/write behave atomic and b.) guarantee multi-thread safety
     private volatile long lastChanged = 0;
 
+    private BeanConverterFactory beanConverter;
+
     public ConfigImpl(ClassLoader classLoader)
     {
         this.classLoader = classLoader;
+        this.beanConverter = new BeanConverterFactory();
     }
 
     /**
@@ -135,6 +139,11 @@ public class ConfigImpl implements Config
         return configSources;
     }
 
+    public BeanConverterFactory getBeanConverter()
+    {
+        return beanConverter;
+    }
+
     @Override
     public ConfigSnapshot snapshotFor(ConfigResolver.TypedResolver<?>... typedResolvers)
     {
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 c4642b9..3f7441e 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
@@ -22,7 +22,6 @@ 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.apache.deltaspike.core.api.projectstage.ProjectStage;
-import org.apache.deltaspike.core.impl.config.converter.BeanConverter;
 import org.apache.deltaspike.core.spi.config.ConfigSource;
 import org.apache.deltaspike.core.util.ClassUtils;
 import org.apache.deltaspike.core.util.ExceptionUtils;
@@ -132,7 +131,7 @@ public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
     @Override
     public <N> ConfigResolver.TypedResolver<N> asBean(Class<N> clazz)
     {
-        return asBean(clazz, BeanConverter.detectConverter(clazz));
+        return asBean(clazz, config.getBeanConverter().detectConverter(clazz));
     }
 
     @Override
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverter.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java
similarity index 86%
rename from deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverter.java
rename to deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java
index 6efb3d1..de501f0 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverter.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/converter/BeanConverterFactory.java
@@ -29,22 +29,17 @@ import java.util.stream.Collectors;
 import org.apache.deltaspike.core.api.config.Config;
 
 /**
- * @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
+ * A factory for bean converters.
  */
-public final class BeanConverter
+public final class BeanConverterFactory
 {
-    private static final ConcurrentMap<Class<?>, BiFunction<Config, String, ?>> BEAN_CONVERTER = new ConcurrentHashMap<>();
-
-    private BeanConverter()
-    {
-        // private utility class ct
-    }
+    private final ConcurrentMap<Class<?>, BiFunction<Config, String, ?>> BEAN_CONVERTER = new ConcurrentHashMap<>();
 
     /**
      * Determine the bean converter function to be used according to the rules defined in
      * {@link org.apache.deltaspike.core.api.config.ConfigResolver.UntypedResolver#asBean(Class)}
      */
-    public static <N> BiFunction<Config, String, N> detectConverter(Class<N> clazz)
+    public <N> BiFunction<Config, String, N> detectConverter(Class<N> clazz)
     {
         BiFunction<Config, String, N> beanConverter = (BiFunction<Config, String, N>) BEAN_CONVERTER.get(clazz);
         if (beanConverter == null)