Skip to content

Commit

Permalink
Update SmallRye Config to 2.3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed May 14, 2021
1 parent 92dfd90 commit 1ce1337
Show file tree
Hide file tree
Showing 24 changed files with 424 additions and 93 deletions.
7 changes: 6 additions & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<microprofile-rest-client.version>2.0</microprofile-rest-client.version>
<microprofile-jwt.version>1.2</microprofile-jwt.version>
<smallrye-common.version>1.6.0</smallrye-common.version>
<smallrye-config.version>2.2.0</smallrye-config.version>
<smallrye-config.version>2.3.0</smallrye-config.version>
<smallrye-health.version>3.0.2</smallrye-health.version>
<smallrye-metrics.version>3.0.1</smallrye-metrics.version>
<smallrye-open-api.version>2.1.4</smallrye-open-api.version>
Expand Down Expand Up @@ -3179,6 +3179,11 @@
<artifactId>smallrye-config-common</artifactId>
<version>${smallrye-config.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-validator</artifactId>
<version>${smallrye-config.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-source-yaml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.smallrye.config.ConfigSourceFactory;
import io.smallrye.config.ConfigSourceInterceptor;
import io.smallrye.config.ConfigSourceInterceptorFactory;
import io.smallrye.config.ConfigValidator;
import io.smallrye.config.SmallRyeConfigProviderResolver;

class ConfigBuildSteps {
Expand Down Expand Up @@ -88,7 +89,8 @@ void nativeServiceProviders(
Converter.class,
ConfigSourceInterceptor.class,
ConfigSourceInterceptorFactory.class,
ConfigSourceFactory.class)) {
ConfigSourceFactory.class,
ConfigValidator.class)) {
final String serviceName = serviceClass.getName();
final Set<String> names = ServiceUtil.classNamesNamedIn(classLoader, SERVICES_PREFIX + serviceName);
final List<String> list = names.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -24,35 +25,27 @@ protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws
}

public static class InClassPath extends ApplicationPropertiesConfigSourceLoader implements ConfigSourceProvider {
@Override
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
return super.loadConfigSource(url, 250);
}

@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
return loadConfigSources("application.properties", classLoader);
return loadConfigSources("application.properties", 250, classLoader);
}

@Override
protected List<ConfigSource> tryFileSystem(final URI uri) {
protected List<ConfigSource> tryFileSystem(final URI uri, final int ordinal) {
return new ArrayList<>();
}
}

public static class InFileSystem extends ApplicationPropertiesConfigSourceLoader implements ConfigSourceProvider {
@Override
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
return super.loadConfigSource(url, 260);
}

@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
return loadConfigSources("config/application.properties", classLoader);
return loadConfigSources(
Paths.get(System.getProperty("user.dir"), "config", "application.properties").toUri().toString(), 260,
classLoader);
}

