| 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.rest.config; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import javax.inject.Named; |
| 24 | import javax.inject.Singleton; |
| 25 | |
| 26 | import org.jclouds.http.RequiresHttp; |
| 27 | import org.jclouds.internal.ClassMethodArgs; |
| 28 | import org.jclouds.rest.ConfiguresRestClient; |
| 29 | import org.jclouds.rest.RestContext; |
| 30 | import org.jclouds.rest.internal.RestContextImpl; |
| 31 | |
| 32 | import com.google.common.cache.Cache; |
| 33 | import com.google.common.cache.CacheBuilder; |
| 34 | import com.google.common.collect.ImmutableMap; |
| 35 | import com.google.inject.AbstractModule; |
| 36 | import com.google.inject.Provides; |
| 37 | import com.google.inject.Scopes; |
| 38 | import com.google.inject.TypeLiteral; |
| 39 | import com.google.inject.util.Types; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | @ConfiguresRestClient |
| 46 | @RequiresHttp |
| 47 | public class RestClientModule<S, A> extends AbstractModule { |
| 48 | |
| 49 | protected final Class<A> asyncClientType; |
| 50 | protected final Class<S> syncClientType; |
| 51 | protected final Map<Class<?>, Class<?>> delegates; |
| 52 | |
| 53 | public RestClientModule(Class<S> syncClientType, Class<A> asyncClientType, |
| 54 | Map<Class<?>, Class<?>> delegates) { |
| 55 | this.asyncClientType = asyncClientType; |
| 56 | this.syncClientType = syncClientType; |
| 57 | this.delegates = delegates; |
| 58 | } |
| 59 | |
| 60 | public RestClientModule(Class<S> syncClientType, Class<A> asyncClientType) { |
| 61 | this(syncClientType, asyncClientType, ImmutableMap |
| 62 | .<Class<?>, Class<?>> of(syncClientType, asyncClientType)); |
| 63 | } |
| 64 | |
| 65 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 66 | @Override |
| 67 | protected void configure() { |
| 68 | // Ensures the restcontext can be looked up without generic types. |
| 69 | bind(new TypeLiteral<RestContext>() { |
| 70 | }).to( |
| 71 | (TypeLiteral) TypeLiteral.get(Types.newParameterizedType( |
| 72 | RestContextImpl.class, syncClientType, asyncClientType))).in( |
| 73 | Scopes.SINGLETON); |
| 74 | bindAsyncClient(); |
| 75 | bindClient(); |
| 76 | bindErrorHandlers(); |
| 77 | bindRetryHandlers(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * overrides this to change the default retry handlers for the http engine |
| 82 | * |
| 83 | * ex. |
| 84 | * |
| 85 | * <pre> |
| 86 | * bind(HttpRetryHandler.class).annotatedWith(Redirection.class).to( |
| 87 | * AWSRedirectionRetryHandler.class); |
| 88 | * bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to( |
| 89 | * AWSClientErrorRetryHandler.class); |
| 90 | * </pre> |
| 91 | * |
| 92 | */ |
| 93 | protected void bindRetryHandlers() { |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * overrides this to change the default error handlers for the http engine |
| 98 | * |
| 99 | * ex. |
| 100 | * |
| 101 | * <pre> |
| 102 | * bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to( |
| 103 | * ParseAWSErrorFromXmlContent.class); |
| 104 | * bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to( |
| 105 | * ParseAWSErrorFromXmlContent.class); |
| 106 | * bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to( |
| 107 | * ParseAWSErrorFromXmlContent.class); |
| 108 | * </pre> |
| 109 | * |
| 110 | * |
| 111 | */ |
| 112 | protected void bindErrorHandlers() { |
| 113 | } |
| 114 | |
| 115 | protected void bindAsyncClient() { |
| 116 | BinderUtils.bindAsyncClient(binder(), asyncClientType); |
| 117 | } |
| 118 | |
| 119 | protected void bindClient() { |
| 120 | BinderUtils.bindClient(binder(), syncClientType, asyncClientType, |
| 121 | delegates); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | @Provides |
| 126 | @Singleton |
| 127 | @Named("sync") |
| 128 | Cache<ClassMethodArgs, Object> provideSyncDelegateMap( |
| 129 | CreateClientForCaller createClientForCaller) { |
| 130 | createClientForCaller.sync2Async = delegates; |
| 131 | return CacheBuilder.newBuilder().build(createClientForCaller); |
| 132 | } |
| 133 | |
| 134 | } |