001/* 002 * Copyright 2017-2022 original authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.micronaut.maven.services; 017 018import io.micronaut.context.env.EnvironmentPropertySource; 019import io.micronaut.context.env.MapPropertySource; 020import io.micronaut.context.env.PropertiesPropertySourceLoader; 021import io.micronaut.context.env.PropertySource; 022import io.micronaut.context.env.PropertySourceLoader; 023import io.micronaut.context.env.SystemPropertiesPropertySource; 024import io.micronaut.context.env.yaml.YamlPropertySourceLoader; 025import io.micronaut.core.io.file.DefaultFileSystemResourceLoader; 026import org.apache.maven.project.MavenProject; 027 028import javax.inject.Inject; 029import javax.inject.Singleton; 030import java.io.File; 031import java.util.HashMap; 032import java.util.Map; 033import java.util.Optional; 034 035/** 036 * Parses the Micronaut application configuration. 037 * 038 * @author Álvaro Sánchez-Mariscal 039 * @since 1.1 040 */ 041@Singleton 042public class ApplicationConfigurationService { 043 044 public static final String DEFAULT_PORT = "8080"; 045 046 private final MavenProject mavenProject; 047 private final Map<String, Object> applicationConfiguration; 048 049 @SuppressWarnings("CdiInjectionPointsInspection") 050 @Inject 051 public ApplicationConfigurationService(MavenProject mavenProject) { 052 this.mavenProject = mavenProject; 053 this.applicationConfiguration = parseApplicationConfiguration(); 054 } 055 056 /** 057 * Determines the application port by looking at the <code>MICRONAUT_SERVER_PORT</code> environment variable, the 058 * <code>micronaut.server.port</code> configuration property, or falls back to a default port. 059 * 060 * @return The application port 061 */ 062 public String getServerPort() { 063 return applicationConfiguration.getOrDefault("MICRONAUT_SERVER_PORT", applicationConfiguration.getOrDefault("micronaut.server.port", DEFAULT_PORT)).toString(); 064 } 065 066 private Map<String, Object> parseApplicationConfiguration() { 067 var configuration = new HashMap<String, Object>(); 068 069 PropertySourceLoader[] loaders = { 070 new YamlPropertySourceLoader(), 071 new PropertiesPropertySourceLoader() 072 }; 073 074 for (PropertySourceLoader loader : loaders) { 075 Optional<PropertySource> propertySource = loader.load("application", new DefaultFileSystemResourceLoader(new File(mavenProject.getBasedir(), "src/main/resources"))); 076 if (propertySource.isPresent()) { 077 MapPropertySource mapPropertySource = (MapPropertySource) propertySource.get(); 078 configuration.putAll(mapPropertySource.asMap()); 079 } 080 } 081 082 MapPropertySource[] propertySources = { 083 new EnvironmentPropertySource(), 084 new SystemPropertiesPropertySource() 085 }; 086 for (MapPropertySource propertySource : propertySources) { 087 configuration.putAll(propertySource.asMap()); 088 } 089 090 return configuration; 091 } 092}