| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.trmk.vcloud_0_8.compute.suppliers; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.concat; |
| 23 | import static com.google.common.collect.Sets.newLinkedHashSet; |
| 24 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | import java.util.concurrent.Callable; |
| 29 | import java.util.concurrent.ExecutorService; |
| 30 | import java.util.concurrent.Future; |
| 31 | |
| 32 | import javax.annotation.Resource; |
| 33 | import javax.inject.Inject; |
| 34 | import javax.inject.Named; |
| 35 | import javax.inject.Singleton; |
| 36 | |
| 37 | import org.jclouds.Constants; |
| 38 | import org.jclouds.compute.domain.Image; |
| 39 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 40 | import org.jclouds.logging.Logger; |
| 41 | import org.jclouds.trmk.vcloud_0_8.domain.Org; |
| 42 | |
| 43 | import com.google.common.base.Function; |
| 44 | import com.google.common.base.Supplier; |
| 45 | |
| 46 | /** |
| 47 | * @author Adrian Cole |
| 48 | */ |
| 49 | @Singleton |
| 50 | public class VCloudImageSupplier implements Supplier<Set<? extends Image>> { |
| 51 | |
| 52 | @Resource |
| 53 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 54 | public Logger logger = Logger.NULL; |
| 55 | |
| 56 | private final Supplier<Map<String, ? extends Org>> orgMap; |
| 57 | private final Function<Org, Iterable<? extends Image>> imagesInOrg; |
| 58 | private final ExecutorService executor; |
| 59 | |
| 60 | @Inject |
| 61 | VCloudImageSupplier(Supplier<Map<String, ? extends Org>> orgMap, |
| 62 | Function<Org, Iterable<? extends Image>> imagesInOrg, |
| 63 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) { |
| 64 | this.orgMap = checkNotNull(orgMap, "orgMap"); |
| 65 | this.imagesInOrg = checkNotNull(imagesInOrg, "imagesInOrg"); |
| 66 | this.executor = checkNotNull(executor, "executor"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Set<? extends Image> get() { |
| 71 | Iterable<? extends Org> orgs = checkNotNull(orgMap.get().values(), "orgs"); |
| 72 | Iterable<Iterable<? extends Image>> images = transformParallel(orgs, |
| 73 | new Function<Org, Future<Iterable<? extends Image>>>() { |
| 74 | |
| 75 | @Override |
| 76 | public Future<Iterable<? extends Image>> apply(final Org from) { |
| 77 | checkNotNull(from, "org"); |
| 78 | return executor.submit(new Callable<Iterable<? extends Image>>() { |
| 79 | |
| 80 | @Override |
| 81 | public Iterable<? extends Image> call() throws Exception { |
| 82 | return imagesInOrg.apply(from); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return "imagesInOrg(" + from.getHref() + ")"; |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | }, executor, null, logger, "images in " + orgs); |
| 93 | return newLinkedHashSet(concat(images)); |
| 94 | } |
| 95 | } |