mirror of
https://github.com/apache/struts.git
synced 2026-08-31 19:35:40 +00:00
Adjust usage of the factory
This commit is contained in:
@@ -274,11 +274,8 @@ public class ActionSupport implements Action, Validateable, ValidationAware, Tex
|
||||
*/
|
||||
private TextProvider getTextProvider() {
|
||||
if (textProvider == null) {
|
||||
TextProviderFactory tpf = new TextProviderFactory();
|
||||
if (container != null) {
|
||||
container.inject(tpf);
|
||||
}
|
||||
textProvider = tpf.createInstance(getClass(), this);
|
||||
TextProviderFactory tpf = container.inject(TextProviderFactory.class);
|
||||
textProvider = tpf.createInstance(getClass());
|
||||
}
|
||||
return textProvider;
|
||||
}
|
||||
|
||||
+7
-1
@@ -52,6 +52,7 @@ public class AnnotationActionValidatorManager implements ActionValidatorManager
|
||||
private ValidatorFileParser validatorFileParser;
|
||||
private FileManager fileManager;
|
||||
private boolean reloadingConfigs;
|
||||
private TextProviderFactory textProviderFactory;
|
||||
|
||||
@Inject
|
||||
public void setValidatorFactory(ValidatorFactory fac) {
|
||||
@@ -73,6 +74,11 @@ public class AnnotationActionValidatorManager implements ActionValidatorManager
|
||||
this.reloadingConfigs = Boolean.parseBoolean(reloadingConfigs);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setTextProviderFactory(TextProviderFactory textProviderFactory) {
|
||||
this.textProviderFactory = textProviderFactory;
|
||||
}
|
||||
|
||||
public List<Validator> getValidators(Class clazz, String context) {
|
||||
return getValidators(clazz, context, null);
|
||||
}
|
||||
@@ -116,7 +122,7 @@ public class AnnotationActionValidatorManager implements ActionValidatorManager
|
||||
}
|
||||
|
||||
public void validate(Object object, String context, String method) throws ValidationException {
|
||||
ValidatorContext validatorContext = new DelegatingValidatorContext(object);
|
||||
ValidatorContext validatorContext = new DelegatingValidatorContext(object, textProviderFactory);
|
||||
validate(object, context, validatorContext, method);
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -18,6 +18,7 @@ package com.opensymphony.xwork2.validator;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.FileManager;
|
||||
import com.opensymphony.xwork2.FileManagerFactory;
|
||||
import com.opensymphony.xwork2.TextProviderFactory;
|
||||
import com.opensymphony.xwork2.XWorkConstants;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.ClassLoaderUtil;
|
||||
@@ -50,16 +51,19 @@ import java.util.*;
|
||||
*/
|
||||
public class DefaultActionValidatorManager implements ActionValidatorManager {
|
||||
|
||||
private final static Logger LOG = LogManager.getLogger(DefaultActionValidatorManager.class);
|
||||
|
||||
/** The file suffix for any validation file. */
|
||||
protected static final String VALIDATION_CONFIG_SUFFIX = "-validation.xml";
|
||||
|
||||
private final Map<String, List<ValidatorConfig>> validatorCache = Collections.synchronizedMap(new HashMap<String, List<ValidatorConfig>>());
|
||||
private final Map<String, List<ValidatorConfig>> validatorFileCache = Collections.synchronizedMap(new HashMap<String, List<ValidatorConfig>>());
|
||||
private final Logger LOG = LogManager.getLogger(DefaultActionValidatorManager.class);
|
||||
|
||||
private ValidatorFactory validatorFactory;
|
||||
private ValidatorFileParser validatorFileParser;
|
||||
private FileManager fileManager;
|
||||
private boolean reloadingConfigs;
|
||||
private TextProviderFactory textProviderFactory;
|
||||
|
||||
@Inject
|
||||
public void setValidatorFileParser(ValidatorFileParser parser) {
|
||||
@@ -81,6 +85,10 @@ public class DefaultActionValidatorManager implements ActionValidatorManager {
|
||||
this.reloadingConfigs = Boolean.parseBoolean(reloadingConfigs);
|
||||
}
|
||||
|
||||
public void setTextProviderFactory(TextProviderFactory textProviderFactory) {
|
||||
this.textProviderFactory = textProviderFactory;
|
||||
}
|
||||
|
||||
public synchronized List<Validator> getValidators(Class clazz, String context) {
|
||||
return getValidators(clazz, context, null);
|
||||
}
|
||||
@@ -118,7 +126,7 @@ public class DefaultActionValidatorManager implements ActionValidatorManager {
|
||||
}
|
||||
|
||||
public void validate(Object object, String context, String method) throws ValidationException {
|
||||
ValidatorContext validatorContext = new DelegatingValidatorContext(object);
|
||||
ValidatorContext validatorContext = new DelegatingValidatorContext(object, textProviderFactory);
|
||||
validate(object, context, validatorContext, method);
|
||||
}
|
||||
|
||||
|
||||
+9
-1
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
import com.opensymphony.xwork2.TextProviderFactory;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.validator.*;
|
||||
@@ -45,6 +47,12 @@ public abstract class ValidatorSupport implements Validator, ShortCircuitableVal
|
||||
private String[] messageParameters;
|
||||
protected ValueStack stack;
|
||||
|
||||
protected TextProviderFactory textProviderFactory;
|
||||
|
||||
@Inject
|
||||
public void setTextProviderFactory(TextProviderFactory textProviderFactory) {
|
||||
this.textProviderFactory = textProviderFactory;
|
||||
}
|
||||
|
||||
public void setValueStack(ValueStack stack) {
|
||||
this.stack = stack;
|
||||
@@ -76,7 +84,7 @@ public abstract class ValidatorSupport implements Validator, ShortCircuitableVal
|
||||
defaultMessage = messageKey;
|
||||
}
|
||||
if (validatorContext == null) {
|
||||
validatorContext = new DelegatingValidatorContext(object);
|
||||
validatorContext = new DelegatingValidatorContext(object, textProviderFactory);
|
||||
}
|
||||
List<Object> parsedMessageParameters = null;
|
||||
if (messageParameters != null) {
|
||||
|
||||
+20
-4
@@ -16,6 +16,8 @@
|
||||
package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.CompositeTextProvider;
|
||||
import com.opensymphony.xwork2.TextProvider;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.validator.ActionValidatorManager;
|
||||
@@ -24,6 +26,8 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
import com.opensymphony.xwork2.validator.ValidatorContext;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
@@ -166,24 +170,36 @@ public class VisitorFieldValidator extends FieldValidatorSupport {
|
||||
ValidatorContext validatorContext;
|
||||
|
||||
if (appendPrefix) {
|
||||
validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o));
|
||||
ValidatorContext parent = getValidatorContext();
|
||||
validatorContext = new AppendingValidatorContext(parent, createTextProvider(o, parent), fieldName, getMessage(o));
|
||||
} else {
|
||||
ValidatorContext parent = getValidatorContext();
|
||||
validatorContext = new DelegatingValidatorContext(parent, DelegatingValidatorContext.makeTextProvider(o, parent), parent);
|
||||
CompositeTextProvider textProvider = createTextProvider(o, parent);
|
||||
validatorContext = new DelegatingValidatorContext(parent, textProvider, parent);
|
||||
}
|
||||
|
||||
actionValidatorManager.validate(o, visitorContext, validatorContext);
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
private CompositeTextProvider createTextProvider(Object o, ValidatorContext parent) {
|
||||
List<TextProvider> textProviders = new LinkedList<>();
|
||||
if (o instanceof TextProvider) {
|
||||
textProviders.add((TextProvider) o);
|
||||
}
|
||||
textProviders.add(parent);
|
||||
|
||||
return new CompositeTextProvider(textProviders);
|
||||
}
|
||||
|
||||
|
||||
public static class AppendingValidatorContext extends DelegatingValidatorContext {
|
||||
private String field;
|
||||
private String message;
|
||||
private ValidatorContext parent;
|
||||
|
||||
public AppendingValidatorContext(ValidatorContext parent, Object object, String field, String message) {
|
||||
super(parent, makeTextProvider(object, parent), parent);
|
||||
public AppendingValidatorContext(ValidatorContext parent, TextProvider textProvider, String field, String message) {
|
||||
super(parent, textProvider, parent);
|
||||
|
||||
this.field = field;
|
||||
this.message = message;
|
||||
|
||||
@@ -125,9 +125,8 @@ public class I18n extends Component {
|
||||
}
|
||||
|
||||
if (bundle != null) {
|
||||
TextProviderFactory tpf = new TextProviderFactory();
|
||||
container.inject(tpf);
|
||||
textProvider = tpf.createInstance(bundle, localeProvider);
|
||||
TextProviderFactory tpf = container.inject(TextProviderFactory.class);
|
||||
textProvider = tpf.createInstance(bundle);
|
||||
getStack().push(textProvider);
|
||||
pushed = true;
|
||||
}
|
||||
|
||||
@@ -446,12 +446,8 @@ public class FileUploadInterceptor extends AbstractInterceptor {
|
||||
}
|
||||
|
||||
private TextProvider getTextProvider(Object action) {
|
||||
TextProviderFactory tpf = new TextProviderFactory();
|
||||
if (container != null) {
|
||||
container.inject(tpf);
|
||||
}
|
||||
LocaleProvider localeProvider = getLocaleProvider(action);
|
||||
return tpf.createInstance(action.getClass(), localeProvider);
|
||||
TextProviderFactory tpf = container.inject(TextProviderFactory.class);
|
||||
return tpf.createInstance(action.getClass());
|
||||
}
|
||||
|
||||
private LocaleProvider getLocaleProvider(Object action) {
|
||||
|
||||
@@ -52,11 +52,8 @@ public class I18NAttributeEvaluator extends AbstractAttributeEvaluator {
|
||||
throw new ConfigurationException("There is no ActionContext for current request!");
|
||||
}
|
||||
|
||||
TextProviderFactory tpf = new TextProviderFactory();
|
||||
ctx.getContainer().inject(tpf);
|
||||
LocaleProvider localeProvider = ctx.getContainer().getInstance(LocaleProvider.class);
|
||||
|
||||
TextProvider textProvider = tpf.createInstance(ctx.getActionInvocation().getAction().getClass(), localeProvider);
|
||||
TextProviderFactory tpf = ctx.getContainer().inject(TextProviderFactory.class);
|
||||
TextProvider textProvider = tpf.createInstance(ctx.getActionInvocation().getAction().getClass());
|
||||
|
||||
if (textProvider != null) {
|
||||
LOG.debug("Trying find text [{}] using TextProvider {}", expression, textProvider);
|
||||
|
||||
Reference in New Issue
Block a user