Browse Source

ResourceBundleThemeSource exposes fallbackToSystemLocale and defaultEncoding

Issue: SPR-13209
pull/836/merge
Juergen Hoeller 9 years ago
parent
commit
c7fef87e76
  1. 76
      spring-context/src/main/java/org/springframework/ui/context/support/ResourceBundleThemeSource.java

76
spring-context/src/main/java/org/springframework/ui/context/support/ResourceBundleThemeSource.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,12 +16,13 @@ @@ -16,12 +16,13 @@
package org.springframework.ui.context.support;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.HierarchicalMessageSource;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
@ -41,7 +42,7 @@ import org.springframework.ui.context.ThemeSource; @@ -41,7 +42,7 @@ import org.springframework.ui.context.ThemeSource;
* @see java.util.ResourceBundle
* @see org.springframework.context.support.ResourceBundleMessageSource
*/
public class ResourceBundleThemeSource implements HierarchicalThemeSource {
public class ResourceBundleThemeSource implements HierarchicalThemeSource, BeanClassLoaderAware {
protected final Log logger = LogFactory.getLog(getClass());
@ -49,8 +50,14 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource { @@ -49,8 +50,14 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
private String basenamePrefix = "";
private String defaultEncoding;
private Boolean fallbackToSystemLocale;
private ClassLoader beanClassLoader;
/** Map from theme name to Theme instance */
private final Map<String, Theme> themeCache = new HashMap<String, Theme>();
private final Map<String, Theme> themeCache = new ConcurrentHashMap<String, Theme>();
@Override
@ -85,6 +92,33 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource { @@ -85,6 +92,33 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
this.basenamePrefix = (basenamePrefix != null ? basenamePrefix : "");
}
/**
* Set the default charset to use for parsing resource bundle files.
* <p>{@link ResourceBundleMessageSource}'s default is the
* {@code java.util.ResourceBundle} default encoding: ISO-8859-1.
* @since 4.2
* @see ResourceBundleMessageSource#setDefaultEncoding
*/
public void setDefaultEncoding(String defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}
/**
* Set whether to fall back to the system Locale if no files for a
* specific Locale have been found.
* <p>{@link ResourceBundleMessageSource}'s default is "true".
* @since 4.2
* @see ResourceBundleMessageSource#setFallbackToSystemLocale
*/
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
this.fallbackToSystemLocale = fallbackToSystemLocale;
}
@Override
public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.beanClassLoader = beanClassLoader;
}
/**
* This implementation returns a SimpleTheme instance, holding a
@ -100,20 +134,23 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource { @@ -100,20 +134,23 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
if (themeName == null) {
return null;
}
synchronized (this.themeCache) {
Theme theme = this.themeCache.get(themeName);
if (theme == null) {
String basename = this.basenamePrefix + themeName;
MessageSource messageSource = createMessageSource(basename);
theme = new SimpleTheme(themeName, messageSource);
initParent(theme);
this.themeCache.put(themeName, theme);
if (logger.isDebugEnabled()) {
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
Theme theme = this.themeCache.get(themeName);
if (theme == null) {
synchronized (this.themeCache) {
theme = this.themeCache.get(themeName);
if (theme == null) {
String basename = this.basenamePrefix + themeName;
MessageSource messageSource = createMessageSource(basename);
theme = new SimpleTheme(themeName, messageSource);
initParent(theme);
this.themeCache.put(themeName, theme);
if (logger.isDebugEnabled()) {
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
}
}
}
return theme;
}
return theme;
}
/**
@ -130,6 +167,15 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource { @@ -130,6 +167,15 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
protected MessageSource createMessageSource(String basename) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(basename);
if (this.defaultEncoding != null) {
messageSource.setDefaultEncoding(this.defaultEncoding);
}
if (this.fallbackToSystemLocale != null) {
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
}
if (this.beanClassLoader != null) {
messageSource.setBeanClassLoader(this.beanClassLoader);
}
return messageSource;
}

Loading…
Cancel
Save