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.auth.profile.internal;
17
18 import static com.amazonaws.util.ValidationUtils.assertNotNull;
19
20 import com.amazonaws.annotation.SdkInternalApi;
21 import com.amazonaws.annotation.SdkTestInternalApi;
22 import com.amazonaws.auth.profile.ProfilesConfigFile;
23 import com.amazonaws.profile.path.AwsProfileFileLocationProvider;
24 import java.io.File;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29  * Basic profile config loader to load config file from {@link AwsProfileFileLocationProvider.DEFAULT_CONFIG_LOCATION_PROVIDER}.
30  */

31 @SdkInternalApi
32 public final class BasicProfileConfigFileLoader {
33
34     public static final BasicProfileConfigFileLoader INSTANCE = new BasicProfileConfigFileLoader();
35     private static final Log log = LogFactory.getLog(BasicProfileConfigFileLoader.class);
36
37     private final AwsProfileFileLocationProvider configFileLocationProvider;
38     private volatile String profileName;
39     private volatile ProfilesConfigFile configFile;
40     private volatile boolean profileLoadAttempted;
41
42     private BasicProfileConfigFileLoader() {
43         this.configFileLocationProvider = AwsProfileFileLocationProvider.DEFAULT_CONFIG_LOCATION_PROVIDER;
44     }
45
46     @SdkTestInternalApi
47     public BasicProfileConfigFileLoader(AwsProfileFileLocationProvider configFileLocationProvider) {
48         this.configFileLocationProvider = assertNotNull(configFileLocationProvider, "configFileLocationProvider");
49     }
50
51     /**
52      * @return a {@link BasicProfile} if available, otherwise null.
53      */

54     public BasicProfile getProfile() {
55         ProfilesConfigFile profilesConfigFile = getProfilesConfigFile();
56         if (profilesConfigFile != null) {
57             return profilesConfigFile.getAllBasicProfiles().get(getProfileName());
58         }
59         return null;
60     }
61
62     // ProfilesConfigFile immediately loads the profiles at construction time
63     private ProfilesConfigFile getProfilesConfigFile() {
64         if (!profileLoadAttempted) {
65             synchronized (this) {
66                 if (!profileLoadAttempted) {
67                     File location = null;
68                     try {
69                         location = configFileLocationProvider.getLocation();
70                         if (location != null) {
71                             configFile = new ProfilesConfigFile(location);
72                         }
73                     } catch (Exception e) {
74                         if (log.isWarnEnabled()) {
75                             log.warn("Unable to load config file " + location, e);
76                         }
77                     } finally {
78                         profileLoadAttempted = true;
79                     }
80                 }
81             }
82         }
83         return configFile;
84     }
85
86     private String getProfileName() {
87         if (profileName == null) {
88             synchronized (this) {
89                 if (profileName == null) {
90                     profileName = AwsProfileNameLoader.INSTANCE.loadProfileName();
91                 }
92             }
93         }
94         return profileName;
95     }
96 }
97