public <T> T call(CloudStackCommand cmd, String apiKey, String secretKey, boolean followToAsyncResult,
String responseName, String responseObjName, Class<T> responseClz) throws Exception {
assert(responseName != null);
JsonAccessor json = execute(cmd, apiKey, secretKey);
if(followToAsyncResult && json.tryEval(responseName + ".jobid") != null) {
long startMs = System.currentTimeMillis();
while(System.currentTimeMillis() - startMs < _pollTimeoutMs) {
CloudStackCommand queryJobCmd = new CloudStackCommand("queryAsyncJobResult");
queryJobCmd.setParam("jobId", json.getAsString(responseName + ".jobid"));
JsonAccessor queryAsyncJobResponse = execute(queryJobCmd, apiKey, secretKey);
if(queryAsyncJobResponse.tryEval("queryasyncjobresultresponse") != null) {
int jobStatus = queryAsyncJobResponse.getAsInt("queryasyncjobresultresponse.jobstatus");
switch(jobStatus) {
case 2:
throw new Exception(queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errortext") + " Error Code - " +
queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errorcode") );
case 0 :
try {
Thread.sleep( _pollIntervalMs );
} catch( Exception e ) {}
break;
case 1 :
if(responseObjName != null)
return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult." + responseObjName), responseClz);
else
return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult"), responseClz);
default :
assert(false);
throw new Exception("Operation failed - invalid job status response");
}