1 15 package org.apache.hivemind.impl; 16 17 import java.io.BufferedInputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.URL ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 import java.util.Properties ; 29 30 import org.apache.hivemind.ApplicationRuntimeException; 31 import org.apache.hivemind.Resource; 32 import org.apache.hivemind.internal.MessageFinder; 33 import org.apache.hivemind.util.Defense; 34 import org.apache.hivemind.util.IOUtils; 35 import org.apache.hivemind.util.LocalizedNameGenerator; 36 37 41 public class MessageFinderImpl implements MessageFinder 42 { 43 private static final String EXTENSION = ".properties"; 44 45 private static class Localization 46 { 47 private Locale _locale; 48 49 private Resource _resource; 50 51 Localization(Locale locale, Resource resource) 52 { 53 _locale = locale; 54 _resource = resource; 55 } 56 57 public Locale getLocale() 58 { 59 return _locale; 60 } 61 62 public Resource getResource() 63 { 64 return _resource; 65 } 66 67 } 68 69 private Resource _baseResource; 70 71 private String _baseName; 72 73 private Map _propertiesMap = new HashMap (); 74 75 private Properties _emptyProperties = new Properties (); 76 77 public MessageFinderImpl(Resource baseResource) 78 { 79 Defense.notNull(baseResource, "baseResource"); 80 81 _baseResource = baseResource; 82 83 86 String name = _baseResource.getName(); 87 int dotx = name.lastIndexOf('.'); 88 if (dotx < 1) { 89 _baseName = name; 90 } else { 91 _baseName = name.substring(0, dotx); 92 } 93 } 94 95 public String getMessage(String key, Locale locale) 96 { 97 return findProperties(locale).getProperty(key); 98 } 99 100 private synchronized Properties findProperties(Locale locale) 101 { 102 Properties result = (Properties ) _propertiesMap.get(locale); 103 104 107 if (result == null) 108 result = buildProperties(locale); 109 110 return result; 111 } 112 113 private Properties buildProperties(Locale locale) 114 { 115 Properties result = _emptyProperties; 116 117 List localizations = findLocalizations(locale); 118 119 Iterator i = localizations.iterator(); 120 while (i.hasNext()) 121 { 122 Localization l = (Localization) i.next(); 123 124 result = readProperties(l.getLocale(), l.getResource(), result); 125 } 126 127 return result; 128 } 129 130 135 136 private Properties readProperties(Locale locale, Resource propertiesResource, Properties parent) 137 { 138 Properties result = (Properties ) _propertiesMap.get(locale); 139 140 if (result != null) 141 return result; 142 143 URL url = propertiesResource.getResourceURL(); 144 145 if (url == null) 146 result = parent; 147 else 148 result = readPropertiesFile(url, parent); 149 150 _propertiesMap.put(locale, result); 151 152 return result; 153 } 154 155 private Properties readPropertiesFile(URL url, Properties parent) 156 { 157 InputStream stream = null; 158 159 Properties result = new Properties (parent); 160 161 try 162 { 163 stream = new BufferedInputStream (url.openStream()); 164 165 result.load(stream); 166 167 stream.close(); 168 169 stream = null; 170 } 171 catch (IOException ex) 172 { 173 throw new ApplicationRuntimeException(ImplMessages.unableToReadMessages(url), ex); 174 175 } 176 finally 177 { 178 IOUtils.close(stream); 179 } 180 181 return result; 182 } 183 184 188 189 private List findLocalizations(Locale locale) 190 { 191 List result = new ArrayList (); 192 193 LocalizedNameGenerator g = new LocalizedNameGenerator(_baseName, locale, EXTENSION); 194 195 while (g.more()) 196 { 197 String name = g.next(); 198 199 Localization l = new Localization(g.getCurrentLocale(), _baseResource 200 .getRelativeResource(name)); 201 202 result.add(l); 203 } 204 205 Collections.reverse(result); 206 207 return result; 208 } 209 } | Popular Tags |