2323import org .springframework .core .env .StandardEnvironment ;
2424
2525/**
26- * Simple detection for well known cloud platforms. For more advanced cloud provider
27- * integration consider the Spring Cloud project.
26+ * Simple detection for well known cloud platforms. Detection can be forced using the
27+ * {@code "spring.main.cloud-platform"} configuration property. For more advanced cloud
28+ * provider integration consider the Spring Cloud project.
2829 *
2930 * @author Phillip Webb
31+ * @author Brian Clozel
3032 * @since 1.3.0
31- * @see "https://cloud. spring.io"
33+ * @see "https://spring.io/projects/spring-cloud "
3234 */
3335public enum CloudPlatform {
3436
37+ /**
38+ * No Cloud platform. Useful when false-positives are detected.
39+ */
40+ NONE {
41+
42+ @ Override
43+ public boolean isAutoDetected (Environment environment ) {
44+ return false ;
45+ }
46+
47+ },
48+
3549 /**
3650 * Cloud Foundry platform.
3751 */
3852 CLOUD_FOUNDRY {
3953
4054 @ Override
41- public boolean isActive (Environment environment ) {
55+ public boolean isAutoDetected (Environment environment ) {
4256 return environment .containsProperty ("VCAP_APPLICATION" ) || environment .containsProperty ("VCAP_SERVICES" );
4357 }
4458
@@ -50,7 +64,7 @@ public boolean isActive(Environment environment) {
5064 HEROKU {
5165
5266 @ Override
53- public boolean isActive (Environment environment ) {
67+ public boolean isAutoDetected (Environment environment ) {
5468 return environment .containsProperty ("DYNO" );
5569 }
5670
@@ -62,7 +76,7 @@ public boolean isActive(Environment environment) {
6276 SAP {
6377
6478 @ Override
65- public boolean isActive (Environment environment ) {
79+ public boolean isAutoDetected (Environment environment ) {
6680 return environment .containsProperty ("HC_LANDSCAPE" );
6781 }
6882
@@ -82,14 +96,14 @@ public boolean isActive(Environment environment) {
8296 private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT" ;
8397
8498 @ Override
85- public boolean isActive (Environment environment ) {
99+ public boolean isAutoDetected (Environment environment ) {
86100 if (environment instanceof ConfigurableEnvironment ) {
87- return isActive ((ConfigurableEnvironment ) environment );
101+ return isAutoDetected ((ConfigurableEnvironment ) environment );
88102 }
89103 return false ;
90104 }
91105
92- private boolean isActive (ConfigurableEnvironment environment ) {
106+ private boolean isAutoDetected (ConfigurableEnvironment environment ) {
93107 PropertySource <?> environmentPropertySource = environment .getPropertySources ()
94108 .get (StandardEnvironment .SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME );
95109 if (environmentPropertySource != null ) {
@@ -98,13 +112,13 @@ private boolean isActive(ConfigurableEnvironment environment) {
98112 return true ;
99113 }
100114 if (environmentPropertySource instanceof EnumerablePropertySource ) {
101- return isActive ((EnumerablePropertySource <?>) environmentPropertySource );
115+ return isAutoDetected ((EnumerablePropertySource <?>) environmentPropertySource );
102116 }
103117 }
104118 return false ;
105119 }
106120
107- private boolean isActive (EnumerablePropertySource <?> environmentPropertySource ) {
121+ private boolean isAutoDetected (EnumerablePropertySource <?> environmentPropertySource ) {
108122 for (String propertyName : environmentPropertySource .getPropertyNames ()) {
109123 if (propertyName .endsWith (SERVICE_HOST_SUFFIX )) {
110124 String serviceName = propertyName .substring (0 ,
@@ -124,7 +138,31 @@ private boolean isActive(EnumerablePropertySource<?> environmentPropertySource)
124138 * @param environment the environment
125139 * @return if the platform is active.
126140 */
127- public abstract boolean isActive (Environment environment );
141+ public boolean isActive (Environment environment ) {
142+ return isEnforced (environment ) || isAutoDetected (environment );
143+ }
144+
145+ /**
146+ * Detemines if the platform is enforced by looking at the
147+ * {@code "spring.main.cloud-platform"} configuration property.
148+ * @param environment the environment
149+ * @return if the platform is enforced
150+ */
151+ public boolean isEnforced (Environment environment ) {
152+ String platform = environment .getProperty ("spring.main.cloud-platform" );
153+ if (platform != null ) {
154+ return this .name ().equalsIgnoreCase (platform );
155+ }
156+ return false ;
157+ }
158+
159+ /**
160+ * Determines if the platform is auto-detected by looking for platform-specific
161+ * environment variables.
162+ * @param environment the environment
163+ * @return if the platform is auto-detected.
164+ */
165+ public abstract boolean isAutoDetected (Environment environment );
128166
129167 /**
130168 * Returns if the platform is behind a load balancer and uses
0 commit comments