| 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.vcloud.functions; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.filter; |
| 22 | import static com.google.common.collect.Iterables.transform; |
| 23 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 24 | |
| 25 | import java.net.URI; |
| 26 | import java.util.concurrent.ExecutorService; |
| 27 | import java.util.concurrent.Future; |
| 28 | |
| 29 | import javax.annotation.Resource; |
| 30 | import javax.inject.Inject; |
| 31 | import javax.inject.Named; |
| 32 | import javax.inject.Singleton; |
| 33 | |
| 34 | import org.jclouds.Constants; |
| 35 | import org.jclouds.domain.Location; |
| 36 | import org.jclouds.domain.LocationScope; |
| 37 | import org.jclouds.logging.Logger; |
| 38 | import org.jclouds.vcloud.VCloudAsyncClient; |
| 39 | import org.jclouds.vcloud.domain.Org; |
| 40 | |
| 41 | import com.google.common.base.Function; |
| 42 | import com.google.common.base.Predicate; |
| 43 | import com.google.common.collect.Sets; |
| 44 | |
| 45 | /** |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | @Singleton |
| 49 | public class OrgsForLocations implements Function<Iterable<? extends Location>, Iterable<? extends Org>> { |
| 50 | @Resource |
| 51 | public Logger logger = Logger.NULL; |
| 52 | private final VCloudAsyncClient aclient; |
| 53 | private final ExecutorService executor; |
| 54 | |
| 55 | @Inject |
| 56 | OrgsForLocations(VCloudAsyncClient aclient, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) { |
| 57 | this.aclient = aclient; |
| 58 | this.executor = executor; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Zones are assignable, but we want regions. so we look for zones, whose |
| 63 | * parent is region. then, we use a set to extract the unique set. |
| 64 | */ |
| 65 | @Override |
| 66 | public Iterable<? extends Org> apply(Iterable<? extends Location> from) { |
| 67 | |
| 68 | return transformParallel(Sets.newLinkedHashSet(transform(filter(from, new Predicate<Location>() { |
| 69 | |
| 70 | @Override |
| 71 | public boolean apply(Location input) { |
| 72 | return input.getScope() == LocationScope.ZONE; |
| 73 | } |
| 74 | |
| 75 | }), new Function<Location, URI>() { |
| 76 | |
| 77 | @Override |
| 78 | public URI apply(Location from) { |
| 79 | return URI.create(from.getParent().getId()); |
| 80 | } |
| 81 | |
| 82 | })), new Function<URI, Future<Org>>() { |
| 83 | |
| 84 | @SuppressWarnings("unchecked") |
| 85 | @Override |
| 86 | public Future<Org> apply(URI from) { |
| 87 | return (Future<Org>) aclient.getOrg(from); |
| 88 | } |
| 89 | |
| 90 | }, executor, null, logger, "organizations for uris"); |
| 91 | } |
| 92 | |
| 93 | } |