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