KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > resource > ResourceBundleManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.jsfext.resource;
24
25 import com.sun.enterprise.tools.jsfext.util.Util;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32
33 /**
34  * <p> This class caches <code>ResourceBundle</code> objects per locale.</p>
35  *
36  * @author Ken Paulsen (ken.paulsen@sun.com)
37  */

38 public class ResourceBundleManager {
39
40
41     /**
42      * <p> Use {@link #getInstance()} to obtain an instance.</p>
43      */

44     protected ResourceBundleManager() {
45     }
46
47
48     /**
49      * <p> Use this method to get the instance of this class.</p>
50      */

51     public static ResourceBundleManager getInstance() {
52     if (_instance == null) {
53         _instance = new ResourceBundleManager();
54     }
55     return _instance;
56     }
57
58     /**
59      * <p> This method checks the cache for the requested
60      * <code>ResourceBundle</code>.</p>
61      *
62      * @param baseName Name of the bundle.
63      * @param locale The <code>Locale</code>.
64      *
65      * @return The requested <code>ResourceBundle</code> in the most
66      * appropriate <code>Locale</code>.
67      */

68     protected ResourceBundle JavaDoc getCachedBundle(String JavaDoc baseName, Locale JavaDoc locale) {
69     return (ResourceBundle JavaDoc) _cache.get(getCacheKey(baseName, locale));
70     }
71
72     /**
73      * <p> This method generates a unique key for setting / getting
74      * <code>ResourceBundle</code>s from the cache. It is important to
75      * have different keys per locale (obviously).</p>
76      */

77     protected String JavaDoc getCacheKey(String JavaDoc baseName, Locale JavaDoc locale) {
78     return baseName + "__" + locale.toString();
79     }
80
81     /**
82      * <p> This method adds a <code>ResourceBundle</code> to the cache.</p>
83      */

84     protected void addCachedBundle(String JavaDoc baseName, Locale JavaDoc locale, ResourceBundle JavaDoc bundle) {
85     // Copy the old Map to prevent changing a Map while someone is
86
// accessing it.
87
Map JavaDoc map = new HashMap JavaDoc(_cache);
88
89     // Add the new bundle
90
map.put(getCacheKey(baseName, locale), bundle);
91
92     // Set this new Map as the shared cache Map
93
_cache = map;
94     }
95
96     /**
97      * <p> This method obtains the requested <code>ResourceBundle</code> as
98      * specified by the given <code>basename</code> and
99      * <code>locale</code>.</p>
100      *
101      * @param baseName The base name for the <code>ResourceBundle</code>.
102      * @param locale The desired <code>Locale</code>.
103      */

104     public ResourceBundle JavaDoc getBundle(String JavaDoc baseName, Locale JavaDoc locale) {
105     ResourceBundle JavaDoc bundle = getCachedBundle(baseName, locale);
106     if (bundle == null) {
107         bundle = ResourceBundle.getBundle(baseName, locale,
108             Util.getClassLoader(this));
109         if (bundle != null) {
110         addCachedBundle(baseName, locale, bundle);
111         }
112     }
113     return bundle;
114     }
115
116     /**
117      * <p> This method obtains the requested <code>ResourceBundle</ocde> as
118      * specified by the given basename, locale, and classloader.</p>
119      *
120      * @param baseName The base name for the <code>ResourceBundle</code>.
121      * @param locale The desired <code>Locale</code>.
122      * @param loader The <code>ClassLoader</code> that should be used.
123      */

124     public ResourceBundle JavaDoc getBundle(String JavaDoc baseName, Locale JavaDoc locale, ClassLoader JavaDoc loader) {
125     ResourceBundle JavaDoc bundle = getCachedBundle(baseName, locale);
126     if (bundle == null) {
127         bundle = ResourceBundle.getBundle(baseName, locale, loader);
128         if (bundle != null) {
129         addCachedBundle(baseName, locale, bundle);
130         }
131     }
132     return bundle;
133     }
134
135     /**
136      * <p> Singleton.</p>
137      */

138     private static ResourceBundleManager _instance =
139         new ResourceBundleManager();
140
141     /**
142      * <p> The cache of <code>ResourceBundle</code>s.</p>
143      */

144     private Map JavaDoc _cache = new HashMap JavaDoc();
145 }
146
Popular Tags