use of javax.script.ScriptException in project jmeter by apache.
the class JSR223TestElement method processFileOrScript.
/**
* This method will run inline script or file script with special behaviour for file script:
* - If ScriptEngine implements Compilable script will be compiled and cached
* - If not if will be run
* @param scriptEngine ScriptEngine
* @param bindings {@link Bindings} might be null
* @return Object returned by script
* @throws IOException when reading the script fails
* @throws ScriptException when compiling or evaluation of the script fails
*/
protected Object processFileOrScript(ScriptEngine scriptEngine, Bindings bindings) throws IOException, ScriptException {
if (bindings == null) {
bindings = scriptEngine.createBindings();
}
populateBindings(bindings);
File scriptFile = new File(getFilename());
// Hack: bsh-2.0b5.jar BshScriptEngine implements Compilable but throws
// "java.lang.Error: unimplemented"
boolean supportsCompilable = scriptEngine instanceof Compilable && // NOSONAR // $NON-NLS-1$
!("bsh.engine.BshScriptEngine".equals(scriptEngine.getClass().getName()));
try {
if (!StringUtils.isEmpty(getFilename())) {
if (scriptFile.exists() && scriptFile.canRead()) {
if (supportsCompilable) {
String cacheKey = // $NON-NLS-1$
getScriptLanguage() + "#" + scriptFile.getAbsolutePath() + // $NON-NLS-1$
"#" + scriptFile.lastModified();
CompiledScript compiledScript = compiledScriptsCache.get(cacheKey);
if (compiledScript == null) {
synchronized (compiledScriptsCache) {
compiledScript = compiledScriptsCache.get(cacheKey);
if (compiledScript == null) {
// TODO Charset ?
try (BufferedReader fileReader = new BufferedReader(new FileReader(scriptFile), (int) scriptFile.length())) {
compiledScript = ((Compilable) scriptEngine).compile(fileReader);
compiledScriptsCache.put(cacheKey, compiledScript);
}
}
}
}
return compiledScript.eval(bindings);
} else {
// TODO Charset ?
try (BufferedReader fileReader = new BufferedReader(new FileReader(scriptFile), (int) scriptFile.length())) {
return scriptEngine.eval(fileReader, bindings);
}
}
} else {
throw new ScriptException("Script file '" + scriptFile.getAbsolutePath() + "' does not exist or is unreadable for element:" + getName());
}
} else if (!StringUtils.isEmpty(getScript())) {
if (supportsCompilable && !StringUtils.isEmpty(cacheKey)) {
computeScriptMD5();
CompiledScript compiledScript = compiledScriptsCache.get(this.scriptMd5);
if (compiledScript == null) {
synchronized (compiledScriptsCache) {
compiledScript = compiledScriptsCache.get(this.scriptMd5);
if (compiledScript == null) {
compiledScript = ((Compilable) scriptEngine).compile(getScript());
compiledScriptsCache.put(this.scriptMd5, compiledScript);
}
}
}
return compiledScript.eval(bindings);
} else {
return scriptEngine.eval(getScript(), bindings);
}
} else {
throw new ScriptException("Both script file and script text are empty for element:" + getName());
}
} catch (ScriptException ex) {
Throwable rootCause = ex.getCause();
if (isStopCondition(rootCause)) {
throw (RuntimeException) ex.getCause();
} else {
throw ex;
}
}
}
use of javax.script.ScriptException 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;
}
use of javax.script.ScriptException in project jmeter by apache.
the class JSR223Listener method sampleOccurred.
@Override
public void sampleOccurred(SampleEvent event) {
try {
ScriptEngine scriptEngine = getScriptEngine();
Bindings bindings = scriptEngine.createBindings();
bindings.put("sampleEvent", event);
bindings.put("sampleResult", event.getResult());
processFileOrScript(scriptEngine, bindings);
} catch (ScriptException | IOException e) {
log.error("Problem in JSR223 script, {}", getName(), e);
}
}
use of javax.script.ScriptException in project sling by apache.
the class RhinoJavaScriptEngineTest method testPreserveScopeBetweenEvals.
public void testPreserveScopeBetweenEvals() throws ScriptException {
MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
Bindings context = new SimpleBindings();
engine.eval("var f = 1", context);
Object result = null;
try {
result = engine.eval("f += 1", context);
} catch (ScriptException e) {
TestCase.fail(e.getMessage());
}
assertTrue(result instanceof Double);
assertEquals(2.0, result);
}
use of javax.script.ScriptException in project sling by apache.
the class SightlyScriptEngine method eval.
@Override
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
checkArguments(reader, scriptContext);
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
final SlingHttpServletRequest request = slingBindings.getRequest();
if (request == null) {
throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
}
final Object oldValue = request.getAttribute(SlingBindings.class.getName());
try {
request.setAttribute(SlingBindings.class.getName(), slingBindings);
SightlyCompiledScript compiledScript = internalCompile(reader, scriptContext);
return compiledScript.eval(scriptContext);
} catch (Exception e) {
throw new ScriptException(e);
} finally {
request.setAttribute(SlingBindings.class.getName(), oldValue);
}
}
Aggregations