use of javax.script.Bindings in project incubator-atlas by apache.
the class Titan0Graph method executeGremlinScript.
private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
Object result = null;
ScriptEngine engine = getGremlinScriptEngine();
try {
Bindings bindings = engine.createBindings();
bindings.put("g", getGraph());
result = engine.eval(gremlinQuery, bindings);
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
} finally {
releaseGremlinScriptEngine(engine);
}
return result;
}
use of javax.script.Bindings in project jmeter by apache.
the class JSR223Sampler method sample.
@Override
public SampleResult sample(Entry entry) {
SampleResult result = new SampleResult();
result.setSampleLabel(getName());
result.setSuccessful(true);
result.setResponseCodeOK();
result.setResponseMessageOK();
final String filename = getFilename();
if (filename.length() > 0) {
result.setSamplerData("File: " + filename);
} else {
result.setSamplerData(getScript());
}
result.setDataType(SampleResult.TEXT);
result.sampleStart();
try {
ScriptEngine scriptEngine = getScriptEngine();
Bindings bindings = scriptEngine.createBindings();
bindings.put("SampleResult", result);
Object ret = processFileOrScript(scriptEngine, bindings);
if (ret != null && (result.getResponseData() == null || result.getResponseData().length == 0)) {
result.setResponseData(ret.toString(), null);
}
} catch (IOException | ScriptException e) {
log.error("Problem in JSR223 script {}, message: {}", getName(), e, e);
result.setSuccessful(false);
// $NON-NLS-1$
result.setResponseCode("500");
result.setResponseMessage(e.toString());
}
result.sampleEnd();
return result;
}
use of javax.script.Bindings in project jmeter by apache.
the class Groovy method execute.
/** {@inheritDoc} */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
Bindings bindings = scriptEngine.createBindings();
populateBindings(bindings);
String script = ((CompoundVariable) values[0]).execute();
//$NON-NLS-1$
String varName = "";
if (values.length > 1) {
varName = ((CompoundVariable) values[1]).execute().trim();
}
//$NON-NLS-1$
String resultStr = "";
try {
// Pass in some variables
if (currentSampler != null) {
// $NON-NLS-1$
bindings.put("sampler", currentSampler);
}
if (previousResult != null) {
//$NON-NLS-1$
bindings.put("prev", previousResult);
}
// $NON-NLS-1$ (this name is fixed)
bindings.put("log", log);
// Add variables for access to context and variables
bindings.put("threadName", Thread.currentThread().getName());
JMeterContext jmctx = JMeterContextService.getContext();
// $NON-NLS-1$ (this name is fixed)
bindings.put("ctx", jmctx);
JMeterVariables vars = jmctx.getVariables();
// $NON-NLS-1$ (this name is fixed)
bindings.put("vars", vars);
Properties props = JMeterUtils.getJMeterProperties();
// $NON-NLS-1$ (this name is fixed)
bindings.put("props", props);
// For use in debugging:
// $NON-NLS-1$ (this name is fixed)
bindings.put("OUT", System.out);
// Execute the script
Object out = scriptEngine.eval(script, bindings);
if (out != null) {
resultStr = out.toString();
}
if (varName.length() > 0) {
// vars will be null on TestPlan
if (vars != null) {
vars.put(varName, resultStr);
}
}
} catch (// Mainly for bsh.EvalError
Exception ex) {
log.warn("Error running groovy script", ex);
}
if (log.isDebugEnabled()) {
log.debug("__groovy(" + script + "," + varName + ")=" + resultStr);
}
return resultStr;
}
use of javax.script.Bindings 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.Bindings in project logging-log4j2 by apache.
the class Routes method getPattern.
/**
* Returns the pattern.
* @param event The log event passed to the script (if there is a script.)
* @param scriptStaticVariables The script's static variables.
* @return the pattern.
*/
public String getPattern(final LogEvent event, final ConcurrentMap<Object, Object> scriptStaticVariables) {
if (patternScript != null) {
final ScriptManager scriptManager = configuration.getScriptManager();
final Bindings bindings = scriptManager.createBindings(patternScript);
bindings.put(STATIC_VARIABLES_KEY, scriptStaticVariables);
bindings.put(LOG_EVENT_KEY, event);
final Object object = scriptManager.execute(patternScript.getName(), bindings);
bindings.remove(LOG_EVENT_KEY);
return Objects.toString(object, null);
}
return pattern;
}
Aggregations