Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ExecuteScript extends Action {
- public static final String SCRIPT_FILE_NAME = "ScriptName",
- PARAMETERS = "Parameters",
- SCRIPT_DIRECTORY = "ScriptDirectory",
- ASYNC = "Async";
- @Override
- protected Result run(StepContext stepContext, MatrixContext matrixContext,
- GlobalContext globalContext) throws ResultException, FailoverException
- {
- if (inputParams.size() == 0)
- return noParametersResult();
- String command = buildScriptPath(getInputParams());
- String parameters = buildScriptParameters(getInputParams());
- if (logger.isDebugEnabled())
- logger.debug("Script location: " + command + ". Parameters: " + parameters);
- if (parameters != null && !StringUtils.isEmpty(parameters.trim()))
- command += " " + parameters.trim();
- return executeScript(command);
- }
- protected Result noParametersResult()
- {
- return DefaultResult.failed(String.format("No parameters specified. "
- + "Required parameter: %s. Optional parameters: %s, %s, %s.", SCRIPT_FILE_NAME, SCRIPT_DIRECTORY, PARAMETERS, ASYNC));
- }
- protected String buildScriptPath(Map<String, String> parameters) throws ResultException
- {
- String scriptName = InputParamsUtils.getRequiredString(parameters, SCRIPT_FILE_NAME),
- scriptDir = InputParamsUtils.getStringOrDefault(parameters, SCRIPT_DIRECTORY, null);
- File scriptFile;
- if (scriptDir != null)
- {
- File dir = new File(rootRelative(scriptDir));
- if (!dir.isDirectory())
- throw new ResultException("'"+dir.getAbsolutePath()+"' does not exist or is not a directory");
- scriptFile = new File (dir, scriptName);
- }
- else
- scriptFile = new File(rootRelative(scriptName));
- if (!scriptFile.isFile())
- throw new ResultException("'"+scriptFile.getAbsolutePath()+"' does not exist or is a directory");
- return scriptFile.getAbsolutePath();
- }
- protected String buildScriptParameters(Map<String, String> parameters) throws ResultException
- {
- return parameters.get(PARAMETERS);
- }
- protected ScriptResult doExecuteScript(String command)
- {
- ScriptResult res;
- try
- {
- res = Utils.executeScript(command, null);
- }
- catch (IOException e)
- {
- throw ResultException.failed("Error while executing script '"+command+"'", e);
- }
- if (logger.isDebugEnabled())
- logger.debug("Script executed. "+res.toString());
- return res;
- }
- protected Result executeScriptSync(String command)
- {
- ScriptResult res = doExecuteScript(command);
- Result result = new DefaultResult();
- result.setComment(String.format("Script finished. Code: %d." + Utils.EOL
- + " Output: %s"+ Utils.EOL
- + " Error: %s", res.result, res.outStr, res.errStr));
- result.setSuccess(res.result == 0);
- return result;
- }
- protected Result executeScriptAsync(final String command)
- {
- new Thread(new Runnable()
- {
- @Override
- public void run()
- {
- try
- {
- ScriptResult res = Utils.executeScript(command);
- if (logger.isDebugEnabled())
- logger.debug("Script executed asynchronously, triggered by action '"+getIdInMatrix()+"' from matrix '"+getMatrix().getName()+"'. "+res.toString());
- }
- catch (IOException e)
- {
- logger.error("Error while executing script '"+command+"' asynchronously triggered by action '"+getIdInMatrix()+"' from matrix '"+getMatrix().getName()+"'", e);
- }
- }
- }).start();
- return DefaultResult.passed("Script execution started asynchronously");
- }
- protected Result executeScript(String command)
- {
- if (!InputParamsUtils.getBooleanOrDefault(getInputParams(), ASYNC, false))
- return executeScriptSync(command);
- return executeScriptAsync(command);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement