Advertisement
ibragimova_mariam

ScriptExecute

Aug 29th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. public class ExecuteScript extends Action {
  2.  
  3. public static final String SCRIPT_FILE_NAME = "ScriptName",
  4. PARAMETERS = "Parameters",
  5. SCRIPT_DIRECTORY = "ScriptDirectory",
  6. ASYNC = "Async";
  7.  
  8.  
  9. @Override
  10. protected Result run(StepContext stepContext, MatrixContext matrixContext,
  11. GlobalContext globalContext) throws ResultException, FailoverException
  12. {
  13. if (inputParams.size() == 0)
  14. return noParametersResult();
  15.  
  16. String command = buildScriptPath(getInputParams());
  17. String parameters = buildScriptParameters(getInputParams());
  18. if (logger.isDebugEnabled())
  19. logger.debug("Script location: " + command + ". Parameters: " + parameters);
  20.  
  21. if (parameters != null && !StringUtils.isEmpty(parameters.trim()))
  22. command += " " + parameters.trim();
  23.  
  24. return executeScript(command);
  25. }
  26.  
  27.  
  28. protected Result noParametersResult()
  29. {
  30. return DefaultResult.failed(String.format("No parameters specified. "
  31. + "Required parameter: %s. Optional parameters: %s, %s, %s.", SCRIPT_FILE_NAME, SCRIPT_DIRECTORY, PARAMETERS, ASYNC));
  32. }
  33.  
  34. protected String buildScriptPath(Map<String, String> parameters) throws ResultException
  35. {
  36. String scriptName = InputParamsUtils.getRequiredString(parameters, SCRIPT_FILE_NAME),
  37. scriptDir = InputParamsUtils.getStringOrDefault(parameters, SCRIPT_DIRECTORY, null);
  38.  
  39. File scriptFile;
  40. if (scriptDir != null)
  41. {
  42. File dir = new File(rootRelative(scriptDir));
  43. if (!dir.isDirectory())
  44. throw new ResultException("'"+dir.getAbsolutePath()+"' does not exist or is not a directory");
  45.  
  46. scriptFile = new File (dir, scriptName);
  47. }
  48. else
  49. scriptFile = new File(rootRelative(scriptName));
  50.  
  51. if (!scriptFile.isFile())
  52. throw new ResultException("'"+scriptFile.getAbsolutePath()+"' does not exist or is a directory");
  53. return scriptFile.getAbsolutePath();
  54. }
  55.  
  56. protected String buildScriptParameters(Map<String, String> parameters) throws ResultException
  57. {
  58. return parameters.get(PARAMETERS);
  59. }
  60.  
  61.  
  62. protected ScriptResult doExecuteScript(String command)
  63. {
  64. ScriptResult res;
  65. try
  66. {
  67. res = Utils.executeScript(command, null);
  68. }
  69. catch (IOException e)
  70. {
  71. throw ResultException.failed("Error while executing script '"+command+"'", e);
  72. }
  73.  
  74. if (logger.isDebugEnabled())
  75. logger.debug("Script executed. "+res.toString());
  76. return res;
  77. }
  78.  
  79. protected Result executeScriptSync(String command)
  80. {
  81. ScriptResult res = doExecuteScript(command);
  82.  
  83. Result result = new DefaultResult();
  84. result.setComment(String.format("Script finished. Code: %d." + Utils.EOL
  85. + " Output: %s"+ Utils.EOL
  86. + " Error: %s", res.result, res.outStr, res.errStr));
  87. result.setSuccess(res.result == 0);
  88.  
  89. return result;
  90. }
  91.  
  92. protected Result executeScriptAsync(final String command)
  93. {
  94. new Thread(new Runnable()
  95. {
  96. @Override
  97. public void run()
  98. {
  99. try
  100. {
  101. ScriptResult res = Utils.executeScript(command);
  102. if (logger.isDebugEnabled())
  103. logger.debug("Script executed asynchronously, triggered by action '"+getIdInMatrix()+"' from matrix '"+getMatrix().getName()+"'. "+res.toString());
  104. }
  105. catch (IOException e)
  106. {
  107. logger.error("Error while executing script '"+command+"' asynchronously triggered by action '"+getIdInMatrix()+"' from matrix '"+getMatrix().getName()+"'", e);
  108. }
  109. }
  110. }).start();
  111.  
  112. return DefaultResult.passed("Script execution started asynchronously");
  113. }
  114.  
  115. protected Result executeScript(String command)
  116. {
  117. if (!InputParamsUtils.getBooleanOrDefault(getInputParams(), ASYNC, false))
  118. return executeScriptSync(command);
  119. return executeScriptAsync(command);
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement