| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.blobstore.strategy.internal; |
| 20 | |
| 21 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 22 | |
| 23 | import java.util.concurrent.ExecutorService; |
| 24 | import java.util.concurrent.Future; |
| 25 | |
| 26 | import javax.annotation.Resource; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.Constants; |
| 31 | import org.jclouds.blobstore.AsyncBlobStore; |
| 32 | import org.jclouds.blobstore.domain.Blob; |
| 33 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 34 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 35 | import org.jclouds.blobstore.reference.BlobStoreConstants; |
| 36 | import org.jclouds.blobstore.strategy.GetBlobsInListStrategy; |
| 37 | import org.jclouds.blobstore.strategy.ListBlobsInContainer; |
| 38 | import org.jclouds.http.handlers.BackoffLimitedRetryHandler; |
| 39 | import org.jclouds.logging.Logger; |
| 40 | |
| 41 | import com.google.common.base.Function; |
| 42 | import com.google.inject.Inject; |
| 43 | |
| 44 | /** |
| 45 | * Retrieves all blobs in the blobstore under the current path, by the most efficient means |
| 46 | * possible. |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class GetAllBlobsInListAndRetryOnFailure implements GetBlobsInListStrategy { |
| 52 | |
| 53 | protected final ListBlobsInContainer getAllBlobMetadata; |
| 54 | protected final BackoffLimitedRetryHandler retryHandler; |
| 55 | protected final AsyncBlobStore ablobstore; |
| 56 | protected final ExecutorService userExecutor; |
| 57 | @Resource |
| 58 | @Named(BlobStoreConstants.BLOBSTORE_LOGGER) |
| 59 | protected Logger logger = Logger.NULL; |
| 60 | /** |
| 61 | * maximum duration of an blob Request |
| 62 | */ |
| 63 | @Inject(optional = true) |
| 64 | @Named(Constants.PROPERTY_REQUEST_TIMEOUT) |
| 65 | protected Long maxTime; |
| 66 | |
| 67 | @Inject |
| 68 | GetAllBlobsInListAndRetryOnFailure(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, |
| 69 | ListBlobsInContainer getAllBlobMetadata, AsyncBlobStore ablobstore, BackoffLimitedRetryHandler retryHandler) { |
| 70 | this.userExecutor = userExecutor; |
| 71 | this.ablobstore = ablobstore; |
| 72 | this.getAllBlobMetadata = getAllBlobMetadata; |
| 73 | this.retryHandler = retryHandler; |
| 74 | } |
| 75 | |
| 76 | public Iterable<Blob> execute(final String container, ListContainerOptions options) { |
| 77 | Iterable<? extends BlobMetadata> list = getAllBlobMetadata.execute(container, options); |
| 78 | return transformParallel(list, new Function<BlobMetadata, Future<Blob>>() { |
| 79 | |
| 80 | @Override |
| 81 | public Future<Blob> apply(BlobMetadata from) { |
| 82 | return ablobstore.getBlob(container, from.getName()); |
| 83 | } |
| 84 | |
| 85 | }, userExecutor, maxTime, logger, String.format("getting from containerName: %s", container), retryHandler, 3); |
| 86 | |
| 87 | } |
| 88 | } |