Search in sources :

Example 1 with PayaraConfig

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;
}
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 PayaraConfig

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;
}
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)

Example 3 with PayaraConfig

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);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) PayaraConfig(fish.payara.nucleus.microprofile.config.spi.PayaraConfig) Config(org.eclipse.microprofile.config.Config) Type(java.lang.reflect.Type) PayaraConfig(fish.payara.nucleus.microprofile.config.spi.PayaraConfig) HashSet(java.util.HashSet)

Aggregations

PayaraConfig (fish.payara.nucleus.microprofile.config.spi.PayaraConfig)3 Type (java.lang.reflect.Type)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 ConfigProperty (org.eclipse.microprofile.config.inject.ConfigProperty)2 InjectedPayaraConfig (fish.payara.nucleus.microprofile.config.spi.InjectedPayaraConfig)1 Member (java.lang.reflect.Member)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1 Set (java.util.Set)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 Config (org.eclipse.microprofile.config.Config)1