This commit is contained in:
@@ -23,6 +23,8 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
@@ -36,6 +38,8 @@ import org.springframework.util.CachingMapDecorator;
|
||||
*/
|
||||
class DefaultMessageContext implements StateManageableMessageContext {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DefaultMessageContext.class);
|
||||
|
||||
private MessageSource messageSource;
|
||||
|
||||
private Map objectMessages = new CachingMapDecorator() {
|
||||
@@ -95,8 +99,14 @@ class DefaultMessageContext implements StateManageableMessageContext {
|
||||
|
||||
public void addMessage(MessageResolver messageResolver) {
|
||||
Locale currentLocale = LocaleContextHolder.getLocale();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Resolving message using " + messageResolver);
|
||||
}
|
||||
Message message = messageResolver.resolveMessage(messageSource, currentLocale);
|
||||
List messages = (List) objectMessages.get(message.getSource());
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Adding resolved message " + message);
|
||||
}
|
||||
messages.add(message);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package org.springframework.binding.message;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.context.support.AbstractMessageSource;
|
||||
|
||||
/**
|
||||
* Default message context factory that simply stores messages indexed in a map by their source. Suitable for use in
|
||||
@@ -20,11 +22,19 @@ public class DefaultMessageContextFactory implements MessageContextFactory {
|
||||
* @param messageSource
|
||||
*/
|
||||
public DefaultMessageContextFactory(MessageSource messageSource) {
|
||||
Assert.notNull(messageSource, "The message source is required");
|
||||
if (messageSource == null) {
|
||||
messageSource = new DefaultTextFallbackMessageSource();
|
||||
}
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
public StateManageableMessageContext createMessageContext() {
|
||||
return new DefaultMessageContext(messageSource);
|
||||
}
|
||||
|
||||
private class DefaultTextFallbackMessageSource extends AbstractMessageSource {
|
||||
protected MessageFormat resolveCode(String code, Locale locale) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2004-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.message;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
class DefaultMessageResolver implements MessageResolver, MessageSourceResolvable {
|
||||
private Object source;
|
||||
private String[] codes;
|
||||
private Severity severity;
|
||||
private Object[] args;
|
||||
private String defaultText;
|
||||
|
||||
public DefaultMessageResolver(Object source, String[] codes, Severity severity, Object[] args,
|
||||
String defaultText) {
|
||||
this.source = source;
|
||||
this.codes = codes;
|
||||
this.severity = severity;
|
||||
this.args = args;
|
||||
this.defaultText = defaultText;
|
||||
}
|
||||
|
||||
public Message resolveMessage(MessageSource messageSource, Locale locale) {
|
||||
return new Message(source, messageSource.getMessage(this, locale), severity);
|
||||
}
|
||||
|
||||
// implementing MessageSourceResolver
|
||||
|
||||
public String[] getCodes() {
|
||||
return codes;
|
||||
}
|
||||
|
||||
public Object[] getArguments() {
|
||||
return args;
|
||||
}
|
||||
|
||||
public String getDefaultMessage() {
|
||||
return defaultText;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("source", source).append("severity", severity).append("codes",
|
||||
codes).append("args", args).append("defaultText", defaultText).toString();
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,11 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
/**
|
||||
* A convenient builder for building {@link MessageResolver} objects programmatically. Often used by model code such as
|
||||
@@ -170,41 +170,7 @@ public class MessageBuilder {
|
||||
}
|
||||
String[] codesArray = (String[]) codes.toArray(new String[codes.size()]);
|
||||
Object[] argsArray = args.toArray(new Object[args.size()]);
|
||||
return new BuiltMessageResolver(source, codesArray, severity, argsArray, defaultText);
|
||||
}
|
||||
|
||||
private static class BuiltMessageResolver implements MessageResolver, MessageSourceResolvable {
|
||||
private Object source;
|
||||
private String[] codes;
|
||||
private Severity severity;
|
||||
private Object[] args;
|
||||
private String defaultText;
|
||||
|
||||
public BuiltMessageResolver(Object source, String[] codes, Severity severity, Object[] args, String defaultText) {
|
||||
this.source = source;
|
||||
this.codes = codes;
|
||||
this.severity = severity;
|
||||
this.args = args;
|
||||
this.defaultText = defaultText;
|
||||
}
|
||||
|
||||
public Message resolveMessage(MessageSource messageSource, Locale locale) {
|
||||
return new Message(source, messageSource.getMessage(this, locale), severity);
|
||||
}
|
||||
|
||||
// implementing MessageSourceResolver
|
||||
|
||||
public String[] getCodes() {
|
||||
return codes;
|
||||
}
|
||||
|
||||
public Object[] getArguments() {
|
||||
return args;
|
||||
}
|
||||
|
||||
public String getDefaultMessage() {
|
||||
return defaultText;
|
||||
}
|
||||
return new DefaultMessageResolver(source, codesArray, severity, argsArray, defaultText);
|
||||
}
|
||||
|
||||
private static class ResolvableArgument implements MessageSourceResolvable {
|
||||
@@ -227,6 +193,10 @@ public class MessageBuilder {
|
||||
return arg.toString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("arg", arg).toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user