KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > ResourceBundleWrapper


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.app;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 import org.alfresco.error.AlfrescoRuntimeException;
25 import org.apache.log4j.Logger;
26 import org.apache.log4j.Priority;
27
28 /**
29  * Wrapper around Alfresco Resource Bundle objects. Used to catch and handle missing
30  * resource exception to help identify missing I18N strings in client apps.
31  *
32  * @author Kevin Roast
33  */

34 public final class ResourceBundleWrapper extends ResourceBundle JavaDoc
35 {
36    private static Logger logger = Logger.getLogger(ResourceBundleWrapper.class);
37    
38    private ResourceBundle JavaDoc delegate;
39    
40    /**
41     * Constructor
42     *
43     * @param bundle The ResourceBundle to route calls too
44     */

45    private ResourceBundleWrapper(ResourceBundle JavaDoc bundle)
46    {
47       this.delegate = bundle;
48    }
49    
50    /**
51     * @see java.util.ResourceBundle#getKeys()
52     */

53    public Enumeration JavaDoc<String JavaDoc> getKeys()
54    {
55       return this.delegate.getKeys();
56    }
57    
58    /**
59     * @see java.util.ResourceBundle#handleGetObject(java.lang.String)
60     */

61    protected Object JavaDoc handleGetObject(String JavaDoc key)
62    {
63       try
64       {
65          return this.delegate.getObject(key);
66       }
67       catch (MissingResourceException JavaDoc err)
68       {
69          if (logger.isEnabledFor(Priority.WARN))
70             logger.warn("Failed to find I18N message string key: " + key);
71          
72          return "$$" + key + "$$";
73       }
74    }
75    
76    /**
77     * Factory method to get a named wrapped resource bundle for a particular locale.
78     *
79     * @param name Bundle name
80     * @param locale Locale to retrieve bundle for
81     *
82     * @return Wrapped ResourceBundle instance for specified locale
83     */

84    public static ResourceBundle JavaDoc getResourceBundle(String JavaDoc name, Locale JavaDoc locale)
85    {
86       ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(name, locale);
87       if (bundle == null)
88       {
89          throw new AlfrescoRuntimeException("Unable to load Alfresco messages bundle: " + name);
90       }
91       
92       // apply our wrapper to catch MissingResourceException
93
return new ResourceBundleWrapper(bundle);
94    }
95 }
96
Popular Tags