public abstract class BaseConfigPropertyProducer extends Object
This contains the fundamental parts for implementing own ConfigProperty producers.
DeltaSpikes own configuration system natively supports only Strings. If you'd like to apply own Converters or extract other types from those Strings, you can simply do this by providing an own Qualifier and a simple CDI producer method for it.
First we write a simple Qualifier:
@Target({ PARAMETER, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE }) @Retention(RUNTIME) @ConfigProperty(named="unused") // the name @Qualifier public @interface NumberConfig { @Nonbinding boolean name(); // the name of the configuration-key to lookup the value @Nonbinding String defaultValue() default ConfigProperty.NULL; @Nonbinding boolean pattern(); // the pattern for NumberFormatter }
The producer method implementation is pretty easy as well:
@ApplicationScoped public class NumberConfigProducer extends BaseConfigPropertyProducer { @Produces @Dependent @NumberConfig public Float produceNumberConfig(InjectionPoint injectionPoint) { // resolve the annotation NumberConfig metaData = getAnnotation(injectionPoint, NumberConfig.class); // get the configured value from the underlying configuration system String configuredValue = getPropertyValue(metaData.name(), metaData.defaultValue()); if (configuredValue == null) { return null; } // format according to the given pattern DecimalFormat df = new DecimalFormat(metaData.pattern(), new DecimalFormatSymbols(Locale.US)); return df.parse(configuredValue).floatValue(); } }
Constructor and Description |
---|
BaseConfigPropertyProducer() |
Modifier and Type | Method and Description |
---|---|
<T> ConfigResolver.TypedResolver<T> |
asResolver(String key,
String stringDefault,
Type ipCls,
Class<? extends ConfigResolver.Converter> converterType,
String parameterizedBy,
boolean projectStageAware,
boolean evaluate) |
protected <T extends Annotation> |
getAnnotation(javax.enterprise.inject.spi.InjectionPoint injectionPoint,
Class<T> targetType) |
protected <T> T |
getPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint,
Class<T> ipCls) |
protected String |
getPropertyValue(String propertyName,
String defaultValue) |
protected String |
getStringPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint)
Inspects the given InjectionPoint and search for a
ConfigProperty
annotation or an Annotation with a ConfigProperty meta-Annotation. |
protected <T> T |
getUntypedPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint,
Type ipCls) |
<T> T |
readEntry(String key,
String stringDefault,
Type ipCls,
Class<? extends ConfigResolver.Converter> converterType,
String parameterizedBy,
boolean projectStageAware,
boolean evaluate) |
protected String getStringPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint)
Inspects the given InjectionPoint and search for a ConfigProperty
annotation or an Annotation with a ConfigProperty
meta-Annotation.
The name and defaultValue information will be used to resolve the
configured value.
injectionPoint
- current injection pointprotected <T> T getPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint, Class<T> ipCls)
protected <T> T getUntypedPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint, Type ipCls)
protected String getPropertyValue(String propertyName, String defaultValue)
propertyName
- the name of the property keydefaultValue
- the default value to return if no configured property is found or
ConfigProperty.NULL
if no default value should be returned.protected <T extends Annotation> T getAnnotation(javax.enterprise.inject.spi.InjectionPoint injectionPoint, Class<T> targetType)
T
- typeinjectionPoint
- current injection pointtargetType
- target typepublic <T> T readEntry(String key, String stringDefault, Type ipCls, Class<? extends ConfigResolver.Converter> converterType, String parameterizedBy, boolean projectStageAware, boolean evaluate)
public <T> ConfigResolver.TypedResolver<T> asResolver(String key, String stringDefault, Type ipCls, Class<? extends ConfigResolver.Converter> converterType, String parameterizedBy, boolean projectStageAware, boolean evaluate)
Copyright © 2017 The Apache Software Foundation. All rights reserved.