| 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 java.util.List; |
| 22 | |
| 23 | import javax.inject.Singleton; |
| 24 | |
| 25 | import org.jclouds.blobstore.BlobStore; |
| 26 | import org.jclouds.blobstore.domain.PageSet; |
| 27 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 28 | import org.jclouds.blobstore.internal.BlobRuntimeException; |
| 29 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 30 | import org.jclouds.blobstore.options.ListContainerOptions.ImmutableListContainerOptions; |
| 31 | import org.jclouds.blobstore.strategy.ListContainerStrategy; |
| 32 | |
| 33 | import com.google.common.base.Throwables; |
| 34 | import com.google.common.collect.Iterables; |
| 35 | import com.google.common.collect.Lists; |
| 36 | import com.google.inject.Inject; |
| 37 | |
| 38 | /** |
| 39 | * Retrieves all metadata in the blobstore by the most efficient means possible. |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class ConcatenateContainerLists implements ListContainerStrategy { |
| 45 | |
| 46 | protected final BlobStore connection; |
| 47 | |
| 48 | @Inject |
| 49 | public ConcatenateContainerLists(BlobStore connection) { |
| 50 | this.connection = connection; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Iterable<? extends StorageMetadata> execute(String container, ListContainerOptions options) { |
| 55 | try { |
| 56 | boolean truncated = true; |
| 57 | List<PageSet<? extends StorageMetadata>> listings = Lists.newArrayList(); |
| 58 | while (truncated) { |
| 59 | PageSet<? extends StorageMetadata> listing = connection.list(container, options); |
| 60 | truncated = listing.getNextMarker() != null; |
| 61 | if (truncated) { |
| 62 | options = options instanceof ImmutableListContainerOptions ? options.clone() |
| 63 | .afterMarker(listing.getNextMarker()) : options.afterMarker(listing |
| 64 | .getNextMarker()); |
| 65 | } |
| 66 | listings.add(listing); |
| 67 | } |
| 68 | return Iterables.concat(listings); |
| 69 | } catch (Exception e) { |
| 70 | Throwables.propagateIfPossible(e, BlobRuntimeException.class); |
| 71 | throw new BlobRuntimeException("Error getting resource metadata in container: " |
| 72 | + container, e); |
| 73 | } |
| 74 | } |
| 75 | } |