@Override
protected List<ConfigSource> tryClassPath(final URI uri, final ClassLoader classLoader) {
protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader) {
return new ArrayList<>();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.quarkus.runtime.configuration;

import static io.smallrye.config.AbstractLocationConfigSourceFactory.SMALLRYE_LOCATIONS;
import static io.smallrye.config.DotEnvConfigSourceProvider.dotEnvSources;
import static io.smallrye.config.ProfileConfigSourceInterceptor.SMALLRYE_PROFILE;
import static io.smallrye.config.ProfileConfigSourceInterceptor.SMALLRYE_PROFILE_PARENT;
import static io.smallrye.config.PropertiesConfigSourceProvider.classPathSources;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_LOCATIONS;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE_PARENT;
import static io.smallrye.config.SmallRyeConfigBuilder.META_INF_MICROPROFILE_CONFIG_PROPERTIES;

import java.io.IOException;
Expand Down Expand Up @@ -116,11 +116,11 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final b

public static SmallRyeConfigBuilder emptyConfigBuilder() {
final SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
builder.withDefaultValue(SMALLRYE_PROFILE, ProfileManager.getActiveProfile());
builder.withDefaultValue(SMALLRYE_CONFIG_PROFILE, ProfileManager.getActiveProfile());

final Map<String, String> relocations = new HashMap<>();
relocations.put(SMALLRYE_LOCATIONS, "quarkus.config.locations");
relocations.put(SMALLRYE_PROFILE_PARENT, "quarkus.config.profile.parent");
relocations.put(SMALLRYE_CONFIG_LOCATIONS, "quarkus.config.locations");
relocations.put(SMALLRYE_CONFIG_PROFILE_PARENT, "quarkus.config.profile.parent");
// Override the priority, because of the ProfileConfigSourceInterceptor and profile.parent.
builder.withInterceptorFactories(new ConfigSourceInterceptorFactory() {
@Override
Expand All @@ -137,6 +137,7 @@ public OptionalInt getPriority() {
builder.addDefaultInterceptors();
builder.addDiscoveredInterceptors();
builder.addDiscoveredConverters();
builder.addDiscoveredValidator();
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.quarkus.arc.deployment;

import static io.quarkus.arc.deployment.ConfigClassBuildItem.Type.MAPPING;
import static io.quarkus.arc.deployment.ConfigClassBuildItem.Type.PROPERTIES;
import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;
import static io.smallrye.config.ConfigMappings.ConfigMappingWithPrefix.configMappingWithPrefix;
import static io.smallrye.config.ConfigMappings.ConfigClassWithPrefix.configClassWithPrefix;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.eclipse.microprofile.config.inject.ConfigProperties.UNCONFIGURED_PREFIX;
import static org.jboss.jandex.AnnotationInstance.create;
import static org.jboss.jandex.AnnotationTarget.Kind.CLASS;
import static org.jboss.jandex.AnnotationTarget.Kind.FIELD;
Expand Down Expand Up @@ -57,14 +61,13 @@
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.ConfigMappingLoader;
import io.smallrye.config.ConfigMappingMetadata;
import io.smallrye.config.ConfigMappings.ConfigMappingWithPrefix;
import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix;
import io.smallrye.config.inject.ConfigProducer;

/**
* MicroProfile Config related build steps.
*/
public class ConfigBuildStep {

private static final DotName MP_CONFIG_PROPERTY_NAME = DotName.createSimple(ConfigProperty.class.getName());
private static final DotName MP_CONFIG_PROPERTIES_NAME = DotName.createSimple(ConfigProperties.class.getName());
private static final DotName MP_CONFIG_VALUE_NAME = DotName.createSimple(ConfigValue.class.getName());
Expand Down Expand Up @@ -226,13 +229,11 @@ public void transform(TransformationContext context) {
}

@BuildStep
void generateConfigMappings(
void generateConfigClasses(
CombinedIndexBuildItem combinedIndex,
BeanRegistrationPhaseBuildItem beanRegistrationPhase,
BuildProducer<GeneratedClassBuildItem> generatedClasses,
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses,
BuildProducer<ConfigMappingBuildItem> configMappings,
BuildProducer<BeanConfiguratorBuildItem> beanConfigurationRegistry) {
BuildProducer<ConfigClassBuildItem> configClasses) {

List<AnnotationInstance> mappingAnnotations = new ArrayList<>();
mappingAnnotations.addAll(combinedIndex.getIndex().getAnnotations(CONFIG_MAPPING_NAME));
Expand All @@ -243,18 +244,20 @@ void generateConfigMappings(
AnnotationValue annotationPrefix = instance.value("prefix");

if (target.kind().equals(FIELD)) {
if (annotationPrefix != null && !annotationPrefix.asString().equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
configMappings.produce(
new ConfigMappingBuildItem(toClass(target.asField().type().name()), annotationPrefix.asString()));
if (annotationPrefix != null && !annotationPrefix.asString().equals(UNCONFIGURED_PREFIX)) {
configClasses.produce(
toConfigClassBuildItem(instance, toClass(target.asField().type().name()),
annotationPrefix.asString()));
continue;
}
}

if (target.kind().equals(METHOD_PARAMETER)) {
if (annotationPrefix != null && !annotationPrefix.asString().equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
if (annotationPrefix != null && !annotationPrefix.asString().equals(UNCONFIGURED_PREFIX)) {
ClassType classType = target.asMethodParameter().method().parameters()
.get(target.asMethodParameter().position()).asClassType();
configMappings.produce(new ConfigMappingBuildItem(toClass(classType.name()), annotationPrefix.asString()));
configClasses
.produce(toConfigClassBuildItem(instance, toClass(classType.name()), annotationPrefix.asString()));
continue;
}
}
Expand All @@ -263,11 +266,12 @@ void generateConfigMappings(
continue;
}

Class<?> type = toClass(target.asClass().name());
Class<?> configClass = toClass(target.asClass().name());
String prefix = Optional.ofNullable(annotationPrefix).map(AnnotationValue::asString).orElse("");

List<ConfigMappingMetadata> configMappingsMetadata = ConfigMappingLoader.getConfigMappingsMetadata(type);
List<ClassInfo> mappingsInfo = new ArrayList<>();
List<ConfigMappingMetadata> configMappingsMetadata = ConfigMappingLoader.getConfigMappingsMetadata(configClass);
Set<String> generatedClassesNames = new HashSet<>();
Set<ClassInfo> mappingsInfo = new HashSet<>();
configMappingsMetadata.forEach(mappingMetadata -> {
generatedClasses.produce(
new GeneratedClassBuildItem(true, mappingMetadata.getClassName(), mappingMetadata.getClassBytes()));
Expand All @@ -276,6 +280,8 @@ void generateConfigMappings(
reflectiveClasses
.produce(ReflectiveClassBuildItem.builder(mappingMetadata.getClassName()).constructors(true).build());

generatedClassesNames.add(mappingMetadata.getClassName());

ClassInfo mappingInfo = combinedIndex.getIndex()
.getClassByName(DotName.createSimple(mappingMetadata.getInterfaceType().getName()));
if (mappingInfo != null) {
Expand All @@ -296,40 +302,63 @@ void generateConfigMappings(
}
}

configMappings.produce(new ConfigMappingBuildItem(type, prefix));
configClasses.produce(toConfigClassBuildItem(instance, configClass, generatedClassesNames, prefix));
}
}

@BuildStep
void beanConfigClasses(
List<ConfigClassBuildItem> configClasses,
BeanRegistrationPhaseBuildItem beanRegistrationPhase,
BuildProducer<BeanConfiguratorBuildItem> beanConfigurationRegistry) {

for (ConfigClassBuildItem configClass : configClasses) {
if (configClass.getGeneratedClasses().isEmpty()) {
continue;
}

List<AnnotationInstance> qualifiers = new ArrayList<>();
if (instance.name().equals(MP_CONFIG_PROPERTIES_NAME)) {
if (configClass.isProperties()) {
qualifiers.add(
create(MP_CONFIG_PROPERTIES_NAME, null, new AnnotationValue[] { createStringValue("prefix", prefix) }));
create(MP_CONFIG_PROPERTIES_NAME, null,
new AnnotationValue[] { createStringValue("prefix", configClass.getPrefix()) }));
}

beanConfigurationRegistry.produce(new BeanConfiguratorBuildItem(
beanRegistrationPhase.getContext()
.configure(type)
.types(type)
.configure(configClass.getConfigClass())
.types(configClass.getConfigClass())
.qualifiers(qualifiers.toArray(new AnnotationInstance[] {}))
.creator(ConfigMappingCreator.class)
.param("type", type)
.param("prefix", prefix)));
.param("type", configClass.getConfigClass())
.param("prefix", configClass.getPrefix())));
}
}

@BuildStep
@Record(RUNTIME_INIT)
void registerConfigMappings(
void registerConfigClasses(
RecorderContext context,
ConfigRecorder recorder,
List<ConfigMappingBuildItem> configMappings) throws Exception {
List<ConfigClassBuildItem> configClasses) throws Exception {

context.registerNonDefaultConstructor(
ConfigMappingWithPrefix.class.getDeclaredConstructor(Class.class, String.class),
configMappingWithPrefix -> Stream.of(configMappingWithPrefix.getKlass(), configMappingWithPrefix.getPrefix())
ConfigClassWithPrefix.class.getDeclaredConstructor(Class.class, String.class),
configClassWithPrefix -> Stream.of(configClassWithPrefix.getKlass(), configClassWithPrefix.getPrefix())
.collect(toList()));

recorder.registerConfigMappings(configMappings.stream()
.map(configMapping -> configMappingWithPrefix(configMapping.getInterfaceType(), configMapping.getPrefix()))
.collect(toSet()));
recorder.registerConfigMappings(
configClasses.stream()
.filter(ConfigClassBuildItem::isMapping)
.map(configMapping -> configClassWithPrefix(configMapping.getConfigClass(), configMapping.getPrefix()))
.collect(toSet()));

recorder.registerConfigProperties(
configClasses.stream()
.filter(ConfigClassBuildItem::isProperties)
.map(configProperties -> configClassWithPrefix(configProperties.getConfigClass(),
configProperties.getPrefix()))
.collect(toSet()));
}

private static Class<?> toClass(DotName dotName) {
Expand All @@ -341,6 +370,27 @@ private static Class<?> toClass(DotName dotName) {
}
}

private static ConfigClassBuildItem toConfigClassBuildItem(
AnnotationInstance instance,
Class<?> configClass,
String prefix) {
return toConfigClassBuildItem(instance, configClass, emptySet(), prefix);
}

private static ConfigClassBuildItem toConfigClassBuildItem(
AnnotationInstance instance,
Class<?> configClass,
Set<String> generatedClasses,
String prefix) {
if (instance.name().equals(CONFIG_MAPPING_NAME)) {
return new ConfigClassBuildItem(configClass, generatedClasses, prefix, MAPPING);
} else if (instance.name().equals(MP_CONFIG_PROPERTIES_NAME)) {
return new ConfigClassBuildItem(configClass, generatedClasses, prefix, PROPERTIES);
} else {
throw new IllegalArgumentException();
}
}

private String getPropertyName(String name, ClassInfo declaringClass) {
StringBuilder builder = new StringBuilder();
if (declaringClass.enclosingClass() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.arc.deployment;

import java.util.Set;

import io.quarkus.builder.item.MultiBuildItem;

public final class ConfigClassBuildItem extends MultiBuildItem {
private final Class<?> configClass;
private final Set<String> generatedClasses;
private final String prefix;
private final Type type;

public ConfigClassBuildItem(
final Class<?> configClass,
final Set<String> generatedClasses,
final String prefix,
final Type type) {

this.configClass = configClass;
this.generatedClasses = generatedClasses;
this.prefix = prefix;
this.type = type;
}

public Class<?> getConfigClass() {
return configClass;
}

public Set<String> getGeneratedClasses() {
return generatedClasses;
}

public String getPrefix() {
return prefix;
}

public Type getType() {
return type;
}

public boolean isMapping() {
return Type.MAPPING.equals(type);
}

public boolean isProperties() {
return Type.PROPERTIES.equals(type);
}

public enum Type {
MAPPING,
PROPERTIES;
}
}
Loading

0 comments on commit 1ce1337

Please sign in to comment.