use of org.eclipse.smarthome.model.core.ModelRepository in project smarthome by eclipse.
the class ScriptExecution method callScript.
/**
* Calls a script which must be located in the configurations/scripts folder.
*
* @param scriptName the name of the script (if the name does not end with
* the .script file extension it is added)
*
* @return the return value of the script
* @throws ScriptExecutionException if an error occurs during the execution
*/
public static Object callScript(String scriptName) throws ScriptExecutionException {
ModelRepository repo = ScriptServiceUtil.getModelRepository();
if (repo != null) {
String scriptNameWithExt = scriptName;
if (!StringUtils.endsWith(scriptName, Script.SCRIPT_FILEEXT)) {
scriptNameWithExt = scriptName + "." + Script.SCRIPT_FILEEXT;
}
XExpression expr = (XExpression) repo.getModel(scriptNameWithExt);
if (expr != null) {
ScriptEngine scriptEngine = ScriptServiceUtil.getScriptEngine();
if (scriptEngine != null) {
Script script = scriptEngine.newScriptFromXExpression(expr);
return script.execute();
} else {
throw new ScriptExecutionException("Script engine is not available.");
}
} else {
throw new ScriptExecutionException("Script '" + scriptName + "' cannot be found.");
}
} else {
throw new ScriptExecutionException("Model repository is not available.");
}
}
Aggregations