Search in sources :

Example 1 with PropertiesPropertySourceLoader

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();
}
Also used : ResourceLoader(io.micronaut.core.io.ResourceLoader) PropertiesPropertySourceLoader(io.micronaut.context.env.PropertiesPropertySourceLoader)

Aggregations

PropertiesPropertySourceLoader (io.micronaut.context.env.PropertiesPropertySourceLoader)1 ResourceLoader (io.micronaut.core.io.ResourceLoader)1