Skip to content

Add ReloadableResourceBundleMessageSource Resource selection hook #30369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,38 @@ protected PropertiesHolder getProperties(String filename) {
}
}

/**
* Select a concrete existing {@link Resource} from a {@code bundleName}, potentially
* checking multiple source (eg. file extensions). In case no suitable concrete
* Resource exists this method returns a Resource for which {@link Resource#exists()}
* returns {@code false}, which gets subsequently ignored.
* <p>This can be leveraged to check the last modification timestamp and to load
* properties from alternative sources. For example an XML blob in a database, or
* properties serialized in a custom format like JSON...
* <p>The default implementation first checks for an existing file Resource with the
* {@code .properties} extension, and otherwise returns a file Resource with the
* {@code .xml} extension.
* <p>When overriding this method, {@link #loadProperties(Resource, String)} MUST be
* capable of loading properties from any of the {@link Resource} this method can return.
* As a consequence, implementors are strongly encouraged to also override
* {@link #loadProperties(Resource, String)}.
* <p>As an alternative, one could set the {@link #setPropertiesPersister(PropertiesPersister)}
* with an instance capable of dealing with all resources returned by this method.
* Please note however that the default {@code loadProperties} detects XML resource
* filenames and uses {@link PropertiesPersister#loadFromXml(Properties, InputStream)},
* and the two {@link PropertiesPersister#load(Properties, InputStream) load} methods
* otherwise.
* @return the {@code Resource} to use
* @since 6.1.0
*/
protected Resource determineResource(String filename) {
Resource propertiesResource = this.resourceLoader.getResource(filename + PROPERTIES_SUFFIX);
if (propertiesResource.exists()) {
return propertiesResource;
}
return this.resourceLoader.getResource(filename + XML_SUFFIX);
}

/**
* Refresh the PropertiesHolder for the given bundle filename.
* The holder can be {@code null} if not cached before, or a timed-out cache entry
Expand All @@ -408,11 +440,7 @@ protected PropertiesHolder getProperties(String filename) {
protected PropertiesHolder refreshProperties(String filename, @Nullable PropertiesHolder propHolder) {
long refreshTimestamp = (getCacheMillis() < 0 ? -1 : System.currentTimeMillis());

Resource resource = this.resourceLoader.getResource(filename + PROPERTIES_SUFFIX);
if (!resource.exists()) {
resource = this.resourceLoader.getResource(filename + XML_SUFFIX);
}

Resource resource = determineResource(filename);
if (resource.exists()) {
long fileTimestamp = -1;
if (getCacheMillis() >= 0) {
Expand Down Expand Up @@ -451,7 +479,7 @@ protected PropertiesHolder refreshProperties(String filename, @Nullable Properti
else {
// Resource does not exist.
if (logger.isDebugEnabled()) {
logger.debug("No properties file found for [" + filename + "] - neither plain properties nor XML");
logger.debug("No properties file found for [" + filename + "]");
}
// Empty holder representing "not found".
propHolder = new PropertiesHolder();
Expand Down