001package org.kuali.common.devops.archive.sweep; 002 003import static com.google.common.base.Stopwatch.createStarted; 004import static com.google.common.collect.Lists.newArrayList; 005 006import java.util.List; 007import java.util.concurrent.Callable; 008 009import org.kuali.common.aws.s3.S3Service; 010import org.kuali.common.aws.s3.model.PutFileRequest; 011import org.kuali.common.core.build.ValidatingBuilder; 012import org.kuali.common.devops.archive.sweep.model.PutFileResult; 013import org.kuali.common.util.inform.PercentCompleteInformer; 014 015import com.google.common.base.Stopwatch; 016import com.google.common.collect.ImmutableList; 017 018public final class PutFilesCallable implements Callable<List<PutFileResult>> { 019 020 private final ImmutableList<PutFileRequest> requests; 021 private final S3Service s3; 022 private final PercentCompleteInformer informer; 023 024 @Override 025 public List<PutFileResult> call() { 026 List<PutFileResult> results = newArrayList(); 027 for (PutFileRequest request : requests) { 028 Stopwatch sw = createStarted(); 029 PutFileResult.Builder builder = PutFileResult.builder().withFile(request.getFile()); 030 try { 031 s3.putFile(request); 032 } catch (Exception e) { 033 builder.withException(e); 034 } 035 PutFileResult result = builder.time(sw).build(); 036 results.add(result); 037 informer.incrementProgress(); 038 } 039 return results; 040 } 041 042 public List<PutFileRequest> getRequests() { 043 return requests; 044 } 045 046 public S3Service getS3() { 047 return s3; 048 } 049 050 public PercentCompleteInformer getInformer() { 051 return informer; 052 } 053 054 private PutFilesCallable(Builder builder) { 055 this.requests = ImmutableList.copyOf(builder.requests); 056 this.s3 = builder.s3; 057 this.informer = builder.informer; 058 } 059 060 public static Builder builder() { 061 return new Builder(); 062 } 063 064 public static class Builder extends ValidatingBuilder<PutFilesCallable> { 065 066 private List<PutFileRequest> requests; 067 private S3Service s3; 068 private PercentCompleteInformer informer; 069 070 public Builder withRequests(List<PutFileRequest> requests) { 071 this.requests = requests; 072 return this; 073 } 074 075 public Builder withS3(S3Service s3) { 076 this.s3 = s3; 077 return this; 078 } 079 080 public Builder withInformer(PercentCompleteInformer informer) { 081 this.informer = informer; 082 return this; 083 } 084 085 @Override 086 public PutFilesCallable build() { 087 return validate(new PutFilesCallable(this)); 088 } 089 } 090 091}