View Javadoc

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 com.google.common.collect.Iterables.concat;
22  import static com.google.common.collect.Iterables.filter;
23  import static com.google.common.collect.Iterables.transform;
24  import static com.google.common.collect.Lists.newArrayList;
25  import static com.google.common.collect.Sets.newLinkedHashSet;
26  
27  import java.util.List;
28  import java.util.Set;
29  
30  import javax.inject.Singleton;
31  
32  import org.jclouds.blobstore.domain.BlobMetadata;
33  import org.jclouds.blobstore.domain.StorageMetadata;
34  import org.jclouds.blobstore.domain.StorageType;
35  import org.jclouds.blobstore.options.ListContainerOptions;
36  import org.jclouds.blobstore.strategy.ListBlobsInContainer;
37  import org.jclouds.blobstore.strategy.ListContainerStrategy;
38  
39  import com.google.common.base.Function;
40  import com.google.common.base.Predicate;
41  import com.google.inject.Inject;
42  
43  /**
44   * Retrieves all blobs in the blobstore by the most efficient means possible.
45   * 
46   * @author Adrian Cole
47   */
48  @Singleton
49  public class ListContainerAndRecurseThroughFolders implements ListBlobsInContainer {
50  
51     protected final ListContainerStrategy lister;
52  
53     @Inject
54     ListContainerAndRecurseThroughFolders(ListContainerStrategy lister) {
55        this.lister = lister;
56     }
57  
58     @Override
59     public Set<? extends BlobMetadata> execute(final String containerName, final ListContainerOptions options) {
60        final List<Iterable<? extends BlobMetadata>> lists = newArrayList();
61        Iterable<? extends StorageMetadata> pwdList = lister.execute(containerName, options);
62        for (StorageMetadata md : filter(pwdList, new Predicate<StorageMetadata>() {
63           @Override
64           public boolean apply(StorageMetadata input) {
65              return (input.getType() == StorageType.FOLDER || input.getType() == StorageType.RELATIVE_PATH)
66                       && options.isRecursive();
67           }
68        })) {
69           String directory = (options.getDir() != null) ? options.getDir() + "/" + md.getName() : md.getName();
70           lists.add(execute(containerName, options.clone().inDirectory(directory)));
71        }
72        lists.add(transform(filter(pwdList, new Predicate<StorageMetadata>() {
73           @Override
74           public boolean apply(StorageMetadata input) {
75              return input.getType() == StorageType.BLOB;
76           }
77        }), new Function<StorageMetadata, BlobMetadata>() {
78           @Override
79           public BlobMetadata apply(StorageMetadata from) {
80              return (BlobMetadata) from;
81           }
82        }));
83        return newLinkedHashSet(concat(lists));
84     }
85  }