KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bpel > ws > consumption > partner > resource > ResourceDictionaryFactory


1 package org.jboss.test.bpel.ws.consumption.partner.resource;
2
3 import java.util.Locale JavaDoc;
4 import java.util.MissingResourceException JavaDoc;
5 import java.util.ResourceBundle JavaDoc;
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 /**
14  * @author Alejandro Guizar
15  * @version $Revision: 43322 $ $Date: 2006-04-03 12:19:24 -0400 (Mon, 03 Apr 2006) $
16  */

17 public class ResourceDictionaryFactory extends DictionaryFactory {
18     
19     private static final Log log = LogFactory.getLog(ResourceDictionaryFactory.class);
20
21   /** {@inheritDoc} */
22   public boolean acceptsLocales(Locale JavaDoc sourceLocale, Locale JavaDoc targetLocale) {
23     return getBundle(sourceLocale, targetLocale) != null;
24   }
25   
26   /** {@inheritDoc} */
27   public Dictionary createDictionary(Locale JavaDoc sourceLocale, Locale JavaDoc targetLocale) {
28     return new ResourceDictionary(getBundle(sourceLocale, targetLocale));
29   }
30   
31   protected ResourceBundle JavaDoc getBundle(Locale JavaDoc sourceLocale, Locale JavaDoc targetLocale) {
32     String JavaDoc sourceLanguage = sourceLocale.getLanguage();
33     log.debug("loading bundle: sourceLanguage=" + sourceLanguage + ", targetLocale=" + targetLocale);
34     ResourceBundle JavaDoc bundle;
35     try {
36         bundle = ResourceBundle.getBundle(getBaseName(sourceLanguage), targetLocale);
37         String JavaDoc 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 JavaDoc e) {
48       bundle = null;
49         log.debug("bundle not found", e);
50     }
51     return bundle;
52   }
53   
54   protected String JavaDoc getBaseName(String JavaDoc sourceLanguage) {
55     StringBuffer JavaDoc baseName = new StringBuffer JavaDoc(getClass().getName());
56     baseName.setLength(baseName.lastIndexOf(".") + 1);
57     baseName.append(sourceLanguage);
58     return baseName.toString();
59   }
60 }
61
Popular Tags