Skip to content

Commit

Permalink
Allow to extend ConfigProducer.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 1, 2020
1 parent 022afc2 commit dcbf235
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected void registerCustomBeans(@Observes AfterBeanDiscovery abd, BeanManager
}

for (Class<?> customType : customTypes) {
abd.addBean(new ConfigInjectionBean(bm, customType));
abd.addBean(new ConfigInjectionBean<>(bm, customType));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public class ConfigInjectionBean<T> implements Bean<T>, PassivationCapable {
}

private final BeanManager bm;
private final Class clazz;
private final Class<?> clazz;

/**
* only access via {@link #getConfig(}
* only access via {@link #getConfig()}
*/
private Config _config;

public ConfigInjectionBean(BeanManager bm, Class clazz) {
public ConfigInjectionBean(BeanManager bm, Class<?> clazz) {
this.bm = bm;
this.clazz = clazz;
}
Expand All @@ -81,6 +81,7 @@ public boolean isNullable() {
}

@Override
@SuppressWarnings("unchecked")
public T create(CreationalContext<T> context) {
InjectionPoint ip = (InjectionPoint) bm.getInjectableReference(new InjectionPointMetadataInjectionPoint(), context);
Annotated annotated = ip.getAnnotated();
Expand All @@ -101,17 +102,13 @@ public T create(CreationalContext<T> context) {
return (T) getConfig().getValue(key, paramTypeClass);
}
} else {
Class annotatedTypeClass = (Class) annotated.getBaseType();
if (defaultValue == null || defaultValue.length() == 0) {
Class<?> annotatedTypeClass = (Class<?>) annotated.getBaseType();
if (defaultValue.length() == 0) {
return (T) getConfig().getValue(key, annotatedTypeClass);
} else {
Config config = getConfig();
Optional<T> optionalValue = config.getOptionalValue(key, annotatedTypeClass);
if (optionalValue.isPresent()) {
return optionalValue.get();
} else {
return (T) ((SmallRyeConfig) config).convert(defaultValue, annotatedTypeClass);
}
Optional<T> optionalValue = (Optional<T>) getConfig().getOptionalValue(key, annotatedTypeClass);
return optionalValue.orElseGet(
() -> (T) ((SmallRyeConfig) getConfig()).convert(defaultValue, annotatedTypeClass));
}
}

Expand Down Expand Up @@ -187,7 +184,7 @@ public Type getType() {
@SuppressWarnings("serial")
@Override
public Set<Annotation> getQualifiers() {
return Collections.<Annotation> singleton(new AnnotationLiteral<Default>() {
return Collections.singleton(new AnnotationLiteral<Default>() {
});
}

Expand Down
42 changes: 19 additions & 23 deletions cdi/src/main/java/io/smallrye/config/inject/ConfigProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config.inject;

import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;

import java.io.Serializable;
import java.util.*;
import java.util.function.Supplier;

Expand All @@ -39,130 +37,128 @@
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
*/
@ApplicationScoped
public class ConfigProducer implements Serializable {

public class ConfigProducer {
@Produces
Config getConfig(InjectionPoint injectionPoint) {
// return the Config for the TCCL
protected Config getConfig(InjectionPoint injectionPoint) {
return ConfigProvider.getConfig(getContextClassLoader());
}

@Dependent
@Produces
@ConfigProperty
String produceStringConfigProperty(InjectionPoint ip) {
protected String produceStringConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Long getLongValue(InjectionPoint ip) {
protected Long getLongValue(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Integer getIntegerValue(InjectionPoint ip) {
protected Integer getIntegerValue(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Float produceFloatConfigProperty(InjectionPoint ip) {
protected Float produceFloatConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Double produceDoubleConfigProperty(InjectionPoint ip) {
protected Double produceDoubleConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Boolean produceBooleanConfigProperty(InjectionPoint ip) {
protected Boolean produceBooleanConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Short produceShortConfigProperty(InjectionPoint ip) {
protected Short produceShortConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Byte produceByteConfigProperty(InjectionPoint ip) {
protected Byte produceByteConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
Character produceCharacterConfigProperty(InjectionPoint ip) {
protected Character produceCharacterConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
<T> Optional<T> produceOptionalConfigValue(InjectionPoint ip) {
protected <T> Optional<T> produceOptionalConfigValue(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
<T> Supplier<T> produceSupplierConfigValue(InjectionPoint ip) {
protected <T> Supplier<T> produceSupplierConfigValue(InjectionPoint ip) {
return () -> ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
<T> Set<T> producesSetConfigProperty(InjectionPoint ip) {
protected <T> Set<T> producesSetConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
<T> List<T> producesListConfigProperty(InjectionPoint ip) {
protected <T> List<T> producesListConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
OptionalInt produceOptionalIntConfigProperty(InjectionPoint ip) {
protected OptionalInt produceOptionalIntConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
OptionalLong produceOptionalLongConfigProperty(InjectionPoint ip) {
protected OptionalLong produceOptionalLongConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
OptionalDouble produceOptionalDoubleConfigProperty(InjectionPoint ip) {
protected OptionalDouble produceOptionalDoubleConfigProperty(InjectionPoint ip) {
return ConfigProducerUtil.getValue(ip, getConfig(ip));
}

@Dependent
@Produces
@ConfigProperty
ConfigValue produceConfigValue(InjectionPoint ip) {
protected ConfigValue produceConfigValue(InjectionPoint ip) {
return ConfigProducerUtil.getConfigValue(ip, getConfig(ip));
}
}

0 comments on commit dcbf235

Please sign in to comment.