1 18 19 20 package org.apache.struts.util; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Locale ; 27 import java.util.Properties ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 48 public class PropertyMessageResources extends MessageResources { 49 50 51 53 54 61 public PropertyMessageResources(MessageResourcesFactory factory, 62 String config) { 63 64 super(factory, config); 65 log.trace("Initializing, config='" + config + "'"); 66 67 } 68 69 70 78 public PropertyMessageResources(MessageResourcesFactory factory, 79 String config, boolean returnNull) { 80 81 super(factory, config, returnNull); 82 log.trace("Initializing, config='" + config + 83 "', returnNull=" + returnNull); 84 85 } 86 87 88 90 91 95 protected HashMap locales = new HashMap (); 96 97 98 101 protected static final Log log = 102 LogFactory.getLog(PropertyMessageResources.class); 103 104 105 109 protected HashMap messages = new HashMap (); 110 111 112 114 115 129 public String getMessage(Locale locale, String key) { 130 131 if (log.isDebugEnabled()) { 132 log.debug("getMessage(" + locale + "," + key + ")"); 133 } 134 135 String localeKey = localeKey(locale); 137 String originalKey = messageKey(localeKey, key); 138 String messageKey = null; 139 String message = null; 140 int underscore = 0; 141 boolean addIt = false; 143 while (true) { 145 146 loadLocale(localeKey); 148 149 messageKey = messageKey(localeKey, key); 151 synchronized (messages) { 152 message = (String ) messages.get(messageKey); 153 if (message != null) { 154 if (addIt) { 155 messages.put(originalKey, message); 156 } 157 return (message); 158 } 159 } 160 161 addIt = true; 163 underscore = localeKey.lastIndexOf("_"); 164 if (underscore < 0) { 165 break; 166 } 167 localeKey = localeKey.substring(0, underscore); 168 169 } 170 171 if (!defaultLocale.equals(locale)) { 173 localeKey = localeKey(defaultLocale); 174 messageKey = messageKey(localeKey, key); 175 loadLocale(localeKey); 176 synchronized (messages) { 177 message = (String ) messages.get(messageKey); 178 if (message != null) { 179 messages.put(originalKey, message); 180 return (message); 181 } 182 } 183 } 184 185 localeKey = ""; 187 messageKey = messageKey(localeKey, key); 188 loadLocale(localeKey); 189 synchronized (messages) { 190 message = (String ) messages.get(messageKey); 191 if (message != null) { 192 messages.put(originalKey, message); 193 return (message); 194 } 195 } 196 197 if (returnNull) { 199 return (null); 200 } else { 201 return ("???" + messageKey(locale, key) + "???"); 202 } 203 204 } 205 206 207 209 210 221 protected synchronized void loadLocale(String localeKey) { 222 223 if (log.isTraceEnabled()) { 224 log.trace("loadLocale(" + localeKey + ")"); 225 } 226 227 if (locales.get(localeKey) != null) { 229 return; 230 } 231 232 locales.put(localeKey, localeKey); 233 234 String name = config.replace('.', '/'); 236 if (localeKey.length() > 0) { 237 name += "_" + localeKey; 238 } 239 240 name += ".properties"; 241 InputStream is = null; 242 Properties props = new Properties (); 243 244 if (log.isTraceEnabled()) { 246 log.trace(" Loading resource '" + name + "'"); 247 } 248 249 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 250 if (classLoader == null) { 251 classLoader = this.getClass().getClassLoader(); 252 } 253 254 is = classLoader.getResourceAsStream(name); 255 if (is != null) { 256 try { 257 props.load(is); 258 259 } catch (IOException e) { 260 log.error("loadLocale()", e); 261 } finally { 262 try { 263 is.close(); 264 } catch (IOException e) { 265 log.error("loadLocale()", e); 266 } 267 } 268 } 269 270 if (log.isTraceEnabled()) { 271 log.trace(" Loading resource completed"); 272 } 273 274 if (props.size() < 1) { 276 return; 277 } 278 279 synchronized (messages) { 280 Iterator names = props.keySet().iterator(); 281 while (names.hasNext()) { 282 String key = (String ) names.next(); 283 if (log.isTraceEnabled()) { 284 log.trace(" Saving message key '" + messageKey(localeKey, key)); 285 } 286 messages.put(messageKey(localeKey, key), props.getProperty(key)); 287 } 288 } 289 290 } 291 292 293 } 294 | Popular Tags |