Search in sources :

Example 1 with PLATFORM_BEFORE

use of jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE in project helidon by oracle.

the class MicronautCdiExtension method afterBeanDiscovery.

/**
 * Add all (not yet added) Micronaut beans for injection as long as they are singletons.
 *
 * @param event CDI event
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
void afterBeanDiscovery(@Priority(PLATFORM_BEFORE) @Observes AfterBeanDiscovery event) {
    event.addBean().addType(ApplicationContext.class).id(MICRONAUT_BEAN_PREFIX + "context").scope(ApplicationScoped.class).produceWith(instance -> micronautContext.get());
    // add the remaining Micronaut beans
    for (var entry : unprocessedBeans.entrySet()) {
        Class<?> beanType = entry.getKey();
        List<MicronautBean> beans = entry.getValue();
        List<? extends BeanDefinitionReference<?>> refs = List.of();
        if (beans.size() > 1) {
            // first make sure these are singletons; if not, ignore
            refs = beans.stream().map(MicronautBean::definitionRef).filter(it -> !it.getBeanType().getName().endsWith("$Intercepted")).filter(BeanDefinitionReference::isSingleton).collect(Collectors.toList());
        }
        // primary
        event.addBean().addType(beanType).id(MICRONAUT_BEAN_PREFIX + beanType.getName()).scope(Dependent.class).produceWith(instance -> micronautContext.get().getBean(beanType));
        if (refs.size() > 1) {
            // we must care about qualifiers
            for (var ref : refs) {
                AnnotationMetadata annotationMetadata = ref.getAnnotationMetadata();
                List<Class<? extends Annotation>> qualifiers = annotationMetadata.getAnnotationTypesByStereotype(Qualifier.class);
                Annotation[] synthesized = new Annotation[qualifiers.size()];
                io.micronaut.context.Qualifier[] mq = new io.micronaut.context.Qualifier[qualifiers.size()];
                for (int i = 0; i < qualifiers.size(); i++) {
                    Annotation annotation = annotationMetadata.synthesize(qualifiers.get(i));
                    synthesized[i] = annotation;
                    if (annotation != null) {
                        mq[i] = Qualifiers.byAnnotation(annotation);
                    }
                }
                io.micronaut.context.Qualifier composite = Qualifiers.byQualifiers(mq);
                BeanConfigurator<Object> newBean = event.addBean().addType(beanType).id(MICRONAUT_BEAN_PREFIX + ref.getBeanDefinitionName()).scope(Dependent.class).produceWith(instance -> micronautContext.get().getBean(beanType, composite));
                for (Annotation annotation : synthesized) {
                    newBean.addQualifier(annotation);
                }
            }
        }
    }
    unprocessedBeans.clear();
}
Also used : Dependent(jakarta.enterprise.context.Dependent) Around(io.micronaut.aop.Around) Observes(jakarta.enterprise.event.Observes) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutableMethod(io.micronaut.inject.ExecutableMethod) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) ApplicationContext(io.micronaut.context.ApplicationContext) Extension(jakarta.enterprise.inject.spi.Extension) ProcessAnnotatedType(jakarta.enterprise.inject.spi.ProcessAnnotatedType) BeforeBeanDiscovery(jakarta.enterprise.inject.spi.BeforeBeanDiscovery) MethodInterceptor(io.micronaut.aop.MethodInterceptor) Map(java.util.Map) PLATFORM_AFTER(jakarta.interceptor.Interceptor.Priority.PLATFORM_AFTER) BeanConfigurator(jakarta.enterprise.inject.spi.configurator.BeanConfigurator) Priority(jakarta.annotation.Priority) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) Initialized(jakarta.enterprise.context.Initialized) PLATFORM_BEFORE(jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE) Iterator(java.util.Iterator) PropertySource(io.micronaut.context.env.PropertySource) Collection(java.util.Collection) ServiceDefinition(io.micronaut.core.io.service.ServiceDefinition) Set(java.util.Set) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) AdvisedBeanType(io.micronaut.inject.AdvisedBeanType) Config(org.eclipse.microprofile.config.Config) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) BeforeDestroyed(jakarta.enterprise.context.BeforeDestroyed) Objects(java.util.Objects) List(java.util.List) Type(io.micronaut.context.annotation.Type) Stream(java.util.stream.Stream) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) BeanDefinition(io.micronaut.inject.BeanDefinition) Comparator(java.util.Comparator) Qualifier(jakarta.inject.Qualifier) BeanDefinitionReference(io.micronaut.inject.BeanDefinitionReference) Dependent(jakarta.enterprise.context.Dependent) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Annotation(java.lang.annotation.Annotation) ApplicationContext(io.micronaut.context.ApplicationContext) BeanDefinitionReference(io.micronaut.inject.BeanDefinitionReference) Qualifier(jakarta.inject.Qualifier)

Example 2 with PLATFORM_BEFORE

use of jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE in project helidon by oracle.

the class JwtAuthCdiExtension method registerProvider.

void registerProvider(@Observes @Initialized(ApplicationScoped.class) @Priority(PLATFORM_BEFORE + 5) Object event, BeanManager bm) {
    // Security extension to update and check builder
    SecurityCdiExtension security = bm.getExtension(SecurityCdiExtension.class);
    if (security.securityBuilder().hasProvider(JwtAuthProviderService.PROVIDER_NAME)) {
        return;
    }
    // JAX-RS extension to get to applications to see if we are needed
    JaxRsCdiExtension jaxrs = bm.getExtension(JaxRsCdiExtension.class);
    boolean notNeeded = jaxrs.applicationsToRun().stream().map(JaxRsApplication::applicationClass).flatMap(Optional::stream).map(clazz -> clazz.getAnnotation(LoginConfig.class)).filter(Objects::nonNull).map(LoginConfig::authMethod).noneMatch("MP-JWT"::equals);
    if (notNeeded) {
        return;
    }
    security.securityBuilder().addProvider(JwtAuthProvider.create(config), JwtAuthProviderService.PROVIDER_NAME);
}
Also used : Dependent(jakarta.enterprise.context.Dependent) Arrays(java.util.Arrays) JsonValue(jakarta.json.JsonValue) JaxRsCdiExtension(io.helidon.microprofile.server.JaxRsCdiExtension) AnnotationLiteral(jakarta.enterprise.util.AnnotationLiteral) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) AfterDeploymentValidation(jakarta.enterprise.inject.spi.AfterDeploymentValidation) RuntimeStart(io.helidon.microprofile.cdi.RuntimeStart) AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) BeforeBeanDiscovery(jakarta.enterprise.inject.spi.BeforeBeanDiscovery) Map(java.util.Map) DeploymentException(jakarta.enterprise.inject.spi.DeploymentException) JsonObject(jakarta.json.JsonObject) Method(java.lang.reflect.Method) Initialized(jakarta.enterprise.context.Initialized) Claims(org.eclipse.microprofile.jwt.Claims) PLATFORM_BEFORE(jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE) BeanManager(jakarta.enterprise.inject.spi.BeanManager) TYPE(java.lang.annotation.ElementType.TYPE) FIELD(java.lang.annotation.ElementType.FIELD) Member(java.lang.reflect.Member) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) JaxRsApplication(io.helidon.microprofile.server.JaxRsApplication) Type(java.lang.reflect.Type) Optional(java.util.Optional) JsonNumber(jakarta.json.JsonNumber) JsonArray(jakarta.json.JsonArray) SecurityCdiExtension(io.helidon.microprofile.security.SecurityCdiExtension) Observes(jakarta.enterprise.event.Observes) ClaimValue(org.eclipse.microprofile.jwt.ClaimValue) PARAMETER(java.lang.annotation.ElementType.PARAMETER) ProcessInjectionPoint(jakarta.enterprise.inject.spi.ProcessInjectionPoint) Constructor(java.lang.reflect.Constructor) Instance(jakarta.enterprise.inject.Instance) Retention(java.lang.annotation.Retention) LoginConfig(org.eclipse.microprofile.auth.LoginConfig) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField) Extension(jakarta.enterprise.inject.spi.Extension) Priority(jakarta.annotation.Priority) LinkedList(java.util.LinkedList) Config(io.helidon.config.Config) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) Claim(org.eclipse.microprofile.jwt.Claim) Target(java.lang.annotation.Target) Nonbinding(jakarta.enterprise.util.Nonbinding) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) METHOD(java.lang.annotation.ElementType.METHOD) ParameterizedType(java.lang.reflect.ParameterizedType) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Annotated(jakarta.enterprise.inject.spi.Annotated) Provider(jakarta.inject.Provider) Qualifier(jakarta.inject.Qualifier) RequestScoped(jakarta.enterprise.context.RequestScoped) JsonString(jakarta.json.JsonString) Optional(java.util.Optional) SecurityCdiExtension(io.helidon.microprofile.security.SecurityCdiExtension) Objects(java.util.Objects) JaxRsCdiExtension(io.helidon.microprofile.server.JaxRsCdiExtension)

Aggregations

Priority (jakarta.annotation.Priority)2 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)2 Dependent (jakarta.enterprise.context.Dependent)2 Initialized (jakarta.enterprise.context.Initialized)2 Observes (jakarta.enterprise.event.Observes)2 AfterBeanDiscovery (jakarta.enterprise.inject.spi.AfterBeanDiscovery)2 BeforeBeanDiscovery (jakarta.enterprise.inject.spi.BeforeBeanDiscovery)2 Extension (jakarta.enterprise.inject.spi.Extension)2 Qualifier (jakarta.inject.Qualifier)2 PLATFORM_BEFORE (jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE)2 Config (io.helidon.config.Config)1 RuntimeStart (io.helidon.microprofile.cdi.RuntimeStart)1 SecurityCdiExtension (io.helidon.microprofile.security.SecurityCdiExtension)1 JaxRsApplication (io.helidon.microprofile.server.JaxRsApplication)1 JaxRsCdiExtension (io.helidon.microprofile.server.JaxRsCdiExtension)1 Around (io.micronaut.aop.Around)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 ApplicationContext (io.micronaut.context.ApplicationContext)1 Type (io.micronaut.context.annotation.Type)1 PropertySource (io.micronaut.context.env.PropertySource)1