001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: Messages.java 9603 2007-10-16 07:53:52Z zjin $
023: */
024: package com.bostechcorp.cbesb.common.i18n;
025:
026: import java.text.MessageFormat;
027: import java.util.HashMap;
028: import java.util.Locale;
029: import java.util.Map;
030: import java.util.MissingResourceException;
031: import java.util.ResourceBundle;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: /**
037: * <code>Messages</code> provides facilities for constructing
038: * <code>Message</code> objects and access to core message constants.
039: */
040: public class Messages implements CoreMessageConstants {
041: /**
042: * logger used by this class
043: */
044: protected static transient Log logger = LogFactory
045: .getLog(Messages.class);
046:
047: public static final String DEFAULT_BUNDLE = "jbi";
048:
049: private static Map<String, ResourceBundle> bundles = new HashMap<String, ResourceBundle>();
050:
051: private static Object[] emptyArgs = new Object[] {};
052:
053: public static String get(int code) {
054: return getString(DEFAULT_BUNDLE, code, emptyArgs);
055: }
056:
057: public static String get(int code, Object[] args) {
058: if (args == null) {
059: args = Messages.emptyArgs;
060: }
061: return getString(DEFAULT_BUNDLE, code, args);
062: }
063:
064: public static String get(int code, Object arg1) {
065: if (arg1 == null) {
066: arg1 = "null";
067: }
068: return getString(DEFAULT_BUNDLE, code, new Object[] { arg1 });
069: }
070:
071: public static String get(int code, Object arg1, Object arg2) {
072: if (arg1 == null) {
073: arg1 = "null";
074: }
075: if (arg2 == null) {
076: arg2 = "null";
077: }
078: return getString(DEFAULT_BUNDLE, code, new Object[] { arg1,
079: arg2 });
080: }
081:
082: public static String get(int code, Object arg1, Object arg2,
083: Object arg3) {
084: if (arg1 == null) {
085: arg1 = "null";
086: }
087: if (arg2 == null) {
088: arg2 = "null";
089: }
090: if (arg3 == null) {
091: arg3 = "null";
092: }
093: return getString(DEFAULT_BUNDLE, code, new Object[] { arg1,
094: arg2, arg3 });
095: }
096:
097: public static String get(String bundle, int code) {
098: return getString(bundle, code, emptyArgs);
099: }
100:
101: public static String get(String bundle, int code, Object[] args) {
102: if (args == null) {
103: args = Messages.emptyArgs;
104: }
105: return getString(bundle, code, args);
106: }
107:
108: public static String get(String bundle, int code, Object arg1) {
109: if (arg1 == null) {
110: arg1 = "null";
111: }
112: return getString(bundle, code, new Object[] { arg1 });
113: }
114:
115: public static String get(String bundle, int code, Object arg1,
116: Object arg2) {
117: if (arg1 == null) {
118: arg1 = "null";
119: }
120: if (arg2 == null) {
121: arg2 = "null";
122: }
123: return getString(bundle, code, new Object[] { arg1, arg2 });
124: }
125:
126: public static String get(String bundle, int code, Object arg1,
127: Object arg2, Object arg3) {
128: if (arg1 == null) {
129: arg1 = "null";
130: }
131: if (arg2 == null) {
132: arg2 = "null";
133: }
134: if (arg3 == null) {
135: arg3 = "null";
136: }
137: return getString(bundle, code,
138: new Object[] { arg1, arg2, arg3 });
139: }
140:
141: public static String getString(String bundle, int code,
142: Object[] args) {
143: String m = getBundle(bundle).getString(String.valueOf(code));
144: if (m == null) {
145: logger.error("Failed to find message for id " + code
146: + " in resource bundle " + bundle);
147: return "";
148: }
149: return MessageFormat.format(m, args);
150: }
151:
152: protected static ResourceBundle getBundle(String name) {
153: ResourceBundle bundle = (ResourceBundle) bundles.get(name);
154: if (bundle == null) {
155: // String path = "META-INF.services.org.mule.i18n." + name + "-messages";
156: String dufaultPath = "META-INF.services.com.bostechcorp.cbesb.common.i18n."
157: + name + "-messages";
158: logger.debug("Loading resource bundle: " + dufaultPath);
159: Locale locale = Locale.getDefault();
160: //jbi_zh_CN
161: String path = "META-INF.services.com.bostechcorp.cbesb.common.i18n."
162: + name
163: + "_"
164: + locale.getLanguage()
165: + "_"
166: + locale.getCountry() + "-messages";
167: // System.out.println(">>>>>>>>>>>>>>>>>>>>>lang:"+lang+">>>>>>>>>>>>>>>>>>:country:"+country);
168:
169: try {
170: bundle = ResourceBundle.getBundle(path, locale);
171: } catch (MissingResourceException e) {
172: logger
173: .warn("Failed to find resource bundle using default Locale: "
174: + locale.toString()
175: + ", defaulting to Locale.US. Error was: "
176: + e.getMessage());
177: bundle = ResourceBundle.getBundle(dufaultPath);
178: }
179: bundles.put(name, bundle);
180: }
181: return bundle;
182: }
183: }
|