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; |
20 | |
21 | import static org.jclouds.Constants.PROPERTY_API_VERSION; |
22 | import static org.jclouds.Constants.PROPERTY_CONNECTION_TIMEOUT; |
23 | import static org.jclouds.Constants.PROPERTY_CREDENTIAL; |
24 | import static org.jclouds.Constants.PROPERTY_ENDPOINT; |
25 | import static org.jclouds.Constants.PROPERTY_IDENTITY; |
26 | import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS; |
27 | import static org.jclouds.Constants.PROPERTY_ISO3166_CODES; |
28 | import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT; |
29 | import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST; |
30 | import static org.jclouds.Constants.PROPERTY_MAX_CONNECTION_REUSE; |
31 | import static org.jclouds.Constants.PROPERTY_MAX_REDIRECTS; |
32 | import static org.jclouds.Constants.PROPERTY_MAX_RETRIES; |
33 | import static org.jclouds.Constants.PROPERTY_MAX_SESSION_FAILURES; |
34 | import static org.jclouds.Constants.PROPERTY_PROVIDER; |
35 | import static org.jclouds.Constants.PROPERTY_PROXY_HOST; |
36 | import static org.jclouds.Constants.PROPERTY_PROXY_PASSWORD; |
37 | import static org.jclouds.Constants.PROPERTY_PROXY_PORT; |
38 | import static org.jclouds.Constants.PROPERTY_PROXY_SYSTEM; |
39 | import static org.jclouds.Constants.PROPERTY_PROXY_USER; |
40 | import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME; |
41 | import static org.jclouds.Constants.PROPERTY_RETRY_DELAY_START; |
42 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
43 | import static org.jclouds.Constants.PROPERTY_SO_TIMEOUT; |
44 | import static org.jclouds.Constants.PROPERTY_TRUST_ALL_CERTS; |
45 | import static org.jclouds.Constants.PROPERTY_USER_THREADS; |
46 | |
47 | import java.util.Properties; |
48 | |
49 | import org.jclouds.javax.annotation.Nullable; |
50 | |
51 | import com.google.common.annotations.VisibleForTesting; |
52 | import com.google.common.base.Joiner; |
53 | |
54 | /** |
55 | * Builds properties used in Http engines |
56 | * |
57 | * @author Adrian Cole, Andrew Newdigate |
58 | */ |
59 | public class PropertiesBuilder { |
60 | |
61 | /** |
62 | * @see org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME |
63 | */ |
64 | public PropertiesBuilder relaxSSLHostname(boolean relax) { |
65 | properties.setProperty(PROPERTY_RELAX_HOSTNAME, relax + ""); |
66 | return this; |
67 | } |
68 | |
69 | /** |
70 | * @see org.jclouds.Constants.PROPERTY_TRUST_ALL_CERTS |
71 | */ |
72 | public PropertiesBuilder trustAllCerts(boolean trust) { |
73 | properties.setProperty(PROPERTY_TRUST_ALL_CERTS, trust + ""); |
74 | return this; |
75 | } |
76 | |
77 | /** |
78 | * @see org.jclouds.Constants.PROPERTY_PROXY_SYSTEM |
79 | */ |
80 | public PropertiesBuilder useSystemProxies(boolean useSystemProxies) { |
81 | properties.setProperty(PROPERTY_PROXY_SYSTEM, useSystemProxies + ""); |
82 | return this; |
83 | } |
84 | |
85 | /** |
86 | * @see org.jclouds.Constants.PROPERTY_PROXY_HOST |
87 | */ |
88 | public PropertiesBuilder withProxyHost(String proxyHost) { |
89 | properties.setProperty(PROPERTY_PROXY_HOST, proxyHost); |
90 | return this; |
91 | } |
92 | |
93 | /** |
94 | * @see org.jclouds.Constants.PROPERTY_PROXY_PORT |
95 | */ |
96 | public PropertiesBuilder withProxyPort(int proxyPort) { |
97 | properties.setProperty(PROPERTY_PROXY_PORT, Integer.toString(proxyPort)); |
98 | return this; |
99 | } |
100 | |
101 | /** |
102 | * @see org.jclouds.Constants.PROPERTY_PROXY_USER |
103 | */ |
104 | public PropertiesBuilder withProxyUser(String proxyUser) { |
105 | properties.setProperty(PROPERTY_PROXY_USER, proxyUser); |
106 | return this; |
107 | } |
108 | |
109 | /** |
110 | * @see org.jclouds.Constants.PROPERTY_PROXY_PASSWORD |
111 | */ |
112 | public PropertiesBuilder withProxyPassword(String proxyPassword) { |
113 | properties.setProperty(PROPERTY_PROXY_PASSWORD, proxyPassword); |
114 | return this; |
115 | } |
116 | |
117 | /** |
118 | * @see org.jclouds.Constants.PROPERTY_SO_TIMEOUT |
119 | */ |
120 | public PropertiesBuilder withSOTimeout(long soTimeout) { |
121 | properties.setProperty(PROPERTY_SO_TIMEOUT, Long.toString(soTimeout)); |
122 | return this; |
123 | } |
124 | |
125 | /** |
126 | * @see org.jclouds.Constants.PROPERTY_CONNECTION_TIMEOUT |
127 | */ |
128 | public PropertiesBuilder withConnectionTimeout(long connectionTimeout) { |
129 | properties.setProperty(PROPERTY_CONNECTION_TIMEOUT, Long.toString(connectionTimeout)); |
130 | return this; |
131 | } |
132 | |
133 | /** |
134 | * @see org.jclouds.Constants.PROPERTY_MAX_RETRIES |
135 | */ |
136 | public PropertiesBuilder withMaxRetries(int httpMaxRetries) { |
137 | properties.setProperty(PROPERTY_MAX_RETRIES, Integer.toString(httpMaxRetries)); |
138 | return this; |
139 | } |
140 | |
141 | /** |
142 | * @see org.jclouds.Constants.PROPERTY_RETRY_DELAY_START |
143 | */ |
144 | public PropertiesBuilder withRetriesDelayStart(long delayStart) { |
145 | properties.setProperty(PROPERTY_RETRY_DELAY_START, Long.toString(delayStart)); |
146 | return this; |
147 | } |
148 | |
149 | /** |
150 | * @see org.jclouds.Constants.PROPERTY_MAX_REDIRECTS |
151 | */ |
152 | public PropertiesBuilder withMaxRedirects(int httpMaxRedirects) { |
153 | properties.setProperty(PROPERTY_MAX_REDIRECTS, Integer.toString(httpMaxRedirects)); |
154 | return this; |
155 | } |
156 | |
157 | /** |
158 | * @see org.jclouds.Constants.PROPERTY_MAX_CONNECTION_REUSE |
159 | */ |
160 | public PropertiesBuilder withMaxClientReuse(int poolMaxClientReuse) { |
161 | properties.setProperty(PROPERTY_MAX_CONNECTION_REUSE, Integer.toString(poolMaxClientReuse)); |
162 | return this; |
163 | } |
164 | |
165 | /** |
166 | * @see org.jclouds.Constants.PROPERTY_MAX_SESSION_FAILURES |
167 | */ |
168 | public PropertiesBuilder withMaxSessionFailures(int poolMaxSessionFailures) { |
169 | properties.setProperty(PROPERTY_MAX_SESSION_FAILURES, Integer.toString(poolMaxSessionFailures)); |
170 | return this; |
171 | |
172 | } |
173 | |
174 | /** |
175 | * @see org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS |
176 | */ |
177 | public PropertiesBuilder limitIoWorkerThreadsTo(int poolIoWorkerThreads) { |
178 | properties.setProperty(PROPERTY_IO_WORKER_THREADS, Integer.toString(poolIoWorkerThreads)); |
179 | return this; |
180 | } |
181 | |
182 | /** |
183 | * @see org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS |
184 | */ |
185 | public PropertiesBuilder limitUserThreadsTo(int poolIoWorkerThreads) { |
186 | properties.setProperty(PROPERTY_USER_THREADS, Integer.toString(poolIoWorkerThreads)); |
187 | return this; |
188 | } |
189 | |
190 | /** |
191 | * @see org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT |
192 | */ |
193 | public PropertiesBuilder limitConnectionsTo(int connectionLimit) { |
194 | properties.setProperty(PROPERTY_MAX_CONNECTIONS_PER_CONTEXT, Integer.toString(connectionLimit)); |
195 | return this; |
196 | } |
197 | |
198 | /** |
199 | * @see org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST |
200 | */ |
201 | public PropertiesBuilder limitConnectionsPerHostTo(int connectionLimit) { |
202 | properties.setProperty(PROPERTY_MAX_CONNECTIONS_PER_HOST, Integer.toString(connectionLimit)); |
203 | return this; |
204 | } |
205 | |
206 | protected final Properties properties; |
207 | |
208 | public PropertiesBuilder() { |
209 | this.properties = defaultProperties(); |
210 | } |
211 | |
212 | protected Properties defaultProperties() { |
213 | Properties props = new Properties(); |
214 | props.setProperty(PROPERTY_ISO3166_CODES, ""); |
215 | props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_CONTEXT, 20 + ""); |
216 | props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_HOST, 0 + ""); |
217 | props.setProperty(PROPERTY_SO_TIMEOUT, 60000 + ""); |
218 | props.setProperty(PROPERTY_CONNECTION_TIMEOUT, 60000 + ""); |
219 | props.setProperty(PROPERTY_IO_WORKER_THREADS, 20 + ""); |
220 | props.setProperty(PROPERTY_USER_THREADS, 0 + ""); |
221 | props.setProperty(PROPERTY_MAX_CONNECTION_REUSE, 75 + ""); |
222 | props.setProperty(PROPERTY_MAX_SESSION_FAILURES, 2 + ""); |
223 | props.setProperty(PROPERTY_SESSION_INTERVAL, 60 + ""); |
224 | return props; |
225 | } |
226 | |
227 | public PropertiesBuilder(Properties properties) { |
228 | this(); |
229 | this.properties.putAll(properties); |
230 | } |
231 | |
232 | public PropertiesBuilder provider(String providerName) { |
233 | properties.setProperty(PROPERTY_PROVIDER, providerName); |
234 | return this; |
235 | } |
236 | |
237 | public PropertiesBuilder endpoint(String endpoint) { |
238 | properties.setProperty(PROPERTY_ENDPOINT, endpoint); |
239 | return this; |
240 | } |
241 | |
242 | public PropertiesBuilder iso3166Codes(Iterable<String> codes) { |
243 | properties.setProperty(PROPERTY_ISO3166_CODES, Joiner.on(',').join(codes)); |
244 | return this; |
245 | } |
246 | |
247 | public PropertiesBuilder apiVersion(String apiVersion) { |
248 | properties.setProperty(PROPERTY_API_VERSION, apiVersion); |
249 | return this; |
250 | } |
251 | |
252 | public PropertiesBuilder credentials(String identity, @Nullable String credential) { |
253 | properties.setProperty(PROPERTY_IDENTITY, identity); |
254 | if (credential != null) |
255 | properties.setProperty(PROPERTY_CREDENTIAL, credential); |
256 | return this; |
257 | } |
258 | |
259 | public PropertiesBuilder sessionInterval(long seconds) { |
260 | properties.setProperty(PROPERTY_SESSION_INTERVAL, seconds + ""); |
261 | return this; |
262 | } |
263 | |
264 | @VisibleForTesting |
265 | public Properties build() { |
266 | return properties; |
267 | } |
268 | } |