WW-4789 WW-3788 Marks CONTAINER as deprecated on behalf using helper methods

This commit is contained in:
Lukasz Lenart
2020-04-10 19:39:10 +02:00
parent df687a6d43
commit c253b7f480
22 changed files with 128 additions and 99 deletions
@@ -63,7 +63,7 @@ public class ActionContext implements Serializable {
/**
* Constant for the name of the action being executed.
*
* @deprecated use helper methods instead
* @deprecated scope will be narrowed to "private", use helper methods instead
*/
@Deprecated
public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";
@@ -95,19 +95,23 @@ public class ActionContext implements Serializable {
/**
* Constant for the action's {@link com.opensymphony.xwork2.ActionInvocation invocation} context.
* @deprecated scope will be narrowed to "private", use helper methods instead
*/
@Deprecated
public static final String ACTION_INVOCATION = "com.opensymphony.xwork2.ActionContext.actionInvocation";
/**
* Constant for the map of type conversion errors.
* @deprecated use helper method instead
* @deprecated scope will be narrowed to "private", use helper methods instead
*/
@Deprecated
public static final String CONVERSION_ERRORS = "com.opensymphony.xwork2.ActionContext.conversionErrors";
/**
* Constant for the container
* @deprecated scope will be narrowed to "private", use helper methods instead
*/
@Deprecated
public static final String CONTAINER = "com.opensymphony.xwork2.ActionContext.container";
private final Map<String, Object> context;
@@ -518,4 +522,15 @@ public class ActionContext implements Serializable {
return this;
}
public ActionContext withExtraContext(Map<String, Object> extraContext) {
if (extraContext != null) {
getContext().context.putAll(extraContext);
}
return this;
}
public ActionContext withLocale(Locale locale) {
put(LOCALE, locale);
return this;
}
}
@@ -324,9 +324,10 @@ public class DefaultActionInvocation implements ActionInvocation {
}
protected Map<String, Object> createContextMap() {
Map<String, Object> contextMap;
ActionContext oldContext = ActionContext.getContext();
ActionContext actionContext;
if ((extraContext != null) && (extraContext.containsKey(ActionContext.VALUE_STACK))) {
if (extraContext != null && extraContext.containsKey(ActionContext.VALUE_STACK)) {
// In case the ValueStack was passed in
stack = (ValueStack) extraContext.get(ActionContext.VALUE_STACK);
@@ -334,26 +335,30 @@ public class DefaultActionInvocation implements ActionInvocation {
throw new IllegalStateException("There was a null Stack set into the extra params.");
}
contextMap = stack.getContext();
actionContext = stack.getActionContext();
} else {
// create the value stack
// this also adds the ValueStack to its context
stack = valueStackFactory.createValueStack();
// create the action context
contextMap = stack.getContext();
actionContext = stack.getActionContext();
}
// put extraContext in
if (extraContext != null) {
contextMap.putAll(extraContext);
try {
return actionContext
.bind()
.withExtraContext(extraContext)
.withActionInvocation(this)
.withContainer(container)
.getContextMap();
} finally {
ActionContext.clear();
if (oldContext != null) {
LOG.debug("Re-binding the old context");
oldContext.bind();
}
}
//put this DefaultActionInvocation into the context map
contextMap.put(ActionContext.ACTION_INVOCATION, this);
contextMap.put(ActionContext.CONTAINER, container);
return contextMap;
}
/**
@@ -34,17 +34,17 @@ import ognl.PropertyAccessor;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.StrutsConstants;
/**
* Creates an Ognl value stack
*/
public class OgnlValueStackFactory implements ValueStackFactory {
private static final Logger LOG = LogManager.getLogger(OgnlValueStackFactory.class);
private static final Logger LOG = LogManager.getLogger(OgnlValueStackFactory.class);
protected XWorkConverter xworkConverter;
protected CompoundRootAccessor compoundRootAccessor;
@@ -55,7 +55,7 @@ public class OgnlValueStackFactory implements ValueStackFactory {
protected void setXWorkConverter(XWorkConverter converter) {
this.xworkConverter = converter;
}
@Inject("system")
protected void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
@@ -63,20 +63,44 @@ public class OgnlValueStackFactory implements ValueStackFactory {
public ValueStack createValueStack() {
ValueStack stack = new OgnlValueStack(xworkConverter, compoundRootAccessor, textProvider,
containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess());
containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess());
container.inject(stack);
stack.getContext().put(ActionContext.CONTAINER, container);
return stack;
ActionContext oldContext = ActionContext.getContext();
try {
return stack.getActionContext()
.bind()
.withContainer(container)
.withValueStack(stack)
.getValueStack();
} finally {
ActionContext.clear();
if (oldContext != null) {
LOG.debug("Re-binding the old context");
oldContext.bind();
}
}
}
public ValueStack createValueStack(ValueStack stack) {
ValueStack result = new OgnlValueStack(stack, xworkConverter, compoundRootAccessor,
containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess());
containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess());
container.inject(result);
stack.getContext().put(ActionContext.CONTAINER, container);
return result;
ActionContext oldContext = ActionContext.getContext();
try {
return result.getActionContext()
.bind()
.withContainer(container)
.withValueStack(result)
.getValueStack();
} finally {
ActionContext.clear();
if (oldContext != null) {
LOG.debug("Re-binding the old context");
oldContext.bind();
}
}
}
@Inject
protected void setContainer(Container container) throws ClassNotFoundException {
Set<String> names = container.getInstanceNames(PropertyAccessor.class);
@@ -116,7 +140,7 @@ public class OgnlValueStackFactory implements ValueStackFactory {
/**
* Retrieve allowsStaticMethodAccess state from the container (allows for lazy fetching)
*
*
* @return
*/
protected boolean containerAllowsStaticMethodAccess() {
@@ -125,7 +149,7 @@ public class OgnlValueStackFactory implements ValueStackFactory {
/**
* Retrieve allowStaticFieldAccess state from the container (allows for lazy fetching)
*
*
* @return
*/
protected boolean containerAllowsStaticFieldAccess() {
@@ -164,7 +164,7 @@ public class TextParseUtil {
}
};
TextParser parser = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(TextParser.class);
TextParser parser = stack.getActionContext().getContainer().getInstance(TextParser.class);
return parser.evaluate(openChars, expression, ognlEval, maxLoopCount);
}
@@ -205,8 +205,8 @@ public class TextParseUtil {
}
};
Map<String, Object> context = stack.getContext();
TextParser parser = ((Container)context.get(ActionContext.CONTAINER)).getInstance(TextParser.class);
ActionContext actionContext = stack.getActionContext();
TextParser parser = actionContext.getContainer().getInstance(TextParser.class);
Object result = parser.evaluate(openChars, expression, ognlEval, maxLoopCount);
@@ -216,10 +216,10 @@ public class TextParseUtil {
Collection<Object> casted = (Collection<Object>)result;
resultCol = new ArrayList<>();
XWorkConverter conv = ((Container)context.get(ActionContext.CONTAINER)).getInstance(XWorkConverter.class);
XWorkConverter conv = actionContext.getContainer().getInstance(XWorkConverter.class);
for (Object element : casted) {
String stringElement = (String)conv.convertValue(context, element, String.class);
String stringElement = (String) conv.convertValue(actionContext.getContextMap(), element, String.class);
if (shallBeIncluded(stringElement, excludeEmptyElements)) {
if (evaluator != null) {
stringElement = evaluator.evaluate(stringElement).toString();
@@ -39,14 +39,8 @@ public class XWorkTestCaseHelper {
// Reset the value stack
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().put(ActionContext.CONTAINER, container);
ActionContext.of(stack.getContext()).bind();
// clear out localization
//container.getInstance(LocalizedTextUtil.class).reset();
//ObjectFactory.setObjectFactory(container.getInstance(ObjectFactory.class));
stack.getActionContext().withContainer(container).withValueStack(stack).bind();
return configurationManager;
}
@@ -79,9 +73,8 @@ public class XWorkTestCaseHelper {
// Reset the value stack
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().put(ActionContext.CONTAINER, container);
ActionContext.of(stack.getContext()).bind();
stack.getActionContext().withContainer(container).withValueStack(stack).bind();
return configurationManager;
}
@@ -137,8 +137,7 @@ public class ServletUrlRenderer implements UrlRenderer {
} else {
// no action supplied? ok, then default to the current request
// (action or general URL)
ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(
ActionContext.ACTION_INVOCATION);
ActionInvocation ai = formComponent.getStack().getActionContext().getActionInvocation();
if (ai != null) {
action = ai.getProxy().getActionName();
namespace = ai.getProxy().getNamespace();
@@ -47,9 +47,8 @@ public class StrutsTestCaseHelper {
// Reset the value stack
Container container = du.getContainer();
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().put(ActionContext.CONTAINER, container);
ActionContext.of(stack.getContext()).bind();
stack.getActionContext().withContainer(container).withValueStack(stack).bind();
return du;
}
@@ -61,9 +61,9 @@ public class StrutsUtil {
this.stack = stack;
this.request = request;
this.response = response;
this.ognl = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(OgnlTool.class);
this.urlHelper = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(UrlHelper.class);
this.objectFactory = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(ObjectFactory.class);
this.ognl = stack.getActionContext().getContainer().getInstance(OgnlTool.class);
this.urlHelper = stack.getActionContext().getContainer().getInstance(UrlHelper.class);
this.objectFactory = stack.getActionContext().getContainer().getInstance(ObjectFactory.class);
}
public Object bean(Object aName) throws Exception {
@@ -55,7 +55,7 @@ public abstract class TagModel implements TemplateTransformModel {
public Writer getWriter(Writer writer, Map params)
throws TemplateModelException, IOException {
Component bean = getBean();
Container container = (Container) stack.getContext().get(ActionContext.CONTAINER);
Container container = stack.getActionContext().getContainer();
container.inject(bean);
Map unwrappedParameters = unwrapParameters(params);
@@ -44,7 +44,7 @@ public abstract class ComponentTagSupport extends StrutsBodyTagSupport {
public int doStartTag() throws JspException {
ValueStack stack = getStack();
component = getBean(stack, (HttpServletRequest) pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
Container container = (Container) stack.getContext().get(ActionContext.CONTAINER);
Container container = stack.getActionContext().getContainer();
container.inject(component);
populateParams();
@@ -56,10 +56,10 @@ public class ContextUtil {
map.put(SESSION, req.getSession(false));
map.put(BASE, req.getContextPath());
map.put(STACK, stack);
map.put(OGNL, ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(OgnlTool.class));
map.put(OGNL, stack.getActionContext().getContainer().getInstance(OgnlTool.class));
map.put(STRUTS, new StrutsUtil(stack, req, res));
ActionInvocation invocation = (ActionInvocation) stack.getContext().get(ActionContext.ACTION_INVOCATION);
ActionInvocation invocation = stack.getActionContext().getActionInvocation();
if (invocation != null) {
map.put(ACTION, invocation.getAction());
}
@@ -71,22 +71,23 @@ public class ContextUtil {
* @param context stack's context
* @return boolean
*/
public static boolean isUseAltSyntax(Map context) {
public static boolean isUseAltSyntax(Map<String, Object> context) {
// We didn't make altSyntax static cause, if so, struts.configuration.xml.reload will not work
// plus the Configuration implementation should cache the properties, which the framework's
// configuration implementation does
return "true".equals(((Container)context.get(ActionContext.CONTAINER)).getInstance(String.class, StrutsConstants.STRUTS_TAG_ALTSYNTAX)) ||(
String tagAltSytnax = ActionContext.of(context).getContainer().getInstance(String.class, StrutsConstants.STRUTS_TAG_ALTSYNTAX);
return "true".equals(tagAltSytnax) ||(
(context.containsKey("useAltSyntax") &&
context.get("useAltSyntax") != null &&
"true".equals(context.get("useAltSyntax").toString())));
}
/**
* Returns a String for overriding the default templateSuffix if templateSuffix is on the stack
* @param context stack's context
* @return String
*/
public static String getTemplateSuffix(Map context) {
public static String getTemplateSuffix(Map<String, Object> context) {
return context.containsKey("templateSuffix") ? (String) context.get("templateSuffix") : null;
}
}
@@ -28,10 +28,9 @@ public class ValidatorSupportTest extends XWorkTestCase {
public void testConditionalParseExpression() {
OgnlValueStack stack = (OgnlValueStack) container.getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().put(ActionContext.CONTAINER, container);
stack.getContext().put("something", "somevalue");
ActionContext.of(stack.getContext()).bind();
ActionContext.of(stack.getContext()).withContainer(container).bind();
ValidatorSupport validator = new ValidatorSupport() {
public void validate(Object object) throws ValidationException {
@@ -293,11 +293,10 @@ public class ServletRedirectResultTest extends StrutsInternalTestCase implements
ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
ValueStack mockValueStack = control.createMock(ValueStack.class);
Map<String, Object> mockContext = new HashMap<>();
mockContext.put(ActionContext.CONTAINER, container);
ActionContext actionContext = ActionContext.of(new HashMap<>()).withContainer(container);
expect(mockInvocation.getStack()).andReturn(mockValueStack);
expect(mockValueStack.getContext()).andReturn(mockContext);
expect(mockValueStack.getActionContext()).andReturn(actionContext);
expect(mockInvocation.getStack()).andReturn(mockValueStack);
@@ -308,7 +307,7 @@ public class ServletRedirectResultTest extends StrutsInternalTestCase implements
expect(mockActionProxy.getConfig()).andReturn(actionConfig);
expect(mockInvocation.getInvocationContext()).andReturn(context);
expect(mockValueStack.getContext()).andReturn(mockContext);
expect(mockValueStack.getActionContext()).andReturn(actionContext);
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
@@ -18,15 +18,11 @@
*/
package org.apache.struts2.views.jsp;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;
import com.mockobjects.servlet.MockJspWriter;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsException;
import org.apache.struts2.TestAction;
@@ -34,16 +30,19 @@ import org.apache.struts2.components.Text;
import org.apache.struts2.views.jsp.ui.StrutsBodyContent;
import org.apache.struts2.views.jsp.ui.TestAction1;
import com.mockobjects.servlet.MockJspWriter;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static org.junit.Assert.assertNotEquals;
/**
* TextTagTest
*
*/
public class TextTagTest extends AbstractTagTest {
@@ -173,8 +172,7 @@ public class TextTagTest extends AbstractTagTest {
final StringBuffer buffer = writer.getBuffer();
buffer.delete(0, buffer.length());
ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack();
newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
newStack.getContext().put(ActionContext.CONTAINER, container);
newStack.getActionContext().withLocale(foreignLocale).withContainer(container);
newStack.push(container.inject(TestAction1.class));
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
assertNotSame(ActionContext.getContext().getValueStack().peek(), newStack.peek());
@@ -203,10 +201,9 @@ public class TextTagTest extends AbstractTagTest {
final StringBuffer buffer = writer.getBuffer();
buffer.delete(0, buffer.length());
String value_int = getLocalizedMessage(foreignLocale);
assertFalse(value_default.equals(value_int));
assertNotEquals(value_default, value_int);
ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack(stack);
newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
newStack.getContext().put(ActionContext.CONTAINER, container);
newStack.getActionContext().withLocale(foreignLocale).withContainer(container);
assertNotSame(newStack.getContext().get(ActionContext.LOCALE), ActionContext.getContext().getLocale());
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
assertEquals(ActionContext.getContext().getValueStack().peek(), newStack.peek());
@@ -110,6 +110,6 @@ public class DebugTagTest extends AbstractUITagTest {
configurationManager.reload();
container = configurationManager.getConfiguration().getContainer();
stack.getContext().put(ActionContext.CONTAINER, container);
stack.getActionContext().withContainer(container);
}
}
@@ -36,7 +36,7 @@ public class ContextUtilTest extends StrutsInternalTestCase {
private void setAltSyntax(ValueStack stack, String val) {
Mock container = new Mock(Container.class);
container.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_TAG_ALTSYNTAX)), val);
stack.getContext().put(ActionContext.CONTAINER, container.proxy());
stack.getActionContext().withContainer((Container) container.proxy());
}
public void testAltSyntaxMethod1() throws Exception {
@@ -427,7 +427,7 @@ public class DefaultUrlHelperTest extends StrutsInternalTestCase {
public void setUp() throws Exception {
super.setUp();
stubContainer = new StubContainer(container);
ActionContext.getContext().put(ActionContext.CONTAINER, stubContainer);
ActionContext.getContext().withContainer(stubContainer);
urlHelper = new DefaultUrlHelper();
}
@@ -330,9 +330,8 @@ public class EmbeddedJSPResultTest extends TestCase {
EasyMock.replay(request);
//mock value stack
Map<String, Object> stackContext = new HashMap<>();
ValueStack valueStack = EasyMock.createNiceMock(ValueStack.class);
EasyMock.expect(valueStack.getContext()).andReturn(stackContext).anyTimes();
EasyMock.expect(valueStack.getActionContext()).andReturn(ActionContext.getContext()).anyTimes();
EasyMock.replay(valueStack);
//mock converter
@@ -355,7 +354,6 @@ public class EmbeddedJSPResultTest extends TestCase {
EasyMock.expect(container.getInstance(FileManagerFactory.class)).andReturn(fileManagerFactory).anyTimes();
EasyMock.replay(container);
stackContext.put(ActionContext.CONTAINER, container);
ActionContext.of(new HashMap<>())
.withParameters(HttpParameters.create(params).build())
@@ -21,6 +21,7 @@
package org.apache.struts2.views.java.simple;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
@@ -101,11 +102,13 @@ public abstract class AbstractTest extends TestCase {
context = new TemplateRenderingContext(template, writer, stack, map, null);
stackContext.put(Component.COMPONENT_STACK, new Stack());
ActionContext actionContext = ActionContext.of(stackContext).bind();
request = createNiceMock(HttpServletRequest.class);
expect(request.getContextPath()).andReturn("/some/path").anyTimes();
response = createNiceMock(HttpServletResponse.class);
expect(stack.getActionContext()).andReturn(actionContext).anyTimes();
expect(stack.getContext()).andReturn(stackContext).anyTimes();
Container container = createNiceMock(Container.class);
@@ -114,15 +117,12 @@ public abstract class AbstractTest extends TestCase {
expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes();
TextParser parser = new OgnlTextParser();
expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes();
stackContext.put(ActionContext.CONTAINER, container);
replay(request);
replay(stack);
replay(container);
ActionContext actionContext = ActionContext.of(stackContext).bind();
actionContext.withServletRequest(request);
actionContext.withContainer(container).withServletRequest(request);
}
protected static String s(String input) {
@@ -165,7 +165,7 @@ public class PortletUrlRenderer implements UrlRenderer {
if (formComponent.action != null) {
action = formComponent.findString(formComponent.action);
} else {
ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION);
ActionInvocation ai = formComponent.getStack().getActionContext().getActionInvocation();
action = ai.getProxy().getActionName();
}
@@ -43,7 +43,7 @@ public class PortletUrlRendererTest extends StrutsTestCasePortletTests {
super.setUp();
ActionProxy actionProxy = getActionProxy("/portlettest/test"); // creates new empty ActionContext
ActionContext.getContext().put(ActionContext.ACTION_INVOCATION, actionProxy.getInvocation());
ActionContext.getContext().withActionInvocation(actionProxy.getInvocation());
PortletContext portletCtx = new MockPortletContext();
ActionContext.getContext().put(StrutsStatics.STRUTS_PORTLET_CONTEXT, portletCtx);
@@ -62,7 +62,7 @@ public abstract class AbstractDirective extends Directive {
HttpServletRequest req = (HttpServletRequest) stack.getContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse res = (HttpServletResponse) stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
Component bean = getBean(stack, req, res);
Container container = (Container) stack.getContext().get(ActionContext.CONTAINER);
Container container = stack.getActionContext().getContainer();
container.inject(bean);
// get the parameters
Map params = createPropertyMap(ctx, node);