LoggingWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { WorkflowOperationInstance operation = workflowInstance.getCurrentOperation(); String directoryPath = StringUtils.trimToNull(operation.getConfiguration("directory")); File directory = null; if (directoryPath != null) { directory = new File(directoryPath); try { FileUtils.forceMkdir(directory); } catch (IOException e) { throw new WorkflowOperationException(String.format("Failed to create output directory '%s'", directoryPath), e); } } boolean logMediaPackageJSON = BooleanUtils.toBoolean(operation.getConfiguration("mediapackage-json")); boolean logMediaPackageXML = BooleanUtils.toBoolean(operation.getConfiguration("mediapackage-xml")); boolean logWorkflowInstance = BooleanUtils.toBoolean(operation.getConfiguration("workflowinstance-xml")); if (!(logMediaPackageJSON || logMediaPackageXML || logWorkflowInstance)) { logMediaPackageJSON = true; } MediaPackage mediaPackage = workflowInstance.getMediaPackage(); if (logMediaPackageJSON) { String filename = String.format("workflow-%d-%d-mediapackage-%s.json", workflowInstance.getId(), operation.getId(), mediaPackage.getIdentifier()); saveOrLog(MediaPackageParser.getAsJSON(mediaPackage), directory, filename); } if (logMediaPackageXML) { String filename = String.format("workflow-%d-%d-mediapackage-%s.xml", workflowInstance.getId(), operation.getId(), mediaPackage.getIdentifier()); saveOrLog(MediaPackageParser.getAsXml(mediaPackage), directory, filename); } if (logWorkflowInstance) { String filename = String.format("workflow-%d-%d.xml", workflowInstance.getId(), operation.getId()); try { saveOrLog(WorkflowParser.toXml(workflowInstance), directory, filename); } catch (WorkflowParsingException e) { throw new WorkflowOperationException(e); } } return createResult(mediaPackage, Action.CONTINUE); }  @Override WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context);  }
@Test public void testSimpleLog() throws Exception { EasyMock.replay(workflow, instance); operation.start(workflow, null); }
@Test public void testLogToFile() throws Exception { File tempFolder = testFolder.newFolder("logging.test"); EasyMock.expect(instance.getConfiguration("directory")).andReturn(tempFolder.getAbsolutePath()).anyTimes(); EasyMock.expect(instance.getConfiguration("mediapackage-xml")).andReturn("true").anyTimes(); EasyMock.expect(instance.getConfiguration("workflowinstance-xml")).andReturn("true").anyTimes(); EasyMock.replay(workflow, instance); operation.start(workflow, null); Assert.assertEquals(3, FileUtils.listFilesAndDirs(tempFolder, TrueFileFilter.TRUE, TrueFileFilter.TRUE).size()); }