1 9 package org.jboss.portal.portlet.plugins.language; 10 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Locale ; 14 import java.util.Map ; 15 import java.util.ResourceBundle ; 16 import java.util.MissingResourceException ; 17 18 import org.apache.log4j.Logger; 19 import org.jboss.portal.common.metadata.MetaData; 20 import org.jboss.portal.common.util.ParentChildResourceBundle; 21 import org.jboss.portal.common.util.Tools; 22 import org.jboss.portal.portlet.metadata.LanguagesMetaData; 23 import org.jboss.portal.server.Application; 24 import org.jboss.portal.server.Component; 25 import org.jboss.portal.server.plugins.PluginService; 26 27 32 public class ResourceBundles 33 extends PluginService 34 { 35 36 private Logger log = Logger.getLogger(ResourceBundles.class); 37 38 39 private ResourceBundle defaultBundle; 40 41 42 private Map localeBundles; 43 44 45 private LanguagesMetaData metaData; 46 47 50 public ResourceBundles() 51 { 52 } 53 54 57 public ResourceBundles(LanguagesMetaData metaData, ClassLoader loader) 58 { 59 init(metaData, loader); 60 this.metaData = metaData; 61 } 62 63 public void start() throws Exception 64 { 65 Component component = (Component)container; 66 Application application = component.getApplication(); 67 ClassLoader loader = application.getClassLoader(); 68 init(this.metaData, loader); 69 } 70 71 public void setMetaData(MetaData metaData) 72 { 73 this.metaData = (LanguagesMetaData)metaData; 74 } 75 76 public MetaData getMetaData() 77 { 78 return metaData; 79 } 80 81 84 public ResourceBundle getDefaultBundle() 85 { 86 return defaultBundle; 87 } 88 89 98 public ResourceBundle get(Locale locale) throws IllegalArgumentException 99 { 100 if (locale == null) 102 { 103 throw new IllegalArgumentException ("Locale cannot be null"); 104 } 105 106 ResourceBundle bundle = (ResourceBundle )localeBundles.get(locale); 108 if (bundle != null) 109 { 110 return bundle; 111 } 112 113 if (!locale.getVariant().equals("")) 115 { 116 Locale localeLC = new Locale (locale.getLanguage(), locale.getCountry()); 117 bundle = (ResourceBundle )localeBundles.get(localeLC); 118 if (bundle != null) 119 { 120 Map tmp = new HashMap (localeBundles); 122 tmp.put(localeLC, bundle); 123 localeBundles = tmp; 124 return bundle; 125 } 126 } 127 128 if (!locale.getCountry().equals("")) 130 { 131 Locale localeL = new Locale (locale.getLanguage()); 132 bundle = (ResourceBundle )localeBundles.get(localeL); 133 if (bundle != null) 134 { 135 Map tmp = new HashMap (localeBundles); 137 tmp.put(localeL, bundle); 138 localeBundles = tmp; 139 return bundle; 140 } 141 } 142 143 return defaultBundle; 144 } 145 146 private void init(LanguagesMetaData languagesMD, ClassLoader loader) 147 { 148 LanguagesMetaData.InfoMetaData infoMD = languagesMD.getInfo(); 150 ResourceBundle infoBundle = infoMD != null ? new InfoBundle(infoMD) : null; 151 152 ResourceBundle defaultLoaderBundle = null; 154 String resourceBundle = languagesMD.getResourceBundle(); 155 if (resourceBundle != null) 156 { 157 defaultLoaderBundle = loadBundle(loader, resourceBundle, Locale.ENGLISH); 158 } 159 160 if (defaultLoaderBundle != null) 162 { 163 if (infoBundle != null) 164 { 165 defaultBundle = new ParentChildResourceBundle(infoBundle, defaultLoaderBundle); 166 } 167 else 168 { 169 defaultBundle = defaultLoaderBundle; 170 } 171 } 172 else 173 { 174 if (infoBundle != null) 175 { 176 defaultBundle = infoBundle; 177 } 178 else 179 { 180 defaultBundle = Tools.EMPTY_BUNDLE; 181 } 182 } 183 184 localeBundles = new HashMap (); 186 String resourceBundleName = languagesMD.getResourceBundle(); 187 if (resourceBundleName != null) 188 { 189 for (Iterator i = languagesMD.getSupportedLocales().iterator();i.hasNext();) 191 { 192 Locale tmp = (Locale )i.next(); 193 ResourceBundle localeBundle = new ParentChildResourceBundle(Tools.EMPTY_BUNDLE, defaultBundle); 194 195 ResourceBundle tmpBundle = loadBundle(loader, resourceBundleName, tmp); 196 if (tmpBundle != null) 197 { 198 localeBundle = new ParentChildResourceBundle(localeBundle, tmpBundle); 199 } 200 201 localeBundles.put(tmp, localeBundle); 202 } 203 } 204 else 205 { 206 } 208 } 209 210 213 private ResourceBundle loadBundle(ClassLoader loader, String baseName, Locale locale) 214 { 215 log.debug("Want to load bundle " + baseName + " for locale " + locale); 216 ResourceBundle bundle = null; 217 try 218 { 219 bundle = ResourceBundle.getBundle(baseName, locale, loader); 220 log.debug("Created bundle " + baseName + " for locale " + locale); 221 } 222 catch (MissingResourceException e) 223 { 224 log.warn("Bundle " + baseName + " for locale " + locale + " not found", e); 225 } 226 return bundle; 227 } 228 } 229 | Popular Tags |