Skip to content

Commit

Permalink
WW-4789 WW-3788 ActionContext refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Mar 22, 2020
1 parent 9053c84 commit 3739b66
Show file tree
Hide file tree
Showing 111 changed files with 1,440 additions and 1,447 deletions.
Expand Up @@ -202,7 +202,7 @@ public static LinkedList<String> getChainHistory() {
* @param invocation the DefaultActionInvocation calling the action call stack
*/
public void execute(ActionInvocation invocation) throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
ValueStack stack = invocation.getInvocationContext().getValueStack();
String finalNamespace = this.namespace != null
? TextParseUtil.translateVariables(namespace, stack)
: invocation.getProxy().getNamespace();
Expand All @@ -216,14 +216,14 @@ public void execute(ActionInvocation invocation) throws Exception {
throw new StrutsException("Infinite recursion detected: " + ActionChainResult.getChainHistory().toString());
}

if (ActionChainResult.getChainHistory().isEmpty() && invocation != null && invocation.getProxy() != null) {
if (ActionChainResult.getChainHistory().isEmpty() && invocation.getProxy() != null) {
addToHistory(finalNamespace, invocation.getProxy().getActionName(), invocation.getProxy().getMethod());
}
addToHistory(finalNamespace, finalActionName, finalMethodName);

HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.VALUE_STACK, ActionContext.getContext().getValueStack());
extraContext.put(ActionContext.PARAMETERS, ActionContext.getContext().getParameters());
extraContext.put(ActionContext.VALUE_STACK, invocation.getInvocationContext().getValueStack());
extraContext.put(ActionContext.PARAMETERS, invocation.getInvocationContext().getParameters());
extraContext.put(CHAIN_HISTORY, ActionChainResult.getChainHistory());

LOG.debug("Chaining to action {}", finalActionName);
Expand Down
139 changes: 111 additions & 28 deletions core/src/main/java/com/opensymphony/xwork2/ActionContext.java
Expand Up @@ -22,8 +22,14 @@
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsException;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.mapper.ActionMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
Expand Down Expand Up @@ -99,18 +105,73 @@ public class ActionContext implements Serializable {
* Constant for the container
*/
public static final String CONTAINER = "com.opensymphony.xwork2.ActionContext.container";

private Map<String, Object> context;

/**
* Creates a new ActionContext initialized with another context.
*
* @param context a context map.
*/
public ActionContext(Map<String, Object> context) {
private ActionContext(Map<String, Object> context) {
this.context = context;
}

/**
* Creates a new ActionContext based on passed in Map
* and assign this instance to the current thread
*
* @param context a map with context values
* @return new ActionContext
*/
public static ActionContext of(Map<String, Object> context) {
return new ActionContext(context);
}

public static ActionContext bound(ActionContext actionContext) {
ActionContext.setContext(actionContext);
return ActionContext.getContext();
}

public static ActionContext ofAndBound(Map<String, Object> context) {
return bound(of(context));
}

/**
* Creates a new ActionContext based on passed in ActionContext
* and assign this instance to the current thread
*
* @param actionContext ActionContext to be used for current thread
* @return new ActionContext
*/
public static ActionContext ofAndBound(ActionContext actionContext) {
return bound(actionContext);
}

/**
* Wipes out current ActionContext, use wisely!
*/
public static void clear() {
actionContext.remove();
}

/**
* Sets the action context for the current thread.
*
* @param context the action context.
*/
private static void setContext(ActionContext context) {
actionContext.set(context);
}

/**
* Returns the ActionContext specific to the current thread.
*
* @return the ActionContext for the current thread, is never <tt>null</tt>.
*/
public static ActionContext getContext() {
return actionContext.get();
}

/**
* Sets the action invocation (the execution state).
Expand Down Expand Up @@ -148,24 +209,6 @@ public Map<String, Object> getApplication() {
return (Map<String, Object>) get(APPLICATION);
}

/**
* Sets the action context for the current thread.
*
* @param context the action context.
*/
public static void setContext(ActionContext context) {
actionContext.set(context);
}

/**
* Returns the ActionContext specific to the current thread.
*
* @return the ActionContext for the current thread, is never <tt>null</tt>.
*/
public static ActionContext getContext() {
return actionContext.get();
}

/**
* Gets the context map.
*
Expand All @@ -188,7 +231,7 @@ public void setConversionErrors(Map<String, ConversionData> conversionErrors) {
* Gets the map of conversion errors which occurred when executing the action.
*
* @return the map of conversion errors which occurred when executing the action or an empty map if
* there were no errors.
* there were no errors.
*/
public Map<String, ConversionData> getConversionErrors() {
Map<String, ConversionData> errors = (Map) get(CONVERSION_ERRORS);
Expand Down Expand Up @@ -259,7 +302,7 @@ public void setParameters(HttpParameters parameters) {
* parameters otherwise.
*
* @return a Map of HttpServletRequest parameters or a multipart map when in a servlet environment, or a
* generic Map of parameters otherwise.
* generic Map of parameters otherwise.
*/
public HttpParameters getParameters() {
return (HttpParameters) get(PARAMETERS);
Expand All @@ -268,7 +311,7 @@ public HttpParameters getParameters() {
/**
* Sets a map of action session values.
*
* @param session the session values.
* @param session the session values.
*/
public void setSession(Map<String, Object> session) {
put(SESSION, session);
Expand Down Expand Up @@ -300,25 +343,25 @@ public void setValueStack(ValueStack stack) {
public ValueStack getValueStack() {
return (ValueStack) get(VALUE_STACK);
}

/**
* Gets the container for this request
*
*
* @param cont The container
*/
public void setContainer(Container cont) {
put(CONTAINER, cont);
}

/**
* Sets the container for this request
*
*
* @return The container
*/
public Container getContainer() {
return (Container) get(CONTAINER);
}

public <T> T getInstance(Class<T> type) {
Container cont = getContainer();
if (cont != null) {
Expand Down Expand Up @@ -347,4 +390,44 @@ public Object get(String key) {
public void put(String key, Object value) {
context.put(key, value);
}

public ServletContext getServletContext() {
return (ServletContext) get(StrutsStatics.SERVLET_CONTEXT);
}

public HttpServletRequest getServletRequest() {
return (HttpServletRequest) get(StrutsStatics.HTTP_REQUEST);
}

public HttpServletResponse getServletResponse() {
return (HttpServletResponse) get(StrutsStatics.HTTP_RESPONSE);
}

public void setServletContext(ServletContext servletContext) {
put(StrutsStatics.SERVLET_CONTEXT, servletContext);
}

public void setServletRequest(HttpServletRequest request) {
put(StrutsStatics.HTTP_REQUEST, request);
}

public void setServletResponse(HttpServletResponse response) {
put(StrutsStatics.HTTP_RESPONSE, response);
}

public PageContext getPageContext() {
return (PageContext) get(StrutsStatics.PAGE_CONTEXT);
}

public void setPageContext(PageContext pageContext) {
put(StrutsStatics.PAGE_CONTEXT, pageContext);
}

public ActionMapping getActionMapping() {
return (ActionMapping) get(StrutsStatics.ACTION_MAPPING);
}

public void setActionMapping(ActionMapping actionMapping) {
put(StrutsStatics.ACTION_MAPPING, actionMapping);
}
}
Expand Up @@ -395,7 +395,7 @@ public void init(ActionProxy proxy) {
contextMap.put("action", action);
}

invocationContext = new ActionContext(contextMap);
invocationContext = ActionContext.of(contextMap);
invocationContext.setName(proxy.getActionName());

createInterceptors(proxy);
Expand Down
Expand Up @@ -145,15 +145,15 @@ public String getNamespace() {

public String execute() throws Exception {
ActionContext nestedContext = ActionContext.getContext();
ActionContext.setContext(invocation.getInvocationContext());
ActionContext.bound(invocation.getInvocationContext());

String retCode = null;

try {
retCode = invocation.invoke();
} finally {
if (cleanupContext) {
ActionContext.setContext(nestedContext);
ActionContext.bound(nestedContext);
}
}

Expand Down
Expand Up @@ -213,7 +213,7 @@ public Class<? extends Configuration> type() {
rebuildRuntimeConfiguration();
} finally {
if (oldContext == null) {
ActionContext.setContext(null);
ActionContext.clear();
}
}
return packageProviders;
Expand All @@ -223,8 +223,7 @@ protected ActionContext setContext(Container cont) {
ActionContext context = ActionContext.getContext();
if (context == null) {
ValueStack vs = cont.getInstance(ValueStackFactory.class).createValueStack();
context = new ActionContext(vs.getContext());
ActionContext.setContext(context);
context = ActionContext.ofAndBound(vs.getContext());
}
return context;
}
Expand Down
Expand Up @@ -117,6 +117,11 @@ public Map<String, Object> getContext() {
return context;
}

@Override
public ActionContext getActionContext() {
return ActionContext.of(context);
}

/**
* @see com.opensymphony.xwork2.util.ValueStack#setDefaultType(java.lang.Class)
*/
Expand Down
Expand Up @@ -64,9 +64,8 @@
* @author Eric Hauser
*/
public class ActionAutowiringInterceptor extends AbstractInterceptor implements ApplicationContextAware {
private static final Logger LOG = LogManager.getLogger(ActionAutowiringInterceptor.class);

public static final String APPLICATION_CONTEXT = "com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor.applicationContext";
private static final Logger LOG = LogManager.getLogger(ActionAutowiringInterceptor.class);

private boolean initialized = false;
private ApplicationContext context;
Expand Down Expand Up @@ -121,8 +120,6 @@ public void setAutowireStrategy(Integer autowireStrategy) {
if (factory != null) {
Object bean = invocation.getAction();
factory.autoWireBean(bean);

ActionContext.getContext().put(APPLICATION_CONTEXT, context);
}
return invocation.invoke();
}
Expand Down

0 comments on commit 3739b66

Please sign in to comment.