Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MessageFactory |
|
| 1.3636363636363635;1.364 |
1 | /* | |
2 | * $Id: MessageFactory.java 7976 2007-08-21 14:26:13Z dirk.olmes $ | |
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 | 0 | public abstract class MessageFactory extends Object |
23 | { | |
24 | /** | |
25 | * logger used by this class | |
26 | */ | |
27 | 0 | 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 | 0 | 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/<bundleName>-messages.properties</code>) from | |
40 | * <code>bundleName</code>. | |
41 | * | |
42 | * @param bundleName Name of the bundle without the "messages" suffix and without | |
43 | * file extension. | |
44 | */ | |
45 | protected static String getBundlePath(String bundleName) | |
46 | { | |
47 | 0 | 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 | 0 | Object[] arguments = new Object[] {arg}; |
62 | 0 | String messageString = getString(bundlePath, code, arguments); |
63 | 0 | return new Message(messageString, code, arguments); |
64 | } | |
65 | ||
66 | /** | |
67 | * Factory method to create a new {@link Message} instance that is filled with the formatted | |
68 | * message with id <code>code</code> from the resource bundle <code>bundlePath</code>. | |
69 | * | |
70 | * @param bundlePath complete path to the resource bundle for lookup | |
71 | * @param code numeric code of the message | |
72 | * @param arg1 | |
73 | * @param arg2 | |
74 | * @see getBundlePath() | |
75 | */ | |
76 | protected static Message createMessage(String bundlePath, int code, Object arg1, Object arg2) | |
77 | { | |
78 | 0 | Object[] arguments = new Object[] {arg1, arg2}; |
79 | 0 | String messageString = getString(bundlePath, code, arguments); |
80 | 0 | return new Message(messageString, code, arguments); |
81 | } | |
82 | ||
83 | /** | |
84 | * Factory method to create a new {@link Message} instance that is filled with the formatted | |
85 | * message with id <code>code</code> from the resource bundle <code>bundlePath</code>. | |
86 | * | |
87 | * @param bundlePath complete path to the resource bundle for lookup | |
88 | * @param code numeric code of the message | |
89 | * @param arg1 | |
90 | * @param arg2 | |
91 | * @param arg3 | |
92 | * @see getBundlePath() | |
93 | */ | |
94 | protected static Message createMessage(String bundlePath, int code, Object arg1, Object arg2, | |
95 | Object arg3) | |
96 | { | |
97 | 0 | Object[] arguments = new Object[] {arg1, arg2, arg3}; |
98 | 0 | String messageString = getString(bundlePath, code, arguments); |
99 | 0 | return new Message(messageString, code, arguments); |
100 | } | |
101 | ||
102 | /** | |
103 | * Factory method to create a new {@link Message} instance that is filled with the formatted | |
104 | * message with id <code>code</code> from the resource bundle <code>bundlePath</code>. | |
105 | * | |
106 | * @param bundlePath complete path to the resource bundle for lookup | |
107 | * @param code numeric code of the message | |
108 | */ | |
109 | protected static Message createMessage(String bundlePath, int code) | |
110 | { | |
111 | 0 | String messageString = getString(bundlePath, code, null); |
112 | 0 | return new Message(messageString, code, EMPTY_ARGS); |
113 | } | |
114 | ||
115 | /** | |
116 | * Factory method to create a {@link Message} instance that is not read from a resource bundle. | |
117 | * | |
118 | * @param message Message's message text | |
119 | * @return a Messsage instance that has an error code of -1 and no arguments. | |
120 | */ | |
121 | public static Message createStaticMessage(String message) | |
122 | { | |
123 | 0 | return new Message(message, STATIC_ERROR_CODE, EMPTY_ARGS); |
124 | } | |
125 | ||
126 | /** | |
127 | * Factory method to read the message with code <code>code</code> from the resource bundle. | |
128 | * | |
129 | * @param bundlePath complete path to the resource bundle for lookup | |
130 | * @param code numeric code of the message | |
131 | * @return formatted error message as {@link String} | |
132 | */ | |
133 | protected static String getString(String bundlePath, int code) | |
134 | { | |
135 | 0 | return getString(bundlePath, code, null); |
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 | * @param arg | |
144 | * @return formatted error message as {@link String} | |
145 | */ | |
146 | protected static String getString(String bundlePath, int code, Object arg) | |
147 | { | |
148 | 0 | Object[] arguments = new Object[] {arg}; |
149 | 0 | return getString(bundlePath, code, arguments); |
150 | } | |
151 | ||
152 | /** | |
153 | * Factory method to read the message with code <code>code</code> from the resource bundle. | |
154 | * | |
155 | * @param bundlePath complete path to the resource bundle for lookup | |
156 | * @param code numeric code of the message | |
157 | * @param arg1 | |
158 | * @param arg2 | |
159 | * @return formatted error message as {@link String} | |
160 | */ | |
161 | protected static String getString(String bundlePath, int code, Object arg1, Object arg2) | |
162 | { | |
163 | 0 | Object[] arguments = new Object[] {arg1, arg2}; |
164 | 0 | return getString(bundlePath, code, arguments); |
165 | } | |
166 | ||
167 | protected static String getString(String bundlePath, int code, Object[] args) | |
168 | { | |
169 | // We will throw a MissingResourceException if the bundle name is invalid | |
170 | // This happens if the code references a bundle name that just doesn't exist | |
171 | 0 | ResourceBundle bundle = getBundle(bundlePath); |
172 | ||
173 | try | |
174 | { | |
175 | 0 | String m = bundle.getString(String.valueOf(code)); |
176 | 0 | if (m == null) |
177 | { | |
178 | 0 | logger.error("Failed to find message for id " + code + " in resource bundle " + bundlePath); |
179 | 0 | return ""; |
180 | } | |
181 | ||
182 | 0 | return MessageFormat.format(m, args); |
183 | } | |
184 | 0 | catch (MissingResourceException e) |
185 | { | |
186 | 0 | logger.error("Failed to find message for id " + code + " in resource bundle " + bundlePath); |
187 | 0 | return ""; |
188 | } | |
189 | } | |
190 | ||
191 | /** | |
192 | * @throws MissingResourceException if resource is missing | |
193 | */ | |
194 | private static ResourceBundle getBundle(String bundlePath) | |
195 | { | |
196 | 0 | Locale locale = Locale.getDefault(); |
197 | 0 | logger.debug("Loading resource bundle: " + bundlePath + " for locale " + locale); |
198 | 0 | ResourceBundle bundle = ResourceBundle.getBundle(bundlePath, locale); |
199 | 0 | return bundle; |
200 | } | |
201 | } | |
202 | ||
203 |