001    package org.springframework.richclient.context.support;
002    
003    import java.util.Locale;
004    
005    import org.apache.commons.logging.Log;
006    import org.apache.commons.logging.LogFactory;
007    import org.springframework.beans.factory.config.BeanPostProcessor;
008    import org.springframework.context.MessageSource;
009    import org.springframework.context.MessageSourceAware;
010    import org.springframework.context.NoSuchMessageException;
011    import org.springframework.richclient.util.Assert;
012    
013    public abstract class AbstractConfigurableBeanPostProcessor implements BeanPostProcessor {
014    
015        private static final Log logger = LogFactory.getLog(AbstractConfigurableBeanPostProcessor.class);
016    
017        private MessageSource messageSource;
018    
019        protected AbstractConfigurableBeanPostProcessor(MessageSource messageSource) {
020            this.messageSource = messageSource;
021        }
022    
023        /**
024         * Attempts to load the message corresponding to the given message code using this instance's {@link MessageSource}
025         * and locale.
026         * 
027         * @param messageCode
028         *            The message code that will be used to retrieve the message. Must not be null.
029         * @return The message for the given code, or null if the message code could not be found.
030         * 
031         * @throws IllegalArgumentException
032         *             if {@code messageCode} is null.
033         */
034        protected String loadMessage(String messageCode) {
035    
036            Assert.required(messageCode, "messageCode");
037    
038            if (logger.isDebugEnabled()) {
039                logger.debug("Resolving label with code '" + messageCode + "'");
040            }
041    
042            try {
043                return messageSource.getMessage(messageCode, null, Locale.getDefault());
044            }
045            catch (NoSuchMessageException e) {
046    
047                if (logger.isInfoEnabled()) {
048                    logger.info("The message source is unable to find message code [" + messageCode
049                            + "]. Ignoring and returning null.");
050                }
051    
052                return null;
053            }
054    
055        }
056    }