From a4ade73cdb768ee15bc8dd032904743f84326ee2 Mon Sep 17 00:00:00 2001 From: Gary Wu Date: Tue, 9 May 2017 12:41:43 -0700 Subject: Refactor WorkflowAsyncResource Change-Id: I794b606687343851f71d63ac055ed1898320216a Signed-off-by: Gary Wu --- .../service/WorkflowAsyncCommonResource.java | 10 ++-- .../workflow/service/WorkflowAsyncResource.java | 58 +++++++++++----------- .../WorkflowAsyncInfrastructureResource.java | 10 ++-- 3 files changed, 34 insertions(+), 44 deletions(-) (limited to 'bpmn') 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 c0ea0cf874..f3ad810a3e 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 b13ac46784..1bd1dfde70 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 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 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 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 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 inputVariables) { + return getOrCreate(inputVariables, "mso-business-key"); } - private String getRequestId(Map 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 inputVariables) { + return getOrCreate(inputVariables, "mso-request-id"); } private long getWaitTime(Map 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 inputVariables) { MsoLogger.setServiceName("MSO." + processKey); if (inputVariables != null) { @@ -260,26 +260,24 @@ public abstract class WorkflowAsyncResource { } } - private String getKeyValueFromInputVariables(Map inputVariables, String key) { + private static String getKeyValueFromInputVariables(Map 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 getInputVariables(VariableMapImpl variableMap) { + private static Map getInputVariables(VariableMapImpl variableMap) { Map inputVariables = new HashMap(); @SuppressWarnings("unchecked") Map vMap = (Map) 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 09454f0876..0cc81bf0c6 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")); + } } -- cgit 1.2.3-korg