001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.container.om.common;
007:
008: import java.io.Serializable;
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.Locale;
012: import java.util.Map;
013: import java.util.MissingResourceException;
014: import java.util.ResourceBundle;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018: import org.apache.pluto.om.common.Language;
019: import org.apache.pluto.om.common.LanguageSet;
020:
021: /**
022: * Implementation of Apache Pluto object model.
023: * @author Ken Weiner, kweiner@unicon.net
024: * @version $Revision: 35013 $
025: */
026: public class LanguageSetImpl implements LanguageSet, Serializable {
027:
028: private String title = null;
029: private String shortTitle = null;
030: private String keywords = null;
031: private String resources = null;
032: private ClassLoader classLoader = null;
033: private Map languages = null; // Locale --> Language
034: private boolean resourceBundleInitialized = false;
035: private boolean hasResourceBundle = false;
036: private static final Log log = LogFactory
037: .getLog(LanguageSetImpl.class);
038:
039: public LanguageSetImpl(String title, String shortTitle,
040: String keywords, String resources) {
041: languages = new HashMap();
042: this .title = title;
043: this .shortTitle = shortTitle;
044: this .keywords = keywords;
045: this .resources = resources;
046:
047: hasResourceBundle = resources != null;
048: }
049:
050: // LanguageSet methods
051:
052: public Iterator iterator() {
053: if (!resourceBundleInitialized) {
054: initResourceBundle();
055: resourceBundleInitialized = true;
056: }
057: return languages.values().iterator();
058: }
059:
060: public Iterator getLocales() {
061: return languages.keySet().iterator();
062: }
063:
064: public Language get(Locale locale) {
065: if (!resourceBundleInitialized) {
066: initResourceBundle();
067: resourceBundleInitialized = true;
068: }
069: return (Language) languages.get(locale);
070: }
071:
072: public Locale getDefaultLocale() {
073: Locale defaultLocale = Locale.getDefault();
074: return defaultLocale;
075: }
076:
077: // Additional methods
078:
079: public void setResources(String resourceBundleBase) {
080: this .resources = resourceBundleBase;
081: }
082:
083: public void setClassLoader(ClassLoader classLoader) {
084: this .classLoader = classLoader;
085: }
086:
087: public void addLocale(Locale locale) {
088: // The null Language values will be replaced
089: // during the initResourceBundle() method
090: // since we don't have the right class loader at the
091: // time this method is called
092: languages.put(locale, null);
093: }
094:
095: private void addLanguage(Locale locale) {
096: ResourceBundle resourceBundle = null;
097: if (hasResourceBundle) {
098: resourceBundle = loadResourceBundle(locale);
099: }
100: Language language = createLanguage(locale, resourceBundle);
101: languages.put(language.getLocale(), language);
102: }
103:
104: // Create Language object with data from this class (title, short-title, description, keywords)
105: private Language createLanguage(Locale locale, ResourceBundle bundle) {
106: LanguageImpl lang = new LanguageImpl(locale, bundle, title,
107: shortTitle, keywords);
108: return (Language) lang;
109: }
110:
111: // Loads resource bundle files from WEB-INF/classes directory
112: private ResourceBundle loadResourceBundle(Locale locale) {
113: ResourceBundle resourceBundle = null;
114: try {
115: if (classLoader != null) {
116: resourceBundle = ResourceBundle.getBundle(resources,
117: locale, classLoader);
118: } else {
119: resourceBundle = ResourceBundle.getBundle(resources,
120: locale, Thread.currentThread()
121: .getContextClassLoader());
122: }
123: } catch (MissingResourceException mre) {
124: if (log.isErrorEnabled()) {
125: log.error("Cannot obtain portlet resource bundle '"
126: + resources + "'", mre);
127: }
128: }
129: return resourceBundle;
130: }
131:
132: private void initResourceBundle() {
133: // Assume that by now, we have a proper webapp class loader
134: addLocale(Locale.getDefault());
135: for (Iterator iter = languages.keySet().iterator(); iter
136: .hasNext();) {
137: Locale locale = (Locale) iter.next();
138: addLanguage(locale);
139: }
140: }
141: }
|