@@ -73,6 +73,10 @@ public class DefaultEnvironment extends PropertySourcePropertyResolver implement
73
73
private static final String K8S_ENV = "KUBERNETES_SERVICE_HOST" ;
74
74
private static final String PCF_ENV = "VCAP_SERVICES" ;
75
75
private static final String HEROKU_DYNO = "DYNO" ;
76
+ private static final int DEFAULT_READ_TIMEOUT = 500 ;
77
+ private static final int DEFAULT_CONNECT_TIMEOUT = 500 ;
78
+ public static final String DIGITAL_OCEAN_URL = "http://169.254.169.254/metadata/v1.json" ;
79
+ public static final String GOOGLE_COMPUTE_METADATA = "http://metadata.google.internal" ;
76
80
77
81
protected final ClassPathResourceLoader resourceLoader ;
78
82
@@ -732,10 +736,7 @@ private static ComputePlatform determineCloudProvider() {
732
736
@ SuppressWarnings ("MagicNumber" )
733
737
private static boolean isGoogleCompute () {
734
738
try {
735
- URL url = new URL ("http://metadata.google.internal" );
736
- HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
737
- con .setReadTimeout (500 );
738
- con .setConnectTimeout (500 );
739
+ final HttpURLConnection con = instantiateHttpURLConnectionForEnvironmentCheck (GOOGLE_COMPUTE_METADATA );
739
740
con .setRequestMethod ("GET" );
740
741
con .setDoOutput (true );
741
742
int responseCode = con .getResponseCode ();
@@ -805,10 +806,17 @@ private static boolean isEC2Windows() {
805
806
return false ;
806
807
}
807
808
809
+ private static HttpURLConnection instantiateHttpURLConnectionForEnvironmentCheck (String spec ) throws IOException {
810
+ final URL url = new URL (spec );
811
+ final HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
812
+ con .setReadTimeout (DEFAULT_READ_TIMEOUT );
813
+ con .setConnectTimeout (DEFAULT_CONNECT_TIMEOUT );
814
+ return con ;
815
+ }
816
+
808
817
private static boolean isDigitalOcean () {
809
818
try {
810
- final URL url = new URL ("http://169.254.169.254/metadata/v1.json" );
811
- final HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
819
+ final HttpURLConnection con = instantiateHttpURLConnectionForEnvironmentCheck (DIGITAL_OCEAN_URL );
812
820
con .setRequestMethod ("HEAD" );
813
821
int responseCode = con .getResponseCode ();
814
822
return responseCode == 200 ;
0 commit comments