use of javax.script.ScriptEngine in project logging-log4j2 by apache.
the class ScriptManager method addScript.
public void addScript(final AbstractScript script) {
final ScriptEngine engine = manager.getEngineByName(script.getLanguage());
if (engine == null) {
logger.error("No ScriptEngine found for language " + script.getLanguage() + ". Available languages are: " + languages);
return;
}
if (engine.getFactory().getParameter(KEY_THREADING) == null) {
scriptRunners.put(script.getName(), new ThreadLocalScriptRunner(script));
} else {
scriptRunners.put(script.getName(), new MainScriptRunner(engine, script));
}
if (script instanceof ScriptFile) {
final ScriptFile scriptFile = (ScriptFile) script;
final Path path = scriptFile.getPath();
if (scriptFile.isWatched() && path != null) {
watchManager.watchFile(path.toFile(), this);
}
}
}
use of javax.script.ScriptEngine in project logging-log4j2 by apache.
the class ScriptManager method fileModified.
@Override
public void fileModified(final File file) {
final ScriptRunner runner = scriptRunners.get(file.toString());
if (runner == null) {
logger.info("{} is not a running script");
return;
}
final ScriptEngine engine = runner.getScriptEngine();
final AbstractScript script = runner.getScript();
if (engine.getFactory().getParameter(KEY_THREADING) == null) {
scriptRunners.put(script.getName(), new ThreadLocalScriptRunner(script));
} else {
scriptRunners.put(script.getName(), new MainScriptRunner(engine, script));
}
}
use of javax.script.ScriptEngine in project jmeter by apache.
the class JSR223PostProcessor method process.
@Override
public void process() {
try {
ScriptEngine scriptEngine = getScriptEngine();
processFileOrScript(scriptEngine, null);
} catch (ScriptException | IOException e) {
log.error("Problem in JSR223 script, {}", getName(), e);
}
}
use of javax.script.ScriptEngine in project jmeter by apache.
the class JavaScript method executeWithNashorn.
/**
*
* @param previousResult {@link SampleResult}
* @param currentSampler {@link Sampler}
* @param jmctx {@link JMeterContext}
* @param vars {@link JMeterVariables}
* @param script Javascript code
* @param varName variable name
* @return result as String
* @throws InvalidVariableException
*/
private String executeWithNashorn(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
String resultStr = null;
try {
ScriptContext newContext = new SimpleScriptContext();
ScriptEngine engine = getInstance().getEngineByName(JavaScript.NASHORN_ENGINE_NAME);
Bindings bindings = engine.createBindings();
// Set up some objects for the script to play with
//$NON-NLS-1$
bindings.put("log", log);
//$NON-NLS-1$
bindings.put("ctx", jmctx);
//$NON-NLS-1$
bindings.put("vars", vars);
//$NON-NLS-1$
bindings.put("props", JMeterUtils.getJMeterProperties());
//$NON-NLS-1$
bindings.put("threadName", Thread.currentThread().getName());
//$NON-NLS-1$
bindings.put("sampler", currentSampler);
//$NON-NLS-1$
bindings.put("sampleResult", previousResult);
newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
Object result = engine.eval(script, newContext);
resultStr = result.toString();
if (varName != null && vars != null) {
// vars can be null if run from TestPlan
vars.put(varName, resultStr);
}
} catch (Exception e) {
log.error("Error processing Javascript: [" + script + "]\n", e);
throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
}
return resultStr;
}
use of javax.script.ScriptEngine in project jmeter by apache.
the class JSR223Assertion method getResult.
@Override
public AssertionResult getResult(SampleResult response) {
AssertionResult result = new AssertionResult(getName());
try {
ScriptEngine scriptEngine = getScriptEngine();
Bindings bindings = scriptEngine.createBindings();
bindings.put("SampleResult", response);
bindings.put("AssertionResult", result);
processFileOrScript(scriptEngine, bindings);
result.setError(false);
} catch (IOException | ScriptException e) {
log.error("Problem in JSR223 script: {}", getName(), e);
result.setError(true);
result.setFailureMessage(e.toString());
}
return result;
}
Aggregations