Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.kamhon.ieagle.function.language.service.impl; import java.text.MessageFormat; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.Properties; import java.util.ResourceBundle; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import net.kamhon.ieagle.application.ServerConfiguration; import net.kamhon.ieagle.function.language.dao.LanguageDao; import net.kamhon.ieagle.function.language.dao.LanguageResDao; import net.kamhon.ieagle.function.language.service.LanguageFrameworkService; import net.kamhon.ieagle.function.language.vo.Language; import net.kamhon.ieagle.function.language.vo.LanguageRes; import net.kamhon.ieagle.function.language.vo.LanguageResKey; import net.kamhon.ieagle.util.CollectionUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service(LanguageFrameworkService.BEAN_NAME) public class LanguageFrameworkServiceImpl implements LanguageFrameworkService { private static final Log log = LogFactory.getLog(LanguageFrameworkServiceImpl.class); private static final ConcurrentMap<String, Properties> dbBundles = new ConcurrentHashMap<String, Properties>(); // this used by PRODUCTION MODE to read all text from DB. private boolean isInitLanguage = false; @Autowired private LanguageDao languageDao; @Autowired private LanguageResDao languageResDao; @Autowired private Locale systemLocale; @Autowired private ServerConfiguration serverConfiguration; @Autowired private Object exclusiveKeyPrefix; @Autowired private Object resourceBaseNames; public String getText(String key, Locale locale, Object[] argsArray) { String msg = null; if (locale == null) { locale = systemLocale; } if (serverConfiguration.isProductionMode()) { if (!isInitLanguage) { log.info("init languages"); synchronized (dbBundles) { dbBundles.clear(); initLanguage(); isInitLanguage = true; } } if (dbBundles.containsKey(locale.getLanguage())) { Properties prop = dbBundles.get(locale.getLanguage()); msg = prop.getProperty(key); } // text not found, look on default locale if (msg == null && !locale.getLanguage().equalsIgnoreCase(systemLocale.getLanguage())) { if (dbBundles.containsKey(systemLocale.getLanguage())) { Properties prop = dbBundles.get(systemLocale.getLanguage()); msg = prop.getProperty(key); } } } // if STAGING MODE else { LanguageRes res = languageResDao.get(new LanguageResKey(locale.getLanguage(), key)); if (res != null) { msg = res.getResValue(); } else if (!locale.getLanguage().equalsIgnoreCase(systemLocale.getLanguage())) { res = languageResDao.get(new LanguageResKey(systemLocale.getLanguage(), key)); if (res != null) { msg = res.getResValue(); } } // just for development only if (msg == null) { if (!checkIfInPrefixList(key)) { log.debug("****************************************"); log.debug("key[" + key + "] can't found at db!!!!"); log.debug("***************************************"); } } } if (msg == null) { msg = getTextByResourceBundle(key, locale); } if (msg != null) { // format the code if (argsArray != null && argsArray.length > 0) { return MessageFormat.format(msg, argsArray); } return msg; } else { return key; } } private String getTextByResourceBundle(String key, Locale locale) { String msg = null; if (resourceBaseNames instanceof List) { for (String baseName : (List<String>) resourceBaseNames) { ResourceBundle rb = ResourceBundle.getBundle(baseName, locale); try { msg = rb.getString(key); } catch (MissingResourceException ex) { // log.debug("key " + key + " not found!!"); } if (msg != null) break; } } return msg; } @SuppressWarnings("unchecked") private boolean checkIfInPrefixList(String key) { if (exclusiveKeyPrefix instanceof List) { if (CollectionUtil.isNotEmpty((List<String>) exclusiveKeyPrefix)) { for (String prefix : (List<String>) exclusiveKeyPrefix) { if (key.startsWith(prefix)) { return true; } } } } return false; } private void initLanguage() { List<String> languages = languageResDao.findAllLanguages(); for (String language : languages) { List<LanguageRes> languageReses = languageResDao.findLanguageReses(language); Properties prop = new Properties(); for (LanguageRes languageRes : languageReses) { prop.setProperty(languageRes.getId().getKeyCode(), languageRes.getResValue()); } dbBundles.put(language, prop); } } public void resetLanguageCache() { synchronized (dbBundles) { isInitLanguage = false; } } @Override public List<Language> findLanguages() { return languageDao.findAll(); } @Override public Language findLanguageByCode(String languageCode) { return languageDao.get(languageCode); } @Override public void saveLanguage(Language language) { languageDao.save(language); } public void setLanguageDao(LanguageDao languageDao) { this.languageDao = languageDao; } public void setLanguageResDao(LanguageResDao languageResDao) { this.languageResDao = languageResDao; } public void setSystemLocale(Locale systemLocale) { this.systemLocale = systemLocale; } public void setServerConfiguration(ServerConfiguration serverConfiguration) { this.serverConfiguration = serverConfiguration; } public void setExclusiveKeyPrefix(List<String> exclusiveKeyPrefix) { this.exclusiveKeyPrefix = exclusiveKeyPrefix; } public void setResourceBaseNames(List<String> resourceBaseNames) { this.resourceBaseNames = resourceBaseNames; } }