forked from AnythingLinux/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.java
More file actions
385 lines (353 loc) · 14.9 KB
/
Connection.java
File metadata and controls
385 lines (353 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.xensource.xenapi;
import java.net.URL;
import java.util.Map;
import java.util.TimeZone;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfig;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcHttpClientConfig;
import org.apache.xmlrpc.client.XmlRpcSun15HttpTransportFactory;
import com.xensource.xenapi.Types.BadServerResponse;
import com.xensource.xenapi.Types.SessionAuthenticationFailed;
import com.xensource.xenapi.Types.XenAPIException;
/**
* Represents a connection to a XenServer. Creating a new instance of this class initialises a new XmlRpcClient that is
* then used by all method calls: each method call in xenapi takes a Connection as a parameter, composes an XMLRPC
* method call, and dispatches it on the Connection's client via the dispatch method.
*/
public class Connection
{
/**
* The version of the bindings that this class belongs to.
*/
public static final String BINDINGS_VERSION = "6.1.0-1";
/**
* true if the connection is to the Rio edition of XenServer. Certain function calls are not allowed.
*
* @deprecated Use getAPIVersion() instead.
*/
@Deprecated
public Boolean rioConnection = false;
private APIVersion apiVersion;
protected int _wait = 600;
/**
* Updated when Session.login_with_password() is called.
*/
public APIVersion getAPIVersion()
{
return apiVersion;
}
/**
* The opaque reference to the session used by this connection
*/
private String sessionReference;
/**
* As seen by the xmlrpc library. From our point of view it's a server.
*/
private final XmlRpcClient client;
private final boolean deprecatedConstructorUsed;
/**
* Creates a connection to a particular server using a given username and password. This object can then be passed
* in to any other API calls.
*
* This constructor calls Session.loginWithPassword, passing itself as the first parameter.
*
* When this constructor is used, a call to dispose() (also called in the Connection's finalizer) will attempt a
* Session.logout on this connection.
*
* @deprecated Use a constructor that takes a URL as the first parameter instead.
*/
@Deprecated
public Connection(String client, String username, String password) throws java.net.MalformedURLException,
XmlRpcException, BadServerResponse, SessionAuthenticationFailed, XenAPIException
{
deprecatedConstructorUsed = true;
// To login normally we call login_with_password(username, password, "1.X"). On rio this call fails and we
// should use login_with_password(username,password) instead, and note that we are talking to a rio host so that we
// can refuse to make certain miami-specific calls
final String ApiVersion = APIVersion.latest().toString();
this.client = getClientFromURL(new URL(client));
try
{
//first try to login the modern way
this.sessionReference = loginWithPassword(this.client, username, password, ApiVersion);
} catch (BadServerResponse e)
{
//oops, something went wrong
String[] errDesc = e.errorDescription;
//was the problem that the host was running rio? If so it will have complained that it got three parameters
//instead of two. Let us carefully verify the details of this complaint
if (0 == errDesc[0].compareTo("MESSAGE_PARAMETER_COUNT_MISMATCH")
&& 0 == errDesc[1].compareTo("session.login_with_password")
&& 0 == errDesc[2].compareTo("2")
&& 0 == errDesc[3].compareTo("3"))
{
//and if so, we can have another go, using the older login method, and see how that goes.
this.sessionReference = loginWithPassword(this.client, username, password);
//success!. Note that we are talking to an old host on this connection
this.rioConnection = true;
} else
{
//Hmm... Can't solve this here. Let upstairs know about the problem.
throw e;
}
}
try
{
setAPIVersion(new Session(sessionReference));
}
catch (XenAPIException exn)
{
dispose();
throw exn;
}
catch (XmlRpcException exn)
{
dispose();
throw exn;
}
}
/**
* Creates a connection to a particular server using a given username and password. This object can then be passed
* in to any other API calls.
*
* Note this constructor does NOT call Session.loginWithPassword; the programmer is responsible for calling it,
* passing the Connection as a parameter. No attempt to connect to the server is made until login is called.
*
* When this constructor is used, a call to dispose() will do nothing. The programmer is responsible for manually
* logging out the Session.
*/
public Connection(URL url, int wait)
{
deprecatedConstructorUsed = false;
_wait = wait;
this.client = getClientFromURL(url);
}
/**
* Creates a connection to a particular server using a given username and password. This object can then be passed
* in to any other API calls.
*
* The additional sessionReference parameter must be a reference to a logged-in Session. Any method calls on this
* Connection will use it. This constructor does not call Session.loginWithPassword, and dispose() on the resulting
* Connection object does not call Session.logout. The programmer is responsible for ensuring the Session is logged
* in and out correctly.
*/
public Connection(URL url, String sessionReference)
{
deprecatedConstructorUsed = false;
this.client = getClientFromURL(url);
this.sessionReference = sessionReference;
}
protected void finalize() throws Throwable
{
dispose();
super.finalize();
}
/**
* Nothrow guarantee.
*/
public void dispose()
{
if (!deprecatedConstructorUsed)
{
// We only need to do the Session.logout if they used the old deprecated constructor.
return;
}
try
{
if (sessionReference != null)
{
String method_call = "session.logout";
Object[] method_params = { Marshalling.toXMLRPC(this.sessionReference) };
client.execute(method_call, method_params);
sessionReference = null;
}
}
catch (XmlRpcException exn)
{
}
}
/**
* @deprecated The programmer is now responsible for calling login/logout themselves.
*/
@Deprecated
private static String loginWithPassword(XmlRpcClient client, String username, String password)
throws BadServerResponse, XmlRpcException, SessionAuthenticationFailed
{
String method_call = "session.login_with_password";
Object[] method_params = { Marshalling.toXMLRPC(username), Marshalling.toXMLRPC(password) };
Map response = (Map) client.execute(method_call, method_params);
if (response.get("Status").equals("Success"))
{
return (String) response.get("Value");
} else if (response.get("Status").equals("Failure"))
{
Object[] error = (Object[]) response.get("ErrorDescription");
if (error[0].equals("SESSION_AUTHENTICATION_FAILED"))
{
throw new SessionAuthenticationFailed();
}
}
throw new BadServerResponse(response);
}
/**
* @deprecated The programmer is now responsible for calling login/logout themselves.
*/
@Deprecated
private static String loginWithPassword(XmlRpcClient client, String username, String password, String ApiVersion)
throws BadServerResponse, XmlRpcException, SessionAuthenticationFailed
{
String method_call = "session.login_with_password";
Object[] method_params = { Marshalling.toXMLRPC(username), Marshalling.toXMLRPC(password),
Marshalling.toXMLRPC(ApiVersion) };
Map response = (Map) client.execute(method_call, method_params);
if (response.get("Status").equals("Success"))
{
return (String) response.get("Value");
} else if (response.get("Status").equals("Failure"))
{
Object[] error = (Object[]) response.get("ErrorDescription");
if (error[0].equals("SESSION_AUTHENTICATION_FAILED"))
{
throw new SessionAuthenticationFailed();
}
}
throw new BadServerResponse(response);
}
private XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
public XmlRpcClientConfigImpl getConfig()
{
return config;
}
private XmlRpcClient getClientFromURL(URL url)
{
config.setTimeZone(TimeZone.getTimeZone("UTC"));
config.setServerURL(url);
config.setReplyTimeout(_wait * 1000);
config.setConnectionTimeout(5000);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
return client;
}
/*
* Because the binding calls are constructing their own parameter lists, they need to be able to get to
* the session reference directly. This is all rather ugly and needs redone
* Changed to public to allow easier integration with HTTP-level streaming interface,
* see CA-15447
*/
public String getSessionReference()
{
return this.sessionReference;
}
/**
* The (auto-generated parts of) the bindings dispatch XMLRPC calls on this Connection's client through this method.
*/
protected Map dispatch(String method_call, Object[] method_params) throws XmlRpcException, XenAPIException
{
Map response = (Map) client.execute(method_call, method_params);
if (!deprecatedConstructorUsed)
{
// We are using the new-style constructor which doesn't perform login.
// Set this Connection's Session reference from the value returned on the wire.
if (method_call.equals("session.login_with_password") &&
response.get("Status").equals("Success"))
{
// Store the Session reference and ask the server what the
// API version it's using is.
Session session = Types.toSession(response.get("Value"));
sessionReference = session.ref;
setAPIVersion(session);
}
else if (method_call.equals("session.slave_local_login_with_password") &&
response.get("Status").equals("Success"))
{
// Store the Session reference and assume the latest API version.
sessionReference = Types.toSession(response.get("Value")).ref;
apiVersion = APIVersion.latest();
}
else if (method_call.equals("session.logout"))
{
// Work around a bug in XenServer 5.0 and below.
// session.login_with_password should have rejected us with
// HOST_IS_SLAVE, but instead we don't find out until later.
// We don't want to leak the session, so we need to log out
// this session from the master instead.
if (response.get("Status").equals("Failure"))
{
Object[] error = (Object[]) response.get("ErrorDescription");
if (error.length == 2 && error[0].equals("HOST_IS_SLAVE"))
{
try
{
URL client_url =
((XmlRpcHttpClientConfig)client.getClientConfig()).getServerURL();
Connection tmp_conn =
new Connection(new URL(client_url.getProtocol(),
(String)error[1],
client_url.getPort(),
client_url.getFile()), _wait);
tmp_conn.sessionReference = sessionReference;
try
{
Session.logout(tmp_conn);
}
finally
{
tmp_conn.dispose();
}
}
catch (Exception exn2)
{
// Ignore -- we're going to throw HostIsSlave anyway.
}
}
}
// Clear the stored Session reference.
this.sessionReference = null;
}
}
return Types.checkResponse(response);
}
private void setAPIVersion(Session session) throws XenAPIException, XmlRpcException
{
try
{
long major = session.getThisHost(this).getAPIVersionMajor(this);
long minor = session.getThisHost(this).getAPIVersionMinor(this);
apiVersion = APIVersion.fromMajorMinor(major, minor);
}
catch (BadServerResponse exn)
{
apiVersion = APIVersion.API_1_1;
}
}
}