@Test public void testExecShellScriptCommandWithAddEnvironmentVariables() throws Exception { TestWorkItemManager manager = new TestWorkItemManager(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("ShellScriptLocation", "src/test/resources/TestScript.sh"); Map<String, String> envVariableList = new HashMap<>(); envVariableList.put("testVariable", "tested"); workItem.setParameter("AddEnvironmentVariable", envVariableList); ExecShellScriptWorkItemHandler handler = new ExecShellScriptWorkItemHandler(); handler.setLogThrownException(true); handler.executeWorkItem(workItem, manager); assertNotNull(manager.getResults()); assertEquals(1, manager.getResults().size()); assertTrue(manager.getResults().containsKey(workItem.getId())); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); List<String> result = (List<String>) results.get(ExecWorkItemHandler.RESULT); assertNotNull(result); assertTrue(result.contains("tested")); }
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { try { RequiredParameterValidator.validate(this.getClass(), workItem); String shellScriptLocation = (String) workItem.getParameter("ShellScriptLocation"); String tout = (String) workItem.getParameter("TimeoutInMillis"); Long timeout = null; if (tout != null) { timeout = Long.parseLong(tout); } else { timeout = 1000l; } Map<String, String> addEnvironmentVariables = (Map<String, String>) workItem.getParameter("AddEnvironmentVariable"); List<String> removeEnvironmentVariables = (List<String>) workItem.getParameter("RemoveEnvironmentVariable"); logger.debug("ShellScriptLocation " + shellScriptLocation + " Timeout " + timeout); List<String> output = new ArrayList<>(); Map<String, Object> results = new HashMap<>(); Process process; try { List<String> commandList = new ArrayList<String>(); commandList.add("sh"); commandList.add(shellScriptLocation); ProcessBuilder processBuilder = new ProcessBuilder(commandList); Map<String, String> envVariables = processBuilder.environment(); if (null != addEnvironmentVariables && !addEnvironmentVariables.isEmpty()) { logger.debug("addEnvironmentVariables " + addEnvironmentVariables); envVariables.putAll(addEnvironmentVariables); } if (null != removeEnvironmentVariables && !removeEnvironmentVariables.isEmpty()) { logger.debug("removeEnvironmentVariables " + removeEnvironmentVariables); removeEnvironmentVariables.stream().forEach(variable -> envVariables.remove(variable)); } process = processBuilder.start(); if (!process.waitFor(timeout, TimeUnit.MILLISECONDS)) { logger.error("Terminating the process as the Process timed out: " + timeout); process.destroyForcibly(); throw new IOException("Process timed out"); } BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.add(line); logger.debug("Output line " + line); } } catch (IOException e) { logger.error("Error executing the work item IO Exception: " + e.getMessage()); handleException(e); } catch (InterruptedException e) { logger.error("Error executing the work item Interrupted Exception: " + e.getMessage()); handleException(e); } catch (Exception e) { logger.error("Error executing the work item with Exception: " + e.getMessage()); handleException(e); } results.put(RESULT, output); manager.completeWorkItem(workItem.getId(), results); } catch (Throwable t) { handleException(t); } }
ExecShellScriptWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { try { RequiredParameterValidator.validate(this.getClass(), workItem); String shellScriptLocation = (String) workItem.getParameter("ShellScriptLocation"); String tout = (String) workItem.getParameter("TimeoutInMillis"); Long timeout = null; if (tout != null) { timeout = Long.parseLong(tout); } else { timeout = 1000l; } Map<String, String> addEnvironmentVariables = (Map<String, String>) workItem.getParameter("AddEnvironmentVariable"); List<String> removeEnvironmentVariables = (List<String>) workItem.getParameter("RemoveEnvironmentVariable"); logger.debug("ShellScriptLocation " + shellScriptLocation + " Timeout " + timeout); List<String> output = new ArrayList<>(); Map<String, Object> results = new HashMap<>(); Process process; try { List<String> commandList = new ArrayList<String>(); commandList.add("sh"); commandList.add(shellScriptLocation); ProcessBuilder processBuilder = new ProcessBuilder(commandList); Map<String, String> envVariables = processBuilder.environment(); if (null != addEnvironmentVariables && !addEnvironmentVariables.isEmpty()) { logger.debug("addEnvironmentVariables " + addEnvironmentVariables); envVariables.putAll(addEnvironmentVariables); } if (null != removeEnvironmentVariables && !removeEnvironmentVariables.isEmpty()) { logger.debug("removeEnvironmentVariables " + removeEnvironmentVariables); removeEnvironmentVariables.stream().forEach(variable -> envVariables.remove(variable)); } process = processBuilder.start(); if (!process.waitFor(timeout, TimeUnit.MILLISECONDS)) { logger.error("Terminating the process as the Process timed out: " + timeout); process.destroyForcibly(); throw new IOException("Process timed out"); } BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.add(line); logger.debug("Output line " + line); } } catch (IOException e) { logger.error("Error executing the work item IO Exception: " + e.getMessage()); handleException(e); } catch (InterruptedException e) { logger.error("Error executing the work item Interrupted Exception: " + e.getMessage()); handleException(e); } catch (Exception e) { logger.error("Error executing the work item with Exception: " + e.getMessage()); handleException(e); } results.put(RESULT, output); manager.completeWorkItem(workItem.getId(), results); } catch (Throwable t) { handleException(t); } } }
ExecShellScriptWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { try { RequiredParameterValidator.validate(this.getClass(), workItem); String shellScriptLocation = (String) workItem.getParameter("ShellScriptLocation"); String tout = (String) workItem.getParameter("TimeoutInMillis"); Long timeout = null; if (tout != null) { timeout = Long.parseLong(tout); } else { timeout = 1000l; } Map<String, String> addEnvironmentVariables = (Map<String, String>) workItem.getParameter("AddEnvironmentVariable"); List<String> removeEnvironmentVariables = (List<String>) workItem.getParameter("RemoveEnvironmentVariable"); logger.debug("ShellScriptLocation " + shellScriptLocation + " Timeout " + timeout); List<String> output = new ArrayList<>(); Map<String, Object> results = new HashMap<>(); Process process; try { List<String> commandList = new ArrayList<String>(); commandList.add("sh"); commandList.add(shellScriptLocation); ProcessBuilder processBuilder = new ProcessBuilder(commandList); Map<String, String> envVariables = processBuilder.environment(); if (null != addEnvironmentVariables && !addEnvironmentVariables.isEmpty()) { logger.debug("addEnvironmentVariables " + addEnvironmentVariables); envVariables.putAll(addEnvironmentVariables); } if (null != removeEnvironmentVariables && !removeEnvironmentVariables.isEmpty()) { logger.debug("removeEnvironmentVariables " + removeEnvironmentVariables); removeEnvironmentVariables.stream().forEach(variable -> envVariables.remove(variable)); } process = processBuilder.start(); if (!process.waitFor(timeout, TimeUnit.MILLISECONDS)) { logger.error("Terminating the process as the Process timed out: " + timeout); process.destroyForcibly(); throw new IOException("Process timed out"); } BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.add(line); logger.debug("Output line " + line); } } catch (IOException e) { logger.error("Error executing the work item IO Exception: " + e.getMessage()); handleException(e); } catch (InterruptedException e) { logger.error("Error executing the work item Interrupted Exception: " + e.getMessage()); handleException(e); } catch (Exception e) { logger.error("Error executing the work item with Exception: " + e.getMessage()); handleException(e); } results.put(RESULT, output); manager.completeWorkItem(workItem.getId(), results); } catch (Throwable t) { handleException(t); } }  }
ExecShellScriptWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { try { RequiredParameterValidator.validate(this.getClass(), workItem); String shellScriptLocation = (String) workItem.getParameter("ShellScriptLocation"); String tout = (String) workItem.getParameter("TimeoutInMillis"); Long timeout = null; if (tout != null) { timeout = Long.parseLong(tout); } else { timeout = 1000l; } Map<String, String> addEnvironmentVariables = (Map<String, String>) workItem.getParameter("AddEnvironmentVariable"); List<String> removeEnvironmentVariables = (List<String>) workItem.getParameter("RemoveEnvironmentVariable"); logger.debug("ShellScriptLocation " + shellScriptLocation + " Timeout " + timeout); List<String> output = new ArrayList<>(); Map<String, Object> results = new HashMap<>(); Process process; try { List<String> commandList = new ArrayList<String>(); commandList.add("sh"); commandList.add(shellScriptLocation); ProcessBuilder processBuilder = new ProcessBuilder(commandList); Map<String, String> envVariables = processBuilder.environment(); if (null != addEnvironmentVariables && !addEnvironmentVariables.isEmpty()) { logger.debug("addEnvironmentVariables " + addEnvironmentVariables); envVariables.putAll(addEnvironmentVariables); } if (null != removeEnvironmentVariables && !removeEnvironmentVariables.isEmpty()) { logger.debug("removeEnvironmentVariables " + removeEnvironmentVariables); removeEnvironmentVariables.stream().forEach(variable -> envVariables.remove(variable)); } process = processBuilder.start(); if (!process.waitFor(timeout, TimeUnit.MILLISECONDS)) { logger.error("Terminating the process as the Process timed out: " + timeout); process.destroyForcibly(); throw new IOException("Process timed out"); } BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.add(line); logger.debug("Output line " + line); } } catch (IOException e) { logger.error("Error executing the work item IO Exception: " + e.getMessage()); handleException(e); } catch (InterruptedException e) { logger.error("Error executing the work item Interrupted Exception: " + e.getMessage()); handleException(e); } catch (Exception e) { logger.error("Error executing the work item with Exception: " + e.getMessage()); handleException(e); } results.put(RESULT, output); manager.completeWorkItem(workItem.getId(), results); } catch (Throwable t) { handleException(t); } }  void executeWorkItem(WorkItem workItem, WorkItemManager manager); void abortWorkItem(WorkItem workItem,
            WorkItemManager manager); }
ExecShellScriptWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { try { RequiredParameterValidator.validate(this.getClass(), workItem); String shellScriptLocation = (String) workItem.getParameter("ShellScriptLocation"); String tout = (String) workItem.getParameter("TimeoutInMillis"); Long timeout = null; if (tout != null) { timeout = Long.parseLong(tout); } else { timeout = 1000l; } Map<String, String> addEnvironmentVariables = (Map<String, String>) workItem.getParameter("AddEnvironmentVariable"); List<String> removeEnvironmentVariables = (List<String>) workItem.getParameter("RemoveEnvironmentVariable"); logger.debug("ShellScriptLocation " + shellScriptLocation + " Timeout " + timeout); List<String> output = new ArrayList<>(); Map<String, Object> results = new HashMap<>(); Process process; try { List<String> commandList = new ArrayList<String>(); commandList.add("sh"); commandList.add(shellScriptLocation); ProcessBuilder processBuilder = new ProcessBuilder(commandList); Map<String, String> envVariables = processBuilder.environment(); if (null != addEnvironmentVariables && !addEnvironmentVariables.isEmpty()) { logger.debug("addEnvironmentVariables " + addEnvironmentVariables); envVariables.putAll(addEnvironmentVariables); } if (null != removeEnvironmentVariables && !removeEnvironmentVariables.isEmpty()) { logger.debug("removeEnvironmentVariables " + removeEnvironmentVariables); removeEnvironmentVariables.stream().forEach(variable -> envVariables.remove(variable)); } process = processBuilder.start(); if (!process.waitFor(timeout, TimeUnit.MILLISECONDS)) { logger.error("Terminating the process as the Process timed out: " + timeout); process.destroyForcibly(); throw new IOException("Process timed out"); } BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.add(line); logger.debug("Output line " + line); } } catch (IOException e) { logger.error("Error executing the work item IO Exception: " + e.getMessage()); handleException(e); } catch (InterruptedException e) { logger.error("Error executing the work item Interrupted Exception: " + e.getMessage()); handleException(e); } catch (Exception e) { logger.error("Error executing the work item with Exception: " + e.getMessage()); handleException(e); } results.put(RESULT, output); manager.completeWorkItem(workItem.getId(), results); } catch (Throwable t) { handleException(t); } }  void executeWorkItem(WorkItem workItem, WorkItemManager manager); void abortWorkItem(WorkItem workItem,
            WorkItemManager manager); static final String RESULT; }