message resolution fix

This commit is contained in:
Keith Donald
2008-05-14 03:07:29 +00:00
parent 8f9ad87fc8
commit 7010a806a0
14 changed files with 122 additions and 192 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.binding.message;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -28,8 +29,8 @@ 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.context.support.AbstractMessageSource;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;
import org.springframework.util.CachingMapDecorator;
/**
@@ -37,7 +38,7 @@ import org.springframework.util.CachingMapDecorator;
*
* @author Keith Donald
*/
class DefaultMessageContext implements StateManageableMessageContext {
public class DefaultMessageContext implements StateManageableMessageContext {
private static final Log logger = LogFactory.getLog(DefaultMessageContext.class);
@@ -49,10 +50,25 @@ class DefaultMessageContext implements StateManageableMessageContext {
}
};
/**
* Creates a new default message context.
*/
public DefaultMessageContext() {
init(null);
}
/**
* Creates a new default message context.
* @param messageSource the message source to resolve messages added to this context (may be null)
*/
public DefaultMessageContext(MessageSource messageSource) {
init(messageSource);
}
public MessageSource getMessageSource() {
return messageSource;
}
// implementing message context
public Message[] getAllMessages() {
@@ -69,7 +85,6 @@ class DefaultMessageContext implements StateManageableMessageContext {
}
public Message[] getMessagesByCriteria(MessageCriteria criteria) {
Assert.notNull(criteria, "The message criteria is required");
List messages = new ArrayList();
Iterator it = sourceMessages.values().iterator();
while (it.hasNext()) {
@@ -125,10 +140,17 @@ class DefaultMessageContext implements StateManageableMessageContext {
sourceMessages.putAll((Map) messagesMemento);
}
public void setMessageSource(MessageSource messageSource) {
if (messageSource == null) {
messageSource = new DefaultTextFallbackMessageSource();
}
this.messageSource = messageSource;
}
// internal helpers
private void init(MessageSource messageSource) {
this.messageSource = messageSource;
setMessageSource(messageSource);
// create the 'null' source message list eagerly to ensure global messages are indexed first
this.sourceMessages.get(null);
}
@@ -137,4 +159,9 @@ class DefaultMessageContext implements StateManageableMessageContext {
return new ToStringCreator(this).append("sourceMessages", sourceMessages).toString();
}
private static class DefaultTextFallbackMessageSource extends AbstractMessageSource {
protected MessageFormat resolveCode(String code, Locale locale) {
return null;
}
}
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2004-2008 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.text.MessageFormat;
import java.util.Locale;
import org.springframework.context.MessageSource;
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
* most Spring applications that use Spring message sources for message resource bundles. Holds a reference to a Spring
* message resource bundle for performing message text resolution.
*
* @author Keith Donald
*/
public class DefaultMessageContextFactory implements MessageContextFactory {
private MessageSource messageSource;
/**
* Create a new message context factory.
* @param messageSource
*/
public DefaultMessageContextFactory(MessageSource messageSource) {
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;
}
}
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2004-2008 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;
/**
* A factory for creating message context's whose internal state can be externally managed. Encapsulates the message
* context implementation used in a given environment.
*
* @author Keith Donald
*/
public interface MessageContextFactory {
/**
* Create a new message context.
* @return the message context, initially empty, capable of having its state managed by an external care-taker.
*/
public StateManageableMessageContext createMessageContext();
}

View File

@@ -17,6 +17,8 @@ package org.springframework.binding.message;
import java.io.Serializable;
import org.springframework.context.MessageSource;
/**
* A message context whose internal state can be managed by an external care-taker. State management employs the GOF
* Memento pattern. This context can produce a serializable memento representing its internal state at any time. A
@@ -27,7 +29,7 @@ import java.io.Serializable;
public interface StateManageableMessageContext extends MessageContext {
/**
* Create a serializable memento (token) representing a snapshot of the internal state of this message context.
* Create a serializable memento, or token representing a snapshot of the internal state of this message context.
* @return the messages memento
*/
public Serializable createMessagesMemento();
@@ -38,4 +40,11 @@ public interface StateManageableMessageContext extends MessageContext {
* @param messagesMemento the messages memento
*/
public void restoreMessages(Serializable messagesMemento);
/**
* Configure the message source used to resolve messages added to this context.
* @param messageSource the message source
* @see MessageContext#addMessage(MessageResolver)
*/
public void setMessageSource(MessageSource messageSource);
}