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