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.commons.vfs2.util; 018 019import java.text.MessageFormat; 020import java.util.MissingResourceException; 021import java.util.ResourceBundle; 022import java.util.concurrent.ConcurrentHashMap; 023import java.util.concurrent.ConcurrentMap; 024 025/** 026 * Formats messages. 027 */ 028public final class Messages { 029 /** 030 * Map from message code to MessageFormat object for the message. 031 */ 032 private static ConcurrentMap<String, MessageFormat> messageMap = new ConcurrentHashMap<>(); 033 private static final ResourceBundle RESOURCES = new CombinedResources("org.apache.commons.vfs2.Resources"); 034 035 private Messages() { 036 } 037 038 /** 039 * Formats a message. 040 * 041 * @param code The message code. 042 * @return The formatted message. 043 */ 044 public static String getString(final String code) { 045 return getString(code, new Object[0]); 046 } 047 048 /** 049 * Formats a message. 050 * 051 * @param code The message code. 052 * @param param The message parameter. 053 * @return The formatted message. 054 * @deprecated Will be removed in 3.0 in favor of {@link #getString(String, Object[])} When removed, calls to this 055 * method will automatically recompile to {@link #getString(String, Object[])} 056 */ 057 @Deprecated 058 public static String getString(final String code, final Object param) { 059 return getString(code, new Object[] { param }); 060 } 061 062 /** 063 * Formats a message. 064 * 065 * @param code The message code. 066 * @param params The message parameters. 067 * @return The formatted message. 068 */ 069 public static String getString(final String code, final Object... params) { 070 try { 071 if (code == null) { 072 return null; 073 } 074 075 final MessageFormat msg = findMessage(code); 076 return msg.format(params); 077 } catch (final MissingResourceException mre) { 078 return "Unknown message with code \"" + code + "\"."; 079 } 080 } 081 082 /** 083 * Locates a message by its code. 084 */ 085 private static MessageFormat findMessage(final String code) throws MissingResourceException { 086 // Check if the message is cached 087 MessageFormat msg = messageMap.get(code); 088 if (msg != null) { 089 return msg; 090 } 091 092 final String msgText = RESOURCES.getString(code); 093 msg = new MessageFormat(msgText); 094 messageMap.putIfAbsent(code, msg); 095 return messageMap.get(code); 096 } 097}