Skip to content

Commit

Permalink
Rename ConfigClassWithPrefix to ConfigClass and use it in SmallRyeCon…
Browse files Browse the repository at this point in the history
…figBuilder
  • Loading branch information
radcortez committed Oct 4, 2024
1 parent 0b79353 commit 1fbb4a1
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 176 deletions.
48 changes: 22 additions & 26 deletions cdi/src/main/java/io/smallrye/config/inject/ConfigExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static io.smallrye.config.ConfigMappings.registerConfigMappings;
import static io.smallrye.config.ConfigMappings.registerConfigProperties;
import static io.smallrye.config.ConfigMappings.ConfigClassWithPrefix.configClassWithPrefix;
import static io.smallrye.config.inject.ConfigProducer.isClassHandledByConfigProducer;
import static io.smallrye.config.inject.InjectionMessages.formatInjectionPoint;
import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;
Expand Down Expand Up @@ -57,7 +56,7 @@
import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix;
import io.smallrye.config.ConfigMappings.ConfigClass;
import io.smallrye.config.ConfigValidationException;
import io.smallrye.config.SmallRyeConfig;

Expand All @@ -69,13 +68,13 @@
public class ConfigExtension implements Extension {
private final Set<InjectionPoint> configPropertyInjectionPoints = new HashSet<>();
/** ConfigProperties for SmallRye Config */
private final Set<ConfigClassWithPrefix> configProperties = new HashSet<>();
private final Set<ConfigClass> configProperties = new HashSet<>();
/** ConfigProperties for CDI */
private final Set<ConfigClassWithPrefix> configPropertiesBeans = new HashSet<>();
private final Set<ConfigClass> configPropertiesBeans = new HashSet<>();
/** ConfigMappings for SmallRye Config */
private final Set<ConfigClassWithPrefix> configMappings = new HashSet<>();
private final Set<ConfigClass> configMappings = new HashSet<>();
/** ConfigMappings for CDI */
private final Set<ConfigClassWithPrefix> configMappingBeans = new HashSet<>();
private final Set<ConfigClass> configMappingBeans = new HashSet<>();

public ConfigExtension() {
}
Expand All @@ -95,48 +94,45 @@ protected void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd, BeanManage
protected void processConfigProperties(
@Observes @WithAnnotations(ConfigProperties.class) ProcessAnnotatedType<?> processAnnotatedType) {
// Even if we filter in the CDI event, beans containing injection points of ConfigProperties are also fired.
if (processAnnotatedType.getAnnotatedType().isAnnotationPresent(ConfigProperties.class)) {
AnnotatedType<?> annotatedType = processAnnotatedType.getAnnotatedType();
if (annotatedType.isAnnotationPresent(ConfigProperties.class)) {
// We are going to veto, because it may be a managed bean, and we will use a configurator bean
processAnnotatedType.veto();

// Each config class is both in SmallRyeConfig and managed by a configurator bean.
// CDI requires more beans for injection points due to binding prefix.
ConfigClassWithPrefix properties = configClassWithPrefix(processAnnotatedType.getAnnotatedType().getJavaClass(),
processAnnotatedType.getAnnotatedType().getAnnotation(ConfigProperties.class).prefix());
// Unconfigured is represented as an empty String in SmallRye Config
if (!properties.getPrefix().equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
configProperties.add(properties);
} else {
configProperties.add(ConfigClassWithPrefix.configClassWithPrefix(properties.getKlass(), ""));
}
ConfigClass properties = ConfigClass.configClass(annotatedType.getJavaClass(),
annotatedType.getAnnotation(ConfigProperties.class).prefix());
configProperties.add(properties);
configPropertiesBeans.add(properties);
}
}

protected void processConfigMappings(
@Observes @WithAnnotations(ConfigMapping.class) ProcessAnnotatedType<?> processAnnotatedType) {
// Even if we filter in the CDI event, beans containing injection points of ConfigMapping are also fired.
if (processAnnotatedType.getAnnotatedType().isAnnotationPresent(ConfigMapping.class)) {
AnnotatedType<?> annotatedType = processAnnotatedType.getAnnotatedType();
if (annotatedType.isAnnotationPresent(ConfigMapping.class)) {
// We are going to veto, because it may be a managed bean, and we will use a configurator bean
processAnnotatedType.veto();

// Each config class is both in SmallRyeConfig and managed by a configurator bean.
// CDI requires a single configurator bean per class due to non-binding prefix.
ConfigClassWithPrefix mapping = configClassWithPrefix(processAnnotatedType.getAnnotatedType().getJavaClass(),
processAnnotatedType.getAnnotatedType().getAnnotation(ConfigMapping.class).prefix());
ConfigClass mapping = ConfigClass.configClass(annotatedType.getJavaClass());
configMappings.add(mapping);
configMappingBeans.add(mapping);
}
}

protected void processConfigInjectionPoints(@Observes ProcessInjectionPoint<?, ?> pip) {
if (pip.getInjectionPoint().getAnnotated().isAnnotationPresent(ConfigProperty.class)) {
configPropertyInjectionPoints.add(pip.getInjectionPoint());
InjectionPoint injectionPoint = pip.getInjectionPoint();
if (injectionPoint.getAnnotated().isAnnotationPresent(ConfigProperty.class)) {
configPropertyInjectionPoints.add(injectionPoint);
}

if (pip.getInjectionPoint().getAnnotated().isAnnotationPresent(ConfigProperties.class)) {
ConfigClassWithPrefix properties = configClassWithPrefix((Class<?>) pip.getInjectionPoint().getType(),
pip.getInjectionPoint().getAnnotated().getAnnotation(ConfigProperties.class).prefix());
if (injectionPoint.getAnnotated().isAnnotationPresent(ConfigProperties.class)) {
ConfigClass properties = ConfigClass.configClass((Class<?>) injectionPoint.getType(),
injectionPoint.getAnnotated().getAnnotation(ConfigProperties.class).prefix());

// If the prefix is empty at the injection point, fallbacks to the class prefix (already registered)
if (!properties.getPrefix().equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
Expand All @@ -147,9 +143,9 @@ protected void processConfigInjectionPoints(@Observes ProcessInjectionPoint<?, ?
}

// Add to SmallRyeConfig config classes with a different prefix by injection point
if (pip.getInjectionPoint().getAnnotated().isAnnotationPresent(ConfigMapping.class)) {
ConfigClassWithPrefix mapping = configClassWithPrefix((Class<?>) pip.getInjectionPoint().getType(),
pip.getInjectionPoint().getAnnotated().getAnnotation(ConfigMapping.class).prefix());
if (injectionPoint.getAnnotated().isAnnotationPresent(ConfigMapping.class)) {
ConfigClass mapping = ConfigClass.configClass((Class<?>) injectionPoint.getType(),
injectionPoint.getAnnotated().getAnnotation(ConfigMapping.class).prefix());
// If the prefix is empty at the injection point, fallbacks to the class prefix (already registered)
if (!mapping.getPrefix().isEmpty()) {
configMappings.add(mapping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
import org.eclipse.microprofile.config.ConfigProvider;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix;
import io.smallrye.config.ConfigMappings.ConfigClass;
import io.smallrye.config.SmallRyeConfig;

public class ConfigMappingInjectionBean<T> implements Bean<T> {
private final BeanManager bm;
private final ConfigClassWithPrefix configClassWithPrefix;
private final ConfigClass configClass;

public ConfigMappingInjectionBean(final ConfigClassWithPrefix configClassWithPrefix, final BeanManager bm) {
public ConfigMappingInjectionBean(final ConfigClass configClass, final BeanManager bm) {
this.bm = bm;
this.configClassWithPrefix = configClassWithPrefix;
this.configClass = configClass;
}

@Override
public Class<T> getBeanClass() {
return (Class<T>) configClassWithPrefix.getKlass();
return (Class<T>) configClass.getKlass();
}

@Override
Expand All @@ -44,7 +44,7 @@ public T create(final CreationalContext<T> creationalContext) {
InjectionPoint injectionPoint = (InjectionPoint) bm.getInjectableReference(new MetadataInjectionPoint(),
creationalContext);

String prefix = configClassWithPrefix.getPrefix();
String prefix = configClass.getPrefix();
if (injectionPoint != null && injectionPoint.getAnnotated() != null) {
ConfigMapping configMapping = injectionPoint.getAnnotated().getAnnotation(ConfigMapping.class);
if (configMapping != null) {
Expand All @@ -63,7 +63,7 @@ public void destroy(final T instance, final CreationalContext<T> creationalConte

@Override
public Set<Type> getTypes() {
return Collections.singleton(configClassWithPrefix.getKlass());
return Collections.singleton(configClass.getKlass());
}

@Override
Expand All @@ -78,7 +78,7 @@ public Class<? extends Annotation> getScope() {

@Override
public String getName() {
return this.getClass().getSimpleName() + "_" + configClassWithPrefix.getKlass().getName();
return this.getClass().getSimpleName() + "_" + configClass.getKlass().getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperties;

import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix;
import io.smallrye.config.ConfigMappings.ConfigClass;
import io.smallrye.config.SmallRyeConfig;

public class ConfigPropertiesInjectionBean<T> implements Bean<T> {
private final ConfigClassWithPrefix configClassWithPrefix;
private final ConfigClass configClass;
private final Set<Annotation> qualifiers;

ConfigPropertiesInjectionBean(final ConfigClassWithPrefix configClassWithPrefix) {
this.configClassWithPrefix = configClassWithPrefix;
this.qualifiers = Collections.singleton(ConfigProperties.Literal.of(configClassWithPrefix.getPrefix()));
ConfigPropertiesInjectionBean(final ConfigClass configClass) {
this.configClass = configClass;
this.qualifiers = Collections.singleton(ConfigProperties.Literal.of(configClass.getPrefix()));
}

@Override
public Class<T> getBeanClass() {
return (Class<T>) configClassWithPrefix.getKlass();
return (Class<T>) configClass.getKlass();
}

@Override
Expand All @@ -39,9 +39,9 @@ public Set<InjectionPoint> getInjectionPoints() {

@Override
public T create(final CreationalContext<T> creationalContext) {
String prefix = configClassWithPrefix.getPrefix();
String prefix = configClass.getPrefix();
if (prefix.equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
prefix = configClassWithPrefix.getKlass().getAnnotation(ConfigProperties.class).prefix();
prefix = configClass.getKlass().getAnnotation(ConfigProperties.class).prefix();
if (prefix.equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
prefix = "";
}
Expand All @@ -58,7 +58,7 @@ public void destroy(final T instance, final CreationalContext<T> creationalConte

@Override
public Set<Type> getTypes() {
return Collections.singleton(configClassWithPrefix.getKlass());
return Collections.singleton(configClass.getKlass());
}

@Override
Expand All @@ -73,8 +73,8 @@ public Class<? extends Annotation> getScope() {

@Override
public String getName() {
return this.getClass().getSimpleName() + "_" + configClassWithPrefix.getKlass().getName() + "_"
+ configClassWithPrefix.getPrefix();
return this.getClass().getSimpleName() + "_" + configClass.getKlass().getName() + "_"
+ configClass.getPrefix();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
*
* This annotation is also used in CDI aware environments to scan and register Config Mappings. Otherwise, Config
* Mapping interfaces require registration via
* {@link SmallRyeConfigBuilder#withMapping(java.lang.Class, java.lang.String)}.
* {@link SmallRyeConfigBuilder#withMapping(java.lang.Class)}.
*
* @see SmallRyeConfigBuilder#withMapping(java.lang.Class)
* @see SmallRyeConfigBuilder#withMapping(ConfigMappings.ConfigClass)
*/
@Documented
@Target({ FIELD, PARAMETER, TYPE })
Expand Down Expand Up @@ -69,7 +72,7 @@ enum NamingStrategy {

private final Function<String, String> function;

private NamingStrategy(Function<String, String> function) {
NamingStrategy(Function<String, String> function) {
this.function = function;
}

Expand Down
Loading

0 comments on commit 1fbb4a1

Please sign in to comment.