Skip to content

Commit

Permalink
Fixes #3989 - Update Jersey integration module to use CDI API depende…
Browse files Browse the repository at this point in the history
…ncy (#3990)
  • Loading branch information
mnriem authored Sep 18, 2024
1 parent fea423c commit 13bb65d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 51 deletions.
34 changes: 4 additions & 30 deletions extension/eclipse-jersey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>cloud.piranha.extension</groupId>
<artifactId>project</artifactId>
<version>24.10.0-SNAPSHOT</version>
</parent>

<artifactId>piranha-extension-eclipse-jersey</artifactId>
<packaging>jar</packaging>

<name>Piranha - Extension - Eclipse Jersey</name>

<properties>
<jersey.version>3.1.8</jersey.version>
</properties>

<dependencies>
<!-- compile -->
<dependency>
Expand All @@ -28,7 +20,6 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>

<!-- provided -->
<dependency>
<groupId>cloud.piranha.extension</groupId>
Expand All @@ -37,29 +28,20 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.extension</groupId>
<artifactId>piranha-extension-scinitializer</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>cdi-tck-api</artifactId>
<version>4.0.0</version>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>2.0.1.MR</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- runtime -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
Expand All @@ -81,14 +63,6 @@
<artifactId>jersey-cdi1x</artifactId>
<scope>runtime</scope>
</dependency>

<!--
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x-servlet</artifactId>
<version>${jersey.version}</version>
<scope>runtime</scope>
</dependency>-->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
import jakarta.enterprise.inject.spi.Extension;

import java.lang.System.Logger;

import cloud.piranha.core.api.WebApplication;
import cloud.piranha.core.api.WebApplicationExtension;

Expand All @@ -51,6 +49,12 @@ public class JerseyExtension implements WebApplicationExtension, Extension {
*/
private static final Logger LOGGER = System.getLogger(JerseyExtension.class.getName());

/**
* Constructor.
*/
public JerseyExtension() {
}

/**
* Configure the extension.
*
Expand All @@ -62,28 +66,43 @@ public void configure(WebApplication webApplication) {
}

/**
*
* @param beforeBean
* @param beanManager
* Register a source and target bean to force the adding of a class analyzer.
*
* @param beforeBeanDiscovery the BeforeBeanDiscovery.
* @param beanManager the BeanManager.
*/
public void register(@Observes BeforeBeanDiscovery beforeBean, BeanManager beanManager) {
// Force a class analyzer to be added that makes sure a REST resource is not attempted
// to be injected by both CDI and HK2.
//
// See https://github.com/eclipse-ee4j/jersey/issues/5745 on why this is needed
addAnnotatedTypes(beforeBean, beanManager, JerseyTargetBean.class, JerseySourceBean.class);
public void register(@Observes BeforeBeanDiscovery beforeBeanDiscovery,
BeanManager beanManager) {

LOGGER.log(TRACE, "Registering beans to force adding of class analyzer");

/*
* Force a class analyzer to be added that makes sure a REST resource is
* not attempted to be injected by both CDI and HK2.
*
* See https://github.com/eclipse-ee4j/jersey/issues/5745 on why this is
* needed.
*/
addAnnotatedTypes(beforeBeanDiscovery, beanManager,
JerseyTargetBean.class, JerseySourceBean.class);
}

/**
*
* @param beforeBean
* @param beanManager
* @param types
* Add annotated types.
*
* @param beforeBeanDiscovery the BeforeBeanDiscovery.
* @param beanManager the BeanManager.
* @param types the types to add.
*/
public static void addAnnotatedTypes(BeforeBeanDiscovery beforeBean, BeanManager beanManager, Class<?>... types) {
public static void addAnnotatedTypes(BeforeBeanDiscovery beforeBeanDiscovery,
BeanManager beanManager, Class<?>... types) {

LOGGER.log(TRACE, "Adding annotated types");

for (Class<?> type : types) {
beforeBean.addAnnotatedType(beanManager.createAnnotatedType(type), "JerseyExtension " + type.getName());
beforeBeanDiscovery.addAnnotatedType(
beanManager.createAnnotatedType(type),
"JerseyExtension " + type.getName());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@

import jakarta.enterprise.context.Dependent;

/**
* A simple source bean to force the addition of the class analyzer.
*
* @author Arjan Tijms
*/
@Dependent
public class JerseySourceBean {

/**
* Constructor.
*/
public JerseySourceBean() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;

/**
* A simple target bean to force the addition of the class analyzer.
*
* @author Arjan Tijms
*/
@Dependent
public class JerseyTargetBean {

/**
*dd
* Store the source bean.
*/
@Inject
JerseySourceBean bean;


/**
* Constructor.
*/
public JerseyTargetBean() {
}
}
2 changes: 1 addition & 1 deletion extension/eclipse-jersey/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@

requires cloud.piranha.core.api;
requires static cloud.piranha.extension.scinitializer;
requires jakarta.cdi;
requires static jakarta.cdi;
}

0 comments on commit 13bb65d

Please sign in to comment.