11/*
2- * Copyright 2012-2020 the original author or authors.
2+ * Copyright 2012-2021 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2828 *
2929 * @author Phillip Webb
3030 * @author Brian Clozel
31+ * @author Nguyen Sach
3132 * @since 1.3.0
3233 */
3334public enum CloudPlatform {
@@ -131,13 +132,15 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo
131132
132133 };
133134
135+ private static final String PROPERTY_NAME = "spring.main.cloud-platform" ;
136+
134137 /**
135138 * Determines if the platform is active (i.e. the application is running in it).
136139 * @param environment the environment
137140 * @return if the platform is active.
138141 */
139142 public boolean isActive (Environment environment ) {
140- return isEnforced (environment ) || isDetected (environment );
143+ return isEnforced (environment ) || ( isAutoDetectionEnabled ( environment ) && isDetected (environment ) );
141144 }
142145
143146 /**
@@ -148,7 +151,7 @@ public boolean isActive(Environment environment) {
148151 * @since 2.3.0
149152 */
150153 public boolean isEnforced (Environment environment ) {
151- String platform = environment .getProperty ("spring.main.cloud-platform" );
154+ String platform = environment .getProperty (PROPERTY_NAME );
152155 return name ().equalsIgnoreCase (platform );
153156 }
154157
@@ -161,6 +164,16 @@ public boolean isEnforced(Environment environment) {
161164 */
162165 public abstract boolean isDetected (Environment environment );
163166
167+ /**
168+ * Determines if it is enabled that the platform is detected by looking for
169+ * platform-specific environment variables.
170+ * @param environment the environment
171+ * @return if the platform auto-detection is enabled.
172+ */
173+ private boolean isAutoDetectionEnabled (Environment environment ) {
174+ return environment .getProperty (PROPERTY_NAME ) == null ;
175+ }
176+
164177 /**
165178 * Returns if the platform is behind a load balancer and uses
166179 * {@literal X-Forwarded-For} headers.
0 commit comments