1 19 20 package com.sslexplorer.core; 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.struts.util.MessageResourcesFactory; 30 import org.apache.struts.util.PropertyMessageResources; 31 32 39 public class CoreMessageResources extends PropertyMessageResources { 40 41 43 private ClassLoader classLoader; 44 45 52 public CoreMessageResources(MessageResourcesFactory factory, String config, ClassLoader classLoader) { 53 super(factory, config); 54 this.classLoader = classLoader; 55 } 56 57 60 protected synchronized void loadLocale(String localeKey) { 61 62 if (log.isTraceEnabled()) { 63 log.trace("loadLocale(" + localeKey + ")"); 64 } 65 66 if (locales.get(localeKey) != null) { 68 return; 69 } 70 71 locales.put(localeKey, localeKey); 72 73 String name = config.replace('.', '/'); 75 if (localeKey.length() > 0) { 76 name += "_" + localeKey; 77 } 78 79 name += ".properties"; 80 InputStream is = null; 81 Properties props = new Properties (); 82 83 if (log.isTraceEnabled()) { 85 log.trace(" Loading resource '" + name + "'"); 86 } 87 88 is = classLoader.getResourceAsStream(name); 89 if (is != null) { 90 try { 91 props.load(is); 92 93 } catch (IOException e) { 94 log.error("loadLocale()", e); 95 } finally { 96 try { 97 is.close(); 98 } catch (IOException e) { 99 log.error("loadLocale()", e); 100 } 101 } 102 } 103 else { 104 if (log.isDebugEnabled()) 105 log.debug("Failed to locate message resources " + name + "."); 106 } 107 108 if (log.isTraceEnabled()) { 109 log.trace(" Loading resource completed"); 110 } 111 112 if (props.size() < 1) { 114 return; 115 } 116 117 synchronized (messages) { 118 Iterator names = props.keySet().iterator(); 119 while (names.hasNext()) { 120 String key = (String ) names.next(); 121 if (log.isTraceEnabled()) { 122 log.trace(" Saving message key '" + messageKey(localeKey, key)); 123 } 124 messages.put(messageKey(localeKey, key), props.getProperty(key)); 125 } 126 } 127 128 } 129 130 131 136 public Iterator keys() { 137 return messages.keySet().iterator(); 138 } 139 140 145 public void removeKey(String key) { 146 messages.remove(key); 147 } 148 149 154 public HashMap getLocales() { 155 return locales; 156 } 157 158 165 public void setMessage(Locale locale, String key, String value) { 166 setMessage(localeKey(locale), key, value); 167 } 168 169 176 public void setMessage(String localeKey, String key, String value) { 177 messages.put(messageKey(localeKey == null ? "" : localeKey, key), value); 178 } 179 180 } 181 | Popular Tags |