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.internal; |
20 | |
21 | import java.io.IOException; |
22 | import java.net.URI; |
23 | import java.util.Map; |
24 | import java.util.Set; |
25 | |
26 | import javax.annotation.Resource; |
27 | import javax.inject.Inject; |
28 | |
29 | import org.jclouds.domain.Credentials; |
30 | import org.jclouds.domain.Location; |
31 | import org.jclouds.domain.LocationScope; |
32 | import org.jclouds.lifecycle.Closer; |
33 | import org.jclouds.location.Iso3166; |
34 | import org.jclouds.location.Provider; |
35 | import org.jclouds.logging.Logger; |
36 | import org.jclouds.rest.RestContext; |
37 | import org.jclouds.rest.Utils; |
38 | import org.jclouds.rest.annotations.ApiVersion; |
39 | import org.jclouds.rest.annotations.Identity; |
40 | |
41 | import com.google.common.collect.ImmutableMap; |
42 | import com.google.inject.Injector; |
43 | import com.google.inject.Key; |
44 | import com.google.inject.Singleton; |
45 | import com.google.inject.TypeLiteral; |
46 | |
47 | /** |
48 | * @author Adrian Cole |
49 | */ |
50 | @Singleton |
51 | public class RestContextImpl<S, A> implements RestContext<S, A> { |
52 | |
53 | @Resource |
54 | private Logger logger = Logger.NULL; |
55 | private final A asyncApi; |
56 | private final S syncApi; |
57 | private final Closer closer; |
58 | private final URI endpoint; |
59 | private final String identity; |
60 | private final String provider; |
61 | private final String apiVersion; |
62 | private final Utils utils; |
63 | private final Map<String, Credentials> credentialStore; |
64 | private final Set<String> iso3166Codes; |
65 | |
66 | @Inject |
67 | protected RestContextImpl(Closer closer, Map<String, Credentials> credentialStore, Utils utils, Injector injector, |
68 | TypeLiteral<S> syncApi, TypeLiteral<A> asyncApi, @Provider URI endpoint, @Provider String provider, |
69 | @Identity String identity, @ApiVersion String apiVersion, @Iso3166 Set<String> iso3166Codes) { |
70 | this.credentialStore = credentialStore; |
71 | this.utils = utils; |
72 | this.asyncApi = injector.getInstance(Key.get(asyncApi)); |
73 | this.syncApi = injector.getInstance(Key.get(syncApi)); |
74 | this.closer = closer; |
75 | this.endpoint = endpoint; |
76 | this.identity = identity; |
77 | this.provider = provider; |
78 | this.apiVersion = apiVersion; |
79 | this.iso3166Codes = iso3166Codes; |
80 | } |
81 | |
82 | /** |
83 | * {@inheritDoc} |
84 | * |
85 | * @see Closer |
86 | */ |
87 | @Override |
88 | public void close() { |
89 | try { |
90 | closer.close(); |
91 | } catch (IOException e) { |
92 | logger.error(e, "error closing content"); |
93 | } |
94 | } |
95 | |
96 | @Override |
97 | public String getIdentity() { |
98 | return identity; |
99 | } |
100 | |
101 | @Override |
102 | public A getAsyncApi() { |
103 | return asyncApi; |
104 | } |
105 | |
106 | @Override |
107 | public S getApi() { |
108 | return syncApi; |
109 | } |
110 | |
111 | @Override |
112 | public URI getEndpoint() { |
113 | return endpoint; |
114 | } |
115 | |
116 | @Override |
117 | public Utils getUtils() { |
118 | return utils(); |
119 | } |
120 | |
121 | @Override |
122 | public Utils utils() { |
123 | return utils; |
124 | } |
125 | |
126 | @Override |
127 | public String getApiVersion() { |
128 | return apiVersion; |
129 | } |
130 | |
131 | @Override |
132 | public int hashCode() { |
133 | final int prime = 31; |
134 | int result = 1; |
135 | result = prime * result + ((apiVersion == null) ? 0 : apiVersion.hashCode()); |
136 | result = prime * result + ((endpoint == null) ? 0 : endpoint.hashCode()); |
137 | result = prime * result + ((identity == null) ? 0 : identity.hashCode()); |
138 | result = prime * result + ((provider == null) ? 0 : provider.hashCode()); |
139 | return result; |
140 | } |
141 | |
142 | @Override |
143 | public boolean equals(Object obj) { |
144 | if (this == obj) |
145 | return true; |
146 | if (obj == null) |
147 | return false; |
148 | if (getClass() != obj.getClass()) |
149 | return false; |
150 | RestContextImpl<?, ?> other = (RestContextImpl<?, ?>) obj; |
151 | if (apiVersion == null) { |
152 | if (other.apiVersion != null) |
153 | return false; |
154 | } else if (!apiVersion.equals(other.apiVersion)) |
155 | return false; |
156 | if (endpoint == null) { |
157 | if (other.endpoint != null) |
158 | return false; |
159 | } else if (!endpoint.equals(other.endpoint)) |
160 | return false; |
161 | if (identity == null) { |
162 | if (other.identity != null) |
163 | return false; |
164 | } else if (!identity.equals(other.identity)) |
165 | return false; |
166 | if (provider == null) { |
167 | if (other.provider != null) |
168 | return false; |
169 | } else if (!provider.equals(other.provider)) |
170 | return false; |
171 | return true; |
172 | } |
173 | |
174 | @Override |
175 | public String toString() { |
176 | return " [id=" + provider + ", endpoint=" + endpoint + ", apiVersion=" + apiVersion + ", identity=" + identity |
177 | + ", iso3166Codes=" + iso3166Codes + "]"; |
178 | } |
179 | |
180 | @Override |
181 | public Map<String, Credentials> getCredentialStore() { |
182 | return credentialStore; |
183 | } |
184 | |
185 | @Override |
186 | public Map<String, Credentials> credentialStore() { |
187 | return credentialStore; |
188 | } |
189 | |
190 | @Override |
191 | public String getDescription() { |
192 | return null; |
193 | } |
194 | |
195 | @Override |
196 | public String getId() { |
197 | return provider; |
198 | } |
199 | |
200 | @Override |
201 | public Set<String> getIso3166Codes() { |
202 | return iso3166Codes; |
203 | } |
204 | |
205 | @Override |
206 | public Map<String, Object> getMetadata() { |
207 | return ImmutableMap.<String, Object> of("endpoint", endpoint, "apiVersion", apiVersion, "identity", identity); |
208 | } |
209 | |
210 | @Override |
211 | public Location getParent() { |
212 | return null; |
213 | } |
214 | |
215 | @Override |
216 | public LocationScope getScope() { |
217 | return LocationScope.PROVIDER; |
218 | } |
219 | } |