001/* 002 * Copyright 2012-2017 the original author or 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 * http://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 */ 016 017package org.springframework.boot.cloud; 018 019import org.springframework.core.env.Environment; 020 021/** 022 * Simple detection for well known cloud platforms. For more advanced cloud provider 023 * integration consider the Spring Cloud project. 024 * 025 * @author Phillip Webb 026 * @since 1.3.0 027 * @see "http://cloud.spring.io" 028 */ 029public enum CloudPlatform { 030 031 /** 032 * Cloud Foundry platform. 033 */ 034 CLOUD_FOUNDRY { 035 036 @Override 037 public boolean isActive(Environment environment) { 038 return environment.containsProperty("VCAP_APPLICATION") 039 || environment.containsProperty("VCAP_SERVICES"); 040 } 041 042 }, 043 044 /** 045 * Heroku platform. 046 */ 047 HEROKU { 048 049 @Override 050 public boolean isActive(Environment environment) { 051 return environment.containsProperty("DYNO"); 052 } 053 054 }, 055 056 /** 057 * SAP Cloud platform. 058 */ 059 SAP { 060 061 @Override 062 public boolean isActive(Environment environment) { 063 return environment.containsProperty("HC_LANDSCAPE"); 064 } 065 066 }; 067 068 /** 069 * Determines if the platform is active (i.e. the application is running in it). 070 * @param environment the environment 071 * @return if the platform is active. 072 */ 073 public abstract boolean isActive(Environment environment); 074 075 /** 076 * Returns if the platform is behind a load balancer and uses 077 * {@literal X-Forwarded-For} headers. 078 * @return if {@literal X-Forwarded-For} headers are used 079 */ 080 public boolean isUsingForwardHeaders() { 081 return true; 082 } 083 084 /** 085 * Returns the active {@link CloudPlatform} or {@code null} if one cannot be deduced. 086 * @param environment the environment 087 * @return the {@link CloudPlatform} or {@code null} 088 */ 089 public static CloudPlatform getActive(Environment environment) { 090 if (environment != null) { 091 for (CloudPlatform cloudPlatform : values()) { 092 if (cloudPlatform.isActive(environment)) { 093 return cloudPlatform; 094 } 095 } 096 } 097 return null; 098 } 099 100}