io.smallrye.config.inject.ConfigPropertiesInjectionBean Maven / Gradle / Ivy
The newest version!
package io.smallrye.config.inject;
import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.InjectionPoint;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperties;
import io.smallrye.config.ConfigMappings.ConfigClass;
import io.smallrye.config.SmallRyeConfig;
public class ConfigPropertiesInjectionBean implements Bean {
private final ConfigClass configClass;
private final Set qualifiers;
ConfigPropertiesInjectionBean(final ConfigClass configClass) {
this.configClass = configClass;
this.qualifiers = Collections.singleton(ConfigProperties.Literal.of(configClass.getPrefix()));
}
@Override
public Class getBeanClass() {
return (Class) configClass.getKlass();
}
@Override
public Set getInjectionPoints() {
return Collections.emptySet();
}
@Override
public T create(final CreationalContext creationalContext) {
String prefix = configClass.getPrefix();
if (prefix.equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
prefix = configClass.getKlass().getAnnotation(ConfigProperties.class).prefix();
if (prefix.equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
prefix = "";
}
}
SmallRyeConfig config = ConfigProvider.getConfig(getContextClassLoader()).unwrap(SmallRyeConfig.class);
return config.getConfigMapping(getBeanClass(), prefix);
}
@Override
public void destroy(final T instance, final CreationalContext creationalContext) {
}
@Override
public Set getTypes() {
return Collections.singleton(configClass.getKlass());
}
@Override
public Set getQualifiers() {
return qualifiers;
}
@Override
public Class extends Annotation> getScope() {
return Dependent.class;
}
@Override
public String getName() {
return this.getClass().getSimpleName() + "_" + configClass.getKlass().getName() + "_"
+ configClass.getPrefix();
}
@Override
public Set> getStereotypes() {
return Collections.emptySet();
}
@Override
public boolean isAlternative() {
return false;
}
}