From 50a0a72e9d4bcf96bdcb6e1d9adaefa8c11f19e8 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 19 Sep 2017 09:24:08 +0200 Subject: [PATCH] WW-4861 Adds fallback to lookup container using ActionContext --- .../opensymphony/xwork2/ActionSupport.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java b/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java index cb1ac4733..7d794716e 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java +++ b/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java @@ -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;