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
027    private static final long serialVersionUID = 1L;
028    
029    private 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
038    public LocalizedMessageFactory(final String baseName) {
039        this.resourceBundle = null;
040        this.baseName = baseName;
041    }
042
043
044    /**
045     * Gets the resource bundle base name if set.
046     * 
047     * @return the resource bundle base name if set. May be null.
048     */
049    public String getBaseName() {
050        return this.baseName;
051    }
052
053
054    /**
055     * Gets the resource bundle if set.
056     * 
057     * @return the resource bundle if set. May be null.
058     */
059    public ResourceBundle getResourceBundle() {
060        return this.resourceBundle;
061    }
062
063
064    /**
065     * Creates {@link org.apache.logging.log4j.message.StringFormattedMessage} instances.
066     *
067     * @param key The key String, used as a message if the key is absent.
068     * @param params The parameters for the message at the given key.
069     * @return The Message.
070     *
071     * @see org.apache.logging.log4j.message.MessageFactory#newMessage(String, Object...)
072     */
073    @Override
074    public Message newMessage(final String key, final Object... params) {
075        if (resourceBundle == null) {
076            return new LocalizedMessage(baseName,  key, params);
077        }
078        return new LocalizedMessage(resourceBundle, key, params);
079    }
080}