Skip to content

Commit

Permalink
ContextParam cannot be checked in FacesInitializer because there is no
Browse files Browse the repository at this point in the history
guarantee that CDI is present at that moment, so logic has been moved
into ConfigureListener
  • Loading branch information
BalusC committed Dec 14, 2024
1 parent 3a3fd1b commit 89d855b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 49 deletions.
44 changes: 28 additions & 16 deletions impl/src/main/java/com/sun/faces/config/ConfigureListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,6 @@
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.application.WebappLifecycleListener;
import com.sun.faces.el.ELContextImpl;
import com.sun.faces.push.WebsocketEndpoint;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MojarraThreadFactory;
import com.sun.faces.util.ReflectionUtils;
import com.sun.faces.util.Timer;
import com.sun.faces.util.Util;

import jakarta.el.ELManager;
import jakarta.faces.FactoryFinder;
import jakarta.faces.annotation.FacesConfig.ContextParam;
Expand All @@ -79,6 +64,7 @@
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.ServletRegistration;
import jakarta.servlet.ServletRequestEvent;
import jakarta.servlet.ServletRequestListener;
import jakarta.servlet.http.HttpSession;
Expand All @@ -87,13 +73,30 @@
import jakarta.websocket.server.ServerContainer;
import jakarta.websocket.server.ServerEndpointConfig;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.application.WebappLifecycleListener;
import com.sun.faces.el.ELContextImpl;
import com.sun.faces.push.WebsocketEndpoint;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MojarraThreadFactory;
import com.sun.faces.util.ReflectionUtils;
import com.sun.faces.util.Timer;
import com.sun.faces.util.Util;

