Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-2015 Amazon Technologies, Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at:
   *
   *    http://aws.amazon.com/apache2.0
   *
  * 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.services.dynamodbv2.datamodeling;
 
 import java.util.Map;
 
A smart Map for com.amazonaws.services.s3.AmazonS3Client objects. S3ClientCache keeps the clients organized by region, and if provided com.amazonaws.auth.AWSCredentials will create clients on the fly. Otherwise it just return clients given to it with useClient(com.amazonaws.services.s3.AmazonS3Client).
 
 public class S3ClientCache {
     private final Map<RegionTransferManagertransferManagersByRegion = new EnumMap<RegionTransferManager>(Region.class);
 
 
     @Deprecated
     S3ClientCache(AWSCredentials credentials) {
         this(new StaticCredentialsProvider(credentials));
     }

    
Create a client cache using the given AWSCredentialsProvider. If getClient(com.amazonaws.services.s3.model.Region) or getTransferManager(com.amazonaws.services.s3.model.Region) is called and a client has not been provided for the region, the cache will instantiate one from the provided com.amazonaws.auth.AWSCredentialsProvider.

Parameters:
awsCredentialsProvider The credentials provider to use when creating new com.amazonaws.services.s3.AmazonS3Client.
 
     S3ClientCache(AWSCredentialsProvider awsCredentialsProvider) {
         this. = awsCredentialsProvider;
     }

    
Force the client cache to provide a certain client for the region which that client is configured. This can be useful to provide clients with different com.amazonaws.services.s3.S3ClientOptions or use a com.amazonaws.services.s3.AmazonS3EncryptionClient in place of a regular client. Using a new client will also forcibly shut down any com.amazonaws.services.s3.transfer.TransferManager that has been instantiated with that client, with the com.amazonaws.services.s3.transfer.TransferManager.shutdownNow() method.

Parameters:
client An com.amazonaws.services.s3.AmazonS3Client to use in the cache. Its region will be detected automatically.
 
     public void useClient(AmazonS3Client client) {
         Region s3region = client.getRegion();
 
         synchronized () {
             TransferManager tm = .remove(s3region);
             if (tm != null) {
                 tm.shutdownNow();
             }
             .put(s3regionclient);
         }
     }

    
Returns a client for the requested region, or throws an exception when unable.

Parameters:
s3region The region the returned com.amazonaws.services.s3.AmazonS3Client will be configured to use.
Returns:
A client for the given region from the cache, either instantiated automatically from the provided com.amazonaws.auth.AWSCredentials or provided with useClient(com.amazonaws.services.s3.AmazonS3Client).
Throws:
java.lang.IllegalArgumentException When a region is requested that has not been provided to the cache with useClient(com.amazonaws.services.s3.AmazonS3Client), and the cache has no com.amazonaws.auth.AWSCredentials with which a client may be instantiated.
    public AmazonS3Client getClient(Region s3region) {
        if ( s3region == null ) {
            throw new IllegalArgumentException("S3 region must be specified");
        }
        AmazonS3Client client = .get(s3region);
        if (client != null) {
            return client;
        }
        if ( == null) {
            throw new IllegalArgumentException("No credentials provider found to connect to S3");
        }
        client = new AmazonS3Client();
        client.setRegion(s3region.toAWSRegion());
        AmazonS3Client prev = .putIfAbsent(s3regionclient);
        return prev == null ? client : prev;
    }

    
Returns a com.amazonaws.services.s3.transfer.TransferManager for the given region, or throws an exception when unable. The returned com.amazonaws.services.s3.transfer.TransferManager will always be instantiated from whatever com.amazonaws.services.s3.AmazonS3Client is in the cache, whether provided with useClient(com.amazonaws.services.s3.AmazonS3Client) or instantiated automatically from com.amazonaws.auth.AWSCredentials. Any com.amazonaws.services.s3.transfer.TransferManager returned could be shut down if a new underlying com.amazonaws.services.s3.AmazonS3Client is provided with useClient(com.amazonaws.services.s3.AmazonS3Client).

Parameters:
region The region the returned com.amazonaws.services.s3.transfer.TransferManager will be configured to use.
Returns:
A transfer manager for the given region from the cache, or one instantiated automatically from any existing com.amazonaws.services.s3.AmazonS3Client,
    public TransferManager getTransferManager(Region region) {
        synchronized () {
            TransferManager tm = .get(region);
            if (tm == null) {
                tm = new TransferManager(getClient(region));
                .put(regiontm);
            }
            return tm;
        }
    }
New to GrepCode? Check out our FAQ X