1
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
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
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
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