001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.util;
042:
043: import java.util.HashMap;
044: import java.util.Locale;
045: import java.util.Map;
046: import java.util.ResourceBundle;
047:
048: /**
049: * This class caches ResourceBundle objects per locale.
050: *
051: * @author Ken Paulsen (ken.paulsen@sun.com)
052: */
053: public class ResourceBundleManager {
054:
055: /**
056: * Singleton
057: */
058: private static ResourceBundleManager _instance = new ResourceBundleManager();
059:
060: /**
061: * The cache of ResourceBundles.
062: */
063: private Map _cache = new HashMap();
064:
065: /**
066: * Use getInstance() to obtain an instance.
067: */
068: protected ResourceBundleManager() {
069: }
070:
071: /**
072: * Use this method to get the instance of this class.
073: */
074: public static ResourceBundleManager getInstance() {
075: if (_instance == null) {
076: _instance = new ResourceBundleManager();
077: }
078: return _instance;
079: }
080:
081: /**
082: * This method checks the cache for the requested resource bundle.
083: *
084: * @param baseName Name of the bundle
085: * @param locale The locale
086: *
087: * @return The requested ResourceBundle in the most appropriate locale.
088: */
089: protected ResourceBundle getCachedBundle(String baseName,
090: Locale locale) {
091: return (ResourceBundle) _cache
092: .get(getCacheKey(baseName, locale));
093: }
094:
095: /**
096: * This method generates a unique key for setting / getting Resources
097: * bundles from the cache. It is important to have different keys per
098: * locale (obviously).
099: */
100: protected String getCacheKey(String baseName, Locale locale) {
101: return baseName + "__" + locale.toString();
102: }
103:
104: /**
105: * This method adds a ResourceBundle to the cache.
106: */
107: protected void addCachedBundle(String baseName, Locale locale,
108: ResourceBundle bundle) {
109: // Copy the old Map to prevent changing a Map while someone is
110: // accessing it.
111: Map map = new HashMap(_cache);
112:
113: // Add the new bundle
114: map.put(getCacheKey(baseName, locale), bundle);
115:
116: // Set this new Map as the shared cache Map
117: _cache = map;
118: }
119:
120: /**
121: * This method obtains the requested resource bundle as specified by the
122: * given basename and locale.
123: */
124: public ResourceBundle getBundle(String baseName, Locale locale) {
125: ResourceBundle bundle = getCachedBundle(baseName, locale);
126: if (bundle == null) {
127: bundle = ResourceBundle.getBundle(baseName, locale,
128: ClassLoaderFinder
129: .getCurrentLoader(MessageUtil.class));
130: if (bundle != null) {
131: addCachedBundle(baseName, locale, bundle);
132: }
133: }
134: return bundle;
135: }
136:
137: /**
138: * This method obtains the requested resource bundle as specified by the
139: * given basename, locale, and classloader.
140: */
141: public ResourceBundle getBundle(String baseName, Locale locale,
142: ClassLoader loader) {
143: ResourceBundle bundle = getCachedBundle(baseName, locale);
144: if (bundle == null) {
145: bundle = ResourceBundle.getBundle(baseName, locale, loader);
146: if (bundle != null) {
147: addCachedBundle(baseName, locale, bundle);
148: }
149: }
150: return bundle;
151: }
152: }
|