View Javadoc

1   /*
2    * $Id: MessageFactory.java 10790 2008-02-12 20:53:32Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.i18n;
12  
13  import java.text.MessageFormat;
14  import java.util.Locale;
15  import java.util.MissingResourceException;
16  import java.util.ResourceBundle;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  
22  public abstract class MessageFactory extends Object
23  {
24      /**
25       * logger used by this class
26       */
27      private static final Log logger = LogFactory.getLog(MessageFactory.class);
28  
29      /**
30       * This error code is used for {@link Message} instances that are not read from a
31       * resource bundles but are created only with a string.
32       */
33      private static final int STATIC_ERROR_CODE = -1;
34      private static final transient Object[] EMPTY_ARGS = new Object[]{};
35  
36  
37      /**
38       * Computes the bundle's full path 
39       * (<code>META-INF/services/org/mule/i18n/&lt;bundleName&gt;-messages.properties</code>) from
40       * <code>bundleName</code>.
41       * 
42       * @param bundleName Name of the bundle without the &quot;messages&quot; suffix and without
43       *          file extension.
44       */
45      protected static String getBundlePath(String bundleName)
46      {
47          return "META-INF.services.org.mule.i18n." + bundleName + "-messages";
48      }
49      
50      /**
51       * Factory method to create a new {@link Message} instance that is filled with the formatted
52       * message with id <code>code</code> from the resource bundle <code>bundlePath</code>.
53       * 
54       * @param bundlePath complete path to the resource bundle for lookup
55       * @param code numeric code of the message
56       * @param arg
57       * @see getBundlePath()
58       */
59      protected static Message createMessage(String bundlePath, int code, Object arg)
60      {
61          return createMessage(bundlePath, code, new Object[] {arg});
62      }
63      
64      /**
65       * Factory method to create a new {@link Message} instance that is filled with the formatted
66       * message with id <code>code</code> from the resource bundle <code>bundlePath</code>.
67       * 
68       * @param bundlePath complete path to the resource bundle for lookup
69       * @param code numeric code of the message
70       * @param arg1
71       * @param arg2
72       * @see getBundlePath()
73       */
74      protected static Message createMessage(String bundlePath, int code, Object arg1, Object arg2)
75      {
76          return createMessage(bundlePath, code, new Object[] {arg1, arg2});
77      }
78      
79      /**
80       * Factory method to create a new {@link Message} instance that is filled with the formatted
81       * message with id <code>code</code> from the resource bundle <code>bundlePath</code>.
82       * 
83       * @param bundlePath complete path to the resource bundle for lookup
84       * @param code numeric code of the message
85       * @param arg1
86       * @param arg2
87       * @param arg3
88       * @see getBundlePath()
89       */
90      protected static Message createMessage(String bundlePath, int code, Object arg1, Object arg2, 
91          Object arg3)
92      {
93          return createMessage(bundlePath, code, new Object[] {arg1, arg2, arg3});
94      }
95      
96      /**
97       * Factory method to create a new {@link Message} instance that is filled with the formatted
98       * message with id <code>code</code> from the resource bundle <code>bundlePath</code>.
99       * 
100      * <b>Attention:</b> do not confuse this method with 
101      * <code>createMessage(String, int, Object)</code>.
102      * 
103      * @param bundlePath complete path to the resource bundle for lookup
104      * @param code numeric code of the message
105      * @param arguments
106      * @see getBundlePath()
107      */
108     protected static Message createMessage(String bundlePath, int code, Object[] arguments)
109     {
110         String messageString = getString(bundlePath, code, arguments);
111         return new Message(messageString, code, arguments);
112     }
113     
114     /**
115      * Factory method to create a new {@link Message} instance that is filled with the formatted
116      * message with id <code>code</code> from the resource bundle <code>bundlePath</code>.
117      * 
118      * @param bundlePath complete path to the resource bundle for lookup
119      * @param code numeric code of the message
120      */
121     protected static Message createMessage(String bundlePath, int code)
122     {
123         String messageString = getString(bundlePath, code, null);
124         return new Message(messageString, code, EMPTY_ARGS);
125     }
126 
127     /**
128      * Factory method to create a {@link Message} instance that is not read from a resource bundle.
129      * 
130      * @param message Message's message text
131      * @return a Messsage instance that has an error code of -1 and no arguments.
132      */
133     public static Message createStaticMessage(String message)
134     {
135         return new Message(message, STATIC_ERROR_CODE, EMPTY_ARGS);
136     }    
137 
138     /**
139      * Factory method to read the message with code <code>code</code> from the resource bundle.
140      * 
141      * @param bundlePath complete path to the resource bundle for lookup
142      * @param code numeric code of the message
143      * @return formatted error message as {@link String}
144      */
145     protected static String getString(String bundlePath, int code)
146     {
147         return getString(bundlePath, code, null);
148     }
149     
150     /**
151      * Factory method to read the message with code <code>code</code> from the resource bundle.
152      * 
153      * @param bundlePath complete path to the resource bundle for lookup
154      * @param code numeric code of the message
155      * @param arg
156      * @return formatted error message as {@link String}
157      */
158     protected static String getString(String bundlePath, int code, Object arg)
159     {
160         Object[] arguments = new Object[] {arg};
161         return getString(bundlePath, code, arguments);
162     }
163     
164     /**
165      * Factory method to read the message with code <code>code</code> from the resource bundle.
166      * 
167      * @param bundlePath complete path to the resource bundle for lookup
168      * @param code numeric code of the message
169      * @param arg1
170      * @param arg2
171      * @return formatted error message as {@link String}
172      */
173     protected static String getString(String bundlePath, int code, Object arg1, Object arg2)
174     {
175         Object[] arguments = new Object[] {arg1, arg2};
176         return getString(bundlePath, code, arguments);
177     }
178 
179     protected static String getString(String bundlePath, int code, Object[] args)
180     {
181         // We will throw a MissingResourceException if the bundle name is invalid
182         // This happens if the code references a bundle name that just doesn't exist
183         ResourceBundle bundle = getBundle(bundlePath);
184 
185         try
186         {
187             String m = bundle.getString(String.valueOf(code));
188             if (m == null)
189             {
190                 logger.error("Failed to find message for id " + code + " in resource bundle " + bundlePath);
191                 return "";
192             }
193 
194             return MessageFormat.format(m, args);
195         }
196         catch (MissingResourceException e)
197         {
198             logger.error("Failed to find message for id " + code + " in resource bundle " + bundlePath);
199             return "";
200         }
201     }
202 
203     /**
204      * @throws MissingResourceException if resource is missing
205      */
206     private static ResourceBundle getBundle(String bundlePath)
207     {
208         Locale locale = Locale.getDefault();
209         if (logger.isTraceEnabled())
210         {
211             logger.trace("Loading resource bundle: " + bundlePath + " for locale " + locale);
212         }
213         ResourceBundle bundle = ResourceBundle.getBundle(bundlePath, locale);
214         return bundle;
215     }
216 }
217 
218