Skip to content

Commit

Permalink
WFLY-18708 Disable counter-productive "distributable" behavior in Moj…
Browse files Browse the repository at this point in the history
…arra
  • Loading branch information
pferraro committed Nov 2, 2023
1 parent 2d7cc1b commit 68a24e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
5 changes: 5 additions & 0 deletions jsf/subsystem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import jakarta.faces.application.ViewHandler;
import org.jboss.as.jsf.logging.JSFLogger;
import org.jboss.as.jsf.subsystem.JSFResourceDefinition;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
Expand All @@ -18,8 +19,11 @@
import org.jboss.metadata.javaee.spec.ParamValueMetaData;
import org.jboss.metadata.web.jboss.JBossServletMetaData;
import org.jboss.metadata.web.jboss.JBossWebMetaData;
import org.jboss.metadata.web.spec.ListenerMetaData;
import org.jboss.metadata.web.spec.MultipartConfigMetaData;

import com.sun.faces.config.ConfigureListener;

/**
* @author Stuart Douglas
*/
Expand Down Expand Up @@ -66,11 +70,12 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
if(JsfVersionMarker.isJsfDisabled(deploymentUnit)) {
return;
}
if(metaData == null || metaData.getMergedJBossWebMetaData() == null || metaData.getMergedJBossWebMetaData().getServlets() == null) {
JBossWebMetaData webMetaData = (metaData != null) ? metaData.getMergedJBossWebMetaData() : null;
if (webMetaData == null || webMetaData.getServlets() == null) {
return;
}
JBossServletMetaData jsf = null;
for(JBossServletMetaData servlet : metaData.getMergedJBossWebMetaData().getServlets()) {
for(JBossServletMetaData servlet : webMetaData.getServlets()) {
if(JAVAX_FACES_WEBAPP_FACES_SERVLET.equals(servlet.getServletClass())) {
jsf = servlet;
}
Expand All @@ -81,22 +86,34 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
}
if (disallowDoctypeDecl != null) {
// Add the disallowDoctypeDecl context param if it's not already present
setContextParameterIfAbsent(metaData.getMergedJBossWebMetaData(), DISALLOW_DOCTYPE_DECL, disallowDoctypeDecl.toString());
setContextParameterIfAbsent(webMetaData, DISALLOW_DOCTYPE_DECL, disallowDoctypeDecl.toString());
}
// Auto-disable lazy bean validation for distributable web application.
// This can otherwise cause missing @PreDestroy events.
if (metaData.getMergedJBossWebMetaData().getDistributable() != null) {
if (webMetaData.getDistributable() != null) {
// Auto-disable lazy bean validation for distributable web application.
// This can otherwise cause missing @PreDestroy events.
String disabled = Boolean.toString(false);
if (!setContextParameterIfAbsent(metaData.getMergedJBossWebMetaData(), LAZY_BEAN_VALIDATION_PARAM, disabled).equals(disabled)) {
if (!setContextParameterIfAbsent(webMetaData, LAZY_BEAN_VALIDATION_PARAM, disabled).equals(disabled)) {
JSFLogger.ROOT_LOGGER.lazyBeanValidationEnabled();
}

String version = JsfVersionMarker.getVersion(deploymentUnit);
// Disable counter-productive "distributable" logic in Mojarra implementation
if (version.equals(JsfVersionMarker.JSF_4_0) && JSFModuleIdFactory.getInstance().getImplModId(version).getSlot().equals(JSFResourceDefinition.DEFAULT_SLOT)) {
ListenerMetaData mojarraListenerMetaData = new ListenerMetaData();
mojarraListenerMetaData.setListenerClass(ConfigureListener.class.getName());
ListenerMetaData workaroundListenerMetaData = new ListenerMetaData();
workaroundListenerMetaData.setListenerClass(NonDistributableServletContextListener.class.getName());

webMetaData.getListeners().add(mojarraListenerMetaData);
// Ensure workaround listener runs after Mojarra's bootstrap listener
webMetaData.getListeners().add(workaroundListenerMetaData);
}
}
// Set a default buffer size as 1024 is too small
final JBossWebMetaData webMetaData = metaData.getMergedJBossWebMetaData();
// First check the legacy facelets.BUFFER_SIZE property which is required for backwards compatibility
if (!hasContextParam(webMetaData, "facelets.BUFFER_SIZE")) {
// The legacy parameter has not been set, set a default buffer if the current parameter name has not been set.
setContextParameterIfAbsent(metaData.getMergedJBossWebMetaData(), ViewHandler.FACELETS_BUFFER_SIZE_PARAM_NAME, Integer.toString(defaultBufferSize));
setContextParameterIfAbsent(webMetaData, ViewHandler.FACELETS_BUFFER_SIZE_PARAM_NAME, Integer.toString(defaultBufferSize));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.jsf.deployment;

import jakarta.faces.context.FacesContext;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;

import com.sun.faces.config.InitFacesContext;
import com.sun.faces.config.WebConfiguration;

/**
* Workaround for counter-productive "distributable" logic in Mojarra.
* This setting is used to trigger redundant calls to HttpSession.setAttribute(...) for mutated attributes.
*/
public class NonDistributableServletContextListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
FacesContext facesContext = new InitFacesContext(context);
try {
WebConfiguration.getInstance(context).setOptionEnabled(WebConfiguration.BooleanWebContextInitParameter.EnableDistributable, false);
context.setAttribute(WebConfiguration.BooleanWebContextInitParameter.EnableDistributable.getQualifiedName(), Boolean.FALSE);
} finally {
facesContext.release();
}
}
}

0 comments on commit 68a24e1

Please sign in to comment.