use of fish.payara.nucleus.microprofile.config.spi.PayaraConfig 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 fish.payara.nucleus.microprofile.config.spi.PayaraConfig 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;
}
use of fish.payara.nucleus.microprofile.config.spi.PayaraConfig in project Payara by payara.
the class CDIExtension method addDynamicProducers.
/**
* CDI observer creates a synthetic bean with Producer method
* for each Converter type registered in the Config object associated with
* this application.
*
* @param event
* @param bm
*/
public void addDynamicProducers(@Observes AfterBeanDiscovery event, BeanManager bm) {
// Retrieve the config for the application
Config config = ConfigProvider.getConfig();
if (config instanceof PayaraConfig) {
// create a synthetic bean based on the ConfigPropertyProducer which
// has a method we can use to create the correct objects based on
// the InjectionPoint
AnnotatedType<ConfigPropertyProducer> atype = bm.createAnnotatedType(ConfigPropertyProducer.class);
BeanAttributes<?> beanAttr = null;
// first find the producer method
AnnotatedMethod<? super ConfigPropertyProducer> method = null;
for (AnnotatedMethod m : atype.getMethods()) {
if (m.getJavaMember().getName().equals("getGenericProperty")) {
// create a bean attributes based on this method
beanAttr = bm.createBeanAttributes(m);
method = m;
break;
}
}
if (beanAttr != null) {
HashSet<Type> types = new HashSet<>();
types.addAll(((PayaraConfig) config).getConverterTypes());
// add string explictly
types.add(String.class);
for (final Type converterType : types) {
// go through each type which has a converter
// create a bean with a Producer method using the bean factory and with custom bean attributes
Bean<?> bean = bm.createBean(new TypesBeanAttributes<Object>(beanAttr) {
// overrides the bean types to return the type registered for a Converter
@Override
public Set<Type> getTypes() {
HashSet<Type> result = new HashSet<>();
result.add(converterType);
return result;
}
}, ConfigPropertyProducer.class, bm.getProducerFactory(method, null));
event.addBean(bean);
}
}
}
}
Aggregations