001 /** 002 * Copyright 2004-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.maven.mojo.s3; 017 018 import java.io.IOException; 019 020 import org.apache.commons.lang.StringUtils; 021 import org.kuali.common.threads.ElementHandler; 022 import org.kuali.common.threads.ListIteratorContext; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 026 import com.amazonaws.AmazonServiceException; 027 028 public class UpdateDirectoryContextHandler implements ElementHandler<UpdateDirectoryContext> { 029 private final Logger logger = LoggerFactory.getLogger(ListObjectsContextHandler.class); 030 031 UpdateOriginBucketMojo mojo; 032 033 public UpdateDirectoryContextHandler() { 034 this(null); 035 } 036 037 public UpdateDirectoryContextHandler(UpdateOriginBucketMojo mojo) { 038 super(); 039 this.mojo = mojo; 040 } 041 042 @Override 043 public void handleElement(ListIteratorContext<UpdateDirectoryContext> context, int index, 044 UpdateDirectoryContext element) { 045 logger.debug("[Thread:" + lpad(context.getId()) + ", Element:" + lpad(index) + "] " + element.getCopyToKey()); 046 047 try { 048 S3PrefixContext prefixContext = element.getContext(); 049 if (prefixContext.isRoot()) { 050 mojo.updateRoot(element); 051 } else { 052 mojo.updateDirectory(element); 053 } 054 } catch (IOException e) { 055 throw new AmazonServiceException("Unexpected error:", e); 056 } 057 } 058 059 protected String lpad(int i) { 060 return StringUtils.leftPad(i + "", 3, " "); 061 } 062 063 public UpdateOriginBucketMojo getMojo() { 064 return mojo; 065 } 066 067 public void setMojo(UpdateOriginBucketMojo mojo) { 068 this.mojo = mojo; 069 } 070 }