1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.rest.config; |
20 | |
21 | import java.util.Map; |
22 | import java.util.concurrent.ConcurrentMap; |
23 | |
24 | import javax.inject.Named; |
25 | import javax.inject.Singleton; |
26 | |
27 | import org.jclouds.http.RequiresHttp; |
28 | import org.jclouds.internal.ClassMethodArgs; |
29 | import org.jclouds.rest.ConfiguresRestClient; |
30 | import org.jclouds.rest.RestContext; |
31 | import org.jclouds.rest.internal.RestContextImpl; |
32 | |
33 | import com.google.common.collect.ImmutableMap; |
34 | import com.google.common.collect.MapMaker; |
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 | ConcurrentMap<ClassMethodArgs, Object> provideSyncDelegateMap( |
129 | CreateClientForCaller createClientForCaller) { |
130 | createClientForCaller.sync2Async = delegates; |
131 | return new MapMaker().makeComputingMap(createClientForCaller); |
132 | } |
133 | |
134 | } |