aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGary Wu <gary.i.wu@huawei.com>2017-05-09 12:41:43 -0700
committerGary Wu <gary.i.wu@huawei.com>2017-05-09 12:47:05 -0700
commita4ade73cdb768ee15bc8dd032904743f84326ee2 (patch)
treeb803590f1ccfe9b2588d70edd59ab462b7ed84c6
parentb1e5734ef566af5d49ba17d05ca0ab7b56d6666d (diff)
Refactor WorkflowAsyncResource
Change-Id: I794b606687343851f71d63ac055ed1898320216a Signed-off-by: Gary Wu <gary.i.wu@huawei.com>
-rw-r--r--bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncCommonResource.java10
-rw-r--r--bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncResource.java58
-rw-r--r--bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/workflow/service/WorkflowAsyncInfrastructureResource.java10
3 files changed, 34 insertions, 44 deletions
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncCommonResource.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncCommonResource.java
index c0ea0cf..f3ad810 100644
--- a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncCommonResource.java
+++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncCommonResource.java
@@ -26,11 +26,7 @@ import org.camunda.bpm.engine.ProcessEngines;
public class WorkflowAsyncCommonResource extends WorkflowAsyncResource {
- protected ProcessEngineServices getProcessEngineServices() {
- if (pes4junit == null) {
- return ProcessEngines.getProcessEngine("common");
- } else {
- return pes4junit;
- }
- }
+ protected ProcessEngineServices getProcessEngineServices() {
+ return pes4junit.orElse(ProcessEngines.getProcessEngine("common"));
+ }
}
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncResource.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncResource.java
index b13ac46..1bd1dfd 100644
--- a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncResource.java
+++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowAsyncResource.java
@@ -21,6 +21,8 @@ package org.openecomp.mso.bpmn.common.workflow.service;
import java.util.HashMap;
import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
import java.util.UUID;
import javax.ws.rs.Consumes;
@@ -31,7 +33,6 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.camunda.bpm.engine.ProcessEngineServices;
-import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
@@ -54,13 +55,13 @@ import org.slf4j.MDC;
@Path("/async")
public abstract class WorkflowAsyncResource {
- private WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance();
- protected ProcessEngineServices pes4junit = null;
+ private static final WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance();
+ protected Optional<ProcessEngineServices> pes4junit = Optional.empty();
- private MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
+ private final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
private static final String logMarker = "[WRKFLOW-RESOURCE]";
- private static final int DEFAULT_WAIT_TIME = 30000; //default wait time
+ private static final long DEFAULT_WAIT_TIME = 30000; //default wait time
/**
* Asynchronous JAX-RS method that starts a process instance.
@@ -75,7 +76,6 @@ public abstract class WorkflowAsyncResource {
public void startProcessInstanceByKey(final @Suspend(180000) AsynchronousResponse asyncResponse,
@PathParam("processKey") String processKey, VariableMapImpl variableMap) {
- WorkflowResponse response = new WorkflowResponse();
long startTime = System.currentTimeMillis();
Map<String, Object> inputVariables = null;
WorkflowContext workflowContext = null;
@@ -107,6 +107,7 @@ public abstract class WorkflowAsyncResource {
}
msoLogger.debug(logMarker + "Exception in startProcessInstance by key");
+ WorkflowResponse response = new WorkflowResponse();
response.setMessage("Fail" );
response.setResponse("Error occurred while executing the process: " + e);
response.setMessageCode(500);
@@ -205,29 +206,28 @@ public abstract class WorkflowAsyncResource {
return contextHolder.processCallback(processKey, processInstanceId, requestId, callbackResponse);
}
+ private static String getOrCreate(Map<String, Object> inputVariables, String key) {
+ String value = Objects.toString(inputVariables.get(key), null);
+ if (value == null) {
+ value = UUID.randomUUID().toString();
+ inputVariables.put(key, value);
+ }
+ return value;
+ }
+
// Note: the business key is used to identify the process in unit tests
- private String getBusinessKey(Map<String, Object> inputVariables) {
- Object businessKey = inputVariables.get("mso-business-key");
- if (businessKey == null ) {
- businessKey = UUID.randomUUID().toString();
- inputVariables.put("mso-business-key", businessKey);
- }
- return businessKey.toString();
+ private static String getBusinessKey(Map<String, Object> inputVariables) {
+ return getOrCreate(inputVariables, "mso-business-key");
}
- private String getRequestId(Map<String, Object> inputVariables) {
- Object requestId = inputVariables.get("mso-request-id");
- if (requestId == null ) {
- requestId = UUID.randomUUID().toString();
- inputVariables.put("mso-request-id", requestId);
- }
- return requestId.toString();
+ private static String getRequestId(Map<String, Object> inputVariables) {
+ return getOrCreate(inputVariables, "mso-request-id");
}
private long getWaitTime(Map<String, Object> inputVariables)
{
- String timeout = inputVariables.get("mso-service-request-timeout") == null
- ? null : inputVariables.get("mso-service-request-timeout").toString();
+
+ String timeout = Objects.toString(inputVariables.get("mso-service-request-timeout"), null);
if (timeout != null) {
try {
@@ -252,7 +252,7 @@ public abstract class WorkflowAsyncResource {
}
- private void setLogContext(String processKey,
+ private static void setLogContext(String processKey,
Map<String, Object> inputVariables) {
MsoLogger.setServiceName("MSO." + processKey);
if (inputVariables != null) {
@@ -260,26 +260,24 @@ public abstract class WorkflowAsyncResource {
}
}
- private String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) {
+ private static String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) {
if (inputVariables == null) return "";
- Object requestId = inputVariables.get(key);
- if (requestId != null) return requestId.toString();
- return "N/A";
+ return Objects.toString(inputVariables.get(key), "N/A");
}
private boolean isProcessEnded(String processInstanceId) {
ProcessEngineServices pes = getProcessEngineServices();
- return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null ? true : false ;
+ return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null;
}
protected abstract ProcessEngineServices getProcessEngineServices();
public void setProcessEngineServices4junit(ProcessEngineServices pes) {
- pes4junit = pes;
+ pes4junit = Optional.ofNullable(pes);
}
- private Map<String, Object> getInputVariables(VariableMapImpl variableMap) {
+ private static Map<String, Object> getInputVariables(VariableMapImpl variableMap) {
Map<String, Object> inputVariables = new HashMap<String,Object>();
@SuppressWarnings("unchecked")
Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables");
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/workflow/service/WorkflowAsyncInfrastructureResource.java b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/workflow/service/WorkflowAsyncInfrastructureResource.java
index 09454f0..0cc81bf 100644
--- a/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/workflow/service/WorkflowAsyncInfrastructureResource.java
+++ b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/workflow/service/WorkflowAsyncInfrastructureResource.java
@@ -40,11 +40,7 @@ import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncResource;
@Path("/async")
public class WorkflowAsyncInfrastructureResource extends WorkflowAsyncResource {
- protected ProcessEngineServices getProcessEngineServices() {
- if (pes4junit == null) {
- return ProcessEngines.getProcessEngine("infrastructure");
- } else {
- return pes4junit;
- }
- }
+ protected ProcessEngineServices getProcessEngineServices() {
+ return pes4junit.orElse(ProcessEngines.getProcessEngine("infrastructure"));
+ }
}