| 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 org.jclouds.concurrent.FutureIterables.transformParallel; |
| 23 | import static org.jclouds.util.Throwables2.propagateOrNull; |
| 24 | |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Future; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.Constants; |
| 34 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 35 | import org.jclouds.concurrent.ExceptionParsingListenableFuture; |
| 36 | import org.jclouds.concurrent.Futures; |
| 37 | import org.jclouds.logging.Logger; |
| 38 | import org.jclouds.rest.AuthorizationException; |
| 39 | import org.jclouds.vcloud.VCloudAsyncClient; |
| 40 | import org.jclouds.vcloud.VCloudMediaType; |
| 41 | import org.jclouds.vcloud.domain.CatalogItem; |
| 42 | import org.jclouds.vcloud.domain.VAppTemplate; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.base.Predicate; |
| 46 | |
| 47 | /** |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class VAppTemplatesForCatalogItems implements |
| 52 | Function<Iterable<? extends CatalogItem>, Iterable<? extends VAppTemplate>> { |
| 53 | @Resource |
| 54 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 55 | public Logger logger = Logger.NULL; |
| 56 | private final VCloudAsyncClient aclient; |
| 57 | private final ExecutorService executor; |
| 58 | private final ReturnNullOnAuthorizationException returnNullOnAuthorizationException; |
| 59 | |
| 60 | @Singleton |
| 61 | static class ReturnNullOnAuthorizationException implements Function<Exception, VAppTemplate> { |
| 62 | |
| 63 | public VAppTemplate apply(Exception from) { |
| 64 | if (from instanceof AuthorizationException) { |
| 65 | return null; |
| 66 | } |
| 67 | return propagateOrNull(from); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Inject |
| 72 | VAppTemplatesForCatalogItems(VCloudAsyncClient aclient, |
| 73 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, |
| 74 | ReturnNullOnAuthorizationException returnNullOnAuthorizationException) { |
| 75 | this.aclient = aclient; |
| 76 | this.executor = executor; |
| 77 | this.returnNullOnAuthorizationException = returnNullOnAuthorizationException; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Iterable<? extends VAppTemplate> apply(Iterable<? extends CatalogItem> from) { |
| 82 | return transformParallel(filter(from, new Predicate<CatalogItem>() { |
| 83 | |
| 84 | @Override |
| 85 | public boolean apply(CatalogItem input) { |
| 86 | return input.getEntity().getType().equals(VCloudMediaType.VAPPTEMPLATE_XML); |
| 87 | } |
| 88 | |
| 89 | }), new Function<CatalogItem, Future<VAppTemplate>>() { |
| 90 | |
| 91 | @SuppressWarnings("unchecked") |
| 92 | @Override |
| 93 | public Future<VAppTemplate> apply(CatalogItem from) { |
| 94 | return new ExceptionParsingListenableFuture<VAppTemplate>(Futures.makeListenable( |
| 95 | (Future<VAppTemplate>) VCloudAsyncClient.class.cast(aclient).getVAppTemplateClient().getVAppTemplate( |
| 96 | from.getEntity().getHref()), executor), returnNullOnAuthorizationException); |
| 97 | } |
| 98 | |
| 99 | }, executor, null, logger, "vappTemplates in"); |
| 100 | } |
| 101 | |
| 102 | } |