public SenderResult send(Alert alert) {
SenderResult result = new SenderResult();
BufferedReader reader = null;
ScriptEngine engine = null;
Subject subjectWithSession = null;
final SessionManager sessionManager = SessionManager.getInstance();
String language = null;
try {
final Config config = getConfig();
// simulate the login by getting a session ID
config.subject = sessionManager.put(config.subject, pluginComponent.getScriptTimeout() * 1000);
subjectWithSession = config.subject;
result.setSummary(createSummary(config, SUMMARY_TEMPLATE));
ByteArrayOutputStream scriptOutputStream = new ByteArrayOutputStream();
PrintWriter scriptOut = new PrintWriter(scriptOutputStream);
BitsAndFileExtension packageData = getPackageBits(config.packageId, config.repoId);
InputStream packageBits = packageData.packageBits;
String scriptFileExtension = packageData.scriptFileExtension;
language = ScriptEngineFactory.getLanguageByScriptFileExtension(scriptFileExtension);
if (language == null) {
ArrayList<String> supportedExtensions = new ArrayList<String>();
for (String lang : ScriptEngineFactory.getSupportedLanguages()) {
supportedExtensions.add(ScriptEngineFactory.getFileExtensionForLanguage(lang));
}
throw new IllegalArgumentException(
"Could not determine the script engine to use based on the script file extension '"
+ scriptFileExtension + "'. Only the following extensions are currently supported: "
+ supportedExtensions);
}
engine = getScriptEngine(alert, scriptOut, config, language, pluginComponent.getDomainPackagesNames());
reader = new BufferedReader(new InputStreamReader(packageBits));
final BufferedReader rdr = reader;
final ExceptionHolder exceptionHolder = new ExceptionHolder();
final ScriptEngine e = engine;
Thread scriptRunner = new Thread(new Runnable() {
@Override
public void run() {
try {
e.eval(rdr);
} catch (ScriptException e) {
exceptionHolder.scriptException = e;
} catch (Throwable e) {
exceptionHolder.throwable = e;
}
}
}, "Script Runner for alert " + alert);
scriptRunner.setDaemon(true);
scriptRunner.start();
if (pluginComponent.getScriptTimeout() <= 0) {
scriptRunner.join();
} else {
scriptRunner.join(pluginComponent.getScriptTimeout() * 1000);
}
scriptRunner.interrupt();
if (exceptionHolder.scriptException != null) {
LOG.info("The script execution for CLI notification of alert [" + alert + "] failed.",
exceptionHolder.scriptException);
//make things pretty for the UI
ScriptEngineInitializer initializer = ScriptEngineFactory.getInitializer(language);
String message = initializer.extractUserFriendlyErrorMessage(exceptionHolder.scriptException);
int col = exceptionHolder.scriptException.getColumnNumber();
int line = exceptionHolder.scriptException.getLineNumber();
String scriptName = createSummary(config, "script $packageName ($packageVersion) in repo $repoName");
throw new ScriptException(message, scriptName, line, col);
} else if (exceptionHolder.throwable != null) {
LOG.info("The script execution for CLI notification of alert [" + alert + "] failed.",
exceptionHolder.throwable);
throw exceptionHolder.throwable;
}
scriptOut.flush();
String scriptOutput = scriptOutputStream.toString(Charset.defaultCharset().name());
if (scriptOutput.length() == 0) {
scriptOutput = "Script generated no output.";
}
if (scriptOutput.length() > remainingResultSize(result)) {
scriptOutput = scriptOutput.substring(0, remainingResultSize(result));
}
result.addSuccessMessage(scriptOutput);
return result;
} catch (IllegalArgumentException e) {
return SenderResult.getSimpleFailure(e.getMessage()); //well, let's just hope the message doesn't exceed 4k.
} catch (Throwable e) {
result.addFailureMessage(ThrowableUtil.getAllMessages(e, true, remainingResultSize(result)));
return result;
} finally {
if (subjectWithSession != null) {
sessionManager.invalidate(subjectWithSession.getSessionId());
}
if (engine != null) {
returnEngine(engine, language);
}