Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CDI) Auto-detect beans on Rest services Refs: #30187 #30417

Merged
merged 24 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,19 @@
<version>3.1.9.Final</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>


<dependency>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>bean-validator</artifactId>
Expand Down
12 changes: 11 additions & 1 deletion dotCMS/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,17 @@
<version>3.1.9.Final</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
</dependency>

<!-- This is required keep one single instance of the InjectionManager across all ServletContexts -->
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x-servlet</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>bean-validator</artifactId>
Expand All @@ -1429,7 +1440,6 @@
<artifactId>jandex</artifactId>
<version>3.0.5</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,11 @@ public AnalyticsTool() {
}

private static ContentAnalyticsAPI getContentAnalyticsAPI() {
final Optional<ContentAnalyticsAPI> contentAnalyticsAPI = CDIUtils.getBean(ContentAnalyticsAPI.class);
if (!contentAnalyticsAPI.isPresent()) {
throw new DotRuntimeException("Could not instance ContentAnalyticsAPI");
}
return contentAnalyticsAPI.get();
return CDIUtils.getBeanThrows(ContentAnalyticsAPI.class);
}

private static AnalyticsQueryParser getAnalyticsQueryParser() {
final Optional<AnalyticsQueryParser> queryParserOptional = CDIUtils.getBean(AnalyticsQueryParser.class);
if (!queryParserOptional.isPresent()) {
throw new DotRuntimeException("Could not instance AnalyticsQueryParser");
}
return queryParserOptional.get();
return CDIUtils.getBeanThrows(AnalyticsQueryParser.class);
}

