use of io.micronaut.context.env.PropertiesPropertySourceLoader in project micronaut-core by micronaut-projects.
the class PropertiesInfoSource method retrievePropertiesPropertySource.
/**
* <p>Extends {@link io.micronaut.management.endpoint.info.InfoEndpoint} to add a helper method for retrieving a
* {@link PropertySource} from a properties file. </p>
*
* @param path The path to the properties file
* @param prefix prefix for resolving the file (used if not included in {@code path})
* @param extension file extension (used if not included in {@code path})
* @param resourceResolver Instance of {@link ResourceResolver} to resolve the file location
* @return An {@link Optional} of {@link PropertySource} containing the values from the properties file
*/
default Optional<PropertySource> retrievePropertiesPropertySource(String path, String prefix, String extension, ResourceResolver resourceResolver) {
StringBuilder pathBuilder = new StringBuilder();
if ((prefix != null) && !path.startsWith(prefix)) {
pathBuilder.append(prefix);
}
if ((extension != null) && path.endsWith(extension)) {
int index = path.indexOf(extension);
pathBuilder.append(path, 0, index);
} else {
pathBuilder.append(path);
}
String propertiesPath = pathBuilder.toString();
Optional<ResourceLoader> resourceLoader = resourceResolver.getSupportingLoader(propertiesPath);
if (resourceLoader.isPresent()) {
PropertiesPropertySourceLoader propertySourceLoader = new PropertiesPropertySourceLoader();
return propertySourceLoader.load(propertiesPath, resourceLoader.get());
}
return Optional.empty();
}
Aggregations