From 61d2619769a53106ab20a80939db4fbed20807d0 Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Fri, 29 Dec 2023 13:46:05 +1100 Subject: [PATCH] WW-5381 Introduce extension point for CompoundRootAccessor --- .../xwork2/config/impl/DefaultConfiguration.java | 6 ++---- .../providers/StrutsDefaultConfigurationProvider.java | 3 --- .../java/com/opensymphony/xwork2/ognl/OgnlUtil.java | 3 ++- .../xwork2/ognl/OgnlValueStackFactory.java | 10 ++++++---- .../main/java/org/apache/struts2/StrutsConstants.java | 3 +++ .../struts2/config/StrutsBeanSelectionProvider.java | 6 ++++-- core/src/main/resources/struts-beans.xml | 6 +----- 7 files changed, 18 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java b/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java index 4a6ee1373e..e0b92b2bf3 100644 --- a/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java +++ b/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java @@ -89,6 +89,7 @@ import com.opensymphony.xwork2.ognl.OgnlValueStackFactory; import com.opensymphony.xwork2.ognl.SecurityMemberAccess; import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor; +import com.opensymphony.xwork2.ognl.accessor.RootAccessor; import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.OgnlTextParser; import com.opensymphony.xwork2.util.PatternMatcher; @@ -100,8 +101,6 @@ import com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory; import com.opensymphony.xwork2.util.location.LocatableProperties; import com.opensymphony.xwork2.util.reflection.ReflectionProvider; -import ognl.ClassResolver; -import ognl.PropertyAccessor; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -390,8 +389,7 @@ public static ContainerBuilder bootstrapFactories(ContainerBuilder builder) { .factory(TextParser.class, OgnlTextParser.class, Scope.SINGLETON) .factory(ObjectTypeDeterminer.class, DefaultObjectTypeDeterminer.class, Scope.SINGLETON) - .factory(PropertyAccessor.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON) - .factory(ClassResolver.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON) + .factory(RootAccessor.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON) .factory(ExpressionCacheFactory.class, DefaultOgnlExpressionCacheFactory.class, Scope.SINGLETON) .factory(BeanInfoCacheFactory.class, DefaultOgnlBeanInfoCacheFactory.class, Scope.SINGLETON) diff --git a/core/src/main/java/com/opensymphony/xwork2/config/providers/StrutsDefaultConfigurationProvider.java b/core/src/main/java/com/opensymphony/xwork2/config/providers/StrutsDefaultConfigurationProvider.java index 20f3abce88..bf537899a1 100644 --- a/core/src/main/java/com/opensymphony/xwork2/config/providers/StrutsDefaultConfigurationProvider.java +++ b/core/src/main/java/com/opensymphony/xwork2/config/providers/StrutsDefaultConfigurationProvider.java @@ -38,7 +38,6 @@ import com.opensymphony.xwork2.inject.Scope; import com.opensymphony.xwork2.ognl.ObjectProxy; import com.opensymphony.xwork2.ognl.OgnlReflectionContextFactory; -import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor; import com.opensymphony.xwork2.ognl.accessor.HttpParametersPropertyAccessor; import com.opensymphony.xwork2.ognl.accessor.ObjectAccessor; import com.opensymphony.xwork2.ognl.accessor.ObjectProxyPropertyAccessor; @@ -55,7 +54,6 @@ import com.opensymphony.xwork2.security.DefaultNotExcludedAcceptedPatternsChecker; import com.opensymphony.xwork2.security.ExcludedPatternsChecker; import com.opensymphony.xwork2.security.NotExcludedAcceptedPatternsChecker; -import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.PatternMatcher; import com.opensymphony.xwork2.util.WildcardHelper; import com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory; @@ -145,7 +143,6 @@ public void register(ContainerBuilder builder, LocatableProperties props) throws .factory(PropertyAccessor.class, Parameter.class.getName(), ParameterPropertyAccessor.class, Scope.SINGLETON) .factory(MethodAccessor.class, Object.class.getName(), XWorkMethodAccessor.class, Scope.SINGLETON) - .factory(MethodAccessor.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON) .factory(NullHandler.class, Object.class.getName(), InstantiatingNullHandler.class, Scope.SINGLETON) .factory(ActionValidatorManager.class, AnnotationActionValidatorManager.class, Scope.SINGLETON) diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java index c4ee4b655b..681aac57d7 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java @@ -21,6 +21,7 @@ import com.opensymphony.xwork2.conversion.impl.XWorkConverter; import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.ognl.accessor.RootAccessor; import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.reflection.ReflectionException; import ognl.ClassResolver; @@ -857,7 +858,7 @@ protected Map createDefaultContext(Object root) { protected Map createDefaultContext(Object root, ClassResolver resolver) { if (resolver == null) { - resolver = container.getInstance(ClassResolver.class, CompoundRoot.class.getName()); + resolver = container.getInstance(RootAccessor.class); if (resolver == null) { throw new IllegalStateException("Cannot find ClassResolver"); } diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java index b52a23ecb7..088caa0010 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java @@ -24,9 +24,9 @@ import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.ognl.accessor.RootAccessor; +import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; -import ognl.ClassResolver; import ognl.MethodAccessor; import ognl.OgnlRuntime; import ognl.PropertyAccessor; @@ -50,9 +50,11 @@ protected void setXWorkConverter(XWorkConverter converter) { this.xworkConverter = converter; } - @Inject(value = "com.opensymphony.xwork2.util.CompoundRoot") - protected void setClassResolver(ClassResolver classResolver) { - this.compoundRootAccessor = (RootAccessor) classResolver; + @Inject + protected void setCompoundRootAccessor(RootAccessor compoundRootAccessor) { + this.compoundRootAccessor = compoundRootAccessor; + OgnlRuntime.setPropertyAccessor(CompoundRoot.class, compoundRootAccessor); + OgnlRuntime.setMethodAccessor(CompoundRoot.class, compoundRootAccessor); } @Inject("system") diff --git a/core/src/main/java/org/apache/struts2/StrutsConstants.java b/core/src/main/java/org/apache/struts2/StrutsConstants.java index caddee16d6..f39258613e 100644 --- a/core/src/main/java/org/apache/struts2/StrutsConstants.java +++ b/core/src/main/java/org/apache/struts2/StrutsConstants.java @@ -220,6 +220,9 @@ public final class StrutsConstants { public static final String STRUTS_FREEMARKER_WRAPPER_ALT_MAP = "struts.freemarker.wrapper.altMap"; + /** Extension point for the Struts CompoundRootAccessor */ + public static final String STRUTS_COMPOUND_ROOT_ACCESSOR = "struts.compoundRootAccessor"; + /** The name of the xwork converter implementation */ public static final String STRUTS_XWORKCONVERTER = "struts.xworkConverter"; diff --git a/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java b/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java index 2ac92e8fbf..4895fc6f83 100644 --- a/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java +++ b/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java @@ -50,6 +50,7 @@ import com.opensymphony.xwork2.ognl.BeanInfoCacheFactory; import com.opensymphony.xwork2.ognl.ExpressionCacheFactory; import com.opensymphony.xwork2.ognl.SecurityMemberAccess; +import com.opensymphony.xwork2.ognl.accessor.RootAccessor; import com.opensymphony.xwork2.security.AcceptedPatternsChecker; import com.opensymphony.xwork2.security.ExcludedPatternsChecker; import com.opensymphony.xwork2.security.NotExcludedAcceptedPatternsChecker; @@ -387,6 +388,8 @@ public void register(ContainerBuilder builder, LocatableProperties props) { alias(FileManagerFactory.class, StrutsConstants.STRUTS_FILE_MANAGER_FACTORY, builder, props, Scope.SINGLETON); + alias(RootAccessor.class, StrutsConstants.STRUTS_COMPOUND_ROOT_ACCESSOR, builder, props); + alias(XWorkConverter.class, StrutsConstants.STRUTS_XWORKCONVERTER, builder, props); alias(CollectionConverter.class, StrutsConstants.STRUTS_CONVERTER_COLLECTION, builder, props); alias(ArrayConverter.class, StrutsConstants.STRUTS_CONVERTER_ARRAY, builder, props); @@ -428,8 +431,7 @@ public void register(ContainerBuilder builder, LocatableProperties props) { /** Checker is used mostly in interceptors, so there be one instance of checker per interceptor with Scope.PROTOTYPE **/ alias(ExcludedPatternsChecker.class, StrutsConstants.STRUTS_EXCLUDED_PATTERNS_CHECKER, builder, props, Scope.PROTOTYPE); alias(AcceptedPatternsChecker.class, StrutsConstants.STRUTS_ACCEPTED_PATTERNS_CHECKER, builder, props, Scope.PROTOTYPE); - alias(NotExcludedAcceptedPatternsChecker.class, StrutsConstants.STRUTS_NOT_EXCLUDED_ACCEPTED_PATTERNS_CHECKER - , builder, props, Scope.SINGLETON); + alias(NotExcludedAcceptedPatternsChecker.class, StrutsConstants.STRUTS_NOT_EXCLUDED_ACCEPTED_PATTERNS_CHECKER, builder, props, Scope.SINGLETON); alias(DateFormatter.class, StrutsConstants.STRUTS_DATE_FORMATTER, builder, props, Scope.SINGLETON); diff --git a/core/src/main/resources/struts-beans.xml b/core/src/main/resources/struts-beans.xml index 1238c9e056..506f9d1221 100644 --- a/core/src/main/resources/struts-beans.xml +++ b/core/src/main/resources/struts-beans.xml @@ -174,11 +174,9 @@ - - -