001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model;
021
022import java.util.HashMap;
023import java.util.Locale;
024import java.util.Map;
025import java.util.MissingResourceException;
026import java.util.concurrent.ConcurrentHashMap;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class Messages {
032
033    protected final Messages parent;
034
035    protected final MessagesProvider provider;
036
037    protected final Map<String, MessagesBundle> messages;
038
039    protected MessagesBundle defaultMessages;
040
041    protected static final String BUILT_IN_DEFAULT_LANG = "en";
042
043    public Messages(Messages parent, MessagesProvider provider) {
044        this.parent = parent;
045        this.provider = provider;
046        messages = new ConcurrentHashMap<String, MessagesBundle>();
047        String serverDefaultLang = Locale.getDefault().getLanguage();
048        defaultMessages = getMessagesBundle(serverDefaultLang);
049        if (defaultMessages == null) {
050            defaultMessages = new MessagesBundle(null, new HashMap<String, String>());
051        }
052        if (defaultMessages.messages.size() == 0 && !BUILT_IN_DEFAULT_LANG.equals(serverDefaultLang)) {
053            defaultMessages = getMessagesBundle(BUILT_IN_DEFAULT_LANG);
054        }
055    }
056
057    public MessagesBundle getMessagesBundle() {
058        return defaultMessages;
059    }
060
061    public MessagesBundle getMessagesBundle(String language) {
062        if (language == null) {
063            return defaultMessages;
064        }
065        MessagesBundle bundle = messages.get(language);
066        if (bundle == null) {
067            Map<String, String> map = provider.getMessages(language);
068            if (map == null && defaultMessages != null) {
069                return defaultMessages;
070            }
071            MessagesBundle parentBundle = parent != null ? parent.getMessagesBundle(language) : null;
072            bundle = new MessagesBundle(parentBundle, map);
073            messages.put(language, bundle);
074        }
075        return bundle;
076    }
077
078    public Object getObject(String key, String language) {
079        MessagesBundle bundle = getMessagesBundle(language);
080        if (bundle != null) {
081            return bundle.getObject(key);
082        }
083        throw new MissingResourceException("Can't find resource for bundle " + this.getClass().getName() + ", key "
084                + key, Messages.class.getName(), key);
085    }
086
087    public Object getObject(String key) {
088        return getObject(key, null);
089    }
090
091    public String getString(String key) {
092        return getString(key, null);
093    }
094
095    public String getString(String key, String language) {
096        return (String) getObject(key, language);
097    }
098
099    public String[] getStringArray(String key) {
100        return getStringArray(key, null);
101    }
102
103    public String[] getStringArray(String key, String language) {
104        return (String[]) getObject(key, language);
105    }
106
107}