View Javadoc
1   package io.micronaut.build.services;
2   
3   import io.micronaut.context.env.*;
4   import io.micronaut.context.env.yaml.YamlPropertySourceLoader;
5   import io.micronaut.core.io.file.DefaultFileSystemResourceLoader;
6   import org.apache.maven.project.MavenProject;
7   
8   import javax.inject.Inject;
9   import javax.inject.Singleton;
10  import java.io.File;
11  import java.util.HashMap;
12  import java.util.Map;
13  import java.util.Optional;
14  
15  /**
16   * Parses the Micronaut application configuration.
17   *
18   * @author Álvaro Sánchez-Mariscal
19   * @since 1.1
20   */
21  @Singleton
22  public class ApplicationConfigurationService {
23  
24      private final MavenProject mavenProject;
25  
26      @SuppressWarnings("CdiInjectionPointsInspection")
27      @Inject
28      public ApplicationConfigurationService(MavenProject mavenProject) {
29          this.mavenProject = mavenProject;
30      }
31  
32      public Map<String, Object> getApplicationConfiguration() {
33          Map<String, Object> configuration = new HashMap<>();
34  
35          PropertySourceLoader[] loaders = new PropertySourceLoader[]{
36                  new YamlPropertySourceLoader(),
37                  new PropertiesPropertySourceLoader()
38          };
39  
40          for (PropertySourceLoader loader : loaders) {
41              Optional<PropertySource> propertySource = loader.load("application", new DefaultFileSystemResourceLoader(new File(mavenProject.getBasedir(), "src/main/resources")));
42              if (propertySource.isPresent()) {
43                  MapPropertySource mapPropertySource = (MapPropertySource) propertySource.get();
44                  configuration.putAll(mapPropertySource.asMap());
45              }
46          }
47  
48          MapPropertySource[] propertySources = new MapPropertySource[] {
49                  new EnvironmentPropertySource(),
50                  new SystemPropertiesPropertySource()
51          };
52          for (MapPropertySource propertySource : propertySources) {
53              configuration.putAll(propertySource.asMap());
54          }
55  
56          return configuration;
57      }
58  }