Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions java/lib/com/xensource/xenapi/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ public class Connection

private APIVersion apiVersion;

protected int _wait = 600;
/**
* Reply timeout for xml-rpc calls. The default value is 10 minutes.
*/
protected int _replyWait = 600;

/**
* Connection timeout for xml-rpc calls. The default value is 5 seconds.
*/
protected int _connWait = 5;

/**
* Updated when Session.login_with_password() is called.
Expand Down Expand Up @@ -135,7 +143,7 @@ public Connection(String client, String username, String password) throws java.n
}

/**
* Creates a connection to a particular server using a given username and password. This object can then be passed
* Creates a connection to a particular server using a given url. 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,
Expand All @@ -149,16 +157,29 @@ public Connection(URL url)
deprecatedConstructorUsed = false;
this.client = getClientFromURL(url);
}

public Connection(URL url, int wait)
{
this(url);
_wait = wait;

/**
* Creates a connection to a particular server using a given url. 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.
*
* The parameters replyWait and connWait set timeouts for xml-rpc calls.
*/
public Connection(URL url, int replyWait, int connWait)
{
this(url);
_replyWait = replyWait;
_connWait = connWait;
}


/**
* Creates a connection to a particular server using a given username and password. This object can then be passed
* Creates a connection to a particular server using a given url. 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
Expand All @@ -174,6 +195,24 @@ public Connection(URL url, String sessionReference)
this.sessionReference = sessionReference;
}

/**
* Creates a connection to a particular server using a given url. 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.
*
* The parameters replyWait and connWait set timeouts for xml-rpc calls.
*/
public Connection(URL url, String sessionReference, int replyWait, int connWait)
{
this(url, sessionReference);
_replyWait = replyWait;
_connWait = connWait;
}

