| 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.aws.config; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import javax.inject.Singleton; |
| 25 | |
| 26 | import org.jclouds.http.RequiresHttp; |
| 27 | import org.jclouds.location.Region; |
| 28 | import org.jclouds.location.Zone; |
| 29 | import org.jclouds.location.config.ProvideZonesViaProperties; |
| 30 | import org.jclouds.rest.ConfiguresRestClient; |
| 31 | |
| 32 | import com.google.common.base.Function; |
| 33 | import com.google.common.collect.Maps; |
| 34 | import com.google.inject.Provides; |
| 35 | import com.google.inject.Scopes; |
| 36 | import com.google.inject.TypeLiteral; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @ConfiguresRestClient |
| 43 | @RequiresHttp |
| 44 | public class WithZonesFormSigningRestClientModule<S, A> extends FormSigningRestClientModule<S, A> { |
| 45 | |
| 46 | public WithZonesFormSigningRestClientModule(Class<S> syncClientType, Class<A> asyncClientType, |
| 47 | Map<Class<?>, Class<?>> delegates) { |
| 48 | super(syncClientType, asyncClientType, delegates); |
| 49 | } |
| 50 | |
| 51 | public WithZonesFormSigningRestClientModule(Class<S> syncClientType, Class<A> asyncClientType) { |
| 52 | super(syncClientType, asyncClientType); |
| 53 | } |
| 54 | |
| 55 | protected void bindZonesToProvider() { |
| 56 | bindZonesToProvider(ProvideZonesViaProperties.class); |
| 57 | } |
| 58 | |
| 59 | protected void bindZonesToProvider(Class<? extends javax.inject.Provider<Map<String, String>>> providerClass) { |
| 60 | bind(new TypeLiteral<Map<String, String>>() { |
| 61 | }).annotatedWith(Zone.class).toProvider(providerClass).in(Scopes.SINGLETON); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | protected void configure() { |
| 66 | super.configure(); |
| 67 | bindZonesToProvider(); |
| 68 | } |
| 69 | |
| 70 | @Provides |
| 71 | @Singleton |
| 72 | @Zone |
| 73 | protected Map<String, URI> provideZones(@Region final Map<String, URI> regionToEndpoint, |
| 74 | @Zone Map<String, String> availabilityZoneToRegion) { |
| 75 | return Maps.transformValues(availabilityZoneToRegion, new Function<String, URI>() { |
| 76 | |
| 77 | @Override |
| 78 | public URI apply(String from) { |
| 79 | |
| 80 | return regionToEndpoint.get(from); |
| 81 | } |
| 82 | |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | } |