From f00cc7c72d0c0aa12bf4a84dc25ced496e9b6012 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Sat, 8 Mar 2008 23:14:06 +0000 Subject: [PATCH] polish --- .../message/DefaultMessageContext.java | 87 +++++++++++++++++++ .../binding/message/Message.java | 8 +- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 spring-binding/src/main/java/org/springframework/binding/message/DefaultMessageContext.java diff --git a/spring-binding/src/main/java/org/springframework/binding/message/DefaultMessageContext.java b/spring-binding/src/main/java/org/springframework/binding/message/DefaultMessageContext.java new file mode 100644 index 00000000..f76aa320 --- /dev/null +++ b/spring-binding/src/main/java/org/springframework/binding/message/DefaultMessageContext.java @@ -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(); + } + +} \ No newline at end of file diff --git a/spring-binding/src/main/java/org/springframework/binding/message/Message.java b/spring-binding/src/main/java/org/springframework/binding/message/Message.java index 35d24c14..c43f7a68 100644 --- a/spring-binding/src/main/java/org/springframework/binding/message/Message.java +++ b/spring-binding/src/main/java/org/springframework/binding/message/Message.java @@ -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(); + } + }