From ee600f99b1838cf79a649c0181cd13ae3bfed63e Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 2 Jan 2024 21:21:35 +1100 Subject: [PATCH] WW-5379 Use ValueStackProvider marker interface for Velocity context implementation flexibility --- .../struts2/util/ValueStackProvider.java | 30 +++++++++++++++++++ .../views/velocity/StrutsVelocityContext.java | 4 ++- .../components/AbstractDirective.java | 6 ++-- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/org/apache/struts2/util/ValueStackProvider.java diff --git a/core/src/main/java/org/apache/struts2/util/ValueStackProvider.java b/core/src/main/java/org/apache/struts2/util/ValueStackProvider.java new file mode 100644 index 000000000..2a6d1bf0a --- /dev/null +++ b/core/src/main/java/org/apache/struts2/util/ValueStackProvider.java @@ -0,0 +1,30 @@ +/* + * 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 org.apache.struts2.util; + +import com.opensymphony.xwork2.util.ValueStack; + +/** + * @since 6.4.0 + */ +public interface ValueStackProvider { + + ValueStack getValueStack(); + +} diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java index 7ab4f24e7..d18ca6bcf 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java @@ -19,6 +19,7 @@ package org.apache.struts2.views.velocity; import com.opensymphony.xwork2.util.ValueStack; +import org.apache.struts2.util.ValueStackProvider; import org.apache.velocity.VelocityContext; import java.util.ArrayList; @@ -26,7 +27,7 @@ import java.util.Arrays; import java.util.List; import java.util.function.Function; -public class StrutsVelocityContext extends VelocityContext { +public class StrutsVelocityContext extends VelocityContext implements ValueStackProvider { private final ValueStack stack; private final List chainedContexts; @@ -104,6 +105,7 @@ public class StrutsVelocityContext extends VelocityContext { return null; } + @Override public ValueStack getValueStack() { return stack; } diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java index fd984a88d..539f64bdd 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java @@ -22,8 +22,8 @@ import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.util.ValueStack; import org.apache.struts2.ServletActionContext; import org.apache.struts2.components.Component; +import org.apache.struts2.util.ValueStackProvider; import org.apache.struts2.views.util.ContextUtil; -import org.apache.struts2.views.velocity.StrutsVelocityContext; import org.apache.velocity.context.AbstractContext; import org.apache.velocity.context.Context; import org.apache.velocity.context.InternalContextAdapter; @@ -86,8 +86,8 @@ public abstract class AbstractDirective extends Directive { private ValueStack extractValueStack(Context context) { do { - if (context instanceof StrutsVelocityContext) { - return ((StrutsVelocityContext) context).getValueStack(); + if (context instanceof ValueStackProvider) { + return ((ValueStackProvider) context).getValueStack(); } context = extractContext(context); } while (context != null);