View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.util;
18  
19  import java.text.MessageFormat;
20  import java.util.MissingResourceException;
21  import java.util.ResourceBundle;
22  import java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ConcurrentMap;
24  
25  /**
26   * Formats messages.
27   */
28  public final class Messages
29  {
30      /**
31       * Map from message code to MessageFormat object for the message.
32       */
33      private static ConcurrentMap<String, MessageFormat> messageMap = new ConcurrentHashMap<String, MessageFormat>();
34      private static final ResourceBundle RESOURCES = new CombinedResources("org.apache.commons.vfs2.Resources");
35  
36      private Messages()
37      {
38      }
39  
40      /**
41       * Formats a message.
42       *
43       * @param code The message code.
44       * @return The formatted message.
45       */
46      public static String getString(final String code)
47      {
48          return getString(code, new Object[0]);
49      }
50  
51      /**
52       * Formats a message.
53       *
54       * @param code
55       *            The message code.
56       * @param param
57       *            The message parameter.
58       * @return The formatted message.
59       * @deprecated Will be removed in 3.0 in favor of {@link #getString(String, Object[])} When removed, calls to this
60       *             method will automatically recompile to {@link #getString(String, Object[])}
61       */
62      @Deprecated
63      public static String getString(final String code, final Object param)
64      {
65          return getString(code, new Object[]{param});
66      }
67  
68      /**
69       * Formats a message.
70       *
71       * @param code   The message code.
72       * @param params The message parameters.
73       * @return The formatted message.
74       */
75      public static String getString(final String code, final Object... params)
76      {
77          try
78          {
79              if (code == null)
80              {
81                  return null;
82              }
83  
84              final MessageFormat msg = findMessage(code);
85              return msg.format(params);
86          }
87          catch (final MissingResourceException mre)
88          {
89              return "Unknown message with code \"" + code + "\".";
90          }
91      }
92  
93      /**
94       * Locates a message by its code.
95       */
96      private static MessageFormat findMessage(final String code)
97          throws MissingResourceException
98      {
99          // Check if the message is cached
100         MessageFormat msg = messageMap.get(code);
101         if (msg != null)
102         {
103             return msg;
104         }
105 
106         final String msgText = RESOURCES.getString(code);
107         msg = new MessageFormat(msgText);
108         messageMap.putIfAbsent(code, msg);
109         return messageMap.get(code);
110     }
111 }