mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Extracts common interface for LocalizedTextProvider
This commit is contained in:
@@ -24,7 +24,6 @@ import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -53,7 +52,7 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
|
||||
protected ActionConfig config;
|
||||
protected ActionInvocation invocation;
|
||||
protected UnknownHandlerManager unknownHandlerManager;
|
||||
protected LocalizedTextUtil localizedTextUtil;
|
||||
protected LocalizedTextProvider localizedTextProvider;
|
||||
|
||||
protected String actionName;
|
||||
protected String namespace;
|
||||
@@ -116,8 +115,8 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
public void setLocalizedTextUtil(LocalizedTextProvider localizedTextProvider) {
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
public Object getAction() {
|
||||
@@ -212,7 +211,7 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
|
||||
}
|
||||
|
||||
protected String prepareNotAllowedErrorMessage() {
|
||||
return localizedTextUtil.findDefaultText(
|
||||
return localizedTextProvider.findDefaultText(
|
||||
"struts.exception.method-not-allowed",
|
||||
Locale.getDefault(),
|
||||
new String[]{method, actionName}
|
||||
@@ -221,12 +220,12 @@ public class DefaultActionProxy implements ActionProxy, Serializable {
|
||||
|
||||
protected String getErrorMessage() {
|
||||
if ((namespace != null) && (namespace.trim().length() > 0)) {
|
||||
return localizedTextUtil.findDefaultText(
|
||||
return localizedTextProvider.findDefaultText(
|
||||
"xwork.exception.missing-package-action",
|
||||
Locale.getDefault(),
|
||||
new String[]{namespace, actionName});
|
||||
} else {
|
||||
return localizedTextUtil.findDefaultText(
|
||||
return localizedTextProvider.findDefaultText(
|
||||
"xwork.exception.missing-action",
|
||||
Locale.getDefault(),
|
||||
new String[]{actionName});
|
||||
|
||||
@@ -38,14 +38,14 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
|
||||
|
||||
private static final Object[] EMPTY_ARGS = new Object[0];
|
||||
|
||||
protected LocalizedTextUtil localizedTextUtil;
|
||||
protected LocalizedTextProvider localizedTextProvider;
|
||||
|
||||
public DefaultTextProvider() {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
public void setLocalizedTextUtil(LocalizedTextProvider localizedTextProvider) {
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
public boolean hasKey(String key) {
|
||||
@@ -53,7 +53,7 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
|
||||
}
|
||||
|
||||
public String getText(String key) {
|
||||
return localizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale());
|
||||
return localizedTextProvider.findDefaultText(key, ActionContext.getContext().getLocale());
|
||||
}
|
||||
|
||||
public String getText(String key, String defaultValue) {
|
||||
@@ -72,7 +72,7 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
|
||||
params = EMPTY_ARGS;
|
||||
}
|
||||
|
||||
return localizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);
|
||||
return localizedTextProvider.findDefaultText(key, ActionContext.getContext().getLocale(), params);
|
||||
}
|
||||
|
||||
public String getText(String key, String[] args) {
|
||||
@@ -83,7 +83,7 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
|
||||
params = EMPTY_ARGS;
|
||||
}
|
||||
|
||||
return localizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);
|
||||
return localizedTextProvider.findDefaultText(key, ActionContext.getContext().getLocale(), params);
|
||||
}
|
||||
|
||||
public String getText(String key, String defaultValue, List<?> args) {
|
||||
@@ -144,7 +144,7 @@ public class DefaultTextProvider implements TextProvider, Serializable, Unchaina
|
||||
}
|
||||
|
||||
public ResourceBundle getTexts(String bundleName) {
|
||||
return localizedTextUtil.findResourceBundle(bundleName, ActionContext.getContext().getLocale());
|
||||
return localizedTextProvider.findResourceBundle(bundleName, ActionContext.getContext().getLocale());
|
||||
}
|
||||
|
||||
public ResourceBundle getTexts() {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.opensymphony.xwork2;
|
||||
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public interface LocalizedTextProvider extends Serializable {
|
||||
|
||||
String findDefaultText(String aTextName, Locale locale);
|
||||
|
||||
String findDefaultText(String aTextName, Locale locale, Object[] params);
|
||||
|
||||
ResourceBundle findResourceBundle(String aBundleName, Locale locale);
|
||||
|
||||
String findText(Class aClass, String aTextName, Locale locale);
|
||||
|
||||
String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args);
|
||||
|
||||
String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args, ValueStack valueStack);
|
||||
|
||||
String findText(ResourceBundle bundle, String aTextName, Locale locale);
|
||||
|
||||
String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args);
|
||||
|
||||
String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args, ValueStack valueStack);
|
||||
|
||||
void addDefaultResourceBundle(String resourceBundleName);
|
||||
|
||||
@Deprecated
|
||||
void reset();
|
||||
}
|
||||
@@ -16,7 +16,6 @@
|
||||
package com.opensymphony.xwork2;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
@@ -30,7 +29,7 @@ public class TextProviderFactory {
|
||||
|
||||
private TextProvider textProvider;
|
||||
private LocaleProvider localeProvider;
|
||||
private LocalizedTextUtil localizedTextUtil;
|
||||
private LocalizedTextProvider localizedTextProvider;
|
||||
|
||||
@Inject
|
||||
public void setTextProvider(TextProvider textProvider) {
|
||||
@@ -43,8 +42,8 @@ public class TextProviderFactory {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
public void setLocalizedTextUtil(LocalizedTextProvider localizedTextProvider) {
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
public TextProvider createInstance(Class clazz) {
|
||||
@@ -67,7 +66,7 @@ public class TextProviderFactory {
|
||||
|
||||
protected TextProvider getTextProvider(Class clazz) {
|
||||
if (this.textProvider == null) {
|
||||
return new TextProviderSupport(clazz, localeProvider, localizedTextUtil);
|
||||
return new TextProviderSupport(clazz, localeProvider, localizedTextProvider);
|
||||
} else {
|
||||
return textProvider;
|
||||
}
|
||||
@@ -75,7 +74,7 @@ public class TextProviderFactory {
|
||||
|
||||
private TextProvider getTextProvider(ResourceBundle bundle) {
|
||||
if (this.textProvider == null) {
|
||||
return new TextProviderSupport(bundle, localeProvider, localizedTextUtil);
|
||||
return new TextProviderSupport(bundle, localeProvider, localizedTextProvider);
|
||||
}
|
||||
return textProvider;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package com.opensymphony.xwork2;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
|
||||
import java.util.*;
|
||||
@@ -33,7 +32,7 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
|
||||
private Class clazz;
|
||||
private LocaleProvider localeProvider;
|
||||
private ResourceBundle bundle;
|
||||
private LocalizedTextUtil localizedTextUtil;
|
||||
private LocalizedTextProvider localizedTextProvider;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
@@ -47,10 +46,10 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
|
||||
* @param clazz a clazz to use for reading the resource bundle.
|
||||
* @param provider a locale provider.
|
||||
*/
|
||||
public TextProviderSupport(Class clazz, LocaleProvider provider, LocalizedTextUtil localizedTextUtil) {
|
||||
public TextProviderSupport(Class clazz, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
|
||||
this.clazz = clazz;
|
||||
this.localeProvider = provider;
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,10 +58,10 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
|
||||
* @param bundle the resource bundle.
|
||||
* @param provider a locale provider.
|
||||
*/
|
||||
public TextProviderSupport(ResourceBundle bundle, LocaleProvider provider, LocalizedTextUtil localizedTextUtil) {
|
||||
public TextProviderSupport(ResourceBundle bundle, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
|
||||
this.bundle = bundle;
|
||||
this.localeProvider = provider;
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,8 +88,8 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
public void setLocalizedTextUtil(LocalizedTextProvider localizedTextProvider) {
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,9 +103,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 = localizedTextProvider.findText(clazz, key, getLocale(), null, new Object[0] );
|
||||
} else {
|
||||
message = localizedTextUtil.findText(bundle, key, getLocale(), null, new Object[0]);
|
||||
message = localizedTextProvider.findText(bundle, key, getLocale(), null, new Object[0]);
|
||||
}
|
||||
return message != null;
|
||||
}
|
||||
@@ -208,9 +207,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 localizedTextProvider.findText(clazz, key, getLocale(), defaultValue, argsArray);
|
||||
} else {
|
||||
return localizedTextUtil.findText(bundle, key, getLocale(), defaultValue, argsArray);
|
||||
return localizedTextProvider.findText(bundle, key, getLocale(), defaultValue, argsArray);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,9 +228,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 localizedTextProvider.findText(clazz, key, getLocale(), defaultValue, args);
|
||||
} else {
|
||||
return localizedTextUtil.findText(bundle, key, getLocale(), defaultValue, args);
|
||||
return localizedTextProvider.findText(bundle, key, getLocale(), defaultValue, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,9 +258,9 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
|
||||
locale = getLocale();
|
||||
}
|
||||
if (clazz != null) {
|
||||
return localizedTextUtil.findText(clazz, key, locale, defaultValue, argsArray, stack);
|
||||
return localizedTextProvider.findText(clazz, key, locale, defaultValue, argsArray, stack);
|
||||
} else {
|
||||
return localizedTextUtil.findText(bundle, key, locale, defaultValue, argsArray, stack);
|
||||
return localizedTextProvider.findText(bundle, key, locale, defaultValue, argsArray, stack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,9 +288,9 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
|
||||
locale = getLocale();
|
||||
}
|
||||
if (clazz != null) {
|
||||
return localizedTextUtil.findText(clazz, key, locale, defaultValue, args, stack);
|
||||
return localizedTextProvider.findText(clazz, key, locale, defaultValue, args, stack);
|
||||
} else {
|
||||
return localizedTextUtil.findText(bundle, key, locale, defaultValue, args, stack);
|
||||
return localizedTextProvider.findText(bundle, key, locale, defaultValue, args, stack);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -311,7 +310,7 @@ public class TextProviderSupport implements ResourceBundleTextProvider {
|
||||
* @return a resource bundle
|
||||
*/
|
||||
public ResourceBundle getTexts(String aBundleName) {
|
||||
return localizedTextUtil.findResourceBundle(aBundleName, getLocale());
|
||||
return localizedTextProvider.findResourceBundle(aBundleName, getLocale());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -245,7 +245,7 @@ public class DefaultConfiguration implements Configuration {
|
||||
builder.factory(ReflectionProvider.class, OgnlReflectionProvider.class, Scope.SINGLETON);
|
||||
builder.factory(ValueStackFactory.class, OgnlValueStackFactory.class, Scope.SINGLETON);
|
||||
|
||||
builder.factory(LocalizedTextUtil.class, LocalizedTextUtil.class, Scope.SINGLETON);
|
||||
builder.factory(LocalizedTextProvider.class, LocalizedTextUtil.class, Scope.SINGLETON);
|
||||
|
||||
builder.factory(XWorkConverter.class, Scope.SINGLETON);
|
||||
builder.factory(ConversionPropertiesProcessor.class, DefaultConversionPropertiesProcessor.class, Scope.SINGLETON);
|
||||
|
||||
+2
-1
@@ -71,6 +71,7 @@ import com.opensymphony.xwork2.ognl.accessor.XWorkListPropertyAccessor;
|
||||
import com.opensymphony.xwork2.ognl.accessor.XWorkMapPropertyAccessor;
|
||||
import com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor;
|
||||
import com.opensymphony.xwork2.util.CompoundRoot;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import com.opensymphony.xwork2.util.OgnlTextParser;
|
||||
import com.opensymphony.xwork2.util.PatternMatcher;
|
||||
@@ -181,7 +182,7 @@ public class XWorkConfigurationProvider implements ConfigurationProvider {
|
||||
.factory(ActionValidatorManager.class, "no-annotations", DefaultActionValidatorManager.class, Scope.SINGLETON)
|
||||
|
||||
.factory(TextProviderFactory.class, Scope.SINGLETON)
|
||||
.factory(LocalizedTextUtil.class, LocalizedTextUtil.class, Scope.SINGLETON)
|
||||
.factory(LocalizedTextProvider.class, LocalizedTextUtil.class, Scope.SINGLETON)
|
||||
.factory(TextProvider.class, "system", DefaultTextProvider.class, Scope.SINGLETON)
|
||||
.factory(TextProvider.class, TextProviderSupport.class, Scope.SINGLETON)
|
||||
|
||||
|
||||
@@ -194,8 +194,8 @@ public class XWorkConverter extends DefaultTypeConverter {
|
||||
}
|
||||
|
||||
public static String getConversionErrorMessage(String propertyName, ValueStack stack) {
|
||||
LocalizedTextUtil localizedTextUtil = ActionContext.getContext().getContainer().getInstance(LocalizedTextUtil.class);
|
||||
String defaultMessage = localizedTextUtil.findDefaultText("xwork.default.invalid.fieldvalue",
|
||||
LocalizedTextProvider localizedTextProvider = ActionContext.getContext().getContainer().getInstance(LocalizedTextProvider.class);
|
||||
String defaultMessage = localizedTextProvider.findDefaultText("xwork.default.invalid.fieldvalue",
|
||||
ActionContext.getContext().getLocale(),
|
||||
new Object[]{
|
||||
propertyName
|
||||
|
||||
@@ -23,7 +23,7 @@ import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.ClearableValueStack;
|
||||
import com.opensymphony.xwork2.util.Evaluated;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
|
||||
@@ -94,7 +94,7 @@ public class AliasInterceptor extends AbstractInterceptor {
|
||||
protected String aliasesKey = DEFAULT_ALIAS_KEY;
|
||||
|
||||
protected ValueStackFactory valueStackFactory;
|
||||
protected LocalizedTextUtil localizedTextUtil;
|
||||
protected LocalizedTextProvider localizedTextProvider;
|
||||
protected boolean devMode = false;
|
||||
|
||||
@Inject(XWorkConstants.DEV_MODE)
|
||||
@@ -108,8 +108,8 @@ public class AliasInterceptor extends AbstractInterceptor {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
public void setLocalizedTextUtil(LocalizedTextProvider localizedTextProvider) {
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,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 = localizedTextProvider.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);
|
||||
|
||||
+5
-5
@@ -22,7 +22,7 @@ import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.config.entities.Parameterizable;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.ClearableValueStack;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
@@ -93,7 +93,7 @@ public class StaticParametersInterceptor extends AbstractInterceptor {
|
||||
private static final Logger LOG = LogManager.getLogger(StaticParametersInterceptor.class);
|
||||
|
||||
private ValueStackFactory valueStackFactory;
|
||||
private LocalizedTextUtil localizedTextUtil;
|
||||
private LocalizedTextProvider localizedTextProvider;
|
||||
|
||||
@Inject
|
||||
public void setValueStackFactory(ValueStackFactory valueStackFactory) {
|
||||
@@ -106,8 +106,8 @@ public class StaticParametersInterceptor extends AbstractInterceptor {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setLocalizedTextUtil(LocalizedTextUtil localizedTextUtil) {
|
||||
this.localizedTextUtil = localizedTextUtil;
|
||||
public void setLocalizedTextUtil(LocalizedTextProvider localizedTextProvider) {
|
||||
this.localizedTextProvider = localizedTextProvider;
|
||||
}
|
||||
|
||||
public void setParse(String value) {
|
||||
@@ -175,7 +175,7 @@ public class StaticParametersInterceptor extends AbstractInterceptor {
|
||||
} catch (RuntimeException e) {
|
||||
if (devMode) {
|
||||
|
||||
String developerNotification = localizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{
|
||||
String developerNotification = localizedTextProvider.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);
|
||||
|
||||
@@ -23,6 +23,7 @@ package com.opensymphony.xwork2.util;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.ModelDriven;
|
||||
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
@@ -33,7 +34,6 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -88,29 +88,24 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
* @author Rainer Hermanns
|
||||
* @author tm_jee
|
||||
*/
|
||||
public class LocalizedTextUtil implements Serializable {
|
||||
public class LocalizedTextUtil implements LocalizedTextProvider {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(LocalizedTextUtil.class);
|
||||
|
||||
private static final String TOMCAT_RESOURCE_ENTRIES_FIELD = "resourceEntries";
|
||||
|
||||
private static final ConcurrentMap<Integer, List<String>> classLoaderMap = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<Integer, List<String>> classLoaderMap = new ConcurrentHashMap<>();
|
||||
|
||||
private boolean reloadBundles = false;
|
||||
private boolean devMode = false;
|
||||
|
||||
private static final ConcurrentMap<String, ResourceBundle> bundlesMap = new ConcurrentHashMap<>();
|
||||
private static final ConcurrentMap<MessageFormatKey, MessageFormat> messageFormats = new ConcurrentHashMap<>();
|
||||
private static final ConcurrentMap<Integer, ClassLoader> delegatedClassLoaderMap = new ConcurrentHashMap<>();
|
||||
private static final Set<String> missingBundles = Collections.synchronizedSet(new HashSet<String>());
|
||||
|
||||
private static final String RELOADED = "com.opensymphony.xwork2.util.LocalizedTextUtil.reloaded";
|
||||
private static final String XWORK_MESSAGES_BUNDLE = "com/opensymphony/xwork2/xwork-messages";
|
||||
|
||||
static {
|
||||
clearDefaultResourceBundles();
|
||||
}
|
||||
private final ConcurrentMap<String, ResourceBundle> bundlesMap = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<MessageFormatKey, MessageFormat> messageFormats = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<Integer, ClassLoader> delegatedClassLoaderMap = new ConcurrentHashMap<>();
|
||||
private final Set<String> missingBundles = Collections.synchronizedSet(new HashSet<String>());
|
||||
|
||||
private final String RELOADED = "com.opensymphony.xwork2.util.LocalizedTextUtil.reloaded";
|
||||
private final String XWORK_MESSAGES_BUNDLE = "com/opensymphony/xwork2/xwork-messages";
|
||||
|
||||
/**
|
||||
* Clears the internal list of resource bundles.
|
||||
@@ -119,13 +114,11 @@ public class LocalizedTextUtil implements Serializable {
|
||||
*/
|
||||
@Deprecated
|
||||
public static void clearDefaultResourceBundles() {
|
||||
ClassLoader ccl = getCurrentThreadContextClassLoader();
|
||||
List<String> bundles = new ArrayList<>();
|
||||
classLoaderMap.put(ccl.hashCode(), bundles);
|
||||
bundles.add(0, XWORK_MESSAGES_BUNDLE);
|
||||
// no-op
|
||||
}
|
||||
|
||||
public LocalizedTextUtil() {
|
||||
addDefaultResourceBundle("org/apache/struts2/struts-messages");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,6 +136,23 @@ public class LocalizedTextUtil implements Serializable {
|
||||
this.devMode = Boolean.parseBoolean(devMode);
|
||||
}
|
||||
|
||||
@Inject(value = StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES, required = false)
|
||||
public void setCustomI18NBundles(String bundles) {
|
||||
if (bundles != null && bundles.length() > 0) {
|
||||
StringTokenizer customBundles = new StringTokenizer(bundles, ", ");
|
||||
|
||||
while (customBundles.hasMoreTokens()) {
|
||||
String name = customBundles.nextToken();
|
||||
try {
|
||||
LOG.trace("Loading global messages from [{}]", name);
|
||||
addDefaultResourceBundle(name);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not find messages file {}.properties. Skipping", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add's the bundle to the internal list of default bundles.
|
||||
* <p>
|
||||
@@ -151,7 +161,8 @@ public class LocalizedTextUtil implements Serializable {
|
||||
*
|
||||
* @param resourceBundleName the name of the bundle to add.
|
||||
*/
|
||||
public static void addDefaultResourceBundle(String resourceBundleName) {
|
||||
@Override
|
||||
public void addDefaultResourceBundle(String resourceBundleName) {
|
||||
//make sure this doesn't get added more than once
|
||||
final ClassLoader ccl = getCurrentThreadContextClassLoader();
|
||||
synchronized (XWORK_MESSAGES_BUNDLE) {
|
||||
@@ -220,6 +231,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* @param locale the locale the message should be for
|
||||
* @return a localized message based on the specified key, or null if no localized message can be found for it
|
||||
*/
|
||||
@Override
|
||||
public String findDefaultText(String aTextName, Locale locale) {
|
||||
List<String> localList = classLoaderMap.get(Thread.currentThread().getContextClassLoader().hashCode());
|
||||
|
||||
@@ -253,6 +265,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* @param params an array of objects to be substituted into the message text
|
||||
* @return A formatted message based on the specified key, or null if no localized message can be found for it
|
||||
*/
|
||||
@Override
|
||||
public String findDefaultText(String aTextName, Locale locale, Object[] params) {
|
||||
String defaultText = findDefaultText(aTextName, locale);
|
||||
if (defaultText != null) {
|
||||
@@ -272,6 +285,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* @param locale the locale.
|
||||
* @return the bundle, <tt>null</tt> if not found.
|
||||
*/
|
||||
@Override
|
||||
public ResourceBundle findResourceBundle(String aBundleName, Locale locale) {
|
||||
ClassLoader classLoader = getCurrentThreadContextClassLoader();
|
||||
String key = createMissesKey(String.valueOf(classLoader.hashCode()), aBundleName, locale);
|
||||
@@ -312,7 +326,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
/**
|
||||
* @param classLoader a {@link ClassLoader} to look up the bundle from if none can be found on the current thread's classloader
|
||||
*/
|
||||
public static void setDelegatedClassLoader(final ClassLoader classLoader) {
|
||||
public void setDelegatedClassLoader(final ClassLoader classLoader) {
|
||||
synchronized (bundlesMap) {
|
||||
delegatedClassLoaderMap.put(getCurrentThreadContextClassLoader().hashCode(), classLoader);
|
||||
}
|
||||
@@ -321,7 +335,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
/**
|
||||
* @param bundleName Removes the bundle from any cached "misses"
|
||||
*/
|
||||
public static void clearBundle(final String bundleName) {
|
||||
public void clearBundle(final String bundleName) {
|
||||
bundlesMap.remove(getCurrentThreadContextClassLoader().hashCode() + bundleName);
|
||||
}
|
||||
|
||||
@@ -349,6 +363,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* @return the localized text, or null if none can be found and no defaultMessage is provided
|
||||
* @see #findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args)
|
||||
*/
|
||||
@Override
|
||||
public String findText(Class aClass, String aTextName, Locale locale) {
|
||||
return findText(aClass, aTextName, locale, aTextName, new Object[0]);
|
||||
}
|
||||
@@ -399,6 +414,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* resource bundle
|
||||
* @return the localized text, or null if none can be found and no defaultMessage is provided
|
||||
*/
|
||||
@Override
|
||||
public String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args) {
|
||||
ValueStack valueStack = ActionContext.getContext().getValueStack();
|
||||
return findText(aClass, aTextName, locale, defaultMessage, args, valueStack);
|
||||
@@ -456,8 +472,9 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* one in the ActionContext ThreadLocal
|
||||
* @return the localized text, or null if none can be found and no defaultMessage is provided
|
||||
*/
|
||||
@Override
|
||||
public String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args,
|
||||
ValueStack valueStack) {
|
||||
ValueStack valueStack) {
|
||||
String indexedTextName = null;
|
||||
if (aTextName == null) {
|
||||
LOG.warn("Trying to find text with null key!");
|
||||
@@ -641,6 +658,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* @return the localized text, or null if none can be found and no defaultMessage is provided
|
||||
* @see #findText(java.util.ResourceBundle, String, java.util.Locale, String, Object[])
|
||||
*/
|
||||
@Override
|
||||
public String findText(ResourceBundle bundle, String aTextName, Locale locale) {
|
||||
return findText(bundle, aTextName, locale, aTextName, new Object[0]);
|
||||
}
|
||||
@@ -667,6 +685,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* @param args arguments for the message formatter.
|
||||
* @return the localized text, or null if none can be found and no defaultMessage is provided
|
||||
*/
|
||||
@Override
|
||||
public String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args) {
|
||||
ValueStack valueStack = ActionContext.getContext().getValueStack();
|
||||
return findText(bundle, aTextName, locale, defaultMessage, args, valueStack);
|
||||
@@ -695,8 +714,9 @@ public class LocalizedTextUtil implements Serializable {
|
||||
* @param valueStack the OGNL value stack.
|
||||
* @return the localized text, or null if none can be found and no defaultMessage is provided
|
||||
*/
|
||||
@Override
|
||||
public String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args,
|
||||
ValueStack valueStack) {
|
||||
ValueStack valueStack) {
|
||||
try {
|
||||
reloadBundles(valueStack.getContext());
|
||||
|
||||
@@ -773,7 +793,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatWithNullDetection(MessageFormat mf, Object[] args) {
|
||||
private String formatWithNullDetection(MessageFormat mf, Object[] args) {
|
||||
String message = mf.format(args);
|
||||
if ("null".equals(message)) {
|
||||
return null;
|
||||
@@ -782,7 +802,7 @@ public class LocalizedTextUtil implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
private static MessageFormat buildMessageFormat(String pattern, Locale locale) {
|
||||
private MessageFormat buildMessageFormat(String pattern, Locale locale) {
|
||||
MessageFormatKey key = new MessageFormatKey(pattern, locale);
|
||||
MessageFormat format = messageFormats.get(key);
|
||||
if (format == null) {
|
||||
@@ -947,11 +967,10 @@ public class LocalizedTextUtil implements Serializable {
|
||||
*
|
||||
* @deprecated used only in tests
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public void reset() {
|
||||
clearDefaultResourceBundles();
|
||||
bundlesMap.clear();
|
||||
messageFormats.clear();
|
||||
// no-op
|
||||
}
|
||||
|
||||
static class MessageFormatKey {
|
||||
|
||||
@@ -40,7 +40,7 @@ public class XWorkTestCaseHelper {
|
||||
ActionContext.setContext(new ActionContext(stack.getContext()));
|
||||
|
||||
// clear out localization
|
||||
container.getInstance(LocalizedTextUtil.class).reset();
|
||||
//container.getInstance(LocalizedTextUtil.class).reset();
|
||||
|
||||
|
||||
//ObjectFactory.setObjectFactory(container.getInstance(ObjectFactory.class));
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
package org.apache.struts2.config;
|
||||
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.TextProviderFactory;
|
||||
import com.opensymphony.xwork2.factory.UnknownHandlerFactory;
|
||||
import com.opensymphony.xwork2.security.AcceptedPatternsChecker;
|
||||
@@ -390,7 +391,7 @@ public class DefaultBeanSelectionProvider extends AbstractBeanSelectionProvider
|
||||
alias(TextProvider.class, StrutsConstants.STRUTS_XWORKTEXTPROVIDER, builder, props, Scope.PROTOTYPE);
|
||||
alias(TextProviderFactory.class, StrutsConstants.STRUTS_TEXT_PROVIDER_FACTORY, builder, props, Scope.PROTOTYPE);
|
||||
alias(LocaleProvider.class, StrutsConstants.STRUTS_LOCALE_PROVIDER, builder, props);
|
||||
alias(LocalizedTextUtil.class, StrutsConstants.STRUTS_LOCALIZED_TEXT_PROVIDER, builder, props);
|
||||
alias(LocalizedTextProvider.class, StrutsConstants.STRUTS_LOCALIZED_TEXT_PROVIDER, builder, props);
|
||||
|
||||
alias(ActionProxyFactory.class, StrutsConstants.STRUTS_ACTIONPROXYFACTORY, builder, props);
|
||||
alias(ObjectTypeDeterminer.class, StrutsConstants.STRUTS_OBJECTTYPEDETERMINER, builder, props);
|
||||
@@ -434,9 +435,6 @@ public class DefaultBeanSelectionProvider extends AbstractBeanSelectionProvider
|
||||
convertIfExist(props, StrutsConstants.STRUTS_ADDITIONAL_ACCEPTED_PATTERNS, XWorkConstants.ADDITIONAL_ACCEPTED_PATTERNS);
|
||||
convertIfExist(props, StrutsConstants.STRUTS_OVERRIDE_EXCLUDED_PATTERNS, XWorkConstants.OVERRIDE_EXCLUDED_PATTERNS);
|
||||
convertIfExist(props, StrutsConstants.STRUTS_OVERRIDE_ACCEPTED_PATTERNS, XWorkConstants.OVERRIDE_ACCEPTED_PATTERNS);
|
||||
|
||||
LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
|
||||
loadCustomResourceBundles(props);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -462,21 +460,4 @@ public class DefaultBeanSelectionProvider extends AbstractBeanSelectionProvider
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCustomResourceBundles(LocatableProperties props) {
|
||||
String bundles = props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES);
|
||||
if (bundles != null && bundles.length() > 0) {
|
||||
StringTokenizer customBundles = new StringTokenizer(bundles, ", ");
|
||||
|
||||
while (customBundles.hasMoreTokens()) {
|
||||
String name = customBundles.nextToken();
|
||||
try {
|
||||
LOG.trace("Loading global messages from [{}]", name);
|
||||
LocalizedTextUtil.addDefaultResourceBundle(name);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not find messages file {}.properties. Skipping", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 localizedTextProvider.findDefaultText(
|
||||
"struts.exception.missing-package-action.with-context",
|
||||
Locale.getDefault(),
|
||||
new String[]{namespace, actionName, contextPath}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
package org.apache.struts2.util;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -188,8 +189,8 @@ public class TokenHelper {
|
||||
|
||||
if (!token.equals(sessionToken)) {
|
||||
if (LOG.isWarnEnabled()) {
|
||||
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[]{
|
||||
LocalizedTextProvider localizedTextProvider = ActionContext.getContext().getContainer().getInstance(LocalizedTextProvider.class);
|
||||
LOG.warn(localizedTextProvider.findText(TokenHelper.class, "struts.internal.invalid.token", ActionContext.getContext().getLocale(), "Form token {0} does not match the session token {1}.", new Object[]{
|
||||
token, sessionToken
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@
|
||||
<bean type="com.opensymphony.xwork2.conversion.impl.StringConverter" name="struts" class="com.opensymphony.xwork2.conversion.impl.StringConverter" scope="singleton"/>
|
||||
|
||||
<bean type="com.opensymphony.xwork2.TextProviderFactory" name="struts" class="com.opensymphony.xwork2.TextProviderFactory" scope="prototype" />
|
||||
<bean type="com.opensymphony.xwork2.util.LocalizedTextUtil" name="struts" class="com.opensymphony.xwork2.util.LocalizedTextUtil" scope="singleton" />
|
||||
<bean type="com.opensymphony.xwork2.LocalizedTextProvider" name="struts" class="com.opensymphony.xwork2.util.LocalizedTextUtil" scope="singleton" />
|
||||
<bean type="com.opensymphony.xwork2.TextProvider" name="struts" class="com.opensymphony.xwork2.TextProviderSupport" scope="prototype" />
|
||||
<bean type="com.opensymphony.xwork2.LocaleProvider" name="struts" class="com.opensymphony.xwork2.DefaultLocaleProvider" scope="singleton" />
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.opensymphony.xwork2;
|
||||
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -132,8 +130,7 @@ public class DefaultTextProviderTest extends XWorkTestCase {
|
||||
ActionContext.setContext(ctx);
|
||||
ctx.setLocale(Locale.CANADA);
|
||||
|
||||
LocalizedTextUtil.clearDefaultResourceBundles();
|
||||
LocalizedTextUtil.addDefaultResourceBundle(DefaultTextProviderTest.class.getName());
|
||||
container.getInstance(LocalizedTextProvider.class).addDefaultResourceBundle(DefaultTextProviderTest.class.getName());
|
||||
|
||||
tp = container.inject(DefaultTextProvider.class);
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ public class TextProviderSupportTest extends XWorkTestCase {
|
||||
super.setUp();
|
||||
rb = ResourceBundle.getBundle(TextProviderSupportTest.class.getName(), Locale.ENGLISH);
|
||||
|
||||
LocalizedTextUtil ltu = container.getInstance(LocalizedTextUtil.class);
|
||||
LocalizedTextProvider ltu = container.getInstance(LocalizedTextProvider.class);
|
||||
|
||||
tp = new TextProviderSupport(rb, new LocaleProvider() {
|
||||
public Locale getLocale() {
|
||||
|
||||
@@ -39,12 +39,12 @@ import java.util.ResourceBundle;
|
||||
*/
|
||||
public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
|
||||
private LocalizedTextUtil localizedTextUtil;
|
||||
private LocalizedTextProvider localizedTextProvider;
|
||||
|
||||
public void testNpeWhenClassIsPrimitive() throws Exception {
|
||||
ValueStack stack = ActionContext.getContext().getValueStack();
|
||||
stack.push(new MyObject());
|
||||
String result = localizedTextUtil.findText(MyObject.class, "someObj.someI18nKey", Locale.ENGLISH, "default message", null, stack);
|
||||
String result = localizedTextProvider.findText(MyObject.class, "someObj.someI18nKey", Locale.ENGLISH, "default message", null, stack);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@@ -98,11 +98,11 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
}
|
||||
|
||||
public void testNullKeys() {
|
||||
localizedTextUtil.findText(this.getClass(), null, Locale.getDefault());
|
||||
localizedTextProvider.findText(this.getClass(), null, Locale.getDefault());
|
||||
}
|
||||
|
||||
public void testActionGetTextXXX() throws Exception {
|
||||
LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/util/FindMe");
|
||||
localizedTextProvider.addDefaultResourceBundle("com/opensymphony/xwork2/util/FindMe");
|
||||
|
||||
SimpleAction action = new SimpleAction();
|
||||
container.inject(action);
|
||||
@@ -120,34 +120,34 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
}
|
||||
|
||||
public void testAddDefaultResourceBundle() {
|
||||
String text = localizedTextUtil.findDefaultText("foo.range", Locale.getDefault());
|
||||
String text = localizedTextProvider.findDefaultText("foo.range", Locale.getDefault());
|
||||
assertNull("Found message when it should not be available.", text);
|
||||
|
||||
LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/SimpleAction");
|
||||
localizedTextProvider.addDefaultResourceBundle("com/opensymphony/xwork2/SimpleAction");
|
||||
|
||||
String message = localizedTextUtil.findDefaultText("foo.range", Locale.US);
|
||||
String message = localizedTextProvider.findDefaultText("foo.range", Locale.US);
|
||||
assertEquals("Foo Range Message", message);
|
||||
}
|
||||
|
||||
public void testAddDefaultResourceBundle2() throws Exception {
|
||||
LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/SimpleAction");
|
||||
localizedTextProvider.addDefaultResourceBundle("com/opensymphony/xwork2/SimpleAction");
|
||||
|
||||
ActionProxy proxy = actionProxyFactory.createActionProxy("/", "packagelessAction", null, new HashMap<String, Object>(), false, true);
|
||||
proxy.execute();
|
||||
}
|
||||
|
||||
public void testDefaultMessage() throws Exception {
|
||||
String message = localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault());
|
||||
String message = localizedTextProvider.findDefaultText("xwork.error.action.execution", Locale.getDefault());
|
||||
assertEquals("Error during Action invocation", message);
|
||||
}
|
||||
|
||||
public void testDefaultMessageOverride() throws Exception {
|
||||
String message = localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault());
|
||||
String message = localizedTextProvider.findDefaultText("xwork.error.action.execution", Locale.getDefault());
|
||||
assertEquals("Error during Action invocation", message);
|
||||
|
||||
LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/test");
|
||||
localizedTextProvider.addDefaultResourceBundle("com/opensymphony/xwork2/test");
|
||||
|
||||
message = localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault());
|
||||
message = localizedTextProvider.findDefaultText("xwork.error.action.execution", Locale.getDefault());
|
||||
assertEquals("Testing resource bundle override", message);
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
ActionContext.getContext().getValueStack().push(action);
|
||||
ActionContext.getContext().getValueStack().push(action.getModel());
|
||||
|
||||
String message = localizedTextUtil.findText(ModelDrivenAction2.class, "invalid.fieldvalue.barObj.title", Locale.getDefault());
|
||||
String message = localizedTextProvider.findText(ModelDrivenAction2.class, "invalid.fieldvalue.barObj.title", Locale.getDefault());
|
||||
assertEquals("Title is invalid!", message);
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
mockActionInvocation.expectAndReturn("getAction", action);
|
||||
ActionContext.getContext().setActionInvocation((ActionInvocation) mockActionInvocation.proxy());
|
||||
|
||||
String message = localizedTextUtil.findText(ModelDrivenAction2.class, "test.foo", Locale.getDefault());
|
||||
String message = localizedTextProvider.findText(ModelDrivenAction2.class, "test.foo", Locale.getDefault());
|
||||
assertEquals("Foo!", message);
|
||||
}
|
||||
|
||||
@@ -185,46 +185,46 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
mockActionInvocation.expectAndReturn("getAction", action);
|
||||
ActionContext.getContext().setActionInvocation((ActionInvocation) mockActionInvocation.proxy());
|
||||
|
||||
String message = localizedTextUtil.findText(ModelDrivenAction2.class, "package.properties", Locale.getDefault());
|
||||
String message = localizedTextProvider.findText(ModelDrivenAction2.class, "package.properties", Locale.getDefault());
|
||||
assertEquals("It works!", message);
|
||||
}
|
||||
|
||||
public void testParameterizedDefaultMessage() throws Exception {
|
||||
String message = localizedTextUtil.findDefaultText("xwork.exception.missing-action", Locale.getDefault(), new String[]{"AddUser"});
|
||||
String message = localizedTextProvider.findDefaultText("xwork.exception.missing-action", Locale.getDefault(), new String[]{"AddUser"});
|
||||
assertEquals("There is no Action mapped for action name AddUser.", message);
|
||||
}
|
||||
|
||||
public void testParameterizedDefaultMessageWithPackage() throws Exception {
|
||||
String message = localizedTextUtil.findDefaultText("xwork.exception.missing-package-action", Locale.getDefault(), new String[]{"blah", "AddUser"});
|
||||
String message = localizedTextProvider.findDefaultText("xwork.exception.missing-package-action", Locale.getDefault(), new String[]{"blah", "AddUser"});
|
||||
assertEquals("There is no Action mapped for namespace blah and action name AddUser.", message);
|
||||
}
|
||||
|
||||
public void testLocalizedDateFormatIsUsed() throws ParseException {
|
||||
LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/util/LocalizedTextUtilTest");
|
||||
localizedTextProvider.addDefaultResourceBundle("com/opensymphony/xwork2/util/LocalizedTextUtilTest");
|
||||
Date date = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US).parse("01/01/2015");
|
||||
Object[] params = new Object[]{ date };
|
||||
String usDate = localizedTextUtil.findDefaultText("test.format.date", Locale.US, params);
|
||||
String germanDate = localizedTextUtil.findDefaultText("test.format.date", Locale.GERMANY, params);
|
||||
String usDate = localizedTextProvider.findDefaultText("test.format.date", Locale.US, params);
|
||||
String germanDate = localizedTextProvider.findDefaultText("test.format.date", Locale.GERMANY, params);
|
||||
assertEquals(usDate, "1/1/15");
|
||||
assertEquals(germanDate, "01.01.15");
|
||||
}
|
||||
|
||||
public void testXW377() {
|
||||
LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/util/LocalizedTextUtilTest");
|
||||
localizedTextProvider.addDefaultResourceBundle("com/opensymphony/xwork2/util/LocalizedTextUtilTest");
|
||||
|
||||
String text = localizedTextUtil.findText(Bar.class, "xw377", ActionContext.getContext().getLocale(), "xw377", null, ActionContext.getContext().getValueStack());
|
||||
String text = localizedTextProvider.findText(Bar.class, "xw377", ActionContext.getContext().getLocale(), "xw377", null, ActionContext.getContext().getValueStack());
|
||||
assertEquals("xw377", text); // should not log
|
||||
|
||||
String text2 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "notinbundle", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack());
|
||||
String text2 = localizedTextProvider.findText(LocalizedTextUtilTest.class, "notinbundle", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack());
|
||||
assertEquals("hello", text2); // should log WARN
|
||||
|
||||
String text3 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "notinbundle.key", ActionContext.getContext().getLocale(), "notinbundle.key", null, ActionContext.getContext().getValueStack());
|
||||
String text3 = localizedTextProvider.findText(LocalizedTextUtilTest.class, "notinbundle.key", ActionContext.getContext().getLocale(), "notinbundle.key", null, ActionContext.getContext().getValueStack());
|
||||
assertEquals("notinbundle.key", text3); // should log WARN
|
||||
|
||||
String text4 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "xw377", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack());
|
||||
String text4 = localizedTextProvider.findText(LocalizedTextUtilTest.class, "xw377", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack());
|
||||
assertEquals("xw377", text4); // should not log
|
||||
|
||||
String text5 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "username", ActionContext.getContext().getLocale(), null, null, ActionContext.getContext().getValueStack());
|
||||
String text5 = localizedTextProvider.findText(LocalizedTextUtilTest.class, "username", ActionContext.getContext().getLocale(), null, null, ActionContext.getContext().getValueStack());
|
||||
assertEquals("Santa", text5); // should not log
|
||||
}
|
||||
|
||||
@@ -233,9 +233,9 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
// Before this fix loading the bundle for Germany failed since Italy have previously failed and thus the misses cache
|
||||
// contained a false entry
|
||||
|
||||
ResourceBundle rbFrance = localizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.FRANCE);
|
||||
ResourceBundle rbItaly = localizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.ITALY);
|
||||
ResourceBundle rbGermany = localizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.GERMANY);
|
||||
ResourceBundle rbFrance = localizedTextProvider.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.FRANCE);
|
||||
ResourceBundle rbItaly = localizedTextProvider.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.ITALY);
|
||||
ResourceBundle rbGermany = localizedTextProvider.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.GERMANY);
|
||||
|
||||
assertNotNull(rbFrance);
|
||||
assertEquals("Bonjour", rbFrance.getString("hello"));
|
||||
@@ -253,7 +253,7 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
container.inject(provider);
|
||||
loadConfigurationProviders(provider);
|
||||
|
||||
this.localizedTextUtil = container.inject(LocalizedTextUtil.class);
|
||||
localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
|
||||
|
||||
ActionContext.getContext().setLocale(Locale.US);
|
||||
}
|
||||
@@ -261,7 +261,7 @@ public class LocalizedTextUtilTest extends XWorkTestCase {
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
LocalizedTextUtil.clearDefaultResourceBundles();
|
||||
localizedTextProvider = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.Stack;
|
||||
|
||||
import javax.servlet.jsp.tagext.TagSupport;
|
||||
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import org.apache.struts2.views.jsp.AbstractTagTest;
|
||||
import org.apache.struts2.views.jsp.BeanTag;
|
||||
import org.apache.struts2.views.jsp.ElseIfTag;
|
||||
@@ -439,7 +440,7 @@ public class ComponentTest extends AbstractTagTest {
|
||||
t.setPageContext(pageContext);
|
||||
t.setName("textFieldName");
|
||||
|
||||
LocalizedTextUtil.addDefaultResourceBundle("org.apache.struts2.components.temp");
|
||||
container.getInstance(LocalizedTextProvider.class).addDefaultResourceBundle("org.apache.struts2.components.temp");
|
||||
|
||||
I18nTag tag = new I18nTag();
|
||||
tag.setName("org.apache.struts2.components.tempo");
|
||||
|
||||
+17
-13
@@ -24,31 +24,35 @@ package org.apache.struts2.config;
|
||||
import java.util.Locale;
|
||||
|
||||
import com.opensymphony.xwork2.XWorkTestCase;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.config.ConfigurationProvider;
|
||||
import com.opensymphony.xwork2.test.StubConfigurationProvider;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
|
||||
import com.opensymphony.xwork2.inject.ContainerBuilder;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import com.opensymphony.xwork2.util.location.LocatableProperties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class DefaultBeanSelectionProviderTest extends XWorkTestCase {
|
||||
|
||||
public void testRegister() {
|
||||
LocalizedTextUtil.clearDefaultResourceBundles();
|
||||
LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
|
||||
LocalizedTextProvider localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
|
||||
|
||||
LocalizedTextUtil localizedTextUtil = container.inject(LocalizedTextUtil.class);
|
||||
|
||||
assertEquals("The form has already been processed or no token was supplied, please try again.", localizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
|
||||
assertEquals("The form has already been processed or no token was supplied, please try again.", localizedTextProvider.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
|
||||
|
||||
LocatableProperties props = new LocatableProperties();
|
||||
props.setProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES, "testmessages,testmessages2");
|
||||
props.setProperty(StrutsConstants.STRUTS_LOCALE, "US");
|
||||
|
||||
new DefaultBeanSelectionProvider().register(new ContainerBuilder(), props);
|
||||
loadConfigurationProviders(new StubConfigurationProvider() {
|
||||
@Override
|
||||
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
|
||||
props.setProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES, "testmessages,testmessages2");
|
||||
props.setProperty(StrutsConstants.STRUTS_LOCALE, "US");
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals("Replaced message for token tag", localizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
|
||||
localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
|
||||
|
||||
assertEquals("Replaced message for token tag", localizedTextProvider.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
package org.apache.struts2.config;
|
||||
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
@@ -52,11 +53,11 @@ public class SettingsTest extends StrutsInternalTestCase {
|
||||
public void testDefaultResourceBundlesLoaded() {
|
||||
Settings settings = new DefaultSettings();
|
||||
|
||||
LocalizedTextUtil localizedTextUtil = container.inject(LocalizedTextUtil.class);
|
||||
LocalizedTextProvider localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
|
||||
|
||||
assertEquals("testmessages,testmessages2", settings.get(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES));
|
||||
assertEquals("This is a test message", localizedTextUtil.findDefaultText("default.testmessage", Locale.getDefault()));
|
||||
assertEquals("This is another test message", localizedTextUtil.findDefaultText("default.testmessage2", Locale.getDefault()));
|
||||
assertEquals("This is a test message", localizedTextProvider.findDefaultText("default.testmessage", Locale.getDefault()));
|
||||
assertEquals("This is another test message", localizedTextProvider.findDefaultText("default.testmessage2", Locale.getDefault()));
|
||||
}
|
||||
|
||||
public void testSetSettings() {
|
||||
|
||||
@@ -32,7 +32,7 @@ import com.opensymphony.xwork2.config.entities.InterceptorStackConfig;
|
||||
import com.opensymphony.xwork2.config.entities.PackageConfig;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.interceptor.Interceptor;
|
||||
import com.opensymphony.xwork2.util.LocalizedTextUtil;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
|
||||
@@ -55,14 +55,14 @@ import java.util.Map;
|
||||
public class DispatcherTest extends StrutsInternalTestCase {
|
||||
|
||||
public void testDefaultResurceBundlePropertyLoaded() throws Exception {
|
||||
LocalizedTextUtil localizedTextUtil = container.inject(LocalizedTextUtil.class);
|
||||
LocalizedTextProvider localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
|
||||
|
||||
// some i18n messages from xwork-messages.properties
|
||||
assertEquals(localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.US),
|
||||
assertEquals(localizedTextProvider.findDefaultText("xwork.error.action.execution", Locale.US),
|
||||
"Error during Action invocation");
|
||||
|
||||
// some i18n messages from struts-messages.properties
|
||||
assertEquals(localizedTextUtil.findDefaultText("struts.messages.error.uploading", Locale.US,
|
||||
assertEquals(localizedTextProvider.findDefaultText("struts.messages.error.uploading", Locale.US,
|
||||
new Object[] { "some error messages" }),
|
||||
"Error uploading: some error messages");
|
||||
}
|
||||
|
||||
-1
@@ -235,7 +235,6 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
|
||||
if (StringUtils.isEmpty(portletNamespace)) {
|
||||
portletNamespace = "";
|
||||
}
|
||||
LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
|
||||
|
||||
container = dispatcherUtils.getContainer();
|
||||
actionMapper = container.getInstance(ActionMapper.class);
|
||||
|
||||
Reference in New Issue
Block a user