diff --git a/NOTICE b/NOTICE index 451c174..231dcfe 100644 --- a/NOTICE +++ b/NOTICE @@ -1,4 +1,4 @@ -Copyright 2016-2017 original author and authors +Copyright 2009-2017 Mark Struberg This product includes software developed at The Apache Software Foundation (http://www.apache.org/). \ No newline at end of file diff --git a/README.adoc b/README.adoc index 5f6c584..e346ca3 100644 --- a/README.adoc +++ b/README.adoc @@ -19,46 +19,28 @@ ## Status -Microprofile proposal! +The content of this repository is OUTDATED! -== Rational +Please go to the Apache Geronimo Config project for further information. -Many project artifacts (e.g. WAR, EAR) should only be created once and then get installed at different customers, stages, etc -They need to target those different execution environments without necessity of any repackaging. -In other words: depending on the situation they need different configuration. - -This is easily achievable by having a set of default configuration values inside the project artifact. -But be able to overwrite those default values from external. +$> svn co https://svn.apache.org/repos/asf/geronimo/components/config/trunk == History -This very configuration approach has it's roots in the Apache OpenWebBeans internal SPI configuration. -In 2010 it got moved over to the Apache MyFaces CODI project and enhanced to also fit the need of customer projects by Gerhard Petracek and Mark Struberg. -In 2011 we further enhanced it while moving CODI to the Apache DeltaSpike project. -Romain Manni-Bucau (Apache/Tomitribe), Ron Smeral (JBoss) and Anatole Tresch also gave appreciated input in the last years. - -== How it works - -A 'Configuration' consists of the information collected from the registered `javax.config.spi.ConfigSource` s. -These `ConfigSource`s get sorted according to their _ordinal_. -That way it is possible to overwrite configuration with lower importance from outside. - -By default there are 3 default ConfigSources: +This repository contains the original MicroProfile Config proposal and a Config JSR proposal based on the concepts Gerhard and I created in Apache OpenWebBeans, CODI and DeltaSpike. +See the separate branches for more info. -* `System.getenv()` (ordinal=400) -* `System.getProperties()` (ordinal=300) -* all `META-INF/java-config.properties` files on the ClassPath. (ordinal=100, separately configurable via a config_ordinal property inside each file) +== The Config API -That means that I can put my default configuration in a `META-INF/java-config.properties` anywhere on the classpath. -And I can later simply e.g set a system property to change this default configuration. +The API later got moved to the MicroProfile repository which itself later moved under the Eclipse umbrella. +It now can be found at -== Custom ConfigSources +https://github.com/eclipse/microprofile-config/ -It is possible to write and register custom `ConfigSource` s. -An example would be a ConfigSource which gets the configured values from a shared database table in a cluster. -== Building +== The Config IMPL -`$> mvn clean install` +The implementation of MicroProfile Config can be found at the Apache Geronimo Project. -After that the specification PDF can be found in `spec/target/generated-docs/microprofile-config-spec.pdf` \ No newline at end of file +$> svn co https://svn.apache.org/repos/asf/geronimo/components/config/trunk + diff --git a/impl/debug-suite.xml b/impl/debug-suite.xml index 10e8976..b121bee 100644 --- a/impl/debug-suite.xml +++ b/impl/debug-suite.xml @@ -24,7 +24,7 @@ - + diff --git a/impl/pom.xml b/impl/pom.xml index c4b5177..f67ec8a 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -31,7 +31,7 @@ config-impl - 1.1.7.Final + 1.1.13.Final diff --git a/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java b/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java index d999ebe..f2ab338 100644 --- a/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java +++ b/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java @@ -18,6 +18,10 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -31,6 +35,10 @@ import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.geronimo.config.converters.DurationConverter; +import org.apache.geronimo.config.converters.LocalDateConverter; +import org.apache.geronimo.config.converters.LocalDateTimeConverter; +import org.apache.geronimo.config.converters.LocalTimeConverter; import org.apache.geronimo.config.converters.StringConverter; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigSource; @@ -43,11 +51,13 @@ import javax.annotation.Priority; import javax.enterprise.inject.Typed; +import javax.enterprise.inject.Vetoed; /** * @author Mark Struberg */ @Typed +@Vetoed public class ConfigImpl implements Config { protected Logger logger = Logger.getLogger(ConfigImpl.class.getName()); @@ -66,6 +76,11 @@ private void registerDefaultConverter() { converters.put(Float.class, FloatConverter.INSTANCE); converters.put(Integer.class, IntegerConverter.INSTANCE); converters.put(Long.class, LongConverter.INSTANCE); + + converters.put(Duration.class, DurationConverter.INSTANCE); + converters.put(LocalTime.class, LocalTimeConverter.INSTANCE); + converters.put(LocalDate.class, LocalDateConverter.INSTANCE); + converters.put(LocalDateTime.class, LocalDateTimeConverter.INSTANCE); } diff --git a/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java b/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java index 6aeddf0..ad56326 100644 --- a/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java +++ b/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java @@ -31,6 +31,7 @@ import java.util.ServiceLoader; import javax.enterprise.inject.Typed; +import javax.enterprise.inject.Vetoed; import static java.util.Arrays.asList; @@ -39,11 +40,13 @@ * @author Mark Struberg */ @Typed +@Vetoed public class DefaultConfigBuilder implements ConfigBuilder { protected ClassLoader forClassLoader; private final List sources = new ArrayList<>(); private final List> converters = new ArrayList<>(); private boolean ignoreDefaultSources = true; + private boolean ignoreDiscoveredSources = true; @Override public ConfigBuilder addDefaultSources() { @@ -51,6 +54,12 @@ public ConfigBuilder addDefaultSources() { return this; } + @Override + public ConfigBuilder addDiscoveredSources() { + this.ignoreDiscoveredSources = false; + return this; + } + @Override public ConfigBuilder forClassLoader(final ClassLoader loader) { this.forClassLoader = loader; @@ -72,13 +81,19 @@ public ConfigBuilder withConverters(Converter... converters) { @Override public Config build() { List configSources = new ArrayList<>(); + if (forClassLoader == null) { + forClassLoader = Thread.currentThread().getContextClassLoader(); + if (forClassLoader == null) { + forClassLoader = DefaultConfigProvider.class.getClassLoader(); + } + } if (!ignoreDefaultSources) { configSources.addAll(getBuiltInConfigSources(forClassLoader)); } configSources.addAll(sources); - if (!ignoreDefaultSources) { + if (!ignoreDiscoveredSources) { // load all ConfigSource services ServiceLoader configSourceLoader = ServiceLoader.load(ConfigSource.class, forClassLoader); configSourceLoader.forEach(configSource -> configSources.add(configSource)); diff --git a/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java b/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java index f28c716..d70b9d2 100644 --- a/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java +++ b/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java @@ -23,6 +23,7 @@ import java.util.WeakHashMap; import javax.enterprise.inject.Typed; +import javax.enterprise.inject.Vetoed; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigBuilder; @@ -33,6 +34,7 @@ * @author Mark Struberg */ @Typed +@Vetoed public class DefaultConfigProvider extends ConfigProviderResolver { private static Map> configs @@ -41,13 +43,10 @@ public class DefaultConfigProvider extends ConfigProviderResolver { @Override public Config getConfig() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if (cl == null) { - cl = DefaultConfigProvider.class.getClassLoader(); - } - return getConfig(cl); + return getConfig(null); } + @Override public Config getConfig(ClassLoader forClassLoader) { @@ -56,7 +55,7 @@ public Config getConfig(ClassLoader forClassLoader) { synchronized (DefaultConfigProvider.class) { config = existingConfig(forClassLoader); if (config == null) { - config = getBuilder().forClassLoader(forClassLoader).addDefaultSources().build(); + config = getBuilder().forClassLoader(forClassLoader).addDefaultSources().addDiscoveredSources().build(); registerConfig(config, forClassLoader); } } @@ -85,6 +84,15 @@ public ConfigBuilder getBuilder() { @Override public void releaseConfig(Config config) { + if (config == null) { + // get the config from the current TCCL + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) { + classLoader = DefaultConfigProvider.class.getClassLoader(); + } + config = existingConfig(classLoader); + } + if (config != null) { synchronized (DefaultConfigProvider.class) { Iterator>> it = configs.entrySet().iterator(); diff --git a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java index 0d8f43f..94b1b39 100644 --- a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java +++ b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java @@ -16,7 +16,11 @@ */ package org.apache.geronimo.config.cdi; +import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -25,11 +29,15 @@ import javax.enterprise.inject.spi.AfterDeploymentValidation; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.BeforeShutdown; +import javax.enterprise.inject.spi.DeploymentException; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.ProcessInjectionPoint; import javax.inject.Provider; +import org.apache.geronimo.config.DefaultConfigProvider; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; import org.eclipse.microprofile.config.inject.ConfigProperty; /** @@ -37,6 +45,8 @@ */ public class ConfigExtension implements Extension { + private Config config; + private Set injectionPoints = new HashSet<>(); public void collectConfigProducer(@Observes ProcessInjectionPoint pip) { @@ -52,18 +62,41 @@ public void registerConfigProducer(@Observes AfterBeanDiscovery abd, BeanManager .map(ip -> (Class) ip.getType()) .collect(Collectors.toSet()); - // Provider is a ParameterizedType and not a Class, so we need to add it manually + // Provider and Optional are ParameterizedTypes and not a Class, so we need to add them manually types.add(Provider.class); + types.add(Optional.class); types.forEach(type -> abd.addBean(new ConfigInjectionBean(bm, type))); } public void validate(@Observes AfterDeploymentValidation add) { - // TODO report any problems - //X injectionPoints.forEach(); + List deploymentProblems = new ArrayList<>(); + + config = ConfigProvider.getConfig(); + + for (InjectionPoint injectionPoint : injectionPoints) { + Type type = injectionPoint.getType(); + ConfigProperty configProperty = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class); + if (type instanceof Class) { + // a direct injection of a ConfigProperty + // that means a Converter must exist. + String key = ConfigInjectionBean.getConfigKey(injectionPoint, configProperty); + if (!config.getOptionalValue(key, (Class) type).isPresent()) { + deploymentProblems.add("No Config Value exists for " + key); + } + } + } + + if (!deploymentProblems.isEmpty()) { + add.addDeploymentProblem(new DeploymentException("Error while validating Configuration\n" + + String.join("\n", deploymentProblems))); + } + } public void shutdown(@Observes BeforeShutdown bsd) { - //X TODO shutdown config + DefaultConfigProvider.instance().releaseConfig(config); } + + } diff --git a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java index db19583..ba2b39b 100644 --- a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java +++ b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java @@ -29,8 +29,11 @@ import javax.enterprise.context.Dependent; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.Annotated; +import javax.enterprise.inject.spi.AnnotatedMember; +import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.DeploymentException; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.PassivationCapable; import javax.enterprise.util.AnnotationLiteral; @@ -132,6 +135,37 @@ public T create(CreationalContext context) { throw new IllegalStateException("unhandled ConfigProperty"); } + + /** + * Get the property key to use. + * In case the {@link ConfigProperty#name()} is empty we will try to determine the key name from the InjectionPoint. + */ + public static String getConfigKey(InjectionPoint ip, ConfigProperty configProperty) { + String key = configProperty.name(); + if (key.length() > 0) { + return key; + } + if (ip.getAnnotated() instanceof AnnotatedMember) { + AnnotatedMember member = (AnnotatedMember) ip.getAnnotated(); + AnnotatedType declaringType = member.getDeclaringType(); + if (declaringType != null) { + String[] parts = declaringType.getJavaClass().getName().split("."); + String cn = parts[parts.length-1]; + parts[parts.length-1] = Character.toLowerCase(cn.charAt(0)) + (cn.length() > 1 ? cn.substring(1) : ""); + StringBuilder sb = new StringBuilder(parts[0]); + for (int i = 1; i < parts.length; i++) { + sb.append(".").append(parts[i]); + } + + // now add the field name + sb.append(".").append(member.getJavaMember().getName()); + return sb.toString(); + } + } + + throw new IllegalStateException("Could not find default name for @ConfigProperty InjectionPoint " + ip); + } + public Config getConfig() { if (_config == null) { _config = ConfigProvider.getConfig(); diff --git a/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSource.java b/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSource.java index 7d86394..e7c0b2c 100644 --- a/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSource.java +++ b/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSource.java @@ -22,9 +22,14 @@ import java.util.Map; import java.util.Properties; +import javax.enterprise.inject.Typed; +import javax.enterprise.inject.Vetoed; + /** * @author Mark Struberg */ +@Typed +@Vetoed public class PropertyFileConfigSource extends BaseConfigSource { private Map properties; private String fileName; diff --git a/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSourceProvider.java b/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSourceProvider.java index f4be5fa..abbf199 100644 --- a/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSourceProvider.java +++ b/impl/src/main/java/org/apache/geronimo/config/configsource/PropertyFileConfigSourceProvider.java @@ -26,6 +26,9 @@ import java.util.logging.Level; import java.util.logging.Logger; +import javax.enterprise.inject.Typed; +import javax.enterprise.inject.Vetoed; + import org.eclipse.microprofile.config.spi.ConfigSource; import org.eclipse.microprofile.config.spi.ConfigSourceProvider; @@ -36,6 +39,8 @@ * * @author Mark Struberg */ +@Typed +@Vetoed public class PropertyFileConfigSourceProvider implements ConfigSourceProvider { private static final Logger LOG = Logger.getLogger(PropertyFileConfigSourceProvider.class.getName()); diff --git a/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java b/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java index 1c31cc6..6eefca5 100644 --- a/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java +++ b/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java @@ -21,6 +21,9 @@ import java.util.Map; +import javax.enterprise.inject.Typed; +import javax.enterprise.inject.Vetoed; + import org.eclipse.microprofile.config.spi.ConfigSource; /** @@ -31,6 +34,8 @@ * * @author Mark Struberg */ +@Typed +@Vetoed public class SystemEnvConfigSource extends BaseConfigSource { private Map configValues; diff --git a/impl/src/main/java/org/apache/geronimo/config/configsource/SystemPropertyConfigSource.java b/impl/src/main/java/org/apache/geronimo/config/configsource/SystemPropertyConfigSource.java index f5f8ea1..4d741bd 100644 --- a/impl/src/main/java/org/apache/geronimo/config/configsource/SystemPropertyConfigSource.java +++ b/impl/src/main/java/org/apache/geronimo/config/configsource/SystemPropertyConfigSource.java @@ -20,6 +20,9 @@ import java.util.Map; +import javax.enterprise.inject.Typed; +import javax.enterprise.inject.Vetoed; + import org.eclipse.microprofile.config.spi.ConfigSource; /** @@ -27,6 +30,8 @@ * * @author Mark Struberg */ +@Typed +@Vetoed public class SystemPropertyConfigSource extends BaseConfigSource { public SystemPropertyConfigSource() { initOrdinal(400); diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/BooleanConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/BooleanConverter.java index e2b5d3c..9f70a33 100644 --- a/impl/src/main/java/org/apache/geronimo/config/converters/BooleanConverter.java +++ b/impl/src/main/java/org/apache/geronimo/config/converters/BooleanConverter.java @@ -19,11 +19,13 @@ import org.eclipse.microprofile.config.spi.Converter; import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; /** * @author Mark Struberg */ @Priority(1) +@Vetoed public class BooleanConverter implements Converter { public static final BooleanConverter INSTANCE = new BooleanConverter(); @@ -35,6 +37,7 @@ public Boolean convert(String value) { || "1".equalsIgnoreCase(value) || "YES".equalsIgnoreCase(value) || "Y".equalsIgnoreCase(value) + || "ON".equalsIgnoreCase(value) || "JA".equalsIgnoreCase(value) || "J".equalsIgnoreCase(value) || "OUI".equalsIgnoreCase(value); diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/DoubleConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/DoubleConverter.java index c1eb6fb..28cd9f8 100644 --- a/impl/src/main/java/org/apache/geronimo/config/converters/DoubleConverter.java +++ b/impl/src/main/java/org/apache/geronimo/config/converters/DoubleConverter.java @@ -19,11 +19,13 @@ import org.eclipse.microprofile.config.spi.Converter; import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; /** * @author Mark Struberg */ @Priority(1) +@Vetoed public class DoubleConverter implements Converter { public static final DoubleConverter INSTANCE = new DoubleConverter(); diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/DurationConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/DurationConverter.java new file mode 100644 index 0000000..8f04abb --- /dev/null +++ b/impl/src/main/java/org/apache/geronimo/config/converters/DurationConverter.java @@ -0,0 +1,48 @@ +/* + * 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.time.Duration; +import java.time.format.DateTimeParseException; + +import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * @author Mark Struberg + */ +@Priority(1) +@Vetoed +public class DurationConverter implements Converter { + + public static final DurationConverter INSTANCE = new DurationConverter(); + + @Override + public Duration convert(String value) { + if (value != null) { + try { + return Duration.parse(value); + } + catch (DateTimeParseException dtpe) { + throw new IllegalArgumentException(dtpe); + } + } + return null; + } +} diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/FloatConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/FloatConverter.java index 1a4eaac..94c14d7 100644 --- a/impl/src/main/java/org/apache/geronimo/config/converters/FloatConverter.java +++ b/impl/src/main/java/org/apache/geronimo/config/converters/FloatConverter.java @@ -17,6 +17,7 @@ package org.apache.geronimo.config.converters; import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; import org.eclipse.microprofile.config.spi.Converter; @@ -24,6 +25,7 @@ * @author Mark Struberg */ @Priority(1) +@Vetoed public class FloatConverter implements Converter { public static final FloatConverter INSTANCE = new FloatConverter(); diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/IntegerConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/IntegerConverter.java index be32e8a..d34c468 100644 --- a/impl/src/main/java/org/apache/geronimo/config/converters/IntegerConverter.java +++ b/impl/src/main/java/org/apache/geronimo/config/converters/IntegerConverter.java @@ -17,6 +17,7 @@ package org.apache.geronimo.config.converters; import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; import org.eclipse.microprofile.config.spi.Converter; @@ -24,6 +25,7 @@ * @author Mark Struberg */ @Priority(1) +@Vetoed public class IntegerConverter implements Converter { public static final IntegerConverter INSTANCE = new IntegerConverter(); diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/LocalDateConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/LocalDateConverter.java new file mode 100644 index 0000000..41b9968 --- /dev/null +++ b/impl/src/main/java/org/apache/geronimo/config/converters/LocalDateConverter.java @@ -0,0 +1,48 @@ +/* + * 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.time.LocalDate; +import java.time.format.DateTimeParseException; + +import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * @author Mark Struberg + */ +@Priority(1) +@Vetoed +public class LocalDateConverter implements Converter { + + public static final LocalDateConverter INSTANCE = new LocalDateConverter(); + + @Override + public LocalDate convert(String value) { + if (value != null) { + try { + return LocalDate.parse(value); + } + catch (DateTimeParseException dtpe) { + throw new IllegalArgumentException(dtpe); + } + } + return null; + } +} diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/LocalDateTimeConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/LocalDateTimeConverter.java new file mode 100644 index 0000000..fe5c09e --- /dev/null +++ b/impl/src/main/java/org/apache/geronimo/config/converters/LocalDateTimeConverter.java @@ -0,0 +1,48 @@ +/* + * 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.time.LocalDateTime; +import java.time.format.DateTimeParseException; + +import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * @author Mark Struberg + */ +@Priority(1) +@Vetoed +public class LocalDateTimeConverter implements Converter { + + public static final LocalDateTimeConverter INSTANCE = new LocalDateTimeConverter(); + + @Override + public LocalDateTime convert(String value) { + if (value != null) { + try { + return LocalDateTime.parse(value); + } + catch (DateTimeParseException dtpe) { + throw new IllegalArgumentException(dtpe); + } + } + return null; + } +} diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/LocalTimeConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/LocalTimeConverter.java new file mode 100644 index 0000000..326c167 --- /dev/null +++ b/impl/src/main/java/org/apache/geronimo/config/converters/LocalTimeConverter.java @@ -0,0 +1,48 @@ +/* + * 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.time.LocalTime; +import java.time.format.DateTimeParseException; + +import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * @author Mark Struberg + */ +@Priority(1) +@Vetoed +public class LocalTimeConverter implements Converter { + + public static final LocalTimeConverter INSTANCE = new LocalTimeConverter(); + + @Override + public LocalTime convert(String value) { + if (value != null) { + try { + return LocalTime.parse(value); + } + catch (DateTimeParseException dtpe) { + throw new IllegalArgumentException(dtpe); + } + } + return null; + } +} diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/LongConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/LongConverter.java index dc63f5a..007d1c7 100644 --- a/impl/src/main/java/org/apache/geronimo/config/converters/LongConverter.java +++ b/impl/src/main/java/org/apache/geronimo/config/converters/LongConverter.java @@ -19,11 +19,13 @@ import org.eclipse.microprofile.config.spi.Converter; import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; /** * @author Mark Struberg */ @Priority(1) +@Vetoed public class LongConverter implements Converter { public static final LongConverter INSTANCE = new LongConverter(); diff --git a/impl/src/main/java/org/apache/geronimo/config/converters/StringConverter.java b/impl/src/main/java/org/apache/geronimo/config/converters/StringConverter.java index 361f805..0d5ccbc 100644 --- a/impl/src/main/java/org/apache/geronimo/config/converters/StringConverter.java +++ b/impl/src/main/java/org/apache/geronimo/config/converters/StringConverter.java @@ -17,6 +17,7 @@ package org.apache.geronimo.config.converters; import javax.annotation.Priority; +import javax.enterprise.inject.Vetoed; import org.eclipse.microprofile.config.spi.Converter; @@ -26,6 +27,7 @@ * @author Mark Struberg */ @Priority(1) +@Vetoed public class StringConverter implements Converter { public static final StringConverter INSTANCE = new StringConverter();