scope = new TMLScriptRootScope(scopePrototype, _tmlContext);
rcx.setErrorReporter(scope);
rcx.putThreadLocal(TL_ROOTSCOPE, scope);
}
catch (RhinoException exc) {
return new ExpressionResult(null, false, true, new de.innovationgate.webgate.api.WGExpressionException("Error creating tmlscript context", exc.getLocalizedMessage()));
}
// Put custom additional objects into scope
if (_additionalObjects != null) {
Iterator objectNames = _additionalObjects.keySet().iterator();
String objectName;
Object object;
while (objectNames.hasNext()) {
objectName = (String) objectNames.next();
object = _additionalObjects.get(objectName);
if (objectName.startsWith("$")) {
if (objectName.equals(PARAM_SCRIPTTIMEOUT)) {
rcx.scriptTimeout = ((Number) object).intValue();
}
else if (objectName.equals(PARAM_ACTIONDEFINITION)) {
rcx.putThreadLocal(TL_ACTIONDEFINITION, object);
}
else if (objectName.equals(PARAM_ACTIONLOCATOR)) {
rcx.putThreadLocal(TL_ACTIONLOCATOR, object);
}
else if (objectName.equals(RhinoExpressionEngine.PARAM_SCRIPTNAME)) {
rcx.putThreadLocal(TL_SCRIPTNAME, object);
}
}
else {
scope.getData().putScopeObject(objectName, Context.javaToJS(object, scope));
}
}
}
// Default scope objects that may not be overwritten with custom scope objects
scope.getData().putScopeObject(SCOPEOBJECT_RUNTIME, Context.javaToJS(RhinoExpressionEngineImpl.this, scope));
try {
// Eventually disable TMLScript optimization, so Script stack traces are generated
rcx.setOptimizationLevel(RhinoContextFactory.defaultOptimizationLevel);
rcx.setGeneratingDebug(false);
if (_tmlContext.hasVariable(RhinoExpressionEngine.SESSIONVAR_ENABLE_SCRIPTSTACKTRACE)) {
Boolean tmlscriptOptimizationDisabled = (Boolean) _tmlContext.item(RhinoExpressionEngine.SESSIONVAR_ENABLE_SCRIPTSTACKTRACE);
if (tmlscriptOptimizationDisabled != null && tmlscriptOptimizationDisabled.booleanValue() == true) {
if (rcx.getOptimizationLevel() > 0) {
rcx.setOptimizationLevel(0);
}
rcx.setGeneratingDebug(true);
}
}
// Set new main TMLContext for this thread
_tmlContext.makeThreadMainContext();
// Execute
Object result = null;
try {
if (_function != null) {
result = executeFunction(_function, _overrideFunctionScope, _functionParams, rcx, scope);
}
else if (_type == ExpressionEngine.TYPE_SCRIPT) {
result = executeScript(_expression, rcx, scope);
}
else {
result = executeExpression(_expression, rcx, scope);
}
}
// Some syntax error
catch (EcmaError exc) {
String exceptionMessage = exc.getName() + " executing tmlscript: " + exc.getMessage() + "\n" + buildExceptionDetails(exc);
WGExpressionException expressionException;
String scriptStackTrace = filterScriptStackTrace(exc.getScriptStackTrace(TMLSCRIPT_NAME_FILTER));
Throwable cause = (WGUtils.isEmpty(scriptStackTrace) ? exc : null);
expressionException = new de.innovationgate.webgate.api.WGExpressionException(exceptionMessage, exc.lineSource(), cause, scriptStackTrace);
return new ExpressionResult(null, false, true, expressionException);
}
// A script timeout (deprecated, works only in debug mode)
catch (TimeoutError err) {
return new ExpressionResult(null, false, true, new de.innovationgate.webgate.api.WGExpressionException("Script execution timed out after " + err.getTimeout() + " milliseconds.",
_expression));
}
// Some java script error explicitly thrown by the code: Converted to TMLScriptException so the class can be better used outside
catch (JavaScriptException exc) {
String exceptionMessage = "JavaScript exception executing tmlscript: " + exc.getMessage() + " (Exception value: " + exc.getValue() + ")\n" + buildExceptionDetails(exc);
de.innovationgate.webgate.api.WGExpressionException expressionException = new de.innovationgate.webgate.api.WGExpressionException(exceptionMessage, exc.lineSource(), convertToTMLScriptException(exc), filterScriptStackTrace(exc.getScriptStackTrace()));
return new ExpressionResult(null, false, true, expressionException);
}
// Everything else: Wrapped java exceptions, internal rhino malfunctionings etc.
catch (RhinoException exc) {
Throwable cause = exc;
if (cause instanceof WrappedException) {
cause = ((WrappedException) exc).getCause();
}
return new ExpressionResult(null, false, true, new de.innovationgate.webgate.api.WGExpressionException("Exception executing tmlscript: " + cause.getMessage() + ".\n"
+ buildExceptionDetails(exc), exc.lineSource(), cause, exc.getScriptStackTrace()));
}
finally {
/// Remove thread main context
_tmlContext.removeThreadMainContext();
// Restore preserved ThreadLocal values
preserver.restore();
}
// Evaluate and return result;
if (result instanceof NativeJavaObject) {
result = ((NativeJavaObject) result).unwrap();
}
else if (result instanceof NativeArray) {
result = new ArrayList(Arrays.asList(rcx.getElements((NativeArray) result)));
}
else if (result instanceof Undefined) {
result = "";
}
if (result instanceof Boolean) {
Boolean bolResult = (Boolean) result;
return new ExpressionResult(result, bolResult.booleanValue(), !bolResult.booleanValue(), null);
}
else {
return new ExpressionResult(result, false, isJSFalse(result), null);
}
}
catch (Exception e) {
Logger.getLogger("wga").error("Error in TMLScript processing", e);
return new ExpressionResult(null, false, true, new de.innovationgate.webgate.api.WGExpressionException("Unexpected exception: " + e.getClass().getName() + ": " + e.getMessage(),
"(No source available)"));
}
}