View Javadoc

1   package org.kuali.maven.mojo.s3;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.commons.lang.StringUtils;
7   import org.kuali.common.threads.ElementHandler;
8   import org.kuali.common.threads.ListIteratorContext;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  import com.amazonaws.services.s3.AmazonS3Client;
13  import com.amazonaws.services.s3.model.ListObjectsRequest;
14  import com.amazonaws.services.s3.model.ObjectListing;
15  
16  public class ListObjectsContextHandler implements ElementHandler<ListObjectsContext> {
17      private final Logger logger = LoggerFactory.getLogger(ListObjectsContextHandler.class);
18      private final Object mutex = new Object();
19  
20      List<ObjectListing> objectListings;
21  
22      @Override
23      public void handleElement(ListIteratorContext<ListObjectsContext> context, int index, ListObjectsContext element) {
24          S3BucketContext bucketContext = element.getBucketContext();
25          AmazonS3Client client = bucketContext.getClient();
26          ListObjectsRequest request = element.getRequest();
27          request.getPrefix();
28          logger.debug("[Thread:" + lpad(context.getId()) + ", Element:" + lpad(index) + "] " + request.getPrefix());
29          ObjectListing listing = client.listObjects(request);
30          synchronized (mutex) {
31              if (objectListings == null) {
32                  objectListings = new ArrayList<ObjectListing>();
33              }
34              objectListings.add(listing);
35          }
36      }
37  
38      protected String lpad(int i) {
39          return StringUtils.leftPad(i + "", 3, " ");
40      }
41  
42      public List<ObjectListing> getObjectListings() {
43          return objectListings;
44      }
45  
46      public void setObjectListings(List<ObjectListing> objectListings) {
47          this.objectListings = objectListings;
48      }
49  
50  }