Lukasz Lenart created WW-5766:
---------------------------------
Summary: Support struts.scopedConstantProvider in Java-based
configuration (ConstantConfig)
Key: WW-5766
URL: https://issues.apache.org/jira/browse/WW-5766
Project: Struts 2
Issue Type: Improvement
Components: Core
Reporter: Lukasz Lenart
Fix For: 7.5.0
h2. Background
[WW-3476|https://issues.apache.org/jira/browse/WW-3476] adds package-scoped
constants. Part of that is a new bean-selection extension point,
{{ScopedConstantProvider}}, selected through the
{{struts.scopedConstantProvider}} constant
({{StrutsConstants.STRUTS_SCOPED_CONSTANT_PROVIDER}}). It is covered for XML
and properties configuration, but not for Java-based configuration
({{StrutsJavaConfiguration}}).
h2. Problem
Java-based configuration passes constants only through the typed
{{ConstantConfig}} class: {{StrutsJavaConfigurationProvider}} reads them from
{{ConstantConfig.getAllAsStringsMap()}}, and there is no way to set an
arbitrary key. {{ConstantConfig}} has no field for
{{struts.scopedConstantProvider}}, so an application using Java-based
configuration cannot select a custom {{ScopedConstantProvider}}. The only
workaround is to subclass {{ConstantConfig}} and override
{{getAllAsStringsMap()}}.
h2. What to add
In
{{core/src/main/java/org/apache/struts2/config/entities/ConstantConfig.java}},
following the existing {{localizedTextProvider}} mapping:
* a {{BeanConfig}} field {{scopedConstantProvider}}, next to
{{localizedTextProvider}}
* an entry in {{getAllAsStringsMap()}}:
{code:java}map.put(StrutsConstants.STRUTS_SCOPED_CONSTANT_PROVIDER,
beanConfToString(scopedConstantProvider));
{code}
* a getter and two setters, modelled on {{setLocalizedTextProvider}}:
{code:java}public BeanConfig getScopedConstantProvider()
public void setScopedConstantProvider(BeanConfig scopedConstantProvider)
public void setScopedConstantProvider(Class<? extends ScopedConstantProvider>
clazz)
{code}
h2. Tests
* {{ConstantConfigTest.testGetAllAsStringsMap}}: the new key maps to the bean
name.
* {{StrutsJavaConfigurationProviderTest}}: a Java configuration that registers
a custom {{ScopedConstantProvider}} bean through {{beans()}} and selects it
through {{ConstantConfig}} resolves to that bean.
* {{StrutsJavaConfigurationProviderTest}}: a {{ScopableConstants}} bean
registered through {{beans()}} satisfies the startup validation of
{{<scoped-constant>}} (this already works and is not yet covered by a test).
h2. Already working, no change needed
* Registering {{ScopableConstants}} beans and a custom provider bean through
{{StrutsJavaConfiguration.beans()}}.
* {{PackageConfig}}, the startup validation in {{DefaultConfiguration}} and
{{StrutsScopedConstantProvider}} work the same whatever configuration a value
came from.
h2. Out of scope
* Package-level overrides in Java-based configuration:
{{StrutsJavaConfiguration}} has no package concept
({{StrutsJavaConfigurationProvider.loadPackages()}} is empty), so there is
nothing to attach them to.
* Other bean-selection constants that {{ConstantConfig}} does not map either
(for example {{struts.parameterAuthorizer}}, {{struts.parameterAllowlister}},
{{struts.ognlGuard}}, {{struts.proxyService}}). That is a separate gap.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)