Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.auth.profile.internal;
 
 import java.io.File;
 import java.util.Map;
 
 
 
 public class ProfilesConfigFileLoader {
 
     private static final Log LOG = LogFactory.getLog(ProfilesConfigFileLoader.class);
 
     public static Map<StringProfileloadProfiles(File file) {
         if (file == null) {
             throw new IllegalArgumentException(
                     "Unable to load AWS profiles: specified file is null.");
         }
 
         if (!file.exists() || !file.isFile()) {
             throw new IllegalArgumentException(
                     "AWS credential profiles file not found in the given path: "
                             + file.getAbsolutePath());
         }
 
         FileInputStream fis = null;
         try {
             fis = new FileInputStream(file);
             return loadProfiles(fis);
         } catch (IOException ioe) {
             throw new AmazonClientException(
                     "Unable to load AWS credential profiles file at: " + file.getAbsolutePath(), ioe);
         } finally {
             if (fis != nulltry {fis.close();} catch (IOException ioe) {}
         }
     }

    
Loads the credential profiles from the given input stream.

Parameters:
is input stream from where the profile details are read.
Throws:
java.io.IOException
 
     private static Map<StringProfileloadProfiles(InputStream isthrows IOException {
         Map<StringMap<StringString>> allProfileProperties = helper.parseProfileProperties(new Scanner(is));
 
         // Convert the loaded property map to credential objects
         Map<StringProfileprofilesByName = new LinkedHashMap<StringProfile>();
 
         for (Entry<StringMap<StringString>> entry : allProfileProperties.entrySet()) {
             String profileName = entry.getKey();
             Map<StringStringproperties = entry.getValue();
 
             if (profileName.startsWith("profile ")) {
                 .warn("The legacy profile format requires the 'profile ' prefix before the profile name. "
                         + "The latest code does not require such prefix, and will consider it as part of the profile name. "
                         + "Please remove the prefix if you are seeing this warning.");
             }
 
             String accessKey    = properties.get(.);
             String secretKey    = properties.get(.);
             String sessionToken = properties.get(.);
 
             assertParameterNotEmpty(profileName,
                                   "Unable to load credentials into profile: ProfileName is empty.");
             if (accessKey == null) {
                 throw new AmazonClientException(
                         String.format("Unable to load credentials into profile [%s]: AWS Access Key ID is not specified.",
                                       profileName));
             }
             if (secretKey == null) {
                 throw new AmazonClientException(
                         String.format("Unable to load credentials into profile [%s]: AWS Secret Access Key is not specified.",
                                 profileName));
            }
            if (sessionToken == null) {
                profilesByName.put(
                        profileName,
                        new Profile(profileName,
                                    new BasicAWSCredentials(accessKeysecretKey)));
            } else {
                if (sessionToken.isEmpty()) {
                    throw new AmazonClientException(
                            String.format("Unable to load credentials into profile [%s]: AWS Session Token is empty.",
                                    profileName));
                }
                profilesByName.put(
                        profileName,
                        new Profile(profileName,
                                    new BasicSessionCredentials(accessKeysecretKeysessionToken)));
            }
        }
        return profilesByName;
    }

    

Asserts that the specified parameter value is neither empty nor null, and if it is, throws an AmazonClientException with the specified error message.

Parameters:
parameterValue The parameter value being checked.
errorMessage The error message to include in the AmazonClientException if the specified parameter value is empty.
    private static void assertParameterNotEmpty(String parameterValueString errorMessage) {
        if (parameterValue == null || parameterValue.isEmpty())
            throw new AmazonClientException(errorMessage);
    }

    
Implementation of AbstractProfilesConfigFileScanner that groups profile properties into a map while scanning through the credentials profile.
    private static class ProfilesConfigFileLoaderHelper extends AbstractProfilesConfigFileScanner {

        
Map from the parsed profile name to the map of all the property values included the specific profile
        protected final Map<StringMap<StringString>> allProfileProperties = new LinkedHashMap<StringMap<StringString>>();

        
Parses the input and returns a map of all the profile properties.
        public Map<StringMap<StringString>> parseProfileProperties(Scanner scanner) {
            .clear();
            run(scanner);
            return new LinkedHashMap<StringMap<StringString>>();
        }
        @Override
        protected void onEmptyOrCommentLine(String profileNameString line) {
            // Ignore empty or comment line
        }
        @Override
        protected void onProfileStartingLine(String newProfileNameString line) {
            // If the same profile name has already been declared, clobber the
            // previous one
            .put(newProfileNamenew HashMap<StringString>());
        }
        @Override
        protected void onProfileEndingLine(String prevProfileName) {
            // No-op
        }
        @Override
        protected void onProfileProperty(String profileName,
                String propertyKeyString propertyValue,
                boolean isSupportedPropertyString line) {
            if ( !isSupportedProperty ) {
                .info(String.format(
                        "Skip unsupported property name %s in profile [%s].",
                        propertyKeyprofileName));
                return;
            }
            // Not strictly necessary, since the abstract super class guarantees
            // onProfileStartingLine is always invoked before this method.
            // Just to be safe...
            if (.get(profileName) == null) {
                .put(profileNamenew HashMap<StringString>());
            }
            Map<StringStringproperties = .get(profileName);
            if (properties.containsKey(propertyKey)) {
                throw new IllegalArgumentException(
                        "Duplicate property values for ["
                                + propertyKey + "].");
            }
            properties.put(propertyKeypropertyValue);
        }
        @Override
        protected void onEndOfFile() {
            // No-op
        }
    }
New to GrepCode? Check out our FAQ X