1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15
16 package software.amazon.awssdk.regions.providers;
17
18 import java.util.function.Supplier;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 import software.amazon.awssdk.profiles.ProfileFile;
21
22 /**
23  * AWS Region provider that looks for the region in this order:
24  * <ol>
25  *   <li>Check the 'aws.region' system property for the region.</li>
26  *   <li>Check the 'AWS_REGION' environment variable for the region.</li>
27  *   <li>Check the {user.home}/.aws/credentials and {user.home}/.aws/config files for the region.</li>
28  *   <li>If running in EC2, check the EC2 metadata service for the region.</li>
29  * </ol>
30  */

31 @SdkProtectedApi
32 public final class DefaultAwsRegionProviderChain extends AwsRegionProviderChain {
33     public DefaultAwsRegionProviderChain() {
34         super(new SystemSettingsRegionProvider(),
35               new AwsProfileRegionProvider(),
36               new InstanceProfileRegionProvider());
37     }
38
39     private DefaultAwsRegionProviderChain(Builder builder) {
40         super(new SystemSettingsRegionProvider(),
41               new AwsProfileRegionProvider(builder.profileFile, builder.profileName),
42               new InstanceProfileRegionProvider());
43     }
44
45     public static Builder builder() {
46         return new Builder();
47     }
48
49     public static final class Builder {
50         private Supplier<ProfileFile> profileFile;
51         private String profileName;
52
53         private Builder() {
54         }
55
56         public Builder profileFile(Supplier<ProfileFile> profileFile) {
57             this.profileFile = profileFile;
58             return this;
59         }
60
61         public Builder profileName(String profileName) {
62             this.profileName = profileName;
63             return this;
64         }
65
66         public DefaultAwsRegionProviderChain build() {
67             return new DefaultAwsRegionProviderChain(this);
68         }
69     }
70 }
71