001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.context.support;
018
019import java.util.Locale;
020
021import org.springframework.context.HierarchicalMessageSource;
022import org.springframework.context.MessageSource;
023import org.springframework.context.MessageSourceResolvable;
024import org.springframework.context.NoSuchMessageException;
025
026/**
027 * Empty {@link MessageSource} that delegates all calls to the parent MessageSource.
028 * If no parent is available, it simply won't resolve any message.
029 *
030 * <p>Used as placeholder by AbstractApplicationContext, if the context doesn't
031 * define its own MessageSource. Not intended for direct use in applications.
032 *
033 * @author Juergen Hoeller
034 * @since 1.1.5
035 * @see AbstractApplicationContext
036 */
037public class DelegatingMessageSource extends MessageSourceSupport implements HierarchicalMessageSource {
038
039        private MessageSource parentMessageSource;
040
041
042        @Override
043        public void setParentMessageSource(MessageSource parent) {
044                this.parentMessageSource = parent;
045        }
046
047        @Override
048        public MessageSource getParentMessageSource() {
049                return this.parentMessageSource;
050        }
051
052
053        @Override
054        public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
055                if (this.parentMessageSource != null) {
056                        return this.parentMessageSource.getMessage(code, args, defaultMessage, locale);
057                }
058                else {
059                        return renderDefaultMessage(defaultMessage, args, locale);
060                }
061        }
062
063        @Override
064        public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
065                if (this.parentMessageSource != null) {
066                        return this.parentMessageSource.getMessage(code, args, locale);
067                }
068                else {
069                        throw new NoSuchMessageException(code, locale);
070                }
071        }
072
073        @Override
074        public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
075                if (this.parentMessageSource != null) {
076                        return this.parentMessageSource.getMessage(resolvable, locale);
077                }
078                else {
079                        if (resolvable.getDefaultMessage() != null) {
080                                return renderDefaultMessage(resolvable.getDefaultMessage(), resolvable.getArguments(), locale);
081                        }
082                        String[] codes = resolvable.getCodes();
083                        String code = (codes != null && codes.length > 0 ? codes[0] : null);
084                        throw new NoSuchMessageException(code, locale);
085                }
086        }
087
088}