From 8f4d9a6f6f9b1ca7e3bca4259ec49051b0962aca Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Fri, 29 Dec 2023 13:39:14 +1100 Subject: [PATCH] WW-5381 Introduce RootAccessor interface for extension point --- .../xwork2/ognl/OgnlValueStack.java | 21 +++++++------- .../xwork2/ognl/OgnlValueStackFactory.java | 6 ++-- .../ognl/accessor/CompoundRootAccessor.java | 12 +++++--- .../xwork2/ognl/accessor/RootAccessor.java | 29 +++++++++++++++++++ 4 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 core/src/main/java/com/opensymphony/xwork2/ognl/accessor/RootAccessor.java diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java index 9a0abd5b5b..8c3b694d22 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java @@ -24,17 +24,18 @@ import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor; +import com.opensymphony.xwork2.ognl.accessor.RootAccessor; import com.opensymphony.xwork2.util.ClearableValueStack; import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.MemberAccessValueStack; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.reflection.ReflectionContextState; +import ognl.ClassResolver; import ognl.MethodFailedException; import ognl.NoSuchPropertyException; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; -import ognl.PropertyAccessor; import org.apache.commons.lang3.BooleanUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -83,7 +84,7 @@ public class OgnlValueStack implements Serializable, ValueStack, ClearableValueS */ protected OgnlValueStack(ValueStack vs, XWorkConverter xworkConverter, - CompoundRootAccessor accessor, + RootAccessor accessor, TextProvider prov, SecurityMemberAccess securityMemberAccess) { setRoot(xworkConverter, @@ -98,19 +99,19 @@ protected OgnlValueStack(ValueStack vs, /** * @since 6.4.0 */ - protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor accessor, TextProvider prov, SecurityMemberAccess securityMemberAccess) { + protected OgnlValueStack(XWorkConverter xworkConverter, RootAccessor accessor, TextProvider prov, SecurityMemberAccess securityMemberAccess) { this(null, xworkConverter, accessor, prov, securityMemberAccess); } /** * @since 6.4.0 */ - protected OgnlValueStack(ValueStack vs, XWorkConverter xworkConverter, CompoundRootAccessor accessor, SecurityMemberAccess securityMemberAccess) { + protected OgnlValueStack(ValueStack vs, XWorkConverter xworkConverter, RootAccessor accessor, SecurityMemberAccess securityMemberAccess) { this(vs, xworkConverter, accessor, null, securityMemberAccess); } /** - * @deprecated since 6.4.0, use {@link #OgnlValueStack(ValueStack, XWorkConverter, CompoundRootAccessor, TextProvider, SecurityMemberAccess)} instead. + * @deprecated since 6.4.0, use {@link #OgnlValueStack(ValueStack, XWorkConverter, RootAccessor, TextProvider, SecurityMemberAccess)} instead. */ @Deprecated protected OgnlValueStack(ValueStack vs, @@ -122,7 +123,7 @@ protected OgnlValueStack(ValueStack vs, } /** - * @deprecated since 6.4.0, use {@link #OgnlValueStack(XWorkConverter, CompoundRootAccessor, TextProvider, SecurityMemberAccess)} instead. + * @deprecated since 6.4.0, use {@link #OgnlValueStack(XWorkConverter, RootAccessor, TextProvider, SecurityMemberAccess)} instead. */ @Deprecated protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor accessor, TextProvider prov, boolean allowStaticFieldAccess) { @@ -130,7 +131,7 @@ protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor acc } /** - * @deprecated since 6.4.0, use {@link #OgnlValueStack(ValueStack, XWorkConverter, CompoundRootAccessor, SecurityMemberAccess)} instead. + * @deprecated since 6.4.0, use {@link #OgnlValueStack(ValueStack, XWorkConverter, RootAccessor, SecurityMemberAccess)} instead. */ @Deprecated protected OgnlValueStack(ValueStack vs, XWorkConverter xworkConverter, CompoundRootAccessor accessor, boolean allowStaticFieldAccess) { @@ -145,7 +146,7 @@ protected void setOgnlUtil(OgnlUtil ognlUtil) { /** * @since 6.4.0 */ - protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot, SecurityMemberAccess securityMemberAccess) { + protected void setRoot(XWorkConverter xworkConverter, RootAccessor accessor, CompoundRoot compoundRoot, SecurityMemberAccess securityMemberAccess) { this.root = compoundRoot; this.securityMemberAccess = securityMemberAccess; this.context = Ognl.createDefaultContext(this.root, securityMemberAccess, accessor, new OgnlTypeConverterWrapper(xworkConverter)); @@ -156,7 +157,7 @@ protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor acces } /** - * @deprecated since 6.4.0, use {@link #setRoot(XWorkConverter, CompoundRootAccessor, CompoundRoot, SecurityMemberAccess)} instead. + * @deprecated since 6.4.0, use {@link #setRoot(XWorkConverter, RootAccessor, CompoundRoot, SecurityMemberAccess)} instead. */ @Deprecated protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot, boolean allowStaticFieldAccess) { @@ -513,7 +514,7 @@ private Object readResolve() { ActionContext ac = ActionContext.getContext(); Container cont = ac.getContainer(); XWorkConverter xworkConverter = cont.getInstance(XWorkConverter.class); - CompoundRootAccessor accessor = (CompoundRootAccessor) cont.getInstance(PropertyAccessor.class, CompoundRoot.class.getName()); + RootAccessor accessor = (RootAccessor) cont.getInstance(ClassResolver.class, CompoundRoot.class.getName()); TextProvider prov = cont.getInstance(TextProvider.class, "system"); SecurityMemberAccess sma = cont.getInstance(SecurityMemberAccess.class); OgnlValueStack aStack = new OgnlValueStack(xworkConverter, accessor, prov, sma); 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 5585b1a1a2..b52a23ecb7 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java @@ -23,7 +23,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.CompoundRootAccessor; +import com.opensymphony.xwork2.ognl.accessor.RootAccessor; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; import ognl.ClassResolver; @@ -41,7 +41,7 @@ public class OgnlValueStackFactory implements ValueStackFactory { protected XWorkConverter xworkConverter; - protected CompoundRootAccessor compoundRootAccessor; + protected RootAccessor compoundRootAccessor; protected TextProvider textProvider; protected Container container; @@ -52,7 +52,7 @@ protected void setXWorkConverter(XWorkConverter converter) { @Inject(value = "com.opensymphony.xwork2.util.CompoundRoot") protected void setClassResolver(ClassResolver classResolver) { - this.compoundRootAccessor = (CompoundRootAccessor) classResolver; + this.compoundRootAccessor = (RootAccessor) classResolver; } @Inject("system") diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java index 4600c7c97c..be332047bb 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java @@ -22,15 +22,12 @@ import com.opensymphony.xwork2.ognl.OgnlValueStack; import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.ValueStack; -import ognl.ClassResolver; -import ognl.MethodAccessor; import ognl.MethodFailedException; import ognl.NoSuchPropertyException; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; import ognl.OgnlRuntime; -import ognl.PropertyAccessor; import org.apache.commons.lang3.BooleanUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -56,11 +53,12 @@ * @author Rainer Hermanns * @version $Revision$ */ -public class CompoundRootAccessor implements PropertyAccessor, MethodAccessor, ClassResolver { +public class CompoundRootAccessor implements RootAccessor { /** * Used by OGNl to generate bytecode */ + @Override public String getSourceAccessor(OgnlContext context, Object target, Object index) { return null; } @@ -68,6 +66,7 @@ public String getSourceAccessor(OgnlContext context, Object target, Object index /** * Used by OGNl to generate bytecode */ + @Override public String getSourceSetter(OgnlContext context, Object target, Object index) { return null; } @@ -88,6 +87,7 @@ public void useDisallowCustomOgnlMap(String disallowCustomOgnlMap) { this.disallowCustomOgnlMap = BooleanUtils.toBoolean(disallowCustomOgnlMap); } + @Override public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException { CompoundRoot root = (CompoundRoot) target; OgnlContext ognlContext = (OgnlContext) context; @@ -136,6 +136,7 @@ public void setProperty(Map context, Object target, Object name, Object value) t } } + @Override public Object getProperty(Map context, Object target, Object name) throws OgnlException { CompoundRoot root = (CompoundRoot) target; OgnlContext ognlContext = (OgnlContext) context; @@ -181,6 +182,7 @@ public Object getProperty(Map context, Object target, Object name) throws OgnlEx } } + @Override public Object callMethod(Map context, Object target, String name, Object[] objects) throws MethodFailedException { CompoundRoot root = (CompoundRoot) target; @@ -273,10 +275,12 @@ public Object callMethod(Map context, Object target, String name, Object[] objec return null; } + @Override public Object callStaticMethod(Map transientVars, Class aClass, String s, Object[] objects) throws MethodFailedException { return null; } + @Override public Class classForName(String className, Map context) throws ClassNotFoundException { Object root = Ognl.getRoot(context); diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/RootAccessor.java b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/RootAccessor.java new file mode 100644 index 0000000000..481bf2f751 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/RootAccessor.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.opensymphony.xwork2.ognl.accessor; + +import ognl.ClassResolver; +import ognl.MethodAccessor; +import ognl.PropertyAccessor; + +/** + * @since 6.4.0 + */ +public interface RootAccessor extends PropertyAccessor, MethodAccessor, ClassResolver { +}