Skip to content

Commit

Permalink
Move back weld reinit from ConfigureListener into FacesInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Sep 30, 2023
1 parent cdea232 commit 6e8224e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
43 changes: 43 additions & 0 deletions impl/src/main/java/com/sun/faces/config/FacesInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import static com.sun.faces.RIConstants.FACES_SERVLET_REGISTRATION;
import static com.sun.faces.util.Util.isEmpty;
import static java.lang.Boolean.parseBoolean;
import static java.util.logging.Level.WARNING;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import com.sun.faces.util.FacesLogger;

import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.faces.annotation.FacesConfig;
import jakarta.faces.application.ResourceDependencies;
import jakarta.faces.application.ResourceDependency;
Expand Down Expand Up @@ -84,6 +89,8 @@
public class FacesInitializer implements ServletContainerInitializer {

// NOTE: Logging should not be used with this class.
// NOTE: It can, we only need to ensure that the logger is only invoked when there's a (Init)FacesContext.
private static final Logger LOGGER = FacesLogger.CONFIG.getLogger();

public static final String FACES_PACKAGE_PREFIX = "jakarta.faces.";
public static final String MOJARRA_PACKAGE_PREFIX = "com.sun.faces.";
Expand Down Expand Up @@ -112,6 +119,7 @@ public void onStartup(Set<Class<?>> classes, ServletContext servletContext) thro
}

// Other concerns also handled if there is an existing Faces Servlet mapping
handleCdiConcerns(servletContext);
handleWebSocketConcerns(servletContext);

// The Configure listener will do the bulk of initializing (configuring) Faces in a later phase.
Expand Down Expand Up @@ -204,6 +212,41 @@ private static void handleMappingConcerns(ServletContext servletContext) throws
servletContext.setAttribute(FACES_SERVLET_REGISTRATION, newFacesServletRegistration);
}

private static void handleCdiConcerns(ServletContext context) throws ServletException {
// If Weld is used as CDI impl then make sure it doesn't skip initialization when there's no BDA. See also #5232 and #5321

if (context.getAttribute("org.jboss.weld.environment.servlet.jakarta.enterprise.inject.spi.BeanManager") instanceof BeanManager) {
// Already initialized.
return;
}

ClassLoader cl = context.getClassLoader();
Class<?> weldInitializerClass;

try {
weldInitializerClass = cl.loadClass("org.jboss.weld.environment.servlet.EnhancedListener");
}
catch (ClassNotFoundException ignore) {
// Weld is actually not being used. That's OK for now, so just continue as usual.
return;
}

// Weld is being used so let's make sure it doesn't skip initialization when there's no BDA.
context.setInitParameter("org.jboss.weld.environment.servlet.archive.isolation", "false");

if (context.getAttribute("org.jboss.weld.environment.servlet.enhancedListenerUsed") == Boolean.TRUE) {
try {
LOGGER.log(WARNING, "Weld skipped initialization - forcing it to reinitialize");
ServletContainerInitializer weldInitializer = (ServletContainerInitializer) weldInitializerClass.getConstructor().newInstance();
weldInitializer.onStartup(null, context);
}
catch (Exception | LinkageError e) {
// Weld is being used but it failed for unclear reason while CDI should be enabled. That's not OK, so rethrow as ServletException.
throw new ServletException("Reinitializing Weld failed - giving up, please make sure your project contains at least one bean class with a bean defining annotation and retry", e);
}
}
}

private static void handleWebSocketConcerns(ServletContext context) throws ServletException {
if (context.getAttribute(ServerContainer.class.getName()) != null) {
// Already initialized
Expand Down
15 changes: 0 additions & 15 deletions impl/src/main/java/com/sun/faces/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import static java.util.Collections.emptyList;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;

import java.beans.FeatureDescriptor;
import java.io.IOException;
Expand Down Expand Up @@ -101,7 +100,6 @@
import jakarta.faces.event.AbortProcessingException;
import jakarta.faces.render.ResponseStateManager;
import jakarta.faces.webapp.FacesServlet;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import jakarta.servlet.http.HttpServletMapping;
Expand Down Expand Up @@ -1476,19 +1474,6 @@ public static BeanManager getCdiBeanManager(FacesContext facesContext) {
if (result == null && facesContext != null) {
Map<String, Object> applicationMap = facesContext.getExternalContext().getApplicationMap();
result = (BeanManager) applicationMap.get("org.jboss.weld.environment.servlet.jakarta.enterprise.inject.spi.BeanManager");

if (result == null && applicationMap.get("org.jboss.weld.environment.servlet.enhancedListenerUsed") == Boolean.TRUE) {
LOGGER.log(WARNING, "Weld skipped initialization - forcing it to reinitialize");
try {
ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
servletContext.setInitParameter("org.jboss.weld.environment.servlet.archive.isolation", "false");
ServletContainerInitializer weld = (ServletContainerInitializer) Class.forName("org.jboss.weld.environment.servlet.EnhancedListener").getConstructor().newInstance();
weld.onStartup(null, servletContext);
result = (BeanManager) applicationMap.get("org.jboss.weld.environment.servlet.jakarta.enterprise.inject.spi.BeanManager");
} catch (Exception | LinkageError e) {
LOGGER.log(WARNING, "Reinitializing Weld failed - giving up, please make sure your project contains at least one bean class with a bean defining annotation and retry", e);
}
}
}

if (result != null && facesContext != null) {
Expand Down

0 comments on commit 6e8224e

Please sign in to comment.