KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > i18n > Messages


1 /*
2  * $Id: Messages.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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 org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import java.text.MessageFormat JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Locale JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.MissingResourceException JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
22
23 /**
24  * <code>Messages</code> provides facilities for constructing <code>Message</code>
25  * objects and access to core message constants.
26  */

27 public class Messages implements CoreMessageConstants
28 {
29     /**
30      * logger used by this class
31      */

32     protected static final Log logger = LogFactory.getLog(Messages.class);
33
34     public static final String JavaDoc DEFAULT_BUNDLE = "core";
35
36     private static Map JavaDoc bundles = new HashMap JavaDoc();
37
38     private static Object JavaDoc[] emptyArgs = new Object JavaDoc[]{};
39
40     public static String JavaDoc get(int code)
41     {
42         return getString(DEFAULT_BUNDLE, code, emptyArgs);
43     }
44
45     public static String JavaDoc get(int code, Object JavaDoc[] args)
46     {
47         if (args == null)
48         {
49             args = Messages.emptyArgs;
50         }
51         return getString(DEFAULT_BUNDLE, code, args);
52     }
53
54     public static String JavaDoc get(int code, Object JavaDoc arg1)
55     {
56         if (arg1 == null)
57         {
58             arg1 = "null";
59         }
60         return getString(DEFAULT_BUNDLE, code, new Object JavaDoc[]{arg1});
61     }
62
63     public static String JavaDoc get(int code, Object JavaDoc arg1, Object JavaDoc arg2)
64     {
65         if (arg1 == null)
66         {
67             arg1 = "null";
68         }
69         if (arg2 == null)
70         {
71             arg2 = "null";
72         }
73         return getString(DEFAULT_BUNDLE, code, new Object JavaDoc[]{arg1, arg2});
74     }
75
76     public static String JavaDoc get(int code, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3)
77     {
78         if (arg1 == null)
79         {
80             arg1 = "null";
81         }
82         if (arg2 == null)
83         {
84             arg2 = "null";
85         }
86         if (arg3 == null)
87         {
88             arg3 = "null";
89         }
90         return getString(DEFAULT_BUNDLE, code, new Object JavaDoc[]{arg1, arg2, arg3});
91     }
92
93     public static String JavaDoc get(String JavaDoc bundle, int code)
94     {
95         return getString(bundle, code, emptyArgs);
96     }
97
98     public static String JavaDoc get(String JavaDoc bundle, int code, Object JavaDoc[] args)
99     {
100         if (args == null)
101         {
102             args = Messages.emptyArgs;
103         }
104         return getString(bundle, code, args);
105     }
106
107     public static String JavaDoc get(String JavaDoc bundle, int code, Object JavaDoc arg1)
108     {
109         if (arg1 == null)
110         {
111             arg1 = "null";
112         }
113         return getString(bundle, code, new Object JavaDoc[]{arg1});
114     }
115
116     public static String JavaDoc get(String JavaDoc bundle, int code, Object JavaDoc arg1, Object JavaDoc arg2)
117     {
118         if (arg1 == null)
119         {
120             arg1 = "null";
121         }
122         if (arg2 == null)
123         {
124             arg2 = "null";
125         }
126         return getString(bundle, code, new Object JavaDoc[]{arg1, arg2});
127     }
128
129     public static String JavaDoc get(String JavaDoc bundle, int code, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3)
130     {
131         if (arg1 == null)
132         {
133             arg1 = "null";
134         }
135         if (arg2 == null)
136         {
137             arg2 = "null";
138         }
139         if (arg3 == null)
140         {
141             arg3 = "null";
142         }
143         return getString(bundle, code, new Object JavaDoc[]{arg1, arg2, arg3});
144     }
145
146     public static String JavaDoc getString(String JavaDoc bundle, int code, Object JavaDoc[] args)
147     {
148         String JavaDoc m = getBundle(bundle).getString(String.valueOf(code));
149         if (m == null)
150         {
151             logger.error("Failed to find message for id " + code + " in resource bundle " + bundle);
152             return "";
153         }
154         return MessageFormat.format(m, args);
155     }
156
157     protected static ResourceBundle JavaDoc getBundle(String JavaDoc name)
158     {
159         ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc)bundles.get(name);
160         if (bundle == null)
161         {
162             String JavaDoc path = "META-INF.services.org.mule.i18n." + name + "-messages";
163             logger.debug("Loading resource bundle: " + path);
164             Locale JavaDoc locale = Locale.getDefault();
165             try
166             {
167                 bundle = ResourceBundle.getBundle(path, locale);
168             }
169             catch (MissingResourceException JavaDoc e)
170             {
171                 logger.warn("Failed to find resource bundle using default Locale: " + locale.toString()
172                             + ", defaulting to Locale.US. Error was: " + e.getMessage());
173                 bundle = ResourceBundle.getBundle(path);
174             }
175             bundles.put(name, bundle);
176         }
177         return bundle;
178     }
179 }
180
Popular Tags