1
2
3
4
5
6
7
8
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
26
27 private static final Log logger = LogFactory.getLog(MessageFactory.class);
28
29
30
31
32
33 private static final int STATIC_ERROR_CODE = -1;
34 private static final transient Object[] EMPTY_ARGS = new Object[]{};
35
36
37
38
39
40
41
42
43
44
45 protected static String getBundlePath(String bundleName)
46 {
47 return "META-INF.services.org.mule.i18n." + bundleName + "-messages";
48 }
49
50
51
52
53
54
55
56
57
58
59 protected static Message createMessage(String bundlePath, int code, Object arg)
60 {
61 Object[] arguments = new Object[] {arg};
62 String messageString = getString(bundlePath, code, arguments);
63 return new Message(messageString, code, arguments);
64 }
65
66
67
68
69
70
71
72
73
74
75
76 protected static Message createMessage(String bundlePath, int code, Object arg1, Object arg2)
77 {
78 Object[] arguments = new Object[] {arg1, arg2};
79 String messageString = getString(bundlePath, code, arguments);
80 return new Message(messageString, code, arguments);
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94 protected static Message createMessage(String bundlePath, int code, Object arg1, Object arg2,
95 Object arg3)
96 {
97 Object[] arguments = new Object[] {arg1, arg2, arg3};
98 String messageString = getString(bundlePath, code, arguments);
99 return new Message(messageString, code, arguments);
100 }
101
102
103
104
105
106
107
108
109 protected static Message createMessage(String bundlePath, int code)
110 {
111 String messageString = getString(bundlePath, code, null);
112 return new Message(messageString, code, EMPTY_ARGS);
113 }
114
115
116
117
118
119
120
121 public static Message createStaticMessage(String message)
122 {
123 return new Message(message, STATIC_ERROR_CODE, EMPTY_ARGS);
124 }
125
126
127
128
129
130
131
132
133 protected static String getString(String bundlePath, int code)
134 {
135 return getString(bundlePath, code, null);
136 }
137
138
139
140
141
142
143
144
145
146 protected static String getString(String bundlePath, int code, Object arg)
147 {
148 Object[] arguments = new Object[] {arg};
149 return getString(bundlePath, code, arguments);
150 }
151
152
153
154
155
156
157
158
159
160
161 protected static String getString(String bundlePath, int code, Object arg1, Object arg2)
162 {
163 Object[] arguments = new Object[] {arg1, arg2};
164 return getString(bundlePath, code, arguments);
165 }
166
167 protected static String getString(String bundlePath, int code, Object[] args)
168 {
169
170
171 ResourceBundle bundle = getBundle(bundlePath);
172
173 try
174 {
175 String m = bundle.getString(String.valueOf(code));
176 if (m == null)
177 {
178 logger.error("Failed to find message for id " + code + " in resource bundle " + bundlePath);
179 return "";
180 }
181
182 return MessageFormat.format(m, args);
183 }
184 catch (MissingResourceException e)
185 {
186 logger.error("Failed to find message for id " + code + " in resource bundle " + bundlePath);
187 return "";
188 }
189 }
190
191
192
193
194 private static ResourceBundle getBundle(String bundlePath)
195 {
196 Locale locale = Locale.getDefault();
197 logger.debug("Loading resource bundle: " + bundlePath + " for locale " + locale);
198 ResourceBundle bundle = ResourceBundle.getBundle(bundlePath, locale);
199 return bundle;
200 }
201 }
202
203