1 /*
2  * Copyright 2010-2020 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 com.amazonaws.retry.internal;
17
18 import static com.amazonaws.SDKGlobalConfiguration.AWS_RETRY_MODE_ENV_VAR;
19 import static com.amazonaws.SDKGlobalConfiguration.AWS_RETRY_MODE_SYSTEM_PROPERTY;
20
21 import com.amazonaws.annotation.SdkInternalApi;
22 import com.amazonaws.annotation.SdkTestInternalApi;
23 import com.amazonaws.auth.profile.internal.BasicProfile;
24 import com.amazonaws.auth.profile.internal.BasicProfileConfigFileLoader;
25 import com.amazonaws.profile.path.AwsProfileFileLocationProvider;
26 import com.amazonaws.retry.RetryMode;
27
28 /**
29  * Resolves the retryMode in the following order:
30  *
31  * <ul>
32  *   <li>Environment Variable</li>
33  *   <li>Java System Properties</li>
34  *   <li>Credential config file at the default location (~/.aws/config) shared by all AWS SDKs and the AWS CLI</li>
35  * </ul>
36  */

37 @SdkInternalApi
38 public final class RetryModeResolver {
39     private static final String PROFILE_PROPERTY = "retry_mode";
40
41     private final BasicProfileConfigFileLoader configFileLoader;
42     private final RetryMode retryMode;
43
44     public RetryModeResolver() {
45         this.configFileLoader = BasicProfileConfigFileLoader.INSTANCE;
46         this.retryMode = resolveRetryMode();
47     }
48
49     @SdkTestInternalApi
50     RetryModeResolver(AwsProfileFileLocationProvider configFileLocationProvider) {
51         this.configFileLoader = new BasicProfileConfigFileLoader(configFileLocationProvider);
52         this.retryMode = resolveRetryMode();
53     }
54
55     /**
56      * @return the resolved retry mode. If not found, {@link RetryMode.LEGACY} will be returned
57      */

58     public RetryMode retryMode() {
59         return retryMode;
60     }
61
62     private RetryMode systemProperty() {
63         return RetryMode.fromName(System.getProperty(AWS_RETRY_MODE_SYSTEM_PROPERTY));
64     }
65
66     private RetryMode envVar() {
67         return RetryMode.fromName(System.getenv(AWS_RETRY_MODE_ENV_VAR));
68
69     }
70
71     private RetryMode resolveRetryMode() {
72         RetryMode mode = envVar();
73
74         if (mode != null) {
75             return mode;
76         }
77
78         mode = systemProperty();
79         if (mode != null) {
80             return mode;
81         }
82
83         mode = profile();
84         if (mode != null) {
85             return mode;
86         }
87
88         return RetryMode.LEGACY;
89     }
90
91     private RetryMode profile() {
92         BasicProfile profile = configFileLoader.getProfile();
93         if (profile == null) {
94             return null;
95         }
96         String val = profile.getPropertyValue(PROFILE_PROPERTY);
97
98         return RetryMode.fromName(val);
99     }
100 }
101