WW-5379 Use ValueStackProvider marker interface for Velocity context implementation flexibility

This commit is contained in:
Kusal Kithul-Godage
2024-01-02 21:21:35 +11:00
parent 450ee91b03
commit ee600f99b1
3 changed files with 36 additions and 4 deletions
@@ -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();
}
@@ -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<VelocityContext> chainedContexts;
@@ -104,6 +105,7 @@ public class StrutsVelocityContext extends VelocityContext {
return null;
}
@Override
public ValueStack getValueStack() {
return stack;
}
@@ -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);