From 3949afa9ae71438528590721e581e5cb828583f2 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Wed, 8 Sep 2010 19:24:35 -0700 Subject: [PATCH] Refactor queryAsyncJobResult to new API framework. --- .../api/commands/QueryAsyncJobResultCmd.java | 108 +++++++++--------- .../cloud/api/response/AsyncJobResponse.java | 11 ++ .../com/cloud/server/ManagementServer.java | 10 ++ .../cloud/server/ManagementServerImpl.java | 6 + 4 files changed, 78 insertions(+), 57 deletions(-) diff --git a/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java b/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java index d897536585e..ecb0613625b 100644 --- a/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java +++ b/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java @@ -18,32 +18,20 @@ package com.cloud.api.commands; -import java.util.ArrayList; import java.util.Date; -import java.util.List; -import java.util.Map; import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; +import com.cloud.api.response.AsyncJobResponse; import com.cloud.async.AsyncJobResult; -import com.cloud.async.executor.IngressRuleResultObject; -import com.cloud.async.executor.NetworkGroupResultObject; -import com.cloud.exception.PermissionDeniedException; import com.cloud.serializer.SerializerHelper; -import com.cloud.utils.Pair; public class QueryAsyncJobResultCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(QueryAsyncJobResultCmd.class.getName()); private static final String s_name = "queryasyncjobresultresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.JOB_ID, Boolean.TRUE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -64,57 +52,62 @@ public class QueryAsyncJobResultCmd extends BaseCmd { /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + @Override public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Long jobId = (Long)params.get(BaseCmd.Properties.JOB_ID.getName()); - AsyncJobResult result; - - try { - result = getManagementServer().queryAsyncJobResult(jobId); - } catch (PermissionDeniedException e) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Permission denied"); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), jobId)); - returnValues.add(new Pair(BaseCmd.Properties.JOB_STATUS.getName(), Integer.valueOf(result.getJobStatus()))); - returnValues.add(new Pair(BaseCmd.Properties.JOB_PROCESS_STATUS.getName(), Integer.valueOf(result.getProcessStatus()))); - returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_CODE.getName(), Integer.valueOf(result.getResultCode()))); - - Object resultObject = result.getResultObject(); - if(resultObject != null) { - - Class clz = resultObject.getClass(); - if(clz.isPrimitive() || clz.getSuperclass() == Number.class || clz == String.class || clz == Date.class) { - returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "text")); - SerializerHelper.appendPairList(returnValues, resultObject, BaseCmd.Properties.JOB_RESULT.getName()); - } else { - returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "object")); - - if(result.getCmdOriginator() != null && !result.getCmdOriginator().isEmpty()) { - List> resultValues = new ArrayList>(); - if (resultObject instanceof NetworkGroupResultObject) { - serializeNetworkGroupResults(resultValues, (NetworkGroupResultObject)resultObject); - } else { + @Override + public String getResponse() { + AsyncJobResult result = (AsyncJobResult)getResponseObject(); + + AsyncJobResponse response = new AsyncJobResponse(); + response.setId(result.getJobId()); + response.setJobStatus(result.getJobStatus()); + response.setJobProcStatus(result.getProcessStatus()); + response.setJobResultCode(result.getResultCode()); + response.setJobResult(result.getResult()); + + Object resultObject = result.getResultObject(); + if (resultObject != null) { + Class clz = resultObject.getClass(); + if(clz.isPrimitive() || clz.getSuperclass() == Number.class || clz == String.class || clz == Date.class) { + response.setJobResultType("text"); + } else { + response.setJobResultType("object"); + } + } + + /* + Object resultObject = result.getResultObject(); + if (resultObject != null) { + + Class clz = resultObject.getClass(); + if(clz.isPrimitive() || clz.getSuperclass() == Number.class || clz == String.class || clz == Date.class) { + returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "text")); + SerializerHelper.appendPairList(returnValues, resultObject, BaseCmd.Properties.JOB_RESULT.getName()); + } else { + returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "object")); + + if(result.getCmdOriginator() != null && !result.getCmdOriginator().isEmpty()) { + List> resultValues = new ArrayList>(); + if (resultObject instanceof NetworkGroupResultObject) { + serializeNetworkGroupResults(resultValues, (NetworkGroupResultObject)resultObject); + } else { SerializerHelper.appendPairList(resultValues, resultObject, BaseCmd.Properties.JOB_RESULT.getName()); - } - returnValues.add(new Pair(result.getCmdOriginator(), new Object[] { resultValues } )); - } - } - } - return returnValues; - } + } + returnValues.add(new Pair(result.getCmdOriginator(), new Object[] { resultValues } )); + } + } + } + */ + + return SerializerHelper.toSerializedString(response); + } // For now network groups are the only objects with nested objects inside, so we special case serialization to handle this one case. // In the future, if a generic serialization that handles nested objects is implemented then this special case can be removed. + /* private void serializeNetworkGroupResults(List> resultValues, NetworkGroupResultObject resultObject) { if (resultObject != null) { resultValues.add(new Pair(BaseCmd.Properties.ID.getName(), resultObject.getId().toString())); @@ -152,5 +145,6 @@ public class QueryAsyncJobResultCmd extends BaseCmd { resultValues.add(new Pair("ingressrule", ingressDataArray)); } } - } + } + */ } diff --git a/server/src/com/cloud/api/response/AsyncJobResponse.java b/server/src/com/cloud/api/response/AsyncJobResponse.java index bd9471a5070..65f67b9c902 100644 --- a/server/src/com/cloud/api/response/AsyncJobResponse.java +++ b/server/src/com/cloud/api/response/AsyncJobResponse.java @@ -44,6 +44,9 @@ public class AsyncJobResponse implements ResponseObject { @Param(name="jobresultcode") private Integer jobResultCode; + @Param(name="jobresulttype") + private String jobResultType; + @Param(name="jobresult") private String jobResult; @@ -112,6 +115,14 @@ public class AsyncJobResponse implements ResponseObject { this.jobResultCode = jobResultCode; } + public String getJobResultType() { + return jobResultType; + } + + public void setJobResultType(String jobResultType) { + this.jobResultType = jobResultType; + } + public String getJobResult() { return jobResult; } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 40d69e3a83d..d9a2b281d67 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -66,6 +66,7 @@ import com.cloud.api.commands.ListVolumesCmd; import com.cloud.api.commands.ListZonesByCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; +import com.cloud.api.commands.QueryAsyncJobResultCmd; import com.cloud.api.commands.RebootSystemVmCmd; import com.cloud.api.commands.RegisterCmd; import com.cloud.api.commands.RemovePortForwardingServiceCmd; @@ -1601,6 +1602,15 @@ public interface ManagementServer { * @return async-call result object */ AsyncJobResult queryAsyncJobResult(long jobId) throws PermissionDeniedException; + + /** + * Queries for the status or final result of an async job. + * @param cmd the command that specifies the job id + * @return an async-call result object + * @throws PermissionDeniedException + */ + AsyncJobResult queryAsyncJobResult(QueryAsyncJobResultCmd cmd) throws PermissionDeniedException; + AsyncJobVO findInstancePendingAsyncJob(String instanceType, long instanceId); AsyncJobVO findAsyncJobById(long jobId); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index fe6334ee3a7..74af35c1b47 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -104,6 +104,7 @@ import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; import com.cloud.api.commands.PrepareForMaintenanceCmd; import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd; +import com.cloud.api.commands.QueryAsyncJobResultCmd; import com.cloud.api.commands.RebootSystemVmCmd; import com.cloud.api.commands.RegisterCmd; import com.cloud.api.commands.RemovePortForwardingServiceCmd; @@ -7082,6 +7083,11 @@ public class ManagementServerImpl implements ManagementServer { return _diskOfferingDao.search(sc, searchFilter); } + @Override + public AsyncJobResult queryAsyncJobResult(QueryAsyncJobResultCmd cmd) throws PermissionDeniedException { + return queryAsyncJobResult(cmd.getId()); + } + @Override public AsyncJobResult queryAsyncJobResult(long jobId) throws PermissionDeniedException { AsyncJobVO job = _asyncMgr.getAsyncJob(jobId);