mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
WW-4861 Adds fallback to lookup container using ActionContext
This commit is contained in:
@@ -19,6 +19,9 @@ import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.interceptor.ValidationAware;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
@@ -29,6 +32,8 @@ import java.util.*;
|
||||
*/
|
||||
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(ActionSupport.class);
|
||||
|
||||
private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
|
||||
|
||||
private transient TextProvider textProvider;
|
||||
@@ -274,6 +279,7 @@ public class ActionSupport implements Action, Validateable, ValidationAware, Tex
|
||||
* @return reference to field with TextProvider
|
||||
*/
|
||||
protected TextProvider getTextProvider() {
|
||||
checkContainer();
|
||||
if (textProvider == null) {
|
||||
TextProviderFactory tpf = container.getInstance(TextProviderFactory.class);
|
||||
textProvider = tpf.createInstance(getClass());
|
||||
@@ -282,6 +288,7 @@ public class ActionSupport implements Action, Validateable, ValidationAware, Tex
|
||||
}
|
||||
|
||||
protected LocaleProvider getLocaleProvider() {
|
||||
checkContainer();
|
||||
if (localeProvider == null) {
|
||||
LocaleProviderFactory localeProviderFactory = container.getInstance(LocaleProviderFactory.class);
|
||||
localeProvider = localeProviderFactory.createLocaleProvider();
|
||||
@@ -289,6 +296,25 @@ public class ActionSupport implements Action, Validateable, ValidationAware, Tex
|
||||
return localeProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: This a temporary solution, maybe we should consider stop injecting container into beans
|
||||
*/
|
||||
private void checkContainer() {
|
||||
if (container == null) {
|
||||
container = ActionContext.getContext().getContainer();
|
||||
if (container != null) {
|
||||
boolean devMode = Boolean.parseBoolean(container.getInstance(String.class, StrutsConstants.STRUTS_DEVMODE));
|
||||
if (devMode) {
|
||||
LOG.warn("Container is null, action was created manually? Fallback to ActionContext");
|
||||
} else {
|
||||
LOG.debug("Container is null, action was created manually? Fallback to ActionContext");
|
||||
}
|
||||
} else {
|
||||
LOG.warn("Container is null, action was created out of ActionContext scope?!?");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
|
||||
Reference in New Issue
Block a user