001/*
002 * Copyright 2002-2017 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;
025import org.springframework.lang.Nullable;
026
027/**
028 * Empty {@link MessageSource} that delegates all calls to the parent MessageSource.
029 * If no parent is available, it simply won't resolve any message.
030 *
031 * <p>Used as placeholder by AbstractApplicationContext, if the context doesn't
032 * define its own MessageSource. Not intended for direct use in applications.
033 *
034 * @author Juergen Hoeller
035 * @since 1.1.5
036 * @see AbstractApplicationContext
037 */
038public class DelegatingMessageSource extends MessageSourceSupport implements HierarchicalMessageSource {
039
040        @Nullable
041        private MessageSource parentMessageSource;
042
043
044        @Override
045        public void setParentMessageSource(@Nullable MessageSource parent) {
046                this.parentMessageSource = parent;
047        }
048
049        @Override
050        @Nullable
051        public MessageSource getParentMessageSource() {
052                return this.parentMessageSource;
053        }
054
055
056        @Override
057        @Nullable
058        public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
059                if (this.parentMessageSource != null) {
060                        return this.parentMessageSource.getMessage(code, args, defaultMessage, locale);
061                }
062                else if (defaultMessage != null) {
063                        return renderDefaultMessage(defaultMessage, args, locale);
064                }
065                else {
066                        return null;
067                }
068        }
069
070        @Override
071        public String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException {
072                if (this.parentMessageSource != null) {
073                        return this.parentMessageSource.getMessage(code, args, locale);
074                }
075                else {
076                        throw new NoSuchMessageException(code, locale);
077                }
078        }
079
080        @Override
081        public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
082                if (this.parentMessageSource != null) {
083                        return this.parentMessageSource.getMessage(resolvable, locale);
084                }
085                else {
086                        if (resolvable.getDefaultMessage() != null) {
087                                return renderDefaultMessage(resolvable.getDefaultMessage(), resolvable.getArguments(), locale);
088                        }
089                        String[] codes = resolvable.getCodes();
090                        String code = (codes != null && codes.length > 0 ? codes[0] : "");
091                        throw new NoSuchMessageException(code, locale);
092                }
093        }
094
095
096        @Override
097        public String toString() {
098                return this.parentMessageSource != null ? this.parentMessageSource.toString() : "Empty MessageSource";
099        }
100
101}