1 45 package org.openejb.util; 46 47 import java.text.MessageFormat ; 48 import java.util.Enumeration ; 49 import java.util.Hashtable ; 50 import java.util.Locale ; 51 import java.util.MissingResourceException ; 52 import java.util.ResourceBundle ; 53 54 60 public class Messages { 61 static private Hashtable _rbBundles = new Hashtable (); 62 static private Hashtable _rbFormats = new Hashtable (); 63 static private Locale _globalLocale; 64 65 private ResourceBundle _messages; 66 private Hashtable _formats; 67 private Locale _locale; 68 private String _resourceName; 69 70 public Messages(String resourceName) { 71 synchronized (Messages.class) { 72 _locale = _globalLocale; 73 _resourceName = resourceName + ".Messages"; 74 75 ResourceBundle rb = (ResourceBundle ) _rbBundles.get(_resourceName); 76 if (rb == null) { 77 init(); 78 } else { 79 _messages = rb; 80 _formats = (Hashtable ) _rbFormats.get(_resourceName); 81 } 82 } 83 84 } 85 86 protected void init() { 87 try { 88 if (_locale == null) 89 _messages = ResourceBundle.getBundle(_resourceName); 90 else 91 _messages = ResourceBundle.getBundle(_resourceName, _locale); 92 } catch (Exception except) { 93 _messages = new EmptyResourceBundle(); 94 } 95 96 _formats = new Hashtable (); 97 98 _rbBundles.put(_resourceName, _messages); 99 _rbFormats.put(_resourceName, _formats); 100 } 101 102 public String format(String message, Object arg1) { 103 return format(message, new Object [] { arg1 }); 104 } 105 106 public String format(String message, Object arg1, Object arg2) { 107 return format(message, new Object [] { arg1, arg2 }); 108 } 109 110 public String format(String message, Object arg1, Object arg2, Object arg3) { 111 return format(message, new Object [] { arg1, arg2, arg3 }); 112 } 113 114 public String format(String message, Object arg1, Object arg2, Object arg3, Object arg4) { 115 return format(message, new Object [] { arg1, arg2, arg3, arg4 }); 116 } 117 118 public String format(String message, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) { 119 return format(message, new Object [] { arg1, arg2, arg3, arg4, arg5 }); 120 } 121 122 public String format(String message) { 123 return message(message); 124 } 125 126 public String format(String message, Object [] args) { 127 if (_locale != _globalLocale) { 128 synchronized (Messages.class) { 129 init(); 130 } 131 } 132 133 MessageFormat mf; 134 String msg; 135 136 try { 137 mf = (MessageFormat ) _formats.get(message); 138 if (mf == null) { 139 try { 140 msg = _messages.getString(message); 141 } catch (MissingResourceException except) { 142 return message; 143 } 144 mf = new MessageFormat (msg); 145 _formats.put(message, mf); 146 } 147 return mf.format(args); 148 } catch (Exception except) { 149 return "An internal error occured while processing message " + message; 150 } 151 } 152 153 public String message(String message) { 154 if (_locale != _globalLocale) { 155 synchronized (Messages.class) { 156 init(); 157 } 158 } 159 160 try { 161 return _messages.getString(message); 162 } catch (MissingResourceException except) { 163 return message; 164 } 165 } 166 167 static public void setLocale(Locale locale) { 168 synchronized (Messages.class) { 169 _globalLocale = locale; 170 _rbBundles = new Hashtable (); 171 _rbFormats = new Hashtable (); 172 } 173 } 174 175 static { 176 setLocale(Locale.getDefault()); 177 } 178 179 private static final class EmptyResourceBundle extends ResourceBundle implements Enumeration { 180 181 public Enumeration getKeys() { 182 return this; 183 } 184 185 protected Object handleGetObject(String name) { 186 return "[Missing message " + name + "]"; 187 } 188 189 public boolean hasMoreElements() { 190 return false; 191 } 192 193 public Object nextElement() { 194 return null; 195 } 196 197 } 198 199 } 200 | Popular Tags |