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.s3.transfer.internal;
 
 import static com.amazonaws.event.SDKProgressPublisher.publishProgress;
 
 import java.util.List;
 
Monitors an copy operation by periodically checking to see if the operation is completed, and returning a result if so. Otherwise, schedules a copy of itself to be run in the future. When waiting on the result of this class via a Future object, clients must call isDone() and getFuture()
 
 public class CopyMonitor implements Callable<CopyResult>, TransferMonitor {

    
Reference to the Amazon S3 client object that is used to initiate the copy or copy part request.
 
     private final AmazonS3 s3;
    
Thread pool used during multi-part copy is performed.
 
     private final ExecutorService threadPool;
    
A reference to the original copy request received.
 
     private final CopyObjectRequest origReq;
    
Thread pool used for scheduling the monitor to check if the copy operation is completed.
 
     private ScheduledExecutorService timedThreadPool;
    
Reference to the CopyCallable that is used for initiating copy requests.
 
     private final CopyCallable multipartCopyCallable;
     private final CopyImpl transfer;
 
     /*
      * State for tracking the upload's progress
      */
     private String uploadId;
     private final List<Future<PartETag>> futures = new ArrayList<Future<PartETag>>();
 
     /*
      * State for clients wishing to poll for completion
      */
     private boolean isCopyDone = false;
     private Future<CopyResultnextFuture;
 
     public synchronized Future<CopyResultgetFuture() {
         return ;
     }
 
     private synchronized void setNextFuture(Future<CopyResultnextFuture) {
         this. = nextFuture;
     }
 
     public synchronized boolean isDone() {
         return ;
     }
 
     private synchronized void markAllDone() {
          = true;
     }
 
     // TODO: this could be configured in the configuration object (which we're
     // not using right now)
     private int pollInterval = 5000;

    
Constructs a new watcher for copy operation, and then immediately submits it to the thread pool.

Parameters:
manager The com.amazonaws.services.s3.transfer.TransferManager that owns this copy request.
threadPool The java.util.concurrent.ExecutorService to which we should submit new tasks.
multipartCopyCallable The callable responsible for processing the copy asynchronously
copyObjectRequest The original CopyObject request
    public static CopyMonitor create(
            TransferManager manager,
            CopyImpl transfer,
            ExecutorService threadPool,
            CopyCallable multipartCopyCallable,
            CopyObjectRequest copyObjectRequest,
            ProgressListenerChain progressListenerChain) {
        CopyMonitor copyMonitor = new CopyMonitor(managertransfer,
                threadPoolmultipartCopyCallablecopyObjectRequest,
                progressListenerChain);
        copyMonitor.setNextFuture(threadPool.submit(copyMonitor));
        return copyMonitor;
    }
    private CopyMonitor(TransferManager managerCopyImpl transfer,
            ExecutorService threadPoolCopyCallable multipartCopyCallable,
            CopyObjectRequest copyObjectRequest,
            ProgressListenerChain progressListenerChain) {
        this. = manager.getAmazonS3Client();
        this. = multipartCopyCallable;
        this. = threadPool;
        this. = copyObjectRequest;
        this. = transfer;
        this. = progressListenerChain;
    }
    @Override
    public CopyResult call() throws Exception {
        try {
            if ( == null) {
                return copy();
            } else {
                return poll();
            }
        } catch (CancellationException e) {
            .setState(.);
            throw new AmazonClientException("Upload canceled");
        } catch (Exception e) {
            .setState(.);
            throw e;
        }
    }
    public void setTimedThreadPool(ScheduledExecutorService timedThreadPool) {
        this. = timedThreadPool;
    }

    
Polls for a result from a multi-part copy operation and either returns it if complete, or reschedules to poll again later if not.
    private CopyResult poll() throws InterruptedException {
        for (Future<PartETagf : ) {
            if (!f.isDone()) {
                reschedule();
                return null;
            }
        }
        for (Future<PartETagf : ) {
            if (f.isCancelled()) {
                throw new CancellationException();
            }
        }
        return completeMultipartUpload();
    }

    
Initiates the copy operation and checks on the result. If it has completed, returns the result; otherwise, reschedules to check back later.
    private CopyResult copy() throws ExceptionInterruptedException {
        CopyResult result = .call();
        if (result != null) {
            copyComplete();
        } else {
            reschedule();
        }
        return result;
    }
    private void copyComplete() {
        markAllDone();
        // AmazonS3Client takes care of all the events for single part uploads,
        // so we only need to send a completed event for multipart uploads.
        if (.isMultipartCopy()) {
        }
    }
    private void reschedule()  {
            public CopyResult call() throws Exception {
                setNextFuture(.submit(CopyMonitor.this));
                return null;
            }
        }, .));
    }

    
Completes the multipart upload and returns the result.
    private CopyResult completeMultipartUpload() {
                .getDestinationBucketName(),
                .getDestinationKey(), ,
                collectPartETags())
            ;
        copyComplete();
        CopyResult copyResult = new CopyResult();
        copyResult.setSourceBucketName(.getSourceBucketName());
        copyResult.setSourceKey(.getSourceKey());
        copyResult.setDestinationBucketName(result
                .getBucketName());
        copyResult.setDestinationKey(result.getKey());
        copyResult.setETag(result.getETag());
        copyResult.setVersionId(result.getVersionId());
        return copyResult;
    }
    private List<PartETagcollectPartETags() {
        final List<PartETagpartETags = new ArrayList<PartETag>(.size());
        for (Future<PartETagfuture : ) {
            try {
                partETags.add(future.get());
            } catch (Exception e) {
                throw new AmazonClientException("Unable to copy part: "
                        + e.getCause().getMessage(), e.getCause());
            }
        }
        return partETags;
    }
New to GrepCode? Check out our FAQ X