public AnalyticsTool(final ContentAnalyticsAPI contentAnalyticsAPI,
Expand Down

This file was deleted.

28 changes: 21 additions & 7 deletions dotCMS/src/main/java/com/dotcms/cdi/CDIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,34 @@ private CDIUtils() {
}

/**
* Get a bean from CDI container
* Get a bean from CDI container and return an Optional with the bean if found, empty otherwise
* @param clazz the class of the bean
* @return an Optional with the bean if found, empty otherwise
*/
public static <T> Optional<T> getBean(Class<T> clazz) {
try {
return Optional.of(getBeanThrows(clazz));
} catch (Exception e) {
// Exception is already logged in getBeanThrows
}
return Optional.empty();
}


/**
* Get a bean from CDI container but throw an exception if the bean is not found
* @param clazz the class of the bean
* @return the bean
* @param <T> the type of the bean
*/
public static <T> T getBeanThrows(Class<T> clazz) {
try {
return Optional.of(CDI.current().select(clazz).get());
return CDI.current().select(clazz).get();
} catch (Exception e) {
Logger.error(CDIUtils.class,
String.format("Unable to find bean of class [%s] [%s]", clazz, e.getMessage())
);
String errorMessage = String.format("Unable to find bean of class [%s]: %s", clazz, e.getMessage());
Logger.error(CDIUtils.class, errorMessage);
throw new IllegalStateException(errorMessage, e);
}
return Optional.empty();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.function.Consumer;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
* Manages the processing of jobs in a distributed job queue system. This class is responsible for
Expand Down Expand Up @@ -138,7 +139,7 @@ public class JobQueueManagerAPIImpl implements JobQueueManagerAPI {
* - Initializes event handlers for various job state changes.
*/
@Inject
public JobQueueManagerAPIImpl(JobQueue jobQueue,
public JobQueueManagerAPIImpl(@Named("queueProducer") JobQueue jobQueue,
JobQueueConfig jobQueueConfig,
CircuitBreaker circuitBreaker,
RetryStrategy defaultRetryStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dotmarketing.util.Config;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;

/**
* This class is responsible for producing the JobQueue implementation used in the application. It
Expand All @@ -22,17 +23,16 @@ public class JobQueueProducer {
*
* @return A JobQueue instance
*/
@Named("queueProducer")
@Produces
@ApplicationScoped
public JobQueue produceJobQueue() {

if (JOB_QUEUE_IMPLEMENTATION_TYPE.equals("postgres")) {
return new PostgresJobQueue();
}

throw new IllegalStateException(
"Unknown job queue implementation type: " + JOB_QUEUE_IMPLEMENTATION_TYPE
);
throw new IllegalStateException("Unknown job queue implementation type: " + JOB_QUEUE_IMPLEMENTATION_TYPE);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* @see Job
* @see JobState
*/

public class PostgresJobQueue implements JobQueue {

private static final String CREATE_JOB_QUEUE_QUERY = "INSERT INTO job_queue "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.inject.Inject;
import org.glassfish.jersey.server.JSONP;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -57,13 +58,7 @@ public class ContentAnalyticsResource {
private final WebResource webResource;
private final ContentAnalyticsAPI contentAnalyticsAPI;

@SuppressWarnings("unused")
public ContentAnalyticsResource() {
this(CDIUtils.getBean(ContentAnalyticsAPI.class).orElseGet(APILocator::getContentAnalyticsAPI));
}

//@Inject
@VisibleForTesting
@Inject
public ContentAnalyticsResource(final ContentAnalyticsAPI contentAnalyticsAPI) {
this(new WebResource(), contentAnalyticsAPI);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.dotcms.rest.api.v1.job;

import com.dotcms.cdi.CDIUtils;
import com.dotcms.jobs.business.job.Job;
import com.dotcms.jobs.business.job.JobPaginatedResult;
import com.dotcms.rest.ResponseEntityView;
Expand All @@ -14,6 +13,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
Expand All @@ -38,8 +38,9 @@ public class JobQueueResource {

private final JobQueueHelper helper;

public JobQueueResource() {
this(new WebResource(), CDIUtils.getBean(JobQueueHelper.class).orElseThrow(()->new IllegalStateException("JobQueueHelper Bean not found")));
@Inject
public JobQueueResource(final JobQueueHelper helper) {
this(new WebResource(), helper);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.glassfish.jersey.server.spi.Container;

/**
* A new Reloader will get created on each reload there can only be one container at a time
* A new Re-loader will get created on each reload there can only be one container at a time
*/
@Provider
@ApplicationScoped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import io.swagger.v3.jaxrs2.integration.resources.AcceptHeaderOpenApiResource;
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.servers.Server;
Expand Down Expand Up @@ -57,7 +58,7 @@ public DotRestApplication() {
"com.dotcms.rendering.js",
"com.dotcms.ai.rest",
"io.swagger.v3.jaxrs2"
);
).register(CdiComponentProvider.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1443,8 +1443,8 @@ Object create() {
case SYSTEM_API: return new SystemAPIImpl();
case ARTIFICIAL_INTELLIGENCE_API: return new DotAIAPIFacadeImpl();
case ACHECKER_API: return new ACheckerAPIImpl();
case CONTENT_ANALYTICS_API: CDIUtils.getBean(ContentAnalyticsAPI.class).orElseThrow(() -> new DotRuntimeException("Content Analytics API not found"));
case JOB_QUEUE_MANAGER_API: return CDIUtils.getBean(JobQueueManagerAPI.class).orElseThrow(() -> new DotRuntimeException("JobQueueManagerAPI not found"));
case CONTENT_ANALYTICS_API: return CDIUtils.getBeanThrows(ContentAnalyticsAPI.class);
case JOB_QUEUE_MANAGER_API: return CDIUtils.getBeanThrows(JobQueueManagerAPI.class);
}
throw new AssertionError("Unknown API index: " + this);
}
Expand Down
8 changes: 8 additions & 0 deletions dotCMS/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
<scan>
<exclude name="org.apache.**"/>
</scan>
</beans>
9 changes: 0 additions & 9 deletions dotCMS/src/main/webapp/WEB-INF/beans.xml

This file was deleted.

16 changes: 0 additions & 16 deletions dotCMS/src/test/java/com/dotcms/UnitTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
import com.liferay.portal.model.Company;
import com.liferay.portal.model.User;
import java.util.TimeZone;
import org.jboss.weld.bootstrap.api.helpers.RegistrySingletonProvider;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.mockito.Mockito;

Expand All @@ -24,8 +20,6 @@ public abstract class UnitTestBase extends BaseMessageResources {
protected static final ContentTypeAPI contentTypeAPI = mock(ContentTypeAPI.class);
protected static final CompanyAPI companyAPI = mock(CompanyAPI.class);

private static WeldContainer weld;

public static class MyAPILocator extends APILocator {

static {
Expand All @@ -47,10 +41,6 @@ protected CompanyAPI getCompanyAPIImpl() {
@BeforeClass
public static void prepare () throws DotDataException, DotSecurityException, Exception {

weld = new Weld().containerId(RegistrySingletonProvider.STATIC_INSTANCE)
.initialize();

System.out.println("Weld :: " + weld);

Config.initializeConfig();
Config.setProperty("API_LOCATOR_IMPLEMENTATION", MyAPILocator.class.getName());
Expand All @@ -64,10 +54,4 @@ public static void prepare () throws DotDataException, DotSecurityException, Exc
Mockito.lenient().when(companyAPI.getDefaultCompany()).thenReturn(company);
}

@AfterClass
public static void cleanup() {
if( null != weld && weld.isRunning() ){
weld.shutdown();
}
}
}
9 changes: 0 additions & 9 deletions dotCMS/src/test/resources/META-INF/beans.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.dotcms;

import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.runners.model.InitializationError;

/**
* Annotate your JUnit4 test using {@code @DataProviderRunner} class with {@code @RunWith(DataProviderWeldRunner.class)} to run it with Weld container.
*/
public class DataProviderWeldRunner extends DataProviderRunner {

private static final Weld WELD;
private static final WeldContainer CONTAINER;

static {
WELD = new Weld("DataProviderWeldRunner");
CONTAINER = WELD.initialize();
}

/**
* Creates a DataProviderRunner to run supplied {@code clazz}.
*
* @param clazz the test {@link Class} to run
* @throws InitializationError if the test {@link Class} is malformed.
*/
public DataProviderWeldRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}

/**
* Create the test instance using Weld container.
* @return the test instance
* @throws Exception if something goes wrong
*/
@Override
protected Object createTest() throws Exception {
return CONTAINER.instance().select(getTestClass().getJavaClass()).get();
}

}
Loading