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 org.apache.logging.log4j.message.LocalizedMessage} instances for
023 * {@link #newMessage(String, Object...)}.
024 */
025public class LocalizedMessageFactory extends AbstractMessageFactory {
026    private static final long serialVersionUID = -1996295808703146741L;
027
028    // FIXME: cannot use ResourceBundle name for serialization until Java 8
029    private transient final ResourceBundle resourceBundle;
030    private final String baseName;
031
032    public LocalizedMessageFactory(final ResourceBundle resourceBundle) {
033        this.resourceBundle = resourceBundle;
034        this.baseName = null;
035    }
036
037    public LocalizedMessageFactory(final String baseName) {
038        this.resourceBundle = null;
039        this.baseName = baseName;
040    }
041
042    /**
043     * Gets the resource bundle base name if set.
044     *
045     * @return the resource bundle base name if set. May be null.
046     */
047    public String getBaseName() {
048        return this.baseName;
049    }
050
051    /**
052     * Gets the resource bundle if set.
053     *
054     * @return the resource bundle if set. May be null.
055     */
056    public ResourceBundle getResourceBundle() {
057        return this.resourceBundle;
058    }
059
060    /**
061     * Creates {@link org.apache.logging.log4j.message.StringFormattedMessage} instances.
062     *
063     * @param key The key String, used as a message if the key is absent.
064     * @param params The parameters for the message at the given key.
065     * @return The Message.
066     *
067     * @see org.apache.logging.log4j.message.MessageFactory#newMessage(String, Object...)
068     */
069    @Override
070    public Message newMessage(final String key, final Object... params) {
071        if (resourceBundle == null) {
072            return new LocalizedMessage(baseName,  key, params);
073        }
074        return new LocalizedMessage(resourceBundle, key, params);
075    }
076}