Skip to content

Commit

Permalink
Merge pull request #5344 from eclipse-ee4j/faces_issue_1416_add_conte…
Browse files Browse the repository at this point in the history
…xt_param_attributes_to_FacesConfig

Add context param attributes to @FacesConfig
  • Loading branch information
BalusC authored Mar 24, 2024
2 parents 445c971 + cc05332 commit 6490bfe
Show file tree
Hide file tree
Showing 33 changed files with 155 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@

import static com.sun.faces.RIConstants.FACES_CONFIG_VERSION;
import static com.sun.faces.RIConstants.FACES_PREFIX;
import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.AutomaticExtensionlessMapping;
import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.FaceletsSkipComments;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.FaceletsDecorators;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.FaceletsDefaultRefreshPeriod;
import static com.sun.faces.el.ELUtils.buildFacesResolver;
import static com.sun.faces.el.FacesCompositeELResolver.ELResolverChainType.Faces;
import static com.sun.faces.facelets.util.ReflectionUtil.forName;
import static com.sun.faces.util.MessageUtils.APPLICATION_ASSOCIATE_EXISTS_ID;
import static com.sun.faces.util.MessageUtils.getExceptionMessageString;
import static com.sun.faces.util.Util.getFacesConfigXmlVersion;
import static com.sun.faces.util.Util.getFacesServletRegistration;
import static com.sun.faces.util.Util.split;
import static jakarta.faces.FactoryFinder.FACELET_CACHE_FACTORY;
import static jakarta.faces.FactoryFinder.FLOW_HANDLER_FACTORY;
import static jakarta.faces.application.ProjectStage.Development;
import static jakarta.faces.application.ProjectStage.Production;
import static jakarta.faces.application.ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME;
import static java.lang.Long.parseLong;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.logging.Level.FINE;
Expand All @@ -61,7 +54,6 @@
import com.sun.faces.application.resource.ResourceManager;
import com.sun.faces.component.search.SearchExpressionHandlerImpl;
import com.sun.faces.config.ConfigManager;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.el.DemuxCompositeELResolver;
import com.sun.faces.facelets.compiler.Compiler;
import com.sun.faces.facelets.compiler.SAXCompiler;
Expand All @@ -85,6 +77,7 @@
import jakarta.el.ExpressionFactory;
import jakarta.faces.FacesException;
import jakarta.faces.FactoryFinder;
import jakarta.faces.annotation.FacesConfig.ContextParam;
import jakarta.faces.application.Application;
import jakarta.faces.application.NavigationCase;
import jakarta.faces.application.ViewHandler;
Expand Down Expand Up @@ -162,8 +155,6 @@ public class ApplicationAssociate {

private final NamedEventManager namedEventManager;

private final WebConfiguration webConfig;

private FlowHandler flowHandler;

private SearchExpressionHandler searchExpressionHandler;
Expand Down Expand Up @@ -245,7 +236,6 @@ public ApplicationAssociate(ApplicationImpl appImpl) {

navigationMap = new ConcurrentHashMap<>();
injectionProvider = (InjectionProvider) facesContext.getAttributes().get(ConfigManager.INJECTION_PROVIDER_KEY);
webConfig = WebConfiguration.getInstance(externalContext);

annotationManager = new AnnotationManager();

Expand Down Expand Up @@ -313,7 +303,7 @@ public void processEvent(SystemEvent event) {
String facesConfigVersion = getFacesConfigXmlVersion(context);
context.getExternalContext().getApplicationMap().put(FACES_CONFIG_VERSION, facesConfigVersion);

if (webConfig.isOptionEnabled(AutomaticExtensionlessMapping)) {
if (ContextParam.AUTOMATIC_EXTENSIONLESS_MAPPING.isSet(context)) {
getFacesServletRegistration(context)
.ifPresent(registration ->
viewHandler.getViews(context, "/", RETURN_AS_MINIMAL_IMPLICIT_OUTCOME)
Expand All @@ -331,9 +321,8 @@ public void initializeFacelets() {

FacesContext ctx = FacesContext.getCurrentInstance();

Map<String, Object> appMap = ctx.getExternalContext().getApplicationMap();
compiler = createCompiler(appMap, webConfig);
faceletFactory = createFaceletFactory(ctx, compiler, webConfig);
compiler = createCompiler(ctx);
faceletFactory = createFaceletFactory(ctx, compiler);
}

public long getTimeOfInstantiation() {
Expand Down Expand Up @@ -620,20 +609,10 @@ public void relateUrlToDefiningDocumentInJar(URL url, String definingDocumentId)
definingDocumentIdsToTruncatedJarUrls.put(definingDocumentId, candidate);
}

protected DefaultFaceletFactory createFaceletFactory(FacesContext context, Compiler compiler, WebConfiguration webConfig) {
protected DefaultFaceletFactory createFaceletFactory(FacesContext context, Compiler compiler) {

// refresh period
boolean isProduction = applicationImpl.getProjectStage() == Production;
String refreshPeriod;
if (webConfig.isSet(FaceletsDefaultRefreshPeriod)) {
refreshPeriod = webConfig.getOptionValue(FaceletsDefaultRefreshPeriod);
} else if (isProduction) {
refreshPeriod = "-1";
} else {
refreshPeriod = FaceletsDefaultRefreshPeriod.getDefaultValue();
}

long period = parseLong(refreshPeriod);
int period = ContextParam.FACELETS_REFRESH_PERIOD.getValue(context);

// resource resolver
DefaultResourceResolver resolver = new DefaultResourceResolver(applicationImpl.getResourceHandler());
Expand All @@ -647,35 +626,33 @@ protected DefaultFaceletFactory createFaceletFactory(FacesContext context, Compi
return toReturn;
}

protected Compiler createCompiler(Map<String, Object> appMap, WebConfiguration webConfig) {
protected Compiler createCompiler(FacesContext context) {
Compiler newCompiler = new SAXCompiler();

loadDecorators(appMap, newCompiler);
loadDecorators(context, newCompiler);

// Skip params?
newCompiler.setTrimmingComments(webConfig.isOptionEnabled(FaceletsSkipComments));
newCompiler.setTrimmingComments(ContextParam.FACELETS_SKIP_COMMENTS.isSet(context));

addTagLibraries(newCompiler);

return newCompiler;
}

protected void loadDecorators(Map<String, Object> appMap, Compiler newCompiler) {
String decoratorsParamValue = webConfig.getOptionValue(FaceletsDecorators);
protected void loadDecorators(FacesContext context, Compiler newCompiler) {
String[] decorators = ContextParam.FACELETS_DECORATORS.getValue(context);

if (decoratorsParamValue != null) {
for (String decorator : split(appMap, decoratorsParamValue.trim(), ";")) {
try {
newCompiler
.addTagDecorator((TagDecorator) forName(decorator).getDeclaredConstructor().newInstance());
for (String decorator : decorators) {
try {
newCompiler
.addTagDecorator((TagDecorator) forName(decorator).getDeclaredConstructor().newInstance());

if (LOGGER.isLoggable(FINE)) {
LOGGER.log(FINE, "Successfully Loaded Decorator: {0}", decorator);
}
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException e) {
if (LOGGER.isLoggable(SEVERE)) {
LOGGER.log(SEVERE, "Error Loading Decorator: " + decorator, e);
}
if (LOGGER.isLoggable(FINE)) {
LOGGER.log(FINE, "Successfully Loaded Decorator: {0}", decorator);
}
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException e) {
if (LOGGER.isLoggable(SEVERE)) {
LOGGER.log(SEVERE, "Error Loading Decorator: " + decorator, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.sun.faces.application;

import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.ClientWindowMode;
import static com.sun.faces.util.Util.getCdiBeanManager;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;
Expand All @@ -32,12 +31,13 @@

import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.Producer;
import jakarta.faces.annotation.FacesConfig.ContextParam;
import jakarta.faces.context.FacesContext;
import jakarta.faces.flow.Flow;
import jakarta.faces.flow.FlowHandler;
import jakarta.faces.flow.builder.FlowDefinition;

class JavaFlowLoaderHelper {
public class JavaFlowLoaderHelper {

private static final Logger LOGGER = FacesLogger.APPLICATION.getLogger();

Expand Down Expand Up @@ -70,24 +70,26 @@ synchronized void loadFlows(FacesContext context, FlowHandler flowHandler) throw
}
}

private void enableClientWindowModeIfNecessary(FacesContext context) {
WebConfiguration config = WebConfiguration.getInstance(context.getExternalContext());

String optionValue = config.getOptionValue(ClientWindowMode);
public static void enableClientWindowModeIfNecessary(FacesContext context) {
String optionValue = ContextParam.CLIENT_WINDOW_MODE.getValue(context);

boolean clientWindowNeedsEnabling = false;
if ("none".equals(optionValue)) {
clientWindowNeedsEnabling = true;

LOGGER.log(WARNING, "{0} was set to none, but Faces Flows requires {0} is enabled. Setting to ''url''.",
new Object[] { ClientWindowMode.getQualifiedName() });
new Object[] { ContextParam.CLIENT_WINDOW_MODE.getName() });

} else if (optionValue == null) {
clientWindowNeedsEnabling = true;
}

if (clientWindowNeedsEnabling) {
config.setOptionValue(ClientWindowMode, "url");
context.getExternalContext().getApplicationMap().put(JavaFlowLoaderHelper.class.getName(), Boolean.TRUE);
}
}

public static boolean isClientWindowModeForciblyEnabled(FacesContext context) {
return context.getExternalContext().getApplicationMap().get(JavaFlowLoaderHelper.class.getName()) == Boolean.TRUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.sun.faces.application.applicationimpl;

import static com.sun.faces.application.ApplicationImpl.THIS_LIBRARY;
import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.DateTimeConverterUsesSystemTimezone;
import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.RegisterConverterPropertyEditors;
import static com.sun.faces.util.Util.isEmpty;
import static com.sun.faces.util.Util.loadClass;
Expand Down Expand Up @@ -60,6 +59,7 @@
import jakarta.el.ValueExpression;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.faces.FacesException;
import jakarta.faces.annotation.FacesConfig.ContextParam;
import jakarta.faces.application.Application;
import jakarta.faces.application.Resource;
import jakarta.faces.component.UIComponent;
Expand Down Expand Up @@ -164,10 +164,11 @@ public InstanceFactory(ApplicationAssociate applicationAssociate) {
defaultValidatorIds = new LinkedHashSet<>();
behaviorMap = new ViewMemberInstanceFactoryMetadataMap<>(new ConcurrentHashMap<>());

WebConfiguration webConfig = WebConfiguration.getInstance(FacesContext.getCurrentInstance().getExternalContext());
FacesContext context = FacesContext.getCurrentInstance();
WebConfiguration webConfig = WebConfiguration.getInstance(context.getExternalContext());
registerPropertyEditors = webConfig.isOptionEnabled(RegisterConverterPropertyEditors);

passDefaultTimeZone = webConfig.isOptionEnabled(DateTimeConverterUsesSystemTimezone);
passDefaultTimeZone = ContextParam.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE.getValue(context);
if (passDefaultTimeZone) {
systemTimeZone = TimeZone.getDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@

package com.sun.faces.application.applicationimpl;

import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.JakartaFacesProjectStage;
import static jakarta.faces.application.ProjectStage.Development;
import static jakarta.faces.application.ProjectStage.Production;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;

import java.util.logging.Logger;

import com.sun.faces.application.ValidateComponentNesting;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.FacesLogger;

import jakarta.faces.annotation.FacesConfig.ContextParam;
import jakarta.faces.application.Application;
import jakarta.faces.application.ProjectStage;
import jakarta.faces.context.FacesContext;
Expand All @@ -45,9 +43,7 @@ public class Stage {
public ProjectStage getProjectStage(Application application) {

if (projectStage == null) {
String value = fetchProjectStageFromConfig();

setProjectStageFromValue(value, Production);
projectStage = fetchProjectStageFromConfig();

if (projectStage == Development) {
application.subscribeToEvent(PostAddToViewEvent.class, new ValidateComponentNesting());
Expand All @@ -59,38 +55,24 @@ public ProjectStage getProjectStage(Application application) {

// ----------------------------------------------------------- Private methods

private String fetchProjectStageFromConfig() {
WebConfiguration webConfig = WebConfiguration.getInstance(FacesContext.getCurrentInstance().getExternalContext());
private ProjectStage fetchProjectStageFromConfig() {
FacesContext context = FacesContext.getCurrentInstance();
WebConfiguration webConfig = WebConfiguration.getInstance(context.getExternalContext());
String value = webConfig.getEnvironmentEntry(WebConfiguration.WebEnvironmentEntry.ProjectStage);

if (value != null) {
projectStage = ProjectStage.valueOf(value);
if (LOGGER.isLoggable(FINE)) {
LOGGER.log(FINE, "ProjectStage configured via JNDI: {0}", value);
}
} else {
value = webConfig.getOptionValue(JakartaFacesProjectStage);
if (value != null && LOGGER.isLoggable(FINE)) {
projectStage = ContextParam.PROJECT_STAGE.getValue(context);
if (LOGGER.isLoggable(FINE)) {
LOGGER.log(FINE, "ProjectStage configured via servlet context init parameter: {0}", value);
}
}

return value;
}

private void setProjectStageFromValue(String value, ProjectStage defaultStage) {
if (value != null) {
try {
projectStage = ProjectStage.valueOf(value);
} catch (IllegalArgumentException iae) {
if (LOGGER.isLoggable(INFO)) {
LOGGER.log(INFO, "Unable to discern ProjectStage for value {0}.", value);
}
}
}

if (projectStage == null) {
projectStage = defaultStage;
}
return projectStage;
}

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

import static com.sun.faces.RIConstants.FLOW_IN_JAR_PREFIX;
import static com.sun.faces.config.WebConfiguration.META_INF_CONTRACTS_DIR;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.FaceletsSuffix;
import static com.sun.faces.util.Util.ensureLeadingSlash;
import static jakarta.faces.application.ResourceVisitOption.TOP_LEVEL_VIEWS_ONLY;
import static java.util.Spliterator.DISTINCT;
import static java.util.Spliterators.spliteratorUnknownSize;
Expand All @@ -35,11 +35,11 @@
import java.util.stream.Stream;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.Util;

import jakarta.enterprise.inject.Any;
import jakarta.faces.FacesException;
import jakarta.faces.annotation.FacesConfig.ContextParam;
import jakarta.faces.annotation.View;
import jakarta.faces.application.ResourceVisitOption;
import jakarta.faces.context.ExternalContext;
Expand All @@ -55,8 +55,8 @@ public class FaceletWebappResourceHelper extends ResourceHelper {

public FaceletWebappResourceHelper(WebappResourceHelper webappResourceHelper) {
this.webappResourceHelper = webappResourceHelper;
WebConfiguration webConfig = WebConfiguration.getInstance();
configuredExtensions = webConfig.getOptionValue(FaceletsSuffix, " ");
FacesContext context = FacesContext.getCurrentInstance();
configuredExtensions = new String[] { ContextParam.FACELETS_SUFFIX.getValue(context) };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.DefaultResourceMaxAge;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.ResourceBufferSize;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.ResourceExcludes;
import static com.sun.faces.util.RequestStateManager.RESOURCE_REQUEST;
import static com.sun.faces.util.Util.getFacesMapping;
import static com.sun.faces.util.Util.notNegative;
Expand Down Expand Up @@ -51,8 +50,8 @@
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.RequestStateManager;
import com.sun.faces.util.Util;

import jakarta.faces.annotation.FacesConfig.ContextParam;
import jakarta.faces.application.Resource;
import jakarta.faces.application.ResourceHandler;
import jakarta.faces.application.ResourceVisitOption;
Expand Down Expand Up @@ -81,9 +80,10 @@ public class ResourceHandlerImpl extends ResourceHandler {
public ResourceHandlerImpl() {
creationTime = System.currentTimeMillis();
webconfig = WebConfiguration.getInstance();
ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
manager = ApplicationAssociate.getInstance(extContext).getResourceManager();
initExclusions(extContext.getApplicationMap());
initExclusions(context);
initMaxAge();
}

Expand Down Expand Up @@ -559,9 +559,8 @@ private boolean isExcluded(String resourceId) {
* <ul>
* will be used.
*/
private void initExclusions(Map<String, Object> appMap) {
String excludesParam = webconfig.getOptionValue(ResourceExcludes);
String[] patterns = Util.split(appMap, excludesParam, " ");
private void initExclusions(FacesContext context) {
String[] patterns = ContextParam.RESOURCE_EXCLUDES.getValue(context);

excludePatterns = new ArrayList<>(patterns.length);
for (String pattern : patterns) {
Expand Down
Loading

0 comments on commit 6490bfe

Please sign in to comment.