TaskExecutor extends BaseAction<FileSystemEvent> implements
		Action<FileSystemEvent> { public Queue<FileSystemEvent> execute(Queue<FileSystemEvent> events) throws ActionException { listenerForwarder.started(); if (configuration == null) { final ActionException e = new ActionException(this, "DataFlowConfig is null."); listenerForwarder.failed(e); throw e; } if (events == null || events.size() == 0) { final ActionException e = new ActionException(this, "Empty or null incoming events list"); listenerForwarder.failed(e); throw e; } Queue<FileSystemEvent> outEvents = new LinkedList<FileSystemEvent>(); while (events.size() > 0) { final FileSystemEvent event = events.remove(); final File inputFile = event.getSource(); if (inputFile == null) { final ActionException e = new ActionException(this, "Input File is null"); listenerForwarder.failed(e); throw e; } if (!inputFile.exists()) { final ActionException e = new ActionException(this, "Input File doesn't exist"); listenerForwarder.failed(e); throw e; } final String inputFilePath = inputFile.getAbsolutePath(); final String inputFileExt = FilenameUtils.getExtension(inputFilePath); final String xslPath = configuration.getXsl(); final boolean useDefaultScript; String defaultScriptPath = configuration.getDefaultScript(); if (inputFileExt.equalsIgnoreCase("xml")) { if (LOGGER.isInfoEnabled()) LOGGER.info("Using input file as script: " + inputFilePath); defaultScriptPath = inputFilePath; useDefaultScript = false; } else { if (LOGGER.isInfoEnabled()) LOGGER.info("Using default script: " + configuration.getDefaultScript()); useDefaultScript = true; } final String outputName = configuration.getOutputName(); File xslFile = null; InputStream is = null; try { if (xslPath != null && xslPath.trim().length() > 0) { final String path=Path.findLocation(xslPath,getConfigDir().getAbsolutePath()); if (path==null){ final ActionException e = new ActionException(this, "XSL file not found: " + path); listenerForwarder.failed(e); throw e; } xslFile = new File(path); } if (!xslFile.exists()) { final ActionException e = new ActionException(this, "XSL file not found: " + xslPath); listenerForwarder.failed(e); throw e; } File xmlFile = null; String outputFile = null; if (useDefaultScript) { if (defaultScriptPath != null && defaultScriptPath.trim().length() > 0) { final String path = Path.findLocation(xslPath, getConfigDir().getAbsolutePath()); if (path == null){ final ActionException e = new ActionException(this, "XSL file not found: " + path); listenerForwarder.failed(e); throw e; } xmlFile = new File(path); final File outXmlFile = File.createTempFile("script",".xml", getTempDir()); outputFile = setScriptArguments( xmlFile.getAbsolutePath(), inputFilePath, outputName, outXmlFile); xmlFile = outXmlFile; } } else { xmlFile = inputFile; } if (!xmlFile.exists()) { final ActionException e = new ActionException(this, "XML file not found: " + xmlFile); listenerForwarder.failed(e); throw e; } final Source xmlSource = new StreamSource(xmlFile); is = new FileInputStream(xslFile); final String argument = buildArgument(xmlSource, is); if (LOGGER.isDebugEnabled()){ LOGGER.debug("Arguments: "+argument); } final Project project = new Project(); project.init(); final ExecTask execTask = new ExecTask(); execTask.setProject(project); Map<String, String> variables = configuration.getVariables(); if (variables != null && !variables.isEmpty()) { for (String key : variables.keySet()) { Variable var = new Variable(); var.setKey(key); final String value = variables.get(key); if (value != null) { var.setValue(variables.get(key)); execTask.addEnv(var); } } } execTask.setExecutable(configuration.getExecutable()); final String errorFileName = configuration.getErrorFile(); if (errorFileName != null ){ File errorFile = new File(errorFileName); if (!errorFile.exists()) { errorFile = Path.findLocation(errorFileName, getTempDir()); if (errorFile != null && !errorFile.exists()) { try { errorFile.createNewFile(); } catch (Throwable t) { final ActionException e = new ActionException( this, t.getLocalizedMessage(), t); listenerForwarder.failed(e); throw e; } } } if (errorFile.exists()) { if(LOGGER.isDebugEnabled()) LOGGER.debug("Using error file: " + errorFile); execTask.setLogError(true); execTask.setAppend(true); execTask.setError(errorFile); execTask.setFailonerror(true); } } Long timeOut = configuration.getTimeOut(); if (timeOut != null) { execTask.setTimeout(timeOut); } execTask.createArg().setLine(argument); File output = null; if (configuration.getOutput() != null) { output = new File(configuration.getOutput()); if (output.exists() && output.isDirectory()) { final File outXmlFile = File.createTempFile("script",".xml",getTempDir()); String destFile = getScriptArguments(xmlFile.getAbsolutePath(), "srcfile"); if (output.isAbsolute()) { output = new File(output, FilenameUtils.getBaseName(destFile) + configuration.getOutputName().substring(configuration.getOutputName().indexOf("."))); } else { output = Path.findLocation( configuration.getOutput(), inputFile.getParentFile()); output = new File( output, FilenameUtils.getBaseName(inputFile .getName()) + configuration .getOutputName() .substring( configuration .getOutputName() .indexOf( "."))); } } execTask.setOutput(output); } execTask.execute(); File outFile = (outputFile != null ? new File(outputFile) : null); if (configuration.getOutput() != null) { if (new File(configuration.getOutput()).isAbsolute()) { if (output.exists() && output.isFile()) { final File outXmlFile = File.createTempFile("script", ".xml", getTempDir()); outputFile = setScriptArguments( xmlFile.getAbsolutePath(), output.getAbsolutePath(), outputName, outXmlFile); outFile = new File(configuration.getOutput(), FilenameUtils.getBaseName(outputFile)+ ".xml"); FileUtils.copyFile(outXmlFile, outFile); } } else { if (outFile == null) outFile = inputFile; } } else if (outFile == null) { outFile = inputFile; } outEvents.add(new FileSystemEvent(outFile, FileSystemEventType.FILE_ADDED)); } catch (Exception e) { listenerForwarder.failed(e); throw new ActionException(this, e.getMessage(), e); } finally { if (is != null) org.apache.commons.io.IOUtils.closeQuietly(is); } } listenerForwarder.completed(); return outEvents; } TaskExecutor(final TaskExecutorConfiguration configuration); @Override @CheckConfiguration boolean checkConfiguration(); Queue<FileSystemEvent> execute(Queue<FileSystemEvent> events);  }
@Test @Ignore public void testTaskExecution() throws Exception { TaskExecutorConfiguration configuration = new TaskExecutorConfiguration("ID","NAME","DESC"); configuration.setExecutable("c:/Python26/python.exe"); configuration.setErrorFile("errorlog.txt"); configuration.setTimeOut(new Long(10000)); final Map<String, String> variables = new HashMap<String, String>(); variables.put("GDAL_DATA", "data"); variables.put("PATH", "C:\\Python26"); configuration.setVariables(variables); TaskExecutor executor = new TaskExecutor(configuration); executor.execute(null); }