You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2023/05/23 05:24:27 UTC
[struts] branch master updated: WW-5301 Fix custom VelocityManager bean selection
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/master by this push:
new 9a7398f9a WW-5301 Fix custom VelocityManager bean selection
new 88c847e53 Merge pull request #687 from atlassian/WW-5301-velocity-bean-selection
9a7398f9a is described below
commit 9a7398f9af13f0303baba17ccbc6a0d944188570
Author: Kusal Kithul-Godage <gi...@kusal.io>
AuthorDate: Sat May 20 19:56:11 2023 +1000
WW-5301 Fix custom VelocityManager bean selection
---
.../org/apache/struts2/dispatcher/Dispatcher.java | 23 +++++++++++++++++-----
.../apache/struts2/dispatcher/DispatcherTest.java | 4 +++-
.../{struts-plugin.xml => struts-deferred.xml} | 14 -------------
.../velocity/src/main/resources/struts-plugin.xml | 2 --
4 files changed, 21 insertions(+), 22 deletions(-)
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
index f7c3ad764..51ae95d27 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
@@ -78,7 +78,6 @@ import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
@@ -109,6 +108,8 @@ public class Dispatcher {
public static final String MULTIPART_FORM_DATA_REGEX = "^multipart/form-data(?:\\s*;\\s*boundary=[0-9a-zA-Z'()+_,\\-./:=?]{1,70})?(?:\\s*;\\s*charset=[a-zA-Z\\-0-9]{3,14})?";
+ private static final String CONFIG_SPLIT_REGEX = "\\s*,\\s*";
+
/**
* Provide a thread local instance.
*/
@@ -427,10 +428,14 @@ public class Dispatcher {
if (configPaths == null) {
configPaths = DEFAULT_CONFIGURATION_PATHS;
}
- String[] files = configPaths.split("\\s*[,]\\s*");
+ loadConfigPaths(configPaths);
+ }
+
+ private void loadConfigPaths(String configPaths) {
+ String[] files = configPaths.split(CONFIG_SPLIT_REGEX);
for (String file : files) {
if (file.endsWith(".xml")) {
- configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
+ configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, servletContext));
} else {
throw new IllegalArgumentException("Invalid configuration file name");
}
@@ -452,7 +457,7 @@ public class Dispatcher {
private void init_JavaConfigurations() {
String configClasses = initParams.get("javaConfigClasses");
if (configClasses != null) {
- String[] classes = configClasses.split("\\s*[,]\\s*");
+ String[] classes = configClasses.split(CONFIG_SPLIT_REGEX);
for (String cname : classes) {
try {
Class<?> cls = ClassLoaderUtil.loadClass(cname, this.getClass());
@@ -476,7 +481,7 @@ public class Dispatcher {
private void init_CustomConfigurationProviders() {
String configProvs = initParams.get("configProviders");
if (configProvs != null) {
- String[] classes = configProvs.split("\\s*[,]\\s*");
+ String[] classes = configProvs.split(CONFIG_SPLIT_REGEX);
for (String cname : classes) {
try {
Class cls = ClassLoaderUtil.loadClass(cname, this.getClass());
@@ -521,6 +526,13 @@ public class Dispatcher {
configurationManager.addContainerProvider(new StrutsBeanSelectionProvider());
}
+ /**
+ * `struts-deferred.xml` can be used to load configuration which is sensitive to loading order such as 'bean-selection' elements
+ */
+ private void init_DeferredXmlConfigurations() {
+ loadConfigPaths("struts-deferred.xml");
+ }
+
private Container init_PreloadConfiguration() {
return getContainer();
}
@@ -554,6 +566,7 @@ public class Dispatcher {
init_CustomConfigurationProviders(); // [5]
init_FilterInitParameters(); // [6]
init_AliasStandardObjects(); // [7]
+ init_DeferredXmlConfigurations();
Container container = init_PreloadConfiguration();
container.inject(this);
diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
index d19b77803..73a526535 100644
--- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
+++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
@@ -51,6 +51,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
+import java.util.Set;
/**
* Test case for Dispatcher.
@@ -230,12 +231,13 @@ public class DispatcherTest extends StrutsInternalTestCase {
du.init();
Configuration config = du.getConfigurationManager().getConfiguration();
assertNotNull(config);
- HashSet<String> expected = new HashSet<>();
+ Set<String> expected = new HashSet<>();
expected.add("struts-default.xml");
expected.add("struts-beans.xml");
expected.add("struts-excluded-classes.xml");
expected.add("struts-plugin.xml");
expected.add("struts.xml");
+ expected.add("struts-deferred.xml");
assertEquals(expected, config.getLoadedFileNames());
assertTrue(config.getPackageConfigs().size() > 0);
PackageConfig packageConfig = config.getPackageConfig("struts-default");
diff --git a/plugins/velocity/src/main/resources/struts-plugin.xml b/plugins/velocity/src/main/resources/struts-deferred.xml
similarity index 64%
copy from plugins/velocity/src/main/resources/struts-plugin.xml
copy to plugins/velocity/src/main/resources/struts-deferred.xml
index 48ae45bdd..914a6496d 100644
--- a/plugins/velocity/src/main/resources/struts-plugin.xml
+++ b/plugins/velocity/src/main/resources/struts-deferred.xml
@@ -25,20 +25,6 @@
<struts>
- <bean name="struts" class="org.apache.struts2.views.velocity.VelocityManager"/>
-
- <bean type="org.apache.struts2.components.template.TemplateEngine" name="vm"
- class="org.apache.struts2.views.velocity.template.VelocityTemplateEngine"/>
-
- <bean type="org.apache.struts2.views.TagLibraryDirectiveProvider" name="s"
- class="org.apache.struts2.views.velocity.VelocityTagLibrary"/>
-
- <package name="velocity-default" extends="struts-default">
- <result-types>
- <result-type name="velocity" class="org.apache.struts2.views.velocity.result.VelocityResult"/>
- </result-types>
- </package>
-
<bean-selection name="velocityBeans" class="org.apache.struts2.views.velocity.VelocityBeanSelectionProvider"/>
</struts>
diff --git a/plugins/velocity/src/main/resources/struts-plugin.xml b/plugins/velocity/src/main/resources/struts-plugin.xml
index 48ae45bdd..7630d41e3 100644
--- a/plugins/velocity/src/main/resources/struts-plugin.xml
+++ b/plugins/velocity/src/main/resources/struts-plugin.xml
@@ -39,6 +39,4 @@
</result-types>
</package>
- <bean-selection name="velocityBeans" class="org.apache.struts2.views.velocity.VelocityBeanSelectionProvider"/>
-
</struts>