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