diff options
Diffstat (limited to 'bpmn')
8 files changed, 66 insertions, 130 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 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<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/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowContext.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowContext.java index 3d7e333fdf..93aa15c59a 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowContext.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/workflow/service/WorkflowContext.java @@ -72,7 +72,7 @@ public class WorkflowContext implements Delayed { @Override
public long getDelay(TimeUnit unit) {
// 0 or negative means this object is considered to be expired
- return unit.convert(startTime + timeout - System.currentTimeMillis(), unit);
+ return unit.convert(startTime + timeout - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
/**
diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/BaseTask.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/BaseTask.java index 849c8ba4f8..5b43c3fc07 100644 --- a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/BaseTask.java +++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/BaseTask.java @@ -20,6 +20,9 @@ package org.openecomp.mso.bpmn.core; +import java.util.Objects; + +import org.camunda.bpm.engine.ProcessEngineException; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.Expression; import org.camunda.bpm.engine.delegate.JavaDelegate; @@ -185,13 +188,7 @@ public abstract class BaseTask implements JavaDelegate { protected String getOptionalStringField(Expression expression, DelegateExecution execution, String fieldName) { Object o = getFieldImpl(expression, execution, fieldName, true); - if (o instanceof String) { - return (String) o; - } else if (o == null) { - return null; - } else { - return o.toString(); - } + return Objects.toString(o, null); } /** @@ -526,4 +523,15 @@ public abstract class BaseTask implements JavaDelegate { public String getTaskName() { return getClass().getSimpleName(); } + + + /** + * Check if shouldFail variable is set to true. + * @param execution + * @return + */ + protected boolean shouldFail(DelegateExecution execution) { + Boolean shouldFail = (Boolean) execution.getVariable("shouldFail"); + return shouldFail != null && shouldFail; + } } diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadConfigTask.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadConfigTask.java index b46ffcd7f7..09288f0cff 100644 --- a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadConfigTask.java +++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadConfigTask.java @@ -27,7 +27,6 @@ import java.util.Properties; import org.camunda.bpm.engine.ProcessEngineException; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.Expression; - import org.openecomp.mso.logger.MsoLogger; /** @@ -57,38 +56,20 @@ public class ReadConfigTask extends BaseTask { msoLogger.debug("propertiesFile = " + thePropertiesFile); } - Boolean shouldFail = (Boolean) execution.getVariable("shouldFail"); - - if (shouldFail != null && shouldFail) { - throw new ProcessEngineException(getClass().getSimpleName() + " Failed"); - } + if (shouldFail(execution)) { + throw new ProcessEngineException(getTaskName() + " Failed"); + } synchronized (ReadConfigTask.class) { if (properties == null) { properties = new Properties(); - InputStream stream = null; - - try { - stream = getClass().getResourceAsStream(thePropertiesFile); - + try(InputStream stream = getClass().getResourceAsStream(thePropertiesFile)) { if (stream == null) { throw new IOException("Resource not found: " + thePropertiesFile); } properties.load(stream); - - stream.close(); - stream = null; - - } finally { - if (stream != null) { - try { - stream.close(); - } catch (Exception e) { - // Do nothing - } - } } } } diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadFileTask.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadFileTask.java index 389fdc0518..3adba195ea 100644 --- a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadFileTask.java +++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/ReadFileTask.java @@ -24,11 +24,11 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.stream.Collectors; import org.camunda.bpm.engine.ProcessEngineException; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.Expression; - import org.openecomp.mso.logger.MsoLogger; /** @@ -66,45 +66,21 @@ public class ReadFileTask extends BaseTask { + "file = " + theFile); } - Boolean shouldFail = (Boolean) execution.getVariable("shouldFail"); - - if (shouldFail != null && shouldFail) { - throw new ProcessEngineException(getClass().getSimpleName() + " Failed"); - } + if (shouldFail(execution)) { + throw new ProcessEngineException(getTaskName() + " Failed"); + } Object value = execution.getVariable(theInputVariable); if (value == null) { - InputStream xmlStream = null; - - try { - xmlStream = getClass().getResourceAsStream(theFile); + try(InputStream xmlStream = getClass().getResourceAsStream(theFile)) { if (xmlStream == null) { throw new IOException("Resource not found: " + theFile); } BufferedReader reader = new BufferedReader(new InputStreamReader(xmlStream)); - StringBuilder output = new StringBuilder(); - String line; - - while ((line = reader.readLine()) != null) { - output.append(line); - } - - xmlStream.close(); - xmlStream = null; - - value = output.toString(); - - } finally { - if (xmlStream != null) { - try { - xmlStream.close(); - } catch (Exception e) { - // Do nothing - } - } + value = reader.lines().collect(Collectors.joining()); } } execution.setVariable(theInputVariable, value); diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/XQueryScriptTask.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/XQueryScriptTask.java index 31da7376be..e42806e95e 100644 --- a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/XQueryScriptTask.java +++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/XQueryScriptTask.java @@ -25,7 +25,6 @@ import java.io.IOException; import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URI;
-import java.util.Iterator;
import javax.xml.transform.stream.StreamSource;
@@ -33,7 +32,6 @@ import org.camunda.bpm.engine.ProcessEngineException; import org.camunda.bpm.engine.delegate.DelegateExecution;
//import java.util.logging.Logger;
import org.camunda.bpm.engine.delegate.Expression;
-
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;
@@ -97,11 +95,9 @@ public class XQueryScriptTask extends BaseTask { String[] atomicInputVariableArray = (theAtomicInputVariables == null)
? new String[0] : theAtomicInputVariables.split(",[ ]*");
- Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");
-
- if (shouldFail != null && shouldFail) {
- throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
- }
+ if (shouldFail(execution)) {
+ throw new ProcessEngineException(getTaskName() + " Failed");
+ }
// The script could be compiled once and reused, but we are reading it
// and compiling it every time.
@@ -190,9 +186,7 @@ public class XQueryScriptTask extends BaseTask { // Evaluate the query and collect the output.
StringBuilder output = new StringBuilder();
- Iterator<XdmItem> xdmItems = evaluator.iterator();
- while (xdmItems.hasNext()) {
- XdmItem item = xdmItems.next();
+ for(XdmItem item : evaluator) {
if (msoLogger.isDebugEnabled()) {
msoLogger.debug("XQuery result item = " + item);
@@ -218,26 +212,13 @@ public class XQueryScriptTask extends BaseTask { */
private XQueryExecutable compile(XQueryCompiler compiler, String resource)
throws Exception {
- InputStream xqStream = null;
- try {
- xqStream = getClass().getResourceAsStream(resource);
+ try(InputStream xqStream = getClass().getResourceAsStream(resource)) {
if (xqStream == null) {
throw new IOException("Resource not found: " + resource);
}
- XQueryExecutable executable = compiler.compile(xqStream);
- xqStream.close();
- xqStream = null;
- return executable;
- } finally {
- if (xqStream != null) {
- try {
- xqStream.close();
- } catch (Exception e) {
- // Do nothing
- }
- }
+ return compiler.compile(xqStream);
}
}
}
\ No newline at end of file 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"));
+ }
}
|