io.smallrye.config.inject.ConfigMappingInjectionBean 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.Default;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.InjectionPoint;
import org.eclipse.microprofile.config.ConfigProvider;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.ConfigMappings.ConfigClass;
import io.smallrye.config.SmallRyeConfig;
public class ConfigMappingInjectionBean implements Bean {
private final BeanManager bm;
private final ConfigClass configClass;
public ConfigMappingInjectionBean(final ConfigClass configClass, final BeanManager bm) {
this.bm = bm;
this.configClass = configClass;
}
@Override
public Class getBeanClass() {
return (Class) configClass.getKlass();
}
@Override
public Set getInjectionPoints() {
return Collections.emptySet();
}
@Override
public T create(final CreationalContext creationalContext) {
InjectionPoint injectionPoint = (InjectionPoint) bm.getInjectableReference(new MetadataInjectionPoint(),
creationalContext);
String prefix = configClass.getPrefix();
if (injectionPoint != null && injectionPoint.getAnnotated() != null) {
ConfigMapping configMapping = injectionPoint.getAnnotated().getAnnotation(ConfigMapping.class);
if (configMapping != null) {
prefix = configMapping.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 Collections.singleton(Default.Literal.INSTANCE);
}
@Override
public Class extends Annotation> getScope() {
return Dependent.class;
}
@Override
public String getName() {
return this.getClass().getSimpleName() + "_" + configClass.getKlass().getName();
}
@Override
public Set> getStereotypes() {
return Collections.emptySet();
}
@Override
public boolean isAlternative() {
return false;
}
}