View Javadoc
1   /*
2    * Copyright 2017-2022 original authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package io.micronaut.build.services;
17  
18  import io.micronaut.context.env.*;
19  import io.micronaut.context.env.yaml.YamlPropertySourceLoader;
20  import io.micronaut.core.io.file.DefaultFileSystemResourceLoader;
21  import org.apache.maven.project.MavenProject;
22  
23  import javax.inject.Inject;
24  import javax.inject.Singleton;
25  import java.io.File;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.Optional;
29  
30  /**
31   * Parses the Micronaut application configuration.
32   *
33   * @author Álvaro Sánchez-Mariscal
34   * @since 1.1
35   */
36  @Singleton
37  public class ApplicationConfigurationService {
38  
39      public static final String DEFAULT_PORT = "8080";
40  
41      private final MavenProject mavenProject;
42      private final Map<String, Object> applicationConfiguration;
43  
44      @SuppressWarnings("CdiInjectionPointsInspection")
45      @Inject
46      public ApplicationConfigurationService(MavenProject mavenProject) {
47          this.mavenProject = mavenProject;
48          this.applicationConfiguration = parseApplicationConfiguration();
49      }
50  
51      /**
52       * Determines the application port by looking at the <code>MICRONAUT_SERVER_PORT</code> environment variable, the
53       * <code>micronaut.server.port</code> configuration property, or falls back to a default port.
54       */
55      public String getServerPort() {
56          return applicationConfiguration.getOrDefault("MICRONAUT_SERVER_PORT", applicationConfiguration.getOrDefault("micronaut.server.port", DEFAULT_PORT)).toString();
57      }
58  
59      private Map<String, Object> parseApplicationConfiguration() {
60          Map<String, Object> configuration = new HashMap<>();
61  
62          PropertySourceLoader[] loaders = {
63                  new YamlPropertySourceLoader(),
64                  new PropertiesPropertySourceLoader()
65          };
66  
67          for (PropertySourceLoader loader : loaders) {
68              Optional<PropertySource> propertySource = loader.load("application", new DefaultFileSystemResourceLoader(new File(mavenProject.getBasedir(), "src/main/resources")));
69              if (propertySource.isPresent()) {
70                  MapPropertySource mapPropertySource = (MapPropertySource) propertySource.get();
71                  configuration.putAll(mapPropertySource.asMap());
72              }
73          }
74  
75          MapPropertySource[] propertySources = {
76                  new EnvironmentPropertySource(),
77                  new SystemPropertiesPropertySource()
78          };
79          for (MapPropertySource propertySource : propertySources) {
80              configuration.putAll(propertySource.asMap());
81          }
82  
83          return configuration;
84      }
85  }