org.springframework.context.support.ReloadableResourceBundleMessageSourceCustom Maven / Gradle / Ivy
package org.springframework.context.support;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Locale;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
/**
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource#clearCacheIncludingAncestors()
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource#getMergedProperties(Locale)
*/
public class ReloadableResourceBundleMessageSourceCustom extends ReloadableResourceBundleMessageSource {
private Collection getFiles(File file, FileFilter filter) {
Assert.notNull(file, "'file' must not be null");
Assert.notNull(filter, "'filter' must not be null");
Collection collection = new LinkedHashSet();
File[] files = file.listFiles(filter);
if (files == null || files.length == 0) {
return Collections.emptySet();
}
for (File listFile : files) {
if (listFile.isDirectory()) {
collection.addAll(getFiles(listFile, filter));
}
else {
collection.add(listFile);
}
}
return collection;
}
private boolean isBasename(String filename) {
if (filename == null) {
return false;
}
for (Locale locale : Locale.getAvailableLocales()) {
if (!locale.equals(Locale.ROOT) && filename.endsWith('_' + locale.toString())) {
return false;
}
}
return true;
}
public void setResource(Resource resource) {
if (resource != null && resource.exists()) {
try {
File location = resource.getFile();
if (location.exists() && location.isDirectory()) {
for (File listFile : getFiles(location, new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
return pathname.getName().toLowerCase().endsWith(".properties");
}
})) {
String uri = UriComponentsBuilder.fromUri(listFile.toURI()).build().toUriString();
String filename = (filename = listFile.getName()).substring(0, filename.length() - ".properties".length());
if (isBasename(filename)) {
addBasenames(uri.substring(0, uri.length() - ".properties".length()));
}
}
}
}
catch (IOException e) {
if (logger.isWarnEnabled()) {
logger.warn("resource fail..." + e.getMessage());
}
}
}
}
/**
* @see java.net.URL#toExternalForm()
*/
// @Override
// protected PropertiesHolder refreshProperties(String filename, PropertiesHolder propHolder) {
// if (StringUtils.startsWithIgnoreCase(filename, ConstantUtil.PREFIX_CLASSPATH_ALL_URL)) {
// long fileTimestamp = -1L;
// for (Resource resource : ConstantUtil.getResources(filename + ConstantUtil.SUFFIX_PROPERTIES)) {
// String resourceFilename = resource.getFilename();
// if ((filename.contains("_") && !resourceFilename.contains("_")) || (!filename.contains("_") && resourceFilename.contains("_"))) {
// continue;
// }
// Properties properties = propHolder.getProperties() == null ? (propHolder = new PropertiesHolder((properties = new Properties()), fileTimestamp)).getProperties() : propHolder.getProperties();
// try {
// String uri = resource.getURI().toString();
// properties.putAll(super.refreshProperties(uri.substring(0, uri.length() - ConstantUtil.SUFFIX_PROPERTIES.length()), propHolder).getProperties());
// if (fileTimestamp < resource.lastModified()) {
// propHolder.setRefreshTimestamp(resource.lastModified());
// }
// }
// catch (IOException | NullPointerException e) {
// logger.warn("Refresh Properties fail... from " + filename + " (" + e.getMessage() + ")");
// }
// }
// return propHolder;
// }
// return super.refreshProperties(filename, propHolder);
// }
}