/**
* Parse all relevant Faces configuration resources, and configure the Mojarra runtime
* environment.
*/
public class ConfigureListener implements ServletRequestListener, HttpSessionListener, ServletContextListener {

private static final Logger LOGGER = FacesLogger.CONFIG.getLogger();
private static final String[] FACES_SERVLET_MAPPINGS_WITH_XHTML = { "/faces/*", "*.jsf", "*.faces", "*.xhtml" };
private static final String[] FACES_SERVLET_MAPPINGS_WITHOUT_XHTML = { "/faces/*", "*.jsf", "*.faces" };

private ScheduledThreadPoolExecutor webResourcePool;

Expand Down Expand Up @@ -130,7 +133,7 @@ public void contextInitialized(ServletContextEvent servletContextEvent) {
// Check to see if the FacesServlet is present in the
// web.xml. If it is, perform faces configuration as normal,
// otherwise, simply return.
Object facesServletRegistration = servletContext.getAttribute(FACES_SERVLET_REGISTRATION); // If found by FacesInitializer.
ServletRegistration facesServletRegistration = (ServletRegistration) servletContext.getAttribute(FACES_SERVLET_REGISTRATION); // If found by FacesInitializer.

WebXmlProcessor webXmlProcessor = new WebXmlProcessor(servletContext);
if (facesServletRegistration == null) {
Expand All @@ -148,6 +151,15 @@ public void contextInitialized(ServletContextEvent servletContextEvent) {
} else {
LOGGER.log(FINE, "FacesServlet found in deployment descriptor - processing configuration.");
}
} else if (servletContext.getAttribute(FACES_SERVLET_MAPPINGS) != null) { // If automatic mapping needs to be handled.
if (ContextParam.DISABLE_FACESSERVLET_TO_XHTML.isSet(initFacesContext)) {
facesServletRegistration.addMapping(FACES_SERVLET_MAPPINGS_WITHOUT_XHTML);
}
else {
facesServletRegistration.addMapping(FACES_SERVLET_MAPPINGS_WITH_XHTML);
}

servletContext.setAttribute(FACES_SERVLET_MAPPINGS, facesServletRegistration.getMappings());
}

// Do not override if already defined
Expand Down
28 changes: 3 additions & 25 deletions impl/src/main/java/com/sun/faces/config/FacesInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
import static com.sun.faces.RIConstants.ANNOTATED_CLASSES;
import static com.sun.faces.RIConstants.FACES_SERVLET_MAPPINGS;
import static com.sun.faces.RIConstants.FACES_SERVLET_REGISTRATION;
import static com.sun.faces.util.Util.getExistingFacesServletRegistration;
import static com.sun.faces.util.Util.isEmpty;
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.annotation.FacesConfig.ContextParam;
Expand Down Expand Up @@ -66,6 +64,8 @@
import jakarta.servlet.annotation.HandlesTypes;
import jakarta.websocket.server.ServerContainer;

import com.sun.faces.util.FacesLogger;

/**
* Initializes Jakarta Faces if at least one of the following conditions is met:
*
Expand Down Expand Up @@ -98,8 +98,6 @@ public class FacesInitializer implements ServletContainerInitializer {

private static final String FACES_CONFIG_RESOURCE_PATH = "/WEB-INF/faces-config.xml";
private static final String FACES_SERVLET_CLASS_NAME = FacesServlet.class.getName();
private static final String[] FACES_SERVLET_MAPPINGS_WITH_XHTML = { "/faces/*", "*.jsf", "*.faces", "*.xhtml" };
private static final String[] FACES_SERVLET_MAPPINGS_WITHOUT_XHTML = { "/faces/*", "*.jsf", "*.faces" };

// -------------------------------- Methods from ServletContainerInitializer

Expand Down Expand Up @@ -174,18 +172,6 @@ private static boolean isFacesServletRegistrationPresent(ServletContext context)
return getExistingFacesServletRegistration(context) != null;
}

private static ServletRegistration getExistingFacesServletRegistration(ServletContext servletContext) {
Map<String, ? extends ServletRegistration> existing = servletContext.getServletRegistrations();

for (ServletRegistration registration : existing.values()) {
if (FACES_SERVLET_CLASS_NAME.equals(registration.getClassName())) {
return registration;
}
}

return null;
}

private static void handleMappingConcerns(ServletContext servletContext, FacesContext facesContext) throws ServletException {
ServletRegistration existingFacesServletRegistration = getExistingFacesServletRegistration(servletContext);

Expand All @@ -196,14 +182,6 @@ private static void handleMappingConcerns(ServletContext servletContext, FacesCo
}

ServletRegistration newFacesServletRegistration = servletContext.addServlet(FacesServlet.class.getSimpleName(), FACES_SERVLET_CLASS_NAME);

if (ContextParam.DISABLE_FACESSERVLET_TO_XHTML.isSet(facesContext)) {
newFacesServletRegistration.addMapping(FACES_SERVLET_MAPPINGS_WITHOUT_XHTML);
}
else {
newFacesServletRegistration.addMapping(FACES_SERVLET_MAPPINGS_WITH_XHTML);
}

servletContext.setAttribute(FACES_SERVLET_MAPPINGS, newFacesServletRegistration.getMappings());
servletContext.setAttribute(FACES_SERVLET_REGISTRATION, newFacesServletRegistration);
}
Expand Down
16 changes: 8 additions & 8 deletions impl/src/main/java/com/sun/faces/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import com.sun.faces.RIConstants;
import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.config.manager.FacesSchema;
import com.sun.faces.facelets.component.UIRepeat;
import com.sun.faces.io.FastStringWriter;

import jakarta.el.ValueExpression;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.CDI;
Expand Down Expand Up @@ -111,6 +104,13 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.MappingMatch;

import com.sun.faces.RIConstants;
import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.config.manager.FacesSchema;
import com.sun.faces.facelets.component.UIRepeat;
import com.sun.faces.io.FastStringWriter;

/**
* <B>Util</B> is a class ...
*
Expand Down Expand Up @@ -183,7 +183,7 @@ private static Collection<String> getFacesServletMappings(ServletContext servlet
return emptyList();
}

private static ServletRegistration getExistingFacesServletRegistration(ServletContext servletContext) {
public static ServletRegistration getExistingFacesServletRegistration(ServletContext servletContext) {
Map<String, ? extends ServletRegistration> existing = servletContext.getServletRegistrations();
for (ServletRegistration registration : existing.values()) {
if (FACES_SERVLET_CLASS.equals(registration.getClassName())) {
Expand Down

0 comments on commit 89d855b

Please sign in to comment.