ExecuteOnceWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { MediaPackage mediaPackage = workflowInstance.getMediaPackage(); WorkflowOperationInstance operation = workflowInstance.getCurrentOperation(); logger.debug("Running execute workflow operation with ID {}", operation.getId()); String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY)); String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY)); float load = 1.0f; String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY)); if (StringUtils.isNotBlank(loadPropertyStr)) { try { load = Float.parseFloat(loadPropertyStr); } catch (NumberFormatException e) { String description = StringUtils.trimToEmpty(operation.getDescription()); logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description); } } String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY)); String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY)); String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY)); String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY)); boolean setWfProps = Boolean.valueOf(StringUtils.trimToNull(operation.getConfiguration(SET_WF_PROPS_PROPERTY))); MediaPackageElementFlavor targetFlavor = null; if (targetFlavorStr != null) targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr); MediaPackageElement.Type expectedType = null; if (expectedTypeStr != null) { for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) { expectedType = type; break; } if (expectedType == null) throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type"); } MediaPackageElement resultElement = null; try { Job job = executeService.execute(exec, params, mediaPackage, outputFilename, expectedType, load); WorkflowOperationResult result = null; if (!waitForStatus(job).isSuccess()) throw new WorkflowOperationException("Execute operation failed"); if (StringUtils.isNotBlank(job.getPayload())) { if (setWfProps) { resultElement = MediaPackageElementParser.getFromXml(job.getPayload()); final Properties properties = new Properties(); File propertiesFile = workspace.get(resultElement.getURI()); try (InputStream is = new FileInputStream(propertiesFile)) { properties.load(is); } logger.debug("Loaded {} properties from {}", properties.size(), propertiesFile); workspace.deleteFromCollection(ExecuteService.COLLECTION, propertiesFile.getName()); Map<String, String> wfProps = new HashMap<String, String>((Map) properties); result = createResult(mediaPackage, wfProps, Action.CONTINUE, job.getQueueTime()); } else { resultElement = MediaPackageElementParser.getFromXml(job.getPayload()); if (resultElement.getElementType() == MediaPackageElement.Type.Track) { Job inspectionJob = null; inspectionJob = inspectionService.inspect(resultElement.getURI()); JobBarrier barrier = new JobBarrier(job, serviceRegistry, inspectionJob); if (!barrier.waitForJobs().isSuccess()) { throw new ExecuteException("Media inspection of " + resultElement.getURI() + " failed"); } resultElement = MediaPackageElementParser.getFromXml(inspectionJob.getPayload()); } mediaPackage.add(resultElement); URI uri = workspace.moveTo(resultElement.getURI(), mediaPackage.getIdentifier().toString(), resultElement.getIdentifier(), outputFilename); resultElement.setURI(uri); if (targetFlavor != null) resultElement.setFlavor(targetFlavor); if (targetTags != null) { for (String tag : asList(targetTags)) { if (tag.startsWith("-")) resultElement.removeTag(tag.replaceAll("^-+", "")); else resultElement.addTag(tag); } } result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime()); } } else { result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime()); } logger.debug("Execute operation {} completed", operation.getId()); return result; } catch (ExecuteException e) { throw new WorkflowOperationException(e); } catch (MediaPackageException e) { throw new WorkflowOperationException("Some result element couldn't be serialized", e); } catch (NotFoundException e) { throw new WorkflowOperationException("Could not find mediapackage", e); } catch (IOException e) { throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e); } catch (MediaInspectionException e) { throw new WorkflowOperationException("Media inspection of " + resultElement.getURI() + " failed", e); } }  @Override WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context); @Override WorkflowOperationResult skip(WorkflowInstance workflowInstance, JobContext context); @Override String getId(); @Override String getDescription(); @Override void destroy(WorkflowInstance workflowInstance, JobContext context); void setExecuteService(ExecuteService service); void setWorkspace(Workspace workspace); static final String EXEC_PROPERTY; static final String PARAMS_PROPERTY; static final String LOAD_PROPERTY; static final String SOURCE_FLAVOR_PROPERTY; static final String OUTPUT_FILENAME_PROPERTY; static final String EXPECTED_TYPE_PROPERTY; static final String SOURCE_TAGS_PROPERTY; static final String TARGET_FLAVOR_PROPERTY; static final String TARGET_TAGS_PROPERTY; static final String SET_WF_PROPS_PROPERTY; }
@Test public void startParseSomePayloadTest() throws ExecuteException, MediaPackageException { try { setSomePayload(); execOnceWOH.start(workflowInstance, null); } catch (WorkflowOperationException ex) { fail("Should not throw exception" + ex.getMessage()); } }
@Test public void startParseEmptyPayloadTest() throws ExecuteException, MediaPackageException { try { setEmptyPayload(); execOnceWOH.start(workflowInstance, null); } catch (WorkflowOperationException ex) { fail("Should not throw exception" + ex.getMessage()); } }