All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.oneandone.ejbcdiunit.internal.ProducerConfigExtension Maven / Gradle / Ivy

Go to download

A module that can be used together with cdiunit to build en ejb-test-environment.

The newest version!
package com.oneandone.ejbcdiunit.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.util.AnnotationLiteral;

import org.jglue.cdiunit.ProducerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProducerConfigExtension implements Extension {
    private static final Logger log = LoggerFactory.getLogger(ProducerConfigExtension.class);

    private final Method testMethod;

    @SuppressWarnings("unused")
    public ProducerConfigExtension() {
        this(null);
    }

    public ProducerConfigExtension(Method testMethod) {
        this.testMethod = testMethod;
    }

    @SuppressWarnings("unused")
    void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) throws Exception {
        Map, Annotation> values = new HashMap<>();
        // get class annotations first:
        Class declaringClass = testMethod.getDeclaringClass();
        while (declaringClass != null && !declaringClass.equals(Object.class)) {
            addConfigValues(values, declaringClass.getAnnotations());
            declaringClass = declaringClass.getEnclosingClass();
        }
        // method annotations will override class annotations:
        addConfigValues(values, testMethod.getAnnotations());
        for (final Annotation annotation : values.values()) {
            log.debug("Defining bean: value={} class={} ",
                    annotation, annotation.getClass().getName());
            AnnotatedType at = bm.createAnnotatedType(annotation.getClass());
            final InjectionTarget it = bm.createInjectionTarget(at);
            abd.addBean(new Bean() {
                @Override
                public Class getBeanClass() {
                    return annotation.annotationType();
                }

                @Override
                public Set getInjectionPoints() {
                    return it.getInjectionPoints();
                }

                @Override
                public String getName() {
                    return null;
                }

                @Override
                public Set getQualifiers() {
                    Set qualifiers = new HashSet();
                    qualifiers.add(new AnnotationLiteral() {});
                    qualifiers.add(new AnnotationLiteral() {});
                    return qualifiers;
                }

                @Override
                public Class getScope() {
                    return Dependent.class;
                }

                @Override
                public Set> getStereotypes() {
                    return Collections.emptySet();
                }

                @Override
                public Set getTypes() {
                    Set types = new HashSet();
                    types.add(annotation.annotationType());
                    types.add(Annotation.class);
                    types.add(Object.class);
                    return types;
                }

                @Override
                public boolean isAlternative() {
                    return false;
                }

                @Override
                public boolean isNullable() {
                    return false;
                }

                @Override
                public Annotation create(CreationalContext ctx) {
                    // We return the same instance every time (despite @Dependent)
                    // but Annotations are immutable and thus safe to share.
                    return annotation;
                }

                @Override
                public void destroy(Annotation instance,
                        CreationalContext ctx) {
                    ctx.release();
                }

            });
        }
    }

    private static void addConfigValues(Map, Annotation> values, Annotation[] annotations) {
        for (final Annotation annotation : annotations) {
            if (!annotation.annotationType().isAnnotationPresent(ProducerConfig.class)) {
                continue;
            }
            if (!Modifier.isPublic(annotation.annotationType().getModifiers())) {
                throw new RuntimeException("ProducerConfig annotation classes must be public");
            }
            values.put(annotation.annotationType(), annotation);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy