001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.webengine.model; 023 024import java.util.HashMap; 025import java.util.Locale; 026import java.util.Map; 027import java.util.MissingResourceException; 028import java.util.concurrent.ConcurrentHashMap; 029 030/** 031 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 032 */ 033public class Messages { 034 035 protected final Messages parent; 036 037 protected final MessagesProvider provider; 038 039 protected final Map<String, MessagesBundle> messages; 040 041 protected MessagesBundle defaultMessages; 042 043 protected static final String BUILT_IN_DEFAULT_LANG = "en"; 044 045 public Messages(Messages parent, MessagesProvider provider) { 046 this.parent = parent; 047 this.provider = provider; 048 messages = new ConcurrentHashMap<String, MessagesBundle>(); 049 String serverDefaultLang = Locale.getDefault().getLanguage(); 050 defaultMessages = getMessagesBundle(serverDefaultLang); 051 if (defaultMessages == null) { 052 defaultMessages = new MessagesBundle(null, new HashMap<String, String>()); 053 } 054 if (defaultMessages.messages.size() == 0 && !BUILT_IN_DEFAULT_LANG.equals(serverDefaultLang)) { 055 defaultMessages = getMessagesBundle(BUILT_IN_DEFAULT_LANG); 056 } 057 } 058 059 public MessagesBundle getMessagesBundle() { 060 return defaultMessages; 061 } 062 063 public MessagesBundle getMessagesBundle(String language) { 064 if (language == null) { 065 return defaultMessages; 066 } 067 MessagesBundle bundle = messages.get(language); 068 if (bundle == null) { 069 Map<String, String> map = provider.getMessages(language); 070 if (map == null && defaultMessages != null) { 071 return defaultMessages; 072 } 073 MessagesBundle parentBundle = parent != null ? parent.getMessagesBundle(language) : null; 074 bundle = new MessagesBundle(parentBundle, map); 075 messages.put(language, bundle); 076 } 077 return bundle; 078 } 079 080 public Object getObject(String key, String language) { 081 MessagesBundle bundle = getMessagesBundle(language); 082 if (bundle != null) { 083 return bundle.getObject(key); 084 } 085 throw new MissingResourceException("Can't find resource for bundle " + this.getClass().getName() + ", key " 086 + key, Messages.class.getName(), key); 087 } 088 089 public Object getObject(String key) { 090 return getObject(key, null); 091 } 092 093 public String getString(String key) { 094 return getString(key, null); 095 } 096 097 public String getString(String key, String language) { 098 return (String) getObject(key, language); 099 } 100 101 public String[] getStringArray(String key) { 102 return getStringArray(key, null); 103 } 104 105 public String[] getStringArray(String key, String language) { 106 return (String[]) getObject(key, language); 107 } 108 109}