001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.message;
018
019import java.util.ResourceBundle;
020
021/**
022 * Creates {@link FormattedMessage} instances for {@link MessageFactory2} methods (and {@link MessageFactory} by
023 * extension.)
024 * 
025 * <h4>Note to implementors</h4>
026 * <p>
027 * This class does <em>not</em> implement any {@link MessageFactory2} methods and lets the superclass funnel those calls
028 * through {@link #newMessage(String, Object...)}.
029 * </p>
030 */
031public class LocalizedMessageFactory extends AbstractMessageFactory {
032    private static final long serialVersionUID = -1996295808703146741L;
033
034    // FIXME: cannot use ResourceBundle name for serialization until Java 8
035    private transient final ResourceBundle resourceBundle;
036    private final String baseName;
037
038    public LocalizedMessageFactory(final ResourceBundle resourceBundle) {
039        this.resourceBundle = resourceBundle;
040        this.baseName = null;
041    }
042
043    public LocalizedMessageFactory(final String baseName) {
044        this.resourceBundle = null;
045        this.baseName = baseName;
046    }
047
048    /**
049     * Gets the resource bundle base name if set.
050     *
051     * @return the resource bundle base name if set. May be null.
052     */
053    public String getBaseName() {
054        return this.baseName;
055    }
056
057    /**
058     * Gets the resource bundle if set.
059     *
060     * @return the resource bundle if set. May be null.
061     */
062    public ResourceBundle getResourceBundle() {
063        return this.resourceBundle;
064    }
065
066    /**
067     * @since 2.8
068     */
069    @Override
070    public Message newMessage(final String key) {
071        if (resourceBundle == null) {
072            return new LocalizedMessage(baseName,  key);
073        }
074        return new LocalizedMessage(resourceBundle, key);
075    }
076    
077    /**
078     * Creates {@link LocalizedMessage} instances.
079     *
080     * @param key The key String, used as a message if the key is absent.
081     * @param params The parameters for the message at the given key.
082     * @return The LocalizedMessage.
083     *
084     * @see org.apache.logging.log4j.message.MessageFactory#newMessage(String, Object...)
085     */
086    @Override
087    public Message newMessage(final String key, final Object... params) {
088        if (resourceBundle == null) {
089            return new LocalizedMessage(baseName, key, params);
090        }
091        return new LocalizedMessage(resourceBundle, key, params);
092    }
093
094}