ResourceBundleThemeSource exposes fallbackToSystemLocale and defaultEncoding
Issue: SPR-13209
This commit is contained in:
parent
d738dddd8f
commit
c7fef87e76
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,12 +16,13 @@
|
||||||
|
|
||||||
package org.springframework.ui.context.support;
|
package org.springframework.ui.context.support;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||||
import org.springframework.context.HierarchicalMessageSource;
|
import org.springframework.context.HierarchicalMessageSource;
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||||
|
|
@ -41,7 +42,7 @@ import org.springframework.ui.context.ThemeSource;
|
||||||
* @see java.util.ResourceBundle
|
* @see java.util.ResourceBundle
|
||||||
* @see org.springframework.context.support.ResourceBundleMessageSource
|
* @see org.springframework.context.support.ResourceBundleMessageSource
|
||||||
*/
|
*/
|
||||||
public class ResourceBundleThemeSource implements HierarchicalThemeSource {
|
public class ResourceBundleThemeSource implements HierarchicalThemeSource, BeanClassLoaderAware {
|
||||||
|
|
||||||
protected final Log logger = LogFactory.getLog(getClass());
|
protected final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
|
|
@ -49,8 +50,14 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
|
||||||
|
|
||||||
private String basenamePrefix = "";
|
private String basenamePrefix = "";
|
||||||
|
|
||||||
|
private String defaultEncoding;
|
||||||
|
|
||||||
|
private Boolean fallbackToSystemLocale;
|
||||||
|
|
||||||
|
private ClassLoader beanClassLoader;
|
||||||
|
|
||||||
/** Map from theme name to Theme instance */
|
/** 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
|
@Override
|
||||||
|
|
@ -85,6 +92,33 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
|
||||||
this.basenamePrefix = (basenamePrefix != null ? basenamePrefix : "");
|
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
|
* This implementation returns a SimpleTheme instance, holding a
|
||||||
|
|
@ -100,8 +134,10 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
|
||||||
if (themeName == null) {
|
if (themeName == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
synchronized (this.themeCache) {
|
|
||||||
Theme theme = this.themeCache.get(themeName);
|
Theme theme = this.themeCache.get(themeName);
|
||||||
|
if (theme == null) {
|
||||||
|
synchronized (this.themeCache) {
|
||||||
|
theme = this.themeCache.get(themeName);
|
||||||
if (theme == null) {
|
if (theme == null) {
|
||||||
String basename = this.basenamePrefix + themeName;
|
String basename = this.basenamePrefix + themeName;
|
||||||
MessageSource messageSource = createMessageSource(basename);
|
MessageSource messageSource = createMessageSource(basename);
|
||||||
|
|
@ -112,9 +148,10 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
|
||||||
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
|
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return theme;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return theme;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a MessageSource for the given basename,
|
* Create a MessageSource for the given basename,
|
||||||
|
|
@ -130,6 +167,15 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource {
|
||||||
protected MessageSource createMessageSource(String basename) {
|
protected MessageSource createMessageSource(String basename) {
|
||||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||||
messageSource.setBasename(basename);
|
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;
|
return messageSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue