use of org.eclipse.microprofile.config.inject.ConfigProperty in project Payara by payara.
the class ConfigProducer method getOptionalProperty.
/**
* Produces an Optional for the property specified by the ConfigProperty
* and of the type specified
* @param <T>
* @param ip
* @return
*/
@Produces
@ConfigProperty
public <T> Optional<T> getOptionalProperty(InjectionPoint ip) {
// gets the config property annotation
ConfigProperty property = ip.getAnnotated().getAnnotation(ConfigProperty.class);
PayaraConfig config = (PayaraConfig) ConfigProvider.getConfig();
Optional result = Optional.empty();
Type type = ip.getType();
if (type instanceof ParameterizedType) {
// it is an Optional
// get the class of the generic parameterized Optional
Class clazzValue = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
// use the config to get a converted version of the property
Object value = config.getValue(property.name(), property.defaultValue(), clazzValue);
if (value != null && !value.toString().equals(ConfigProperty.UNCONFIGURED_VALUE)) {
result = Optional.ofNullable(value);
}
}
return result;
}
use of org.eclipse.microprofile.config.inject.ConfigProperty in project Payara by payara.
the class ConfigPropertyProducer method getGenericProperty.
/**
* General producer method for injecting a property into a field annotated
* with the @ConfigProperty annotation.
* Note this does not have @Produces annotation as a synthetic bean using this method
* is created in teh CDI Extension.
* @param ip
* @return
*/
@ConfigProperty
@Dependent
public static final Object getGenericProperty(InjectionPoint ip) {
Object result = null;
ConfigProperty property = ip.getAnnotated().getAnnotation(ConfigProperty.class);
PayaraConfig config = (PayaraConfig) ConfigProvider.getConfig();
String name = property.name();
if (name.isEmpty()) {
// derive the property name from the injection point
Class beanClass = null;
Bean bean = ip.getBean();
if (bean == null) {
Member member = ip.getMember();
beanClass = member.getDeclaringClass();
} else {
beanClass = bean.getBeanClass();
}
StringBuilder sb = new StringBuilder(beanClass.getCanonicalName());
sb.append('.');
sb.append(ip.getMember().getName());
name = sb.toString();
}
Type type = ip.getType();
if (type instanceof Class) {
result = config.getValue(name, property.defaultValue(), (Class<?>) type);
} else if (type instanceof ParameterizedType) {
result = config.getValue(name, (Class<?>) ((ParameterizedType) type).getRawType());
}
if (result == null) {
throw new DeploymentException("Microprofile Config Property " + property.name() + " can not be found");
}
return result;
}
Aggregations