This commit is contained in:
Keith Donald
2008-03-08 23:14:06 +00:00
parent af58682d13
commit f00cc7c72d
2 changed files with 94 additions and 1 deletions

View File

@@ -0,0 +1,87 @@
/*
* 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.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.CachingMapDecorator;
/**
* Default message context implementation.
*
* @author Keith Donald
*/
class DefaultMessageContext implements StateManageableMessageContext {
private MessageSource messageSource;
private Map objectMessages = new CachingMapDecorator() {
protected Object create(Object objectId) {
return new ArrayList();
}
};
public DefaultMessageContext(MessageSource messageSource) {
this.messageSource = messageSource;
}
public Serializable createMessagesMemento() {
return new HashMap(objectMessages);
}
public void restoreMessages(Serializable messagesMemento) {
this.objectMessages.putAll((Map) messagesMemento);
}
public void addMessage(MessageResolver messageResolver) {
Locale currentLocale = LocaleContextHolder.getLocale();
Message message = messageResolver.resolveMessage(messageSource, currentLocale);
List messages = (List) objectMessages.get(message.getSource());
messages.add(message);
}
public Message[] getMessages() {
List messages = new ArrayList();
Iterator i = objectMessages.keySet().iterator();
while (i.hasNext()) {
messages.addAll((List) objectMessages.get(i.next()));
}
return (Message[]) messages.toArray(new Message[messages.size()]);
}
public Message[] getMessages(Object source) {
List messages = (List) objectMessages.get(source);
return (Message[]) messages.toArray(new Message[messages.size()]);
}
public void clearMessages() {
objectMessages.clear();
}
public String toString() {
return new ToStringCreator(this).append("objectMessages", objectMessages).toString();
}
}

View File

@@ -2,6 +2,8 @@ package org.springframework.binding.message;
import java.io.Serializable;
import org.springframework.core.style.ToStringCreator;
/**
* An object of communication that provides text information from a source. For example, a validation message may inform
* a web application user a business rule was violated. A messages comes from a source, has text providing the basis for
@@ -24,7 +26,6 @@ public class Message implements Serializable {
* @param severity the message severity
*/
public Message(Object source, String text, Severity severity) {
super();
this.source = source;
this.text = text;
this.severity = severity;
@@ -54,4 +55,9 @@ public class Message implements Serializable {
return severity;
}
public String toString() {
return new ToStringCreator(this).append("source", source).append("severity", severity).append("text", text)
.toString();
}
}