| 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.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.NoSuchElementException; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.vcloud.domain.CatalogItem; |
| 31 | import org.jclouds.vcloud.domain.ReferenceType; |
| 32 | import org.jclouds.vcloud.endpoints.Catalog; |
| 33 | import org.jclouds.vcloud.endpoints.Org; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.base.Supplier; |
| 37 | import com.google.common.collect.Iterables; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class OrgNameCatalogNameVAppTemplateNameToEndpoint implements Function<Object, URI> { |
| 45 | private final Supplier<Map<String, Map<String, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>>> orgCatalogItemMap; |
| 46 | private final ReferenceType defaultOrg; |
| 47 | private final ReferenceType defaultCatalog; |
| 48 | |
| 49 | @Inject |
| 50 | public OrgNameCatalogNameVAppTemplateNameToEndpoint( |
| 51 | Supplier<Map<String, Map<String, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>>> orgCatalogItemMap, |
| 52 | @Org ReferenceType defaultOrg, @Catalog ReferenceType defaultCatalog) { |
| 53 | this.orgCatalogItemMap = orgCatalogItemMap; |
| 54 | this.defaultOrg = defaultOrg; |
| 55 | this.defaultCatalog = defaultCatalog; |
| 56 | } |
| 57 | |
| 58 | @SuppressWarnings("unchecked") |
| 59 | public URI apply(Object from) { |
| 60 | Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args"); |
| 61 | Object org = Iterables.get(orgCatalog, 0); |
| 62 | Object catalog = Iterables.get(orgCatalog, 1); |
| 63 | Object catalogItem = Iterables.get(orgCatalog, 2); |
| 64 | if (org == null) |
| 65 | org = defaultOrg.getName(); |
| 66 | if (catalog == null) |
| 67 | catalog = defaultCatalog.getName(); |
| 68 | Map<String, Map<String, Map<String, ? extends CatalogItem>>> orgCatalogItemMap = this.orgCatalogItemMap.get(); |
| 69 | |
| 70 | if (!orgCatalogItemMap.containsKey(org)) |
| 71 | throw new NoSuchElementException("org: " + org + " not found in " + orgCatalogItemMap.keySet()); |
| 72 | Map<String, Map<String, ? extends CatalogItem>> catalogs = orgCatalogItemMap.get(org); |
| 73 | |
| 74 | if (!catalogs.containsKey(catalog)) |
| 75 | throw new NoSuchElementException("catalog: " + org + "/" + catalog + " not found in " + catalogs.keySet()); |
| 76 | Map<String, ? extends CatalogItem> catalogMap = catalogs.get(catalog); |
| 77 | |
| 78 | if (!catalogMap.containsKey(catalogItem)) |
| 79 | throw new NoSuchElementException("item: " + org + "/" + catalog + "/" + catalogItem + " not found in " |
| 80 | + catalogMap.keySet()); |
| 81 | CatalogItem item = catalogMap.get(catalogItem); |
| 82 | |
| 83 | return checkNotNull(item.getEntity(), "item: " + org + "/" + catalog + "/" + catalogItem + " has no entity") |
| 84 | .getHref(); |
| 85 | } |
| 86 | |
| 87 | } |