001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.util;
017:
018: import java.text.MessageFormat;
019: import java.util.Enumeration;
020: import java.util.Hashtable;
021: import java.util.Locale;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024: import java.util.Arrays;
025:
026: public class Messages {
027: static private Hashtable _rbBundles = new Hashtable();
028: static private Hashtable _rbFormats = new Hashtable();
029: static private Locale _globalLocale;
030:
031: private ResourceBundle _messages;
032: private Hashtable _formats;
033: private Locale _locale;
034: private String _resourceName;
035:
036: public Messages(Class clazz) {
037: this (packageName(clazz));
038: }
039:
040: private static String packageName(Class clazz) {
041: String name = clazz.getName();
042: return name.substring(0, name.lastIndexOf("."));
043: }
044:
045: public Messages(String resourceName) {
046: synchronized (Messages.class) {
047: _locale = _globalLocale;
048: _resourceName = resourceName + ".Messages";
049:
050: ResourceBundle rb = (ResourceBundle) _rbBundles
051: .get(_resourceName);
052: if (rb == null) {
053: init();
054: } else {
055: _messages = rb;
056: _formats = (Hashtable) _rbFormats.get(_resourceName);
057: }
058: }
059:
060: }
061:
062: protected void init() {
063: try {
064: if (_locale == null)
065: _messages = ResourceBundle.getBundle(_resourceName);
066: else
067: _messages = ResourceBundle.getBundle(_resourceName,
068: _locale);
069: } catch (Exception except) {
070: _messages = new EmptyResourceBundle();
071: }
072:
073: _formats = new Hashtable();
074:
075: _rbBundles.put(_resourceName, _messages);
076: _rbFormats.put(_resourceName, _formats);
077: }
078:
079: public String format(String message) {
080: return message(message);
081: }
082:
083: public String format(String message, Object... args) {
084: if (_locale != _globalLocale) {
085: synchronized (Messages.class) {
086: init();
087: }
088: }
089:
090: MessageFormat mf;
091: String msg;
092:
093: try {
094: mf = (MessageFormat) _formats.get(message);
095: if (mf == null) {
096: try {
097: msg = _messages.getString(message);
098: } catch (MissingResourceException except) {
099: return message
100: + (args != null ? " "
101: + Arrays.toString(args) : "");
102: }
103: mf = new MessageFormat(msg);
104: _formats.put(message, mf);
105: }
106: return mf.format(args);
107: } catch (Exception except) {
108: return "An internal error occured while processing message "
109: + message;
110: }
111: }
112:
113: public String message(String message) {
114: if (_locale != _globalLocale) {
115: synchronized (Messages.class) {
116: init();
117: }
118: }
119:
120: try {
121: return _messages.getString(message);
122: } catch (MissingResourceException except) {
123: return message;
124: }
125: }
126:
127: static public void setLocale(Locale locale) {
128: synchronized (Messages.class) {
129: _globalLocale = locale;
130: _rbBundles = new Hashtable();
131: _rbFormats = new Hashtable();
132: }
133: }
134:
135: static {
136: setLocale(Locale.getDefault());
137: }
138:
139: private static final class EmptyResourceBundle extends
140: ResourceBundle implements Enumeration {
141:
142: public Enumeration getKeys() {
143: return this ;
144: }
145:
146: protected Object handleGetObject(String name) {
147: return "[Missing message " + name + "]";
148: }
149:
150: public boolean hasMoreElements() {
151: return false;
152: }
153:
154: public Object nextElement() {
155: return null;
156: }
157:
158: }
159:
160: }
|