| 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 static com.google.common.collect.Iterables.get; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import org.jclouds.javax.annotation.Nullable; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.aws.handlers.AWSClientErrorRetryHandler; |
| 31 | import org.jclouds.aws.handlers.AWSRedirectionRetryHandler; |
| 32 | import org.jclouds.aws.handlers.ParseAWSErrorFromXmlContent; |
| 33 | import org.jclouds.http.HttpErrorHandler; |
| 34 | import org.jclouds.http.HttpRetryHandler; |
| 35 | import org.jclouds.http.RequiresHttp; |
| 36 | import org.jclouds.http.annotation.ClientError; |
| 37 | import org.jclouds.http.annotation.Redirection; |
| 38 | import org.jclouds.http.annotation.ServerError; |
| 39 | import org.jclouds.location.Provider; |
| 40 | import org.jclouds.location.Region; |
| 41 | import org.jclouds.location.config.ProvideRegionToURIViaProperties; |
| 42 | import org.jclouds.logging.Logger.LoggerFactory; |
| 43 | import org.jclouds.rest.ConfiguresRestClient; |
| 44 | import org.jclouds.rest.config.RestClientModule; |
| 45 | |
| 46 | import com.google.common.collect.ImmutableBiMap; |
| 47 | import com.google.inject.Provides; |
| 48 | import com.google.inject.Scopes; |
| 49 | import com.google.inject.TypeLiteral; |
| 50 | |
| 51 | /** |
| 52 | * |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @ConfiguresRestClient |
| 56 | @RequiresHttp |
| 57 | public class AWSRestClientModule<S, A> extends RestClientModule<S, A> { |
| 58 | |
| 59 | public AWSRestClientModule(Class<S> syncClientType, Class<A> asyncClientType, Map<Class<?>, Class<?>> delegates) { |
| 60 | super(syncClientType, asyncClientType, delegates); |
| 61 | } |
| 62 | |
| 63 | public AWSRestClientModule(Class<S> syncClientType, Class<A> asyncClientType) { |
| 64 | super(syncClientType, asyncClientType); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected void bindErrorHandlers() { |
| 69 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(ParseAWSErrorFromXmlContent.class); |
| 70 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(ParseAWSErrorFromXmlContent.class); |
| 71 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(ParseAWSErrorFromXmlContent.class); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void bindRetryHandlers() { |
| 76 | bind(HttpRetryHandler.class).annotatedWith(Redirection.class).to(AWSRedirectionRetryHandler.class); |
| 77 | bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(AWSClientErrorRetryHandler.class); |
| 78 | } |
| 79 | |
| 80 | @Provides |
| 81 | @Singleton |
| 82 | @Nullable |
| 83 | @Region |
| 84 | protected String getDefaultRegion(@Provider URI uri, @Region Map<String, URI> map, LoggerFactory logFactory) { |
| 85 | String region = ImmutableBiMap.copyOf(map).inverse().get(uri); |
| 86 | if (region == null && map.size() > 0) { |
| 87 | logFactory.getLogger(getClass().getName()).warn( |
| 88 | "failed to find region for current endpoint %s in %s; choosing first: %s", uri, map, region); |
| 89 | region = get(map.keySet(), 0); |
| 90 | } |
| 91 | return region; |
| 92 | } |
| 93 | |
| 94 | protected void bindRegionsToProvider() { |
| 95 | bindRegionsToProvider(ProvideRegionToURIViaProperties.class); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void configure() { |
| 100 | super.configure(); |
| 101 | bindRegionsToProvider(); |
| 102 | } |
| 103 | |
| 104 | protected void bindRegionsToProvider(Class<? extends javax.inject.Provider<Map<String, URI>>> providerClass) { |
| 105 | bind(new TypeLiteral<Map<String, URI>>() { |
| 106 | }).annotatedWith(Region.class).toProvider(providerClass).in(Scopes.SINGLETON); |
| 107 | } |
| 108 | |
| 109 | @Provides |
| 110 | @Singleton |
| 111 | @Region |
| 112 | protected Set<String> provideRegions(@Region Map<String, URI> map) { |
| 113 | return map.keySet(); |
| 114 | } |
| 115 | |
| 116 | } |