📄 reloadableresourcebundlemessagesource.java
字号:
if (localeMap != null) {
List filenames = (List) localeMap.get(locale);
if (filenames != null) {
return filenames;
}
}
List filenames = new ArrayList(7);
filenames.addAll(calculateFilenamesForLocale(basename, locale));
if (this.fallbackToSystemLocale && !locale.equals(Locale.getDefault())) {
filenames.addAll(calculateFilenamesForLocale(basename, Locale.getDefault()));
}
filenames.add(basename);
if (localeMap != null) {
localeMap.put(locale, filenames);
}
else {
localeMap = new HashMap();
localeMap.put(locale, filenames);
this.cachedFilenames.put(basename, localeMap);
}
return filenames;
}
}
/**
* Calculate the filenames for the given bundle basename and Locale,
* appending language code, country code, and variant code.
* E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO",
* "messages_de_AT", "messages_de".
* @param basename the basename of the bundle
* @param locale the locale
* @return the List of filenames to check
*/
protected List calculateFilenamesForLocale(String basename, Locale locale) {
List result = new ArrayList(3);
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
StringBuffer temp = new StringBuffer(basename);
if (language.length() > 0) {
temp.append('_').append(language);
result.add(0, temp.toString());
}
if (country.length() > 0) {
temp.append('_').append(country);
result.add(0, temp.toString());
}
if (variant.length() > 0) {
temp.append('_').append(variant);
result.add(0, temp.toString());
}
return result;
}
/**
* Get PropertiesHolder for the given filename, either from the cache
* or freshly loaded.
*/
protected PropertiesHolder getProperties(String filename) {
synchronized (this.cachedProperties) {
PropertiesHolder propHolder = (PropertiesHolder) this.cachedProperties.get(filename);
if (propHolder != null &&
(propHolder.getRefreshTimestamp() < 0 ||
propHolder.getRefreshTimestamp() > System.currentTimeMillis() - this.cacheMillis)) {
return propHolder;
}
else {
return refreshProperties(filename, propHolder);
}
}
}
/**
* Refresh the PropertiesHolder for the given bundle filename.
* The holder can be null if not cached before, or a timed-out cache entry
* (potentially getting re-validated against the current last-modified timestamp).
*/
protected PropertiesHolder refreshProperties(String filename, PropertiesHolder propHolder) {
long refreshTimestamp = (this.cacheMillis < 0) ? -1 : System.currentTimeMillis();
Resource resource = this.resourceLoader.getResource(filename + PROPERTIES_SUFFIX);
try {
long fileTimestamp = -1;
if (this.cacheMillis >= 0) {
// last-modified timestamp of file will just be read if caching with timeout
// (allowing to use classpath resources if caching forever)
fileTimestamp = resource.getFile().lastModified();
if (fileTimestamp == 0) {
throw new IOException("File [" + resource.getFile().getAbsolutePath() + "] does not exist");
}
if (propHolder != null && propHolder.getFileTimestamp() == fileTimestamp) {
if (logger.isDebugEnabled()) {
logger.debug("Re-caching properties for filename [" + filename + "] - file hasn't been modified");
}
propHolder.setRefreshTimestamp(refreshTimestamp);
return propHolder;
}
}
InputStream is = resource.getInputStream();
Properties props = new Properties();
try {
String charset = null;
if (this.fileEncodings != null) {
charset = this.fileEncodings.getProperty(filename);
}
if (charset == null) {
charset = this.defaultEncoding;
}
if (charset != null) {
if (logger.isDebugEnabled()) {
logger.debug("Loading properties for filename [" + filename + "] with charset '" + charset + "'");
}
this.propertiesPersister.load(props, new InputStreamReader(is, charset));
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Loading properties for filename [" + filename + "]");
}
this.propertiesPersister.load(props, is);
}
propHolder = new PropertiesHolder(props, fileTimestamp);
}
finally {
is.close();
}
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Properties file [" + filename + "] not found for MessageSource: " + ex.getMessage());
}
// empty holder representing "not found"
propHolder = new PropertiesHolder();
}
propHolder.setRefreshTimestamp(refreshTimestamp);
this.cachedProperties.put(filename, propHolder);
return propHolder;
}
/**
* Clear the resource bundle cache.
* Following resolve calls will lead to reloading of the properties files.
*/
public void clearCache() {
logger.info("Clearing resource bundle cache");
synchronized (this.cachedProperties) {
this.cachedProperties.clear();
}
}
/**
* Clear the resource bundle caches of this MessageSource and all its ancestors.
* @see #clearCache
*/
public void clearCacheIncludingAncestors() {
clearCache();
if (getParentMessageSource() instanceof ReloadableResourceBundleMessageSource) {
((ReloadableResourceBundleMessageSource) getParentMessageSource()).clearCacheIncludingAncestors();
}
}
public String toString() {
return getClass().getName() + ": basenames=[" + StringUtils.arrayToCommaDelimitedString(this.basenames) + "]";
}
/**
* PropertiesHolder for caching.
* Stores the last-modified timestamp of the source file for efficient
* change detection, and the timestamp of the last refresh attempt
* (updated every time the cache entry gets re-validated).
*/
protected class PropertiesHolder {
private Properties properties;
private long fileTimestamp = -1;
private long refreshTimestamp = -1;
/** Cache to hold already generated MessageFormats per message code */
private final Map cachedMessageFormats = new HashMap();
protected PropertiesHolder(Properties properties, long fileTimestamp) {
this.properties = properties;
this.fileTimestamp = fileTimestamp;
}
protected PropertiesHolder() {
}
protected Properties getProperties() {
return properties;
}
protected long getFileTimestamp() {
return fileTimestamp;
}
protected void setRefreshTimestamp(long refreshTimestamp) {
this.refreshTimestamp = refreshTimestamp;
}
protected long getRefreshTimestamp() {
return refreshTimestamp;
}
protected MessageFormat getMessageFormat(String code, Locale locale) {
synchronized (this.cachedMessageFormats) {
Map localeMap = (Map) this.cachedMessageFormats.get(code);
if (localeMap != null) {
MessageFormat result = (MessageFormat) localeMap.get(locale);
if (result != null) {
return result;
}
}
String msg = this.properties.getProperty(code);
if (msg != null) {
if (localeMap == null) {
localeMap = new HashMap();
this.cachedMessageFormats.put(code, localeMap);
}
MessageFormat result = createMessageFormat(msg, locale);
localeMap.put(locale, result);
return result;
}
return null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -