Class ResteasyCdiExtension

java.lang.Object
org.jboss.resteasy.cdi.ResteasyCdiExtension
All Implemented Interfaces:
jakarta.enterprise.inject.spi.Extension

public class ResteasyCdiExtension extends Object implements jakarta.enterprise.inject.spi.Extension
A CDI Extension that integrates Jakarta REST components with CDI. This extension:
  • Assigns default scopes to discovered Jakarta REST resources (RequestScoped), providers (ApplicationScoped), and Application subclasses (ApplicationScoped)
  • Adds @Inject to @Context-annotated fields, setter methods, and constructors so that CDI can inject context values
  • Registers the @Context and @Default beans for the Jakarta REST context types
  • Wraps InjectionTarget instances for Jakarta REST components within JaxrsInjectionTarget to handle property injection and validation
  • Builds the session bean interface map used by CdiInjectorFactory for EJB lookup
  • Tracks CDI-managed Jakarta REST components in the ResteasyBeanContainer
Author:
Jozef Hartinger, James R. Perkins
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    clearDiscoveredTypes(jakarta.enterprise.inject.spi.AfterDeploymentValidation event)
    Clears the types recorded during bean discovery, which are not used once the deployment is validated.
     
    static boolean
     
    <T extends jakarta.ws.rs.core.Application>
    void
    observeApplications(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
    Adds @Inject to the @Context injection points of Application subclasses and records them, so their scope and whether they are managed by CDI can be decided in observeBeanAttributes(ProcessBeanAttributes, BeanManager).
    <T> void
    observeBeanAttributes(jakarta.enterprise.inject.spi.ProcessBeanAttributes<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
    Decides the scope of the resources, providers and Application subclasses recorded when their annotated type was processed, and whether they are managed by CDI, i.e. added to the bean container.
    void
    observeBeforeBeanDiscovery(jakarta.enterprise.inject.spi.BeforeBeanDiscovery event)
    Obtain BeanManager reference for future use.
    <T> void
    observeInjectionTarget(jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event)
    Wrap InjectionTarget of JAX-RS components within JaxrsInjectionTarget which takes care of JAX-RS property injection.
    <T> void
    observeProviderContracts(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
    Adds @Inject to the @Context injection points of a discovered type which implements a provider contract, is not annotated with @Provider and does not declare a scope.
    <T> void
    observeProviders(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
    Adds @Inject to the @Context injection points of providers and records them, so their scope and whether they are managed by CDI can be decided in observeBeanAttributes(ProcessBeanAttributes, BeanManager).
    <T> void
    observeResources(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
    Adds @Inject to the @Context injection points of resources and records them, so their scope and whether they are managed by CDI can be decided in observeBeanAttributes(ProcessBeanAttributes, BeanManager).
    <T> void
    observeSessionBeans(jakarta.enterprise.inject.spi.ProcessSessionBean<T> event)
    Observes ProcessSessionBean events and creates a (Bean class -> Local interface) map for Session beans with local interfaces.
    void
    processClientBean(jakarta.enterprise.inject.spi.ProcessBean<?> pb)
    Process any client beans.
    void
    registerBeans(jakarta.enterprise.inject.spi.AfterBeanDiscovery event)
    Registers producers for required injections types and for a REST client.
    void
    registerContextProducers(jakarta.enterprise.inject.spi.AfterBeanDiscovery event, jakarta.enterprise.inject.spi.BeanManager beanManager)
    Registers producers for the Jakarta REST context types, the Application and the Jakarta Servlet types which are required to be injectable.
    void
    validateContextTypeBeans(jakarta.enterprise.inject.spi.ProcessBean<?> event)
    Fails the deployment if a bean other than the ones registered by this extension is a @Default or @Context bean for a Jakarta REST context type.
    void
    validateProviderScope(jakarta.enterprise.inject.spi.ProcessManagedBean<?> event)
    Fails the deployment if a provider is explicitly declared @Dependent.
    protected <T> jakarta.enterprise.inject.spi.InjectionTarget<T>
    wrapInjectionTarget(jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ResteasyCdiExtension

      public ResteasyCdiExtension()
  • Method Details

    • isCDIActive

      public static boolean isCDIActive()
    • observeBeforeBeanDiscovery

      public void observeBeforeBeanDiscovery(@Observes jakarta.enterprise.inject.spi.BeforeBeanDiscovery event)
      Obtain BeanManager reference for future use.
      Parameters:
      event - event
    • processClientBean

      public void processClientBean(@Observes jakarta.enterprise.inject.spi.ProcessBean<?> pb)
      Process any client beans.
      Parameters:
      pb - the bean being processed
    • validateContextTypeBeans

      public void validateContextTypeBeans(@Observes jakarta.enterprise.inject.spi.ProcessBean<?> event)
      Fails the deployment if a bean other than the ones registered by this extension is a @Default or @Context bean for a Jakarta REST context type. These would be ambiguous with the beans this extension always registers.
      Parameters:
      event - the bean being processed
    • validateProviderScope

      public void validateProviderScope(@Observes jakarta.enterprise.inject.spi.ProcessManagedBean<?> event)
      Fails the deployment if a provider is explicitly declared @Dependent. Providers are application singletons, while each injection of a @Dependent bean creates a new instance. A provider which declares type variables is excluded, as CDI requires such a bean to be @Dependent. Session beans are excluded, as an unscoped session bean is made @Dependent.
      Parameters:
      event - the managed bean being processed
    • registerBeans

      public void registerBeans(@Observes jakarta.enterprise.inject.spi.AfterBeanDiscovery event)
      Registers producers for required injections types and for a REST client.
      Parameters:
      event - the after bean discovery event
    • registerContextProducers

      public void registerContextProducers(@Observes jakarta.enterprise.inject.spi.AfterBeanDiscovery event, jakarta.enterprise.inject.spi.BeanManager beanManager)
      Registers producers for the Jakarta REST context types, the Application and the Jakarta Servlet types which are required to be injectable.

      The Jakarta REST context types are owned by the implementation, so the @Context and @Default beans are always registered. For the Application and the Servlet types, the @Default bean is only registered if no other bean already provides the type.

      Parameters:
      event - the after bean discovery event
      beanManager - the bean manager
    • clearDiscoveredTypes

      public void clearDiscoveredTypes(@Observes jakarta.enterprise.inject.spi.AfterDeploymentValidation event)
      Clears the types recorded during bean discovery, which are not used once the deployment is validated.
      Parameters:
      event - the after deployment validation event
    • observeResources

      public <T> void observeResources(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
      Adds @Inject to the @Context injection points of resources and records them, so their scope and whether they are managed by CDI can be decided in observeBeanAttributes(ProcessBeanAttributes, BeanManager).
      Type Parameters:
      T - type
      Parameters:
      event - event
      beanManager - the bean manager
    • observeProviders

      public <T> void observeProviders(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
      Adds @Inject to the @Context injection points of providers and records them, so their scope and whether they are managed by CDI can be decided in observeBeanAttributes(ProcessBeanAttributes, BeanManager).
      Type Parameters:
      T - type
      Parameters:
      event - event
      beanManager - the bean manager
    • observeProviderContracts

      public <T> void observeProviderContracts(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
      Adds @Inject to the @Context injection points of a discovered type which implements a provider contract, is not annotated with @Provider and does not declare a scope. Such a type is made an @ApplicationScoped provider, or a @Singleton provider if the type cannot be proxied, in observeBeanAttributes(ProcessBeanAttributes, BeanManager). Providers are application singletons, while such a type would otherwise be @Dependent. This can only happen when the type is discovered without a bean defining annotation, e.g. in a bean-discovery-mode="all" archive, and is registered as a provider through Application.getClasses() or a Feature.

      Only types with a @Context annotation are observed, as other types have no injection points to change. The scope of all such types is set in observeBeanAttributes(ProcessBeanAttributes, BeanManager).

      Type Parameters:
      T - the type
      Parameters:
      event - the annotated type being processed
      beanManager - the bean manager
    • observeBeanAttributes

      public <T> void observeBeanAttributes(@Observes jakarta.enterprise.inject.spi.ProcessBeanAttributes<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
      Decides the scope of the resources, providers and Application subclasses recorded when their annotated type was processed, and whether they are managed by CDI, i.e. added to the bean container. This is only invoked for beans, so a type which is not a bean, e.g. one with a constructor CDI cannot satisfy, is not added to the bean container and is created by RESTEasy.

      The @Path, @Provider and @ApplicationPath stereotypes may define the default scopes. The same defaults are set if no scope is defined, e.g. if the annotations are not stereotypes in the environment or @Path is only present on methods. In addition:

      • A resource which does not define a scope is made @RequestScoped. An unproxyable resource which does not declare a scope is made @Dependent, which does not require a client proxy and is still created for each request.
      • A @Provider which does not define a scope is made @ApplicationScoped. An unproxyable one which does not declare a scope is made @Singleton, which does not require a client proxy.
      • A provider without @Provider which does not declare a scope is made @ApplicationScoped, or @Singleton if it cannot be proxied.
      • A resource or provider which declares type variables and does not declare a scope is made @Dependent, as CDI requires for a bean whose type contains type variables. RESTEasy still creates a single instance of such a provider.
      • An Application subclass which does not declare a scope, e.g. without @ApplicationPath, is made @ApplicationScoped.
      A warning is logged when the default scope of the @Path or @Provider stereotype is overridden, as the application should declare the scope.
      Type Parameters:
      T - the type
      Parameters:
      event - the bean attributes being processed
      beanManager - the bean manager
    • observeApplications

      public <T extends jakarta.ws.rs.core.Application> void observeApplications(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager)
      Adds @Inject to the @Context injection points of Application subclasses and records them, so their scope and whether they are managed by CDI can be decided in observeBeanAttributes(ProcessBeanAttributes, BeanManager). A stateless or singleton session bean is not recorded, and is made @Dependent if it does not declare a scope.
      Type Parameters:
      T - type
      Parameters:
      event - event
      beanManager - the bean manager
    • observeInjectionTarget

      public <T> void observeInjectionTarget(@Observes jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event)
      Wrap InjectionTarget of JAX-RS components within JaxrsInjectionTarget which takes care of JAX-RS property injection.
      Type Parameters:
      T - type
      Parameters:
      event - event
    • observeSessionBeans

      public <T> void observeSessionBeans(@Observes jakarta.enterprise.inject.spi.ProcessSessionBean<T> event)
      Observes ProcessSessionBean events and creates a (Bean class -> Local interface) map for Session beans with local interfaces. This map is necessary since RESTEasy identifies a bean class as JAX-RS components while CDI requires a local interface to be used for lookup.
      Type Parameters:
      T - type
      Parameters:
      event - event
    • getSessionBeanInterface

      public Map<Class<?>,Type> getSessionBeanInterface()
    • wrapInjectionTarget

      protected <T> jakarta.enterprise.inject.spi.InjectionTarget<T> wrapInjectionTarget(jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event)