protected void finalize() throws Throwable
{
dispose();
Expand Down Expand Up @@ -259,14 +298,14 @@ private static String loginWithPassword(XmlRpcClient client, String username, St

public XmlRpcClientConfigImpl getConfig()
{
return config;
return config;
}
private XmlRpcClient getClientFromURL(URL url)
{
config.setTimeZone(TimeZone.getTimeZone("UTC"));
config.setServerURL(url);
config.setReplyTimeout(_wait * 1000);
config.setConnectionTimeout(5000);
config.setReplyTimeout(_replyWait * 1000);
config.setConnectionTimeout(_connWait * 1000);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
return client;
Expand Down Expand Up @@ -330,7 +369,7 @@ else if (method_call.equals("session.logout"))
new Connection(new URL(client_url.getProtocol(),
(String)error[1],
client_url.getPort(),
client_url.getFile()), _wait);
client_url.getFile()), _replyWait, _connWait);
tmp_conn.sessionReference = sessionReference;
try
{
Expand Down
57 changes: 57 additions & 0 deletions java/lib/com/xensource/xenapi/EventBatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.util.Set;

/**
* Class used to map the output of Event.from().
*
* @author Citrix Systems, Inc.
*/
public class EventBatch
{
/**
* The events returned by Event.from().
*/
public Set<Event.Record> events;

/**
* The number of valid objects of all types in the database.
*/
public Object validRefCounts;

/**
* A token that can be used in subsequent calls of Event.from().
* It represents the point from which to generate database events.
*/
public String token;
}
54 changes: 35 additions & 19 deletions java/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,26 @@ let get_method_params_for_xml message =


let gen_method_return_cast message = match message.msg_result with
| None ->
sprintf ""
| Some (ty, _) ->
sprintf " Types.%s(result)" (get_marshall_function ty);;
| None -> sprintf ""
| Some (ty, _) -> sprintf " Types.%s(result)" (get_marshall_function ty);;

let gen_method_return file message =
fprintf file " return%s;\n" (gen_method_return_cast message);;
let gen_method_return file cls message =
if (String.lowercase cls.name) = "event" && (String.lowercase message.msg_name) = "from" then
fprintf file " return Types.toEventBatch(result);\n"
else
fprintf file " return%s;\n" (gen_method_return_cast message)

let rec range = function
| 0 -> []
| i -> (range (i-1)) @ [i]


(* Here is the main method generating function.*)
let gen_method file message async_version =
let gen_method file cls message async_version =
let deprecated_string = get_method_deprecated_string message in
let return_type = get_java_type_or_void message.msg_result in
let return_type =
if (String.lowercase cls.name) = "event" && (String.lowercase message.msg_name) = "from" then "EventBatch"
else get_java_type_or_void message.msg_result in
let method_static = get_method_static_string message in
let method_name = camel_case message.msg_name in
let method_params = get_method_params_for_signature message in
Expand Down Expand Up @@ -295,7 +298,7 @@ let gen_method file message async_version =
fprintf file " */\n";

if async_version then
fprintf file " %s public %s%s %s(%s) throws\n" deprecated_string method_static "Task" (method_name^"Async") method_params
fprintf file " %s public %sTask %s(%s) throws\n" deprecated_string method_static (method_name^"Async") method_params

else
fprintf file " %s public %s%s %s(%s) throws\n" deprecated_string method_static return_type method_name method_params;
Expand Down Expand Up @@ -341,19 +344,19 @@ let gen_method file message async_version =
fprintf file " return Types.toTask(result);\n" )
else (
match message.msg_result with
| None ->
fprintf file " return;\n"
| Some _ ->
fprintf file " Object result = response.get(\"Value\");\n";
gen_method_return file message
| None -> fprintf file " return;\n"
| Some _ -> fprintf file " Object result = response.get(\"Value\");\n";
gen_method_return file cls message
);

fprintf file " }\n\n"

(* Here is the main method generating function.*)
let gen_versioned_method file message versioned_message async_version =
let gen_versioned_method file cls message versioned_message async_version =
let deprecated_string = get_method_deprecated_string message in
let return_type = get_java_type_or_void message.msg_result in
let return_type =
if (String.lowercase cls.name) = "event" && (String.lowercase message.msg_name) = "from" then "EventBatch"
else get_java_type_or_void message.msg_result in
let method_static = get_method_static_string message in
let versioned_name = camel_case versioned_message.msg_name in
let method_params = get_method_params_for_signature versioned_message in
Expand Down Expand Up @@ -409,7 +412,7 @@ let gen_versioned_method file message versioned_message async_version =
if async_version then
fprintf file " return Types.toTask(result);\n"
else
gen_method_return file message;
gen_method_return file cls message;

fprintf file " }\n\n"
;;
Expand All @@ -418,8 +421,8 @@ let gen_versioned_method file message versioned_message async_version =
(*Some methods have an almost identical asynchronous counterpart, which returns*)
(* a Task reference rather than its usual return value*)
let gen_method_and_asynchronous_counterpart file cls message =
if message.msg_async then gen_method file message true;
gen_method file message false;;
if message.msg_async then gen_method file cls message true;
gen_method file cls message false;;

(* Generate the record *)

Expand Down Expand Up @@ -1038,6 +1041,19 @@ public class Types
TypeSet.iter (gen_marshall_func file) !types;
fprintf file "\n";
TypeSet.iter (gen_task_result_func file) !types;
fprintf file "
public static EventBatch toEventBatch(Object object) {
if (object == null) {
return null;
}

Map map = (Map) object;
EventBatch batch = new EventBatch();
batch.token = toString(map.get(\"token\"));
batch.validRefCounts = map.get(\"valid_ref_counts\");
batch.events = toSetOfEventRecord(map.get(\"events\"));
return batch;
}";
fprintf file "}\n"

(* Now run it *)
Expand Down
45 changes: 45 additions & 0 deletions java/samples/EventMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,60 @@
import java.util.Set;

import com.xensource.xenapi.Event;
import com.xensource.xenapi.EventBatch;

/**
* Listens for events on a connection and prints each event out as it is received.
*/
public class EventMonitor extends TestBase
{
private static final int MAX_EVENTS = 100;
private static final double TIMEOUT_SEC = 30;
private static final int TIMEOUT = 30 * 1000;

public static void RunNewTest(ILog logger, TargetServer server) throws Exception
{
TestBase.logger = logger;
try
{
connect(server);

Set<String> everything = new HashSet<String>();
everything.add("*");
Event.register(connection, everything);

int eventsReceived = 0;
long started = System.currentTimeMillis();
String token = "";

while (eventsReceived < MAX_EVENTS && System.currentTimeMillis() - started < TIMEOUT)
{
EventBatch eventBatch = Event.from(connection, everything, token, TIMEOUT_SEC);
announce(eventBatch.events.size() + " event(s) received");

// print the events out in a nice format
String format = "%10s %5s %3s %10s %50s";
logf(format + " date time%n", "class", "id", "uuid", "operation", "reference");
for (Event.Record e : eventBatch.events)
{
logf(format, e.clazz, e.id, e.objUuid, e.operation, e.ref);
logf(" %te/%<tm/%<tY %<tH.%<tM.%<tS %n", e.timestamp);
logln("associated snapshot: " + e.snapshot);
}
eventsReceived += eventBatch.events.size();
}
}
catch (Exception e)
{
logln("exception!");
}
finally
{
disconnect();
}
}

@Deprecated
public static void RunTest(ILog logger, TargetServer server) throws Exception
{
TestBase.logger = logger;
Expand Down
2 changes: 1 addition & 1 deletion java/samples/Https.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean verify(String hostname, SSLSession session)
connection = new Connection(url);

// Log in
Session.loginWithPassword(connection, server.Username, server.Password, "1.3");
Session.loginWithPassword(connection, server.Username, server.Password, "1.3", "testRunner");

// Print a record
for (Host host : Host.getAllRecords(connection).keySet())
Expand Down
11 changes: 11 additions & 0 deletions java/samples/RunTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public static void main(String[] args)

logln("RunTests.java: test run started at " + new Date().toString());

testName = "EventMonitor";
try
{
testStart();
EventMonitor.RunNewTest(textLogger, server);
testSuccess();
} catch (Exception e)
{
testFailure(e);
}

testName = "AddNetwork";
try
{
Expand Down
2 changes: 1 addition & 1 deletion java/samples/SessionReuse.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected static void RunTest(ILog logger, TargetServer server) throws Exception
try
{
// Create a new Session, whose reference is stored in the Connection.
Session.loginWithPassword(connection1, server.Username, server.Password, "1.3");
Session.loginWithPassword(connection1, server.Username, server.Password, "1.3", "testRunner");

// Re-use the Session in a second Connection object
Connection connection2 = new Connection(url, connection1.getSessionReference());
Expand Down
2 changes: 1 addition & 1 deletion java/samples/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected static void connect(TargetServer target) throws Exception
logln(String.format("logging in to '%s' as '%s' with password '%s'...", target.Hostname, target.Username,
target.Password));
logln("Success");
Session.loginWithPassword(connection, target.Username, target.Password, APIVersion.latest().toString());
Session.loginWithPassword(connection, target.Username, target.Password, APIVersion.latest().toString(), "testRunner");
logln(String.format("Session API version is %s", connection.getAPIVersion().toString()));

connectionName = target.Hostname;
Expand Down