| 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.config; |
| 20 | |
| 21 | import static org.jclouds.Constants.PROPERTY_IDENTITY; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.annotation.Resource; |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.logging.Logger; |
| 32 | import org.jclouds.vcloud.domain.Catalog; |
| 33 | import org.jclouds.vcloud.domain.ReferenceType; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.base.Predicate; |
| 37 | import com.google.common.base.Predicates; |
| 38 | import com.google.common.base.Supplier; |
| 39 | import com.google.common.base.Suppliers; |
| 40 | import com.google.common.collect.ImmutableMap; |
| 41 | import com.google.common.collect.ImmutableMap.Builder; |
| 42 | import com.google.inject.AbstractModule; |
| 43 | import com.google.inject.Injector; |
| 44 | import com.google.inject.Provides; |
| 45 | |
| 46 | /** |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | public class DefaultVCloudReferencesModule extends AbstractModule { |
| 51 | |
| 52 | @Override |
| 53 | protected void configure() { |
| 54 | |
| 55 | } |
| 56 | |
| 57 | @Provides |
| 58 | @org.jclouds.vcloud.endpoints.Org |
| 59 | @Singleton |
| 60 | protected ReferenceType provideDefaultOrg(DefaultOrgForUser defaultOrgURIForUser, |
| 61 | @Named(PROPERTY_IDENTITY) String user) { |
| 62 | return defaultOrgURIForUser.apply(user); |
| 63 | } |
| 64 | |
| 65 | @Provides |
| 66 | @Singleton |
| 67 | @org.jclouds.vcloud.endpoints.Org |
| 68 | protected Predicate<ReferenceType> provideDefaultOrgSelector(Injector i) { |
| 69 | return Predicates.alwaysTrue(); |
| 70 | } |
| 71 | |
| 72 | @Provides |
| 73 | @org.jclouds.vcloud.endpoints.TasksList |
| 74 | @Singleton |
| 75 | protected ReferenceType provideDefaultTasksList(DefaultTasksListForOrg defaultTasksListURIForOrg, |
| 76 | @org.jclouds.vcloud.endpoints.Org ReferenceType defaultOrg) { |
| 77 | return defaultTasksListURIForOrg.apply(defaultOrg); |
| 78 | } |
| 79 | |
| 80 | @Provides |
| 81 | @org.jclouds.vcloud.endpoints.Catalog |
| 82 | @Singleton |
| 83 | protected ReferenceType provideDefaultCatalog(DefaultCatalogForOrg defaultCatalogURIForOrg, |
| 84 | @org.jclouds.vcloud.endpoints.Org ReferenceType defaultOrg) { |
| 85 | return defaultCatalogURIForOrg.apply(defaultOrg); |
| 86 | } |
| 87 | |
| 88 | @Provides |
| 89 | @Singleton |
| 90 | @org.jclouds.vcloud.endpoints.Catalog |
| 91 | protected Predicate<ReferenceType> provideDefaultCatalogSelector(Injector i) { |
| 92 | return i.getInstance(WriteableCatalog.class); |
| 93 | } |
| 94 | |
| 95 | @Provides |
| 96 | @Singleton |
| 97 | protected Supplier<Map<URI, ? extends org.jclouds.vcloud.domain.Catalog>> provideCatalogsById( |
| 98 | Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>> supplier) { |
| 99 | return Suppliers |
| 100 | .compose( |
| 101 | new Function<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>, Map<URI, ? extends org.jclouds.vcloud.domain.Catalog>>() { |
| 102 | |
| 103 | @Override |
| 104 | public Map<URI, ? extends Catalog> apply(Map<String, Map<String, ? extends Catalog>> arg0) { |
| 105 | Builder<URI, Catalog> builder = ImmutableMap.<URI, Catalog> builder(); |
| 106 | for (Map<String, ? extends Catalog> v1 : arg0.values()) { |
| 107 | for (Catalog v2 : v1.values()) { |
| 108 | builder.put(v2.getHref(), v2); |
| 109 | } |
| 110 | } |
| 111 | return builder.build(); |
| 112 | } |
| 113 | |
| 114 | }, supplier); |
| 115 | } |
| 116 | |
| 117 | @Singleton |
| 118 | public static class WriteableCatalog implements Predicate<ReferenceType> { |
| 119 | |
| 120 | @Resource |
| 121 | protected Logger logger = Logger.NULL; |
| 122 | |
| 123 | private final Supplier<Map<URI, ? extends org.jclouds.vcloud.domain.Catalog>> catalogsByIdSupplier; |
| 124 | |
| 125 | @Inject |
| 126 | public WriteableCatalog(Supplier<Map<URI, ? extends org.jclouds.vcloud.domain.Catalog>> catalogsByIdSupplier) { |
| 127 | this.catalogsByIdSupplier = catalogsByIdSupplier; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean apply(ReferenceType arg0) { |
| 132 | // TODO: this is inefficient, calculating the index each time, but |
| 133 | // shouldn't be added to constructor as the supplier is an expensive |
| 134 | // call |
| 135 | Map<URI, ? extends Catalog> index = catalogsByIdSupplier.get(); |
| 136 | Catalog catalog = index.get(arg0.getHref()); |
| 137 | if (catalog == null) { |
| 138 | if (logger.isTraceEnabled()) |
| 139 | logger.trace("didn't find catalog %s", arg0); |
| 140 | return false; |
| 141 | } else |
| 142 | return !catalog.isReadOnly(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Provides |
| 147 | @org.jclouds.vcloud.endpoints.VDC |
| 148 | @Singleton |
| 149 | protected ReferenceType provideDefaultVDC(DefaultVDCForOrg defaultVDCURIForOrg, |
| 150 | @org.jclouds.vcloud.endpoints.Org ReferenceType defaultOrg) { |
| 151 | return defaultVDCURIForOrg.apply(defaultOrg); |
| 152 | } |
| 153 | |
| 154 | @Provides |
| 155 | @Singleton |
| 156 | @org.jclouds.vcloud.endpoints.VDC |
| 157 | protected Predicate<ReferenceType> provideDefaultVDCSelector(Injector i) { |
| 158 | return Predicates.alwaysTrue(); |
| 159 | } |
| 160 | |
| 161 | @Provides |
| 162 | @org.jclouds.vcloud.endpoints.Network |
| 163 | @Singleton |
| 164 | protected ReferenceType provideDefaultNetwork(DefaultNetworkForVDC defaultNetworkURIForVDC, |
| 165 | @org.jclouds.vcloud.endpoints.VDC ReferenceType defaultVDC) { |
| 166 | return defaultNetworkURIForVDC.apply(defaultVDC); |
| 167 | } |
| 168 | |
| 169 | @Provides |
| 170 | @Singleton |
| 171 | @org.jclouds.vcloud.endpoints.Network |
| 172 | protected Predicate<ReferenceType> provideDefaultNetworkSelector(Injector i) { |
| 173 | return Predicates.alwaysTrue(); |
| 174 | } |
| 175 | } |