Search in sources :

Example 1 with ConfigProperty

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Optional(java.util.Optional) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) PayaraConfig(fish.payara.nucleus.microprofile.config.spi.PayaraConfig) InjectedPayaraConfig(fish.payara.nucleus.microprofile.config.spi.InjectedPayaraConfig) Produces(javax.enterprise.inject.Produces) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty)

Example 2 with ConfigProperty

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) DeploymentException(javax.enterprise.inject.spi.DeploymentException) PayaraConfig(fish.payara.nucleus.microprofile.config.spi.PayaraConfig) Member(java.lang.reflect.Member) Bean(javax.enterprise.inject.spi.Bean) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Dependent(javax.enterprise.context.Dependent)

Aggregations

PayaraConfig (fish.payara.nucleus.microprofile.config.spi.PayaraConfig)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ConfigProperty (org.eclipse.microprofile.config.inject.ConfigProperty)2 InjectedPayaraConfig (fish.payara.nucleus.microprofile.config.spi.InjectedPayaraConfig)1 Member (java.lang.reflect.Member)1 Optional (java.util.Optional)1 Dependent (javax.enterprise.context.Dependent)1 Produces (javax.enterprise.inject.Produces)1 Bean (javax.enterprise.inject.spi.Bean)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1