001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.model;
023:
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.MissingResourceException;
028: import java.util.ResourceBundle;
029:
030: import org.jboss.logging.Logger;
031: import org.jboss.portal.common.i18n.ResourceBundleFactory;
032: import org.jboss.portal.common.i18n.ResourceBundleManager;
033: import org.jboss.portal.common.util.EmptyResourceBundle;
034:
035: /**
036: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
037: * @version $Revision$
038: */
039: public class MetaDataResourceBundleFactory implements
040: ResourceBundleFactory {
041:
042: private Logger logger = Logger
043: .getLogger(MetaDataResourceBundleFactory.class);
044:
045: public static ResourceBundleManager createResourceBundleManager(
046: ClassLoader classLoader, List supportedLocales,
047: String baseName) {
048: if (classLoader == null) {
049: throw new IllegalArgumentException(
050: "Need a non null classloader");
051: }
052: if (supportedLocales == null) {
053: throw new IllegalArgumentException(
054: "Supported locales cannot be null");
055: }
056:
057: // Create factory
058: MetaDataResourceBundleFactory factory = new MetaDataResourceBundleFactory(
059: classLoader, baseName);
060:
061: // Create manager
062: ResourceBundleManager manager = new ResourceBundleManager(
063: EmptyResourceBundle.INSTANCE, factory);
064:
065: // Preload declared locales
066: for (Iterator i = supportedLocales.iterator(); i.hasNext();) {
067: Locale locale = (Locale) i.next();
068: manager.getResourceBundle(locale);
069: }
070:
071: //
072: return manager;
073: }
074:
075: private ClassLoader classLoader;
076: private String baseName;
077:
078: public MetaDataResourceBundleFactory(ClassLoader classLoader,
079: String baseName) {
080: this .classLoader = classLoader;
081: this .baseName = baseName;
082: }
083:
084: public ResourceBundle getBundle(Locale locale)
085: throws IllegalArgumentException {
086: if (locale == null) {
087: throw new IllegalArgumentException("Locale cannot be null");
088: }
089:
090: try {
091: return ResourceBundle.getBundle(baseName, locale,
092: classLoader);
093: } catch (MissingResourceException e) {
094: logger.warn("Could not find resource bundle: " + baseName
095: + " for locale: " + locale);
096: }
097: return EmptyResourceBundle.INSTANCE;
098: }
099:
100: }
|