Skip to content

Commit

Permalink
WW-4789 WW-3788 Marks CONTAINER as deprecated on behalf using helper …
Browse files Browse the repository at this point in the history
…methods
  • Loading branch information
lukaszlenart committed Apr 10, 2020
1 parent df687a6 commit c253b7f
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 99 deletions.
19 changes: 17 additions & 2 deletions core/src/main/java/com/opensymphony/xwork2/ActionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -518,4 +522,15 @@ public ActionContext usePageContextOrClear(ActionContext actionContext) {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,36 +324,41 @@ protected void createAction(Map<String, Object> contextMap) {
}

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);

if (stack == null) {
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
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;
Expand All @@ -55,28 +55,52 @@ public class OgnlValueStackFactory implements ValueStackFactory {
protected void setXWorkConverter(XWorkConverter converter) {
this.xworkConverter = converter;
}

@Inject("system")
protected void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}

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);
Expand Down Expand Up @@ -116,7 +140,7 @@ protected void setContainer(Container container) throws ClassNotFoundException {

/**
* Retrieve allowsStaticMethodAccess state from the container (allows for lazy fetching)
*
*
* @return
*/
protected boolean containerAllowsStaticMethodAccess() {
Expand All @@ -125,7 +149,7 @@ protected boolean containerAllowsStaticMethodAccess() {

/**
* Retrieve allowStaticFieldAccess state from the container (allows for lazy fetching)
*
*
* @return
*/
protected boolean containerAllowsStaticFieldAccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public Object evaluate(String parsedValue) {
}
};

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);
}
Expand Down Expand Up @@ -205,8 +205,8 @@ public Object evaluate(String parsedValue) {
}
};

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);

Expand All @@ -216,10 +216,10 @@ public Object evaluate(String parsedValue) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@ public static ConfigurationManager setUp() throws Exception {

// 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;
}

Expand Down Expand Up @@ -79,9 +73,8 @@ public void register(ContainerBuilder builder, LocatableProperties props) throws

// 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ public void renderFormUrl(Form formComponent) {
} 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public static Dispatcher initDispatcher(ServletContext ctx, Map<String,String> p
// 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;
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/apache/struts2/util/StrutsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public StrutsUtil(ValueStack stack, HttpServletRequest request, HttpServletRespo
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TagModel(ValueStack stack, HttpServletRequest req, HttpServletResponse re
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public int doEndTag() throws JspException {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public static Map<String, Object> getStandardContext(ValueStack stack, HttpServl
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());
}
Expand All @@ -71,22 +71,23 @@ public static Map<String, Object> getStandardContext(ValueStack stack, HttpServl
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,10 @@ public void testIncludeCollectionParameterInResult() throws Exception {
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);

Expand All @@ -308,7 +307,7 @@ public void testIncludeCollectionParameterInResult() throws Exception {
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));
Expand Down
Loading

0 comments on commit c253b7f

Please sign in to comment.