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 2017/11/17 21:08:33 UTC
svn commit: r1815628 - in /geronimo/components/config/trunk:
impl/debug-suite.xml
impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java
pom.xml
Author: struberg
Date: Fri Nov 17 21:08:32 2017
New Revision: 1815628
URL: http://svn.apache.org/viewvc?rev=1815628&view=rev
Log:
GERONIMO-6595 support implic converters
as defined in the mp-config 1.2 spec
Added:
geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java (with props)
Modified:
geronimo/components/config/trunk/impl/debug-suite.xml
geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
geronimo/components/config/trunk/pom.xml
Modified: geronimo/components/config/trunk/impl/debug-suite.xml
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/debug-suite.xml?rev=1815628&r1=1815627&r2=1815628&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/debug-suite.xml (original)
+++ geronimo/components/config/trunk/impl/debug-suite.xml Fri Nov 17 21:08:32 2017
@@ -24,7 +24,7 @@
<classes>
<!-- Issues in the spec -->
<!-- CDI-437 -->
- <class name="org.eclipse.microprofile.config.tck.CDIPlainInjectionTest">
+ <class name="org.eclipse.microprofile.config.tck.AutoDiscoveredConfigSourceTest">
<methods>
<include name=".*"/>
</methods>
Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java?rev=1815628&r1=1815627&r2=1815628&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java (original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java Fri Nov 17 21:08:32 2017
@@ -33,6 +33,7 @@ import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -41,6 +42,7 @@ import org.apache.geronimo.config.conver
import org.apache.geronimo.config.converters.DoubleConverter;
import org.apache.geronimo.config.converters.DurationConverter;
import org.apache.geronimo.config.converters.FloatConverter;
+import org.apache.geronimo.config.converters.ImplicitConverter;
import org.apache.geronimo.config.converters.InstantConverter;
import org.apache.geronimo.config.converters.IntegerConverter;
import org.apache.geronimo.config.converters.LocalDateConverter;
@@ -70,6 +72,7 @@ public class ConfigImpl implements Confi
protected List<ConfigSource> configSources = new ArrayList<>();
protected Map<Type, Converter> converters = new HashMap<>();
+ protected Map<Type, Converter> implicitConverters = new ConcurrentHashMap<>();
public ConfigImpl() {
@@ -150,10 +153,30 @@ public class ConfigImpl implements Confi
private <T> Converter getConverter(Class<T> asType) {
Converter converter = converters.get(asType);
if (converter == null) {
+ converter = getImplicitConverter(asType);
+ }
+ if (converter == null) {
throw new IllegalArgumentException("No Converter registered for class " + asType);
}
return converter;
}
+
+ private <T> Converter getImplicitConverter(Class<T> asType) {
+ Converter converter = implicitConverters.get(asType);
+ if (converter == null) {
+ synchronized (implicitConverters) {
+ converter = implicitConverters.get(asType);
+ if (converter == null) {
+ // try to check whether the class is an 'implicit converter'
+ converter = ImplicitConverter.getImplicitConverter(asType);
+ if (converter != null) {
+ implicitConverters.putIfAbsent(asType, converter);
+ }
+ }
+ }
+ }
+ return converter;
+ }
public ConfigValueImpl<String> access(String key) {
return new ConfigValueImpl<>(this, key);
Added: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java?rev=1815628&view=auto
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java (added)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java Fri Nov 17 21:08:32 2017
@@ -0,0 +1,101 @@
+/*
+ * 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.converters;
+
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import org.eclipse.microprofile.config.spi.Converter;
+
+/**
+ * A Converter factory + impl for 'common sense converters'
+ *
+ */
+public abstract class ImplicitConverter {
+
+ public static Converter getImplicitConverter(Class<?> clazz) {
+
+ // handle ct with String param
+ Converter converter = hasConverterCt(clazz, String.class);
+ if (converter == null) {
+ converter = hasConverterCt(clazz, CharSequence.class);
+ }
+ if (converter == null) {
+ converter = hasConverterMethod(clazz, "valueOf", String.class);
+ }
+ if (converter == null) {
+ converter = hasConverterMethod(clazz, "valueOf", CharSequence.class);
+ }
+ if (converter == null) {
+ converter = hasConverterMethod(clazz, "parse", String.class);
+ }
+ if (converter == null) {
+ converter = hasConverterMethod(clazz, "parse", CharSequence.class);
+ }
+
+ return converter;
+ }
+
+ private static Converter hasConverterCt(Class<?> clazz, Class<?> paramType) {
+ try {
+ final Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(paramType);
+ if (!declaredConstructor.isAccessible()) {
+ declaredConstructor.setAccessible(true);
+ }
+ return new Converter() {
+ @Override
+ public Object convert(String value) {
+ try {
+ return declaredConstructor.newInstance(value);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+ } catch (NoSuchMethodException e) {
+ // all fine
+ }
+ return null;
+ }
+
+ private static Converter hasConverterMethod(Class<?> clazz, String methodName, Class<?> paramType) {
+ // handle valueOf with CharSequence param
+ try {
+ final Method method = clazz.getMethod(methodName, paramType);
+ if (!method.isAccessible()) {
+ method.setAccessible(true);
+ }
+ if (Modifier.isStatic(method.getModifiers())) {
+ return new Converter() {
+ @Override
+ public Object convert(String value) {
+ try {
+ return method.invoke(null, value);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+ }
+ } catch (NoSuchMethodException e) {
+ // all fine
+ }
+ return null;
+ }
+}
Propchange: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: geronimo/components/config/trunk/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/pom.xml?rev=1815628&r1=1815627&r2=1815628&view=diff
==============================================================================
--- geronimo/components/config/trunk/pom.xml (original)
+++ geronimo/components/config/trunk/pom.xml Fri Nov 17 21:08:32 2017
@@ -50,7 +50,7 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
- <microprofile-config.version>1.1</microprofile-config.version>
+ <microprofile-config.version>1.2-SNAPSHOT</microprofile-config.version>
<arquillian.version>1.1.13.Final</arquillian.version>
<arquillian-weld-embedded.version>2.0.0.Beta5</arquillian-weld-embedded.version>