1 16 package com.blandware.atleap.webapp.util.core; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.util.ApplicationResourceStorage; 20 import com.blandware.atleap.model.core.ContentLocale; 21 import com.blandware.atleap.service.core.ApplicationResourceManager; 22 import com.blandware.atleap.service.exception.BeanNotFoundException; 23 import org.apache.struts.util.RequestUtils; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.springframework.context.ApplicationContext; 27 import org.springframework.web.context.support.WebApplicationContextUtils; 28 29 import javax.servlet.ServletContext ; 30 import javax.servlet.http.HttpServletRequest ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Locale ; 34 import java.util.Set ; 35 36 47 public final class ApplicationResources { 48 49 protected transient final Log log = LogFactory.getLog(ApplicationResources.class); 50 51 54 private static final String INSTANCE_KEY = "com.blandware.atleap.webapp.util.ApplicationResources.INSTANCE"; 55 56 59 private ApplicationResourceStorage applicationResourceStorage; 60 61 64 private ApplicationResourceManager applicationResourceManager; 65 66 69 private ServletContext servletContext; 70 71 76 private ApplicationResources(ServletContext servletContext) { 77 this.servletContext = servletContext; 78 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 79 applicationResourceManager = (ApplicationResourceManager) applicationContext.getBean(Constants.APPLICATION_RESOURCE_MANAGER_BEAN); 80 loadResources(); 81 } 82 83 89 public static ApplicationResources getInstance(ServletContext servletContext) { 90 ApplicationResources applicationResources = (ApplicationResources) servletContext.getAttribute(INSTANCE_KEY); 91 if ( applicationResources == null ) { 92 applicationResources = new ApplicationResources(servletContext); 93 servletContext.setAttribute(INSTANCE_KEY, applicationResources); 94 } 95 return applicationResources; 96 } 97 98 108 public synchronized void updateResource(String localeIdentifier, String key, String value, Long version) throws BeanNotFoundException { 109 applicationResourceManager.updateApplicationResource(localeIdentifier, key, value, version); 110 applicationResourceStorage.putMessage(new Locale(localeIdentifier), key, value); 111 } 112 113 118 public void loadBundle(ContentLocale locale) { 119 ApplicationResourceStorage.Bundle bundle = applicationResourceManager.loadBundle(locale); 120 applicationResourceStorage.mergeBundle(bundle); 121 } 122 123 128 public void dropBundle(String localeIdentifier) { 129 applicationResourceStorage.dropBundle(new Locale(localeIdentifier)); 130 applicationResourceManager.dropBundle(localeIdentifier); 131 } 132 133 135 142 public String getMessage(HttpServletRequest request, String key) { 143 return getMessage(request, key, null); 144 } 145 146 154 public String getMessage(HttpServletRequest request, String key, Object [] args) { 155 Locale locale = RequestUtils.getUserLocale(request, null); 156 return getMessage(locale, key, args); 157 } 158 159 166 public String getMessage(Locale locale, String key) { 167 return getMessage(locale, key, null); 168 } 169 170 178 public String getMessage(Locale locale, String key, Object [] args) { 179 String msg = applicationResourceStorage.getMessage(locale, key, args); 180 if ( msg == null ) { 181 String language = locale.getLanguage(); 182 LocaleUtil localeUtil = LocaleUtil.getInstance(servletContext); 184 ContentLocale defaultLocale = localeUtil.getDefaultLocale(); 185 if ( !defaultLocale.getIdentifier().equalsIgnoreCase(language) ) { 186 msg = applicationResourceStorage.getMessage(new Locale(defaultLocale.getIdentifier()), key, args); 187 } 188 189 if ( msg == null ) { 191 List activeLocales = localeUtil.getActiveLocales(); 192 for ( Iterator i = activeLocales.iterator(); i.hasNext() && msg == null; ) { 193 ContentLocale contentLocale = (ContentLocale) i.next(); 194 195 String identifier = contentLocale.getIdentifier(); 197 if ( !identifier.equalsIgnoreCase(language) && !contentLocale.getDefaultInstance().booleanValue() ) { 198 msg = applicationResourceStorage.getMessage(new Locale(contentLocale.getIdentifier()), key, args); 199 } 200 } 201 } 202 } 203 return msg; 204 } 205 206 212 public Set getKeys(ContentLocale contentLocale) { 213 String localeIdentifier = contentLocale.getIdentifier(); 214 Locale locale = new Locale(localeIdentifier); 215 return applicationResourceStorage.getKeys(locale); 216 } 217 218 223 public Set getKeys() { 224 ContentLocale defaultLocale = LocaleUtil.getInstance(servletContext).getDefaultLocale(); 225 return getKeys(defaultLocale); 226 } 227 228 231 public void reloadResources() { 232 applicationResourceStorage = applicationResourceManager.loadResources(true); 233 } 234 235 238 public void loadResources() { 239 applicationResourceStorage = applicationResourceManager.loadResources(false); 240 if ( log.isInfoEnabled() ) { 241 log.info("Application resources synchronized"); 242 } 243 } 244 245 } 246 | Popular Tags |