1 package org.jboss.test.bpel.ws.consumption.partner.resource; 2 3 import java.util.Locale ; 4 import java.util.MissingResourceException ; 5 import java.util.ResourceBundle ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import org.jboss.test.bpel.ws.consumption.partner.spi.Dictionary; 11 import org.jboss.test.bpel.ws.consumption.partner.spi.DictionaryFactory; 12 13 17 public class ResourceDictionaryFactory extends DictionaryFactory { 18 19 private static final Log log = LogFactory.getLog(ResourceDictionaryFactory.class); 20 21 22 public boolean acceptsLocales(Locale sourceLocale, Locale targetLocale) { 23 return getBundle(sourceLocale, targetLocale) != null; 24 } 25 26 27 public Dictionary createDictionary(Locale sourceLocale, Locale targetLocale) { 28 return new ResourceDictionary(getBundle(sourceLocale, targetLocale)); 29 } 30 31 protected ResourceBundle getBundle(Locale sourceLocale, Locale targetLocale) { 32 String sourceLanguage = sourceLocale.getLanguage(); 33 log.debug("loading bundle: sourceLanguage=" + sourceLanguage + ", targetLocale=" + targetLocale); 34 ResourceBundle bundle; 35 try { 36 bundle = ResourceBundle.getBundle(getBaseName(sourceLanguage), targetLocale); 37 String bundleLanguage = bundle.getLocale().getLanguage(); 38 if (bundleLanguage.equals(targetLocale.getLanguage())) { 39 log.debug("loaded bundle: bundleLanguage=" + bundleLanguage); 40 } 41 else { 42 bundle = null; 43 log.debug("loaded bundle, but it does not correspond to the target locale: " + 44 "bundleLanguage=" + bundleLanguage); 45 } 46 } 47 catch (MissingResourceException e) { 48 bundle = null; 49 log.debug("bundle not found", e); 50 } 51 return bundle; 52 } 53 54 protected String getBaseName(String sourceLanguage) { 55 StringBuffer baseName = new StringBuffer (getClass().getName()); 56 baseName.setLength(baseName.lastIndexOf(".") + 1); 57 baseName.append(sourceLanguage); 58 return baseName.toString(); 59 } 60 } 61 | Popular Tags |