Injects instead of static reference

This commit is contained in:
Lukasz Lenart
2017-03-08 17:43:20 +01:00
parent 239327ea36
commit 4ef6418505
14 changed files with 90 additions and 64 deletions
@@ -53,6 +53,8 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
protected ActionConfig config;
protected ActionInvocation invocation;
protected UnknownHandlerManager unknownHandlerManager;
protected LocalizedTextUtil localizedTextUtil;
protected String actionName;
protected String namespace;
protected String method;
@@ -113,6 +115,11 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
this.actionEventListener = listener;
}
@Inject
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
this.localizedTextUtil = localizedTextUtil;
}
public Object getAction() {
return invocation.getAction();
}
@@ -205,7 +212,7 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
}
protected String prepareNotAllowedErrorMessage() {
return LocalizedTextUtil.findDefaultText(
return localizedTextUtil.findDefaultText(
"struts.exception.method-not-allowed",
Locale.getDefault(),
new String[]{method, actionName}
@@ -214,12 +221,12 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
protected String getErrorMessage() {
if ((namespace != null) && (namespace.trim().length() > 0)) {
return LocalizedTextUtil.findDefaultText(
return localizedTextUtil.findDefaultText(
"xwork.exception.missing-package-action",
Locale.getDefault(),
new String[]{namespace, actionName});
} else {
return LocalizedTextUtil.findDefaultText(
return localizedTextUtil.findDefaultText(
"xwork.exception.missing-action",
Locale.getDefault(),
new String[]{actionName});
@@ -15,6 +15,7 @@
*/
package com.opensymphony.xwork2;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import com.opensymphony.xwork2.util.ValueStack;
@@ -37,15 +38,22 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
private static final Object[] EMPTY_ARGS = new Object[0];
protected LocalizedTextUtil localizedTextUtil;
public DefaultTextProvider() {
}
@Inject
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
this.localizedTextUtil = localizedTextUtil;
}
public boolean hasKey(String key) {
return getText(key) != null;
}
public String getText(String key) {
return LocalizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale());
return localizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale());
}
public String getText(String key, String defaultValue) {
@@ -64,7 +72,7 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
params = EMPTY_ARGS;
}
return LocalizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);
return localizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);
}
public String getText(String key, String[] args) {
@@ -75,7 +83,7 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
params = EMPTY_ARGS;
}
return LocalizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);
return localizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);
}
public String getText(String key, String defaultValue, List<?> args) {
@@ -136,7 +144,7 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
}
public ResourceBundle getTexts(String bundleName) {
return LocalizedTextUtil.findResourceBundle(bundleName, ActionContext.getContext().getLocale());
return localizedTextUtil.findResourceBundle(bundleName, ActionContext.getContext().getLocale());
}
public ResourceBundle getTexts() {
@@ -33,6 +33,7 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
private Class clazz;
private LocaleProvider localeProvider;
private ResourceBundle bundle;
private LocalizedTextUtil localizedTextUtil;
/**
* Default constructor
@@ -85,6 +86,10 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
this.localeProvider = localeProvider;
}
@Inject
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
this.localizedTextUtil = localizedTextUtil;
}
/**
* Checks if a key is available in the resource bundles associated with this action.
@@ -97,9 +102,9 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
public boolean hasKey(String key) {
String message;
if (clazz != null) {
message = LocalizedTextUtil.findText(clazz, key, getLocale(), null, new Object[0] );
message = localizedTextUtil.findText(clazz, key, getLocale(), null, new Object[0] );
} else {
message = LocalizedTextUtil.findText(bundle, key, getLocale(), null, new Object[0]);
message = localizedTextUtil.findText(bundle, key, getLocale(), null, new Object[0]);
}
return message != null;
}
@@ -201,9 +206,9 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
public String getText(String key, String defaultValue, List<?> args) {
Object[] argsArray = ((args != null && !args.equals(Collections.emptyList())) ? args.toArray() : null);
if (clazz != null) {
return LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, argsArray);
return localizedTextUtil.findText(clazz, key, getLocale(), defaultValue, argsArray);
} else {
return LocalizedTextUtil.findText(bundle, key, getLocale(), defaultValue, argsArray);
return localizedTextUtil.findText(bundle, key, getLocale(), defaultValue, argsArray);
}
}
@@ -222,9 +227,9 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
*/
public String getText(String key, String defaultValue, String[] args) {
if (clazz != null) {
return LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, args);
return localizedTextUtil.findText(clazz, key, getLocale(), defaultValue, args);
} else {
return LocalizedTextUtil.findText(bundle, key, getLocale(), defaultValue, args);
return localizedTextUtil.findText(bundle, key, getLocale(), defaultValue, args);
}
}
@@ -252,9 +257,9 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
locale = getLocale();
}
if (clazz != null) {
return LocalizedTextUtil.findText(clazz, key, locale, defaultValue, argsArray, stack);
return localizedTextUtil.findText(clazz, key, locale, defaultValue, argsArray, stack);
} else {
return LocalizedTextUtil.findText(bundle, key, locale, defaultValue, argsArray, stack);
return localizedTextUtil.findText(bundle, key, locale, defaultValue, argsArray, stack);
}
}
@@ -282,9 +287,9 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
locale = getLocale();
}
if (clazz != null) {
return LocalizedTextUtil.findText(clazz, key, locale, defaultValue, args, stack);
return localizedTextUtil.findText(clazz, key, locale, defaultValue, args, stack);
} else {
return LocalizedTextUtil.findText(bundle, key, locale, defaultValue, args, stack);
return localizedTextUtil.findText(bundle, key, locale, defaultValue, args, stack);
}
}
@@ -304,7 +309,7 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
* @return a resource bundle
*/
public ResourceBundle getTexts(String aBundleName) {
return LocalizedTextUtil.findResourceBundle(aBundleName, getLocale());
return localizedTextUtil.findResourceBundle(aBundleName, getLocale());
}
/**
@@ -194,7 +194,8 @@ public class XWorkConverter extends DefaultTypeConverter {
}
public static String getConversionErrorMessage(String propertyName, ValueStack stack) {
String defaultMessage = LocalizedTextUtil.findDefaultText("xwork.default.invalid.fieldvalue",
LocalizedTextUtil localizedTextUtil = ActionContext.getContext().getContainer().getInstance(LocalizedTextUtil.class);
String defaultMessage = localizedTextUtil.findDefaultText("xwork.default.invalid.fieldvalue",
ActionContext.getContext().getLocale(),
new Object[]{
propertyName
@@ -94,11 +94,12 @@ public class AliasInterceptor extends AbstractInterceptor {
protected String aliasesKey = DEFAULT_ALIAS_KEY;
protected ValueStackFactory valueStackFactory;
static boolean devMode = false;
protected LocalizedTextUtil localizedTextUtil;
protected boolean devMode = false;
@Inject(XWorkConstants.DEV_MODE)
public static void setDevMode(String mode) {
devMode = "true".equals(mode);
public void setDevMode(String mode) {
this.devMode = Boolean.parseBoolean(mode);
}
@Inject
@@ -106,6 +107,11 @@ public class AliasInterceptor extends AbstractInterceptor {
this.valueStackFactory = valueStackFactory;
}
@Inject
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
this.localizedTextUtil = localizedTextUtil;
}
/**
* <p>
* Sets the name of the action parameter to look for the alias map.
@@ -173,7 +179,7 @@ public class AliasInterceptor extends AbstractInterceptor {
newStack.setValue(alias, value.get());
} catch (RuntimeException e) {
if (devMode) {
String developerNotification = LocalizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{
String developerNotification = localizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{
"Unexpected Exception caught setting '" + entry.getKey() + "' on '" + action.getClass() + ": " + e.getMessage()
});
LOG.error(developerNotification);
@@ -17,6 +17,7 @@ package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.XWorkConstants;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.security.AcceptedPatternsChecker;
@@ -218,15 +219,19 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
}
protected void notifyDeveloperParameterException(Object action, String property, String message) {
String developerNotification = LocalizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification",
ActionContext.getContext().getLocale(), "Developer Notification:\n{0}",
new Object[]{
"Unexpected Exception caught setting '" + property + "' on '" + action.getClass() + ": " + message
}
);
String developerNotification = "Unexpected Exception caught setting '" + property + "' on '" + action.getClass() + ": " + message;
if (action instanceof TextProvider) {
TextProvider tp = (TextProvider) action;
developerNotification = tp.getText("devmode.notification",
"Developer Notification:\n{0}",
new String[]{ developerNotification }
);
}
LOG.error(developerNotification);
// see https://issues.apache.org/jira/browse/WW-4066
if (action instanceof ValidationAware) {
// see https://issues.apache.org/jira/browse/WW-4066
Collection<String> messages = ((ValidationAware) action).getActionMessages();
messages.add(message);
((ValidationAware) action).setActionMessages(messages);
@@ -93,6 +93,7 @@ public class StaticParametersInterceptor extends AbstractInterceptor {
private static final Logger LOG = LogManager.getLogger(StaticParametersInterceptor.class);
private ValueStackFactory valueStackFactory;
private LocalizedTextUtil localizedTextUtil;
@Inject
public void setValueStackFactory(ValueStackFactory valueStackFactory) {
@@ -102,7 +103,12 @@ public class StaticParametersInterceptor extends AbstractInterceptor {
@Inject(XWorkConstants.DEV_MODE)
public void setDevMode(String mode) {
devMode = BooleanUtils.toBoolean(mode);
}
}
@Inject
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
this.localizedTextUtil = localizedTextUtil;
}
public void setParse(String value) {
this.parse = BooleanUtils.toBoolean(value);
@@ -168,7 +174,8 @@ public class StaticParametersInterceptor extends AbstractInterceptor {
newStack.setValue(entry.getKey(), val);
} catch (RuntimeException e) {
if (devMode) {
String developerNotification = LocalizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{
String developerNotification = localizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{
"Unexpected Exception caught setting '" + entry.getKey() + "' on '" + action.getClass() + ": " + e.getMessage()
});
LOG.error(developerNotification);
@@ -294,16 +294,18 @@ public class Date extends ContextBean {
date = new java.util.Date((long) dateObject);
} else {
if (devMode) {
String developerNotification = LocalizedTextUtil.findText(
Date.class,
"devmode.notification",
ActionContext.getContext().getLocale(),
"Developer Notification:\n{0}",
new Object[]{
"Expression [" + name + "] passed to <s:date/> tag which was evaluated to [" + dateObject + "]("
+ (dateObject != null ? dateObject.getClass() : "null") + ") isn't instance of java.util.Date nor java.util.Calendar nor long!"
}
);
TextProvider tp = findProviderInStack();
String developerNotification = "";
if (tp != null) {
developerNotification = findProviderInStack().getText(
"devmode.notification",
"Developer Notification:\n{0}",
new String[]{
"Expression [" + name + "] passed to <s:date/> tag which was evaluated to [" + dateObject + "]("
+ (dateObject != null ? dateObject.getClass() : "null") + ") isn't instance of java.util.Date nor java.util.Calendar nor long!"
}
);
}
LOG.warn(developerNotification);
} else {
LOG.debug("Expression [{}] passed to <s:date/> tag which was evaluated to [{}]({}) isn't instance of java.util.Date nor java.util.Calendar nor long!",
@@ -121,7 +121,7 @@ public class I18n extends Component {
ResourceBundle bundle = defaultTextProvider.getTexts(name);
if (bundle == null) {
bundle = LocalizedTextUtil.findResourceBundle(name, localeProvider.getLocale());
bundle = container.getInstance(LocalizedTextUtil.class).findResourceBundle(name, localeProvider.getLocale());
}
if (bundle != null) {
@@ -442,15 +442,7 @@ public class Dispatcher {
}
private Container init_PreloadConfiguration() {
Container container = getContainer();
boolean reloadi18n = Boolean.valueOf(container.getInstance(String.class, StrutsConstants.STRUTS_I18N_RELOAD));
LocalizedTextUtil.setReloadBundles(reloadi18n);
boolean devMode = Boolean.valueOf(container.getInstance(String.class, StrutsConstants.STRUTS_DEVMODE));
LocalizedTextUtil.setDevMode(devMode);
return container;
return getContainer();
}
private void init_CheckWebLogicWorkaround(Container container) {
@@ -337,11 +337,7 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest {
for (String name : names) {
List<FileItem> items = files.get(name);
for (FileItem item : items) {
if (LOG.isDebugEnabled()) {
String msg = LocalizedTextUtil.findText(this.getClass(), "struts.messages.removing.file",
Locale.ENGLISH, "no.message.found", new Object[]{name, item});
LOG.debug(msg);
}
LOG.debug("Removing file {} {}", name, item );
if (!item.isInMemory()) {
item.delete();
}
@@ -67,7 +67,7 @@ public class StrutsActionProxy extends DefaultActionProxy {
protected String getErrorMessage() {
if ((namespace != null) && (namespace.trim().length() > 0)) {
String contextPath = ServletActionContext.getRequest().getContextPath();
return LocalizedTextUtil.findDefaultText(
return localizedTextUtil.findDefaultText(
"struts.exception.missing-package-action.with-context",
Locale.getDefault(),
new String[]{namespace, actionName, contextPath}
@@ -188,7 +188,8 @@ public class TokenHelper {
if (!token.equals(sessionToken)) {
if (LOG.isWarnEnabled()) {
LOG.warn(LocalizedTextUtil.findText(TokenHelper.class, "struts.internal.invalid.token", ActionContext.getContext().getLocale(), "Form token {0} does not match the session token {1}.", new Object[]{
LocalizedTextUtil localizedTextUtil = ActionContext.getContext().getContainer().getInstance(LocalizedTextUtil.class);
LOG.warn(localizedTextUtil.findText(TokenHelper.class, "struts.internal.invalid.token", ActionContext.getContext().getLocale(), "Form token {0} does not match the session token {1}.", new Object[]{
token, sessionToken
}));
}
@@ -154,11 +154,7 @@ public class PellMultiPartRequest extends AbstractMultiPartRequest {
String inputValue = (String) fileParameterNames.nextElement();
UploadedFile[] files = getFile(inputValue);
for (UploadedFile currentFile : files) {
if (LOG.isInfoEnabled()) {
String msg = LocalizedTextUtil.findText(this.getClass(), "struts.messages.removing.file", Locale.ENGLISH,
"no.message.found", new Object[]{inputValue, currentFile});
LOG.info(msg);
}
LOG.debug("Removing file {} {}", inputValue, currentFile);
if ((currentFile != null) && currentFile.isFile()) {
if (!currentFile.delete()) {
LOG.warn("Resource Leaking: Could not remove uploaded file [{}]", currentFile.getAbsolutePath());