use of com.evolveum.midpoint.model.common.expression.script.ScriptExpression in project midpoint by Evolveum.
the class Clockwork method evaluateScriptingHook.
private void evaluateScriptingHook(LensContext context, HookType hookType, ScriptExpressionEvaluatorType scriptExpressionEvaluatorType, String shortDesc, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
LOGGER.trace("Evaluating {}", shortDesc);
// TODO: it would be nice to cache this
// null output definition: this script has no output
ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptExpressionEvaluatorType, null, shortDesc);
ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinition(ExpressionConstants.VAR_PRISM_CONTEXT, prismContext);
variables.addVariableDefinition(ExpressionConstants.VAR_MODEL_CONTEXT, context);
LensFocusContext focusContext = context.getFocusContext();
PrismObject focus = null;
if (focusContext != null) {
focus = focusContext.getObjectAny();
}
variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, focus);
Utils.evaluateScript(scriptExpression, context, variables, false, shortDesc, task, result);
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpression in project midpoint by Evolveum.
the class ScriptExecutor method execute.
@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
checkRootAuthorization(globalResult, NAME);
ScriptExpressionEvaluatorType script = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_SCRIPT, true, true, NAME, input, context, ScriptExpressionEvaluatorType.class, globalResult);
String outputItem = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
ItemDefinition<?> outputDefinition = getItemDefinition(outputItem);
ScriptExpression scriptExpression;
try {
scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, "script");
} catch (ExpressionSyntaxException e) {
throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
}
PipelineData output = PipelineData.createEmpty();
for (PipelineItem item : input.getData()) {
PrismValue value = item.getValue();
OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
context.checkTaskStop();
String valueDescription;
long started;
if (value instanceof PrismObjectValue) {
started = operationsHelper.recordStart(context, asObjectType(value));
valueDescription = asObjectType(value).asPrismObject().toString();
} else {
started = 0;
valueDescription = value.toHumanReadableString();
}
Throwable exception = null;
try {
Object outObject = executeScript(scriptExpression, value, context, result);
if (outObject != null) {
addToData(outObject, item.getResult(), output);
} else {
output.add(item);
}
if (value instanceof PrismObjectValue) {
operationsHelper.recordEnd(context, asObjectType(value), started, null);
}
} catch (Throwable ex) {
if (value instanceof PrismObjectValue) {
operationsHelper.recordEnd(context, asObjectType(value), started, ex);
}
exception = processActionException(ex, NAME, value, context);
}
context.println((exception != null ? "Attempted to execute " : "Executed ") + "script on " + valueDescription + exceptionSuffix(exception));
operationsHelper.trimAndCloneResult(result, globalResult, context);
}
return output;
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpression in project midpoint by Evolveum.
the class BasicExpressionFunctions method getPropertyValues.
public <T> Collection<T> getPropertyValues(ObjectType object, String path) {
if (object == null) {
return null;
}
ScriptExpressionEvaluationContext scriptContext = ScriptExpressionEvaluationContext.getThreadLocal();
ScriptExpression scriptExpression = scriptContext.getScriptExpression();
ItemPath itemPath = scriptExpression.parsePath(path);
PrismProperty property = object.asPrismObject().findProperty(itemPath);
if (property == null) {
return new ArrayList<T>(0);
}
return property.getRealValues();
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpression in project midpoint by Evolveum.
the class ClockworkHookHelper method evaluateScriptingHook.
private void evaluateScriptingHook(LensContext<?> context, ScriptExpressionEvaluatorType scriptExpressionEvaluatorType, String shortDesc, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
LOGGER.trace("Evaluating {}", shortDesc);
// TODO: it would be nice to cache this
// null output definition: this script has no output
ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptExpressionEvaluatorType, null, context.getPrivilegedExpressionProfile(), expressionFactory, shortDesc, result);
VariablesMap variables = new VariablesMap();
variables.put(ExpressionConstants.VAR_PRISM_CONTEXT, prismContext, PrismContext.class);
variables.put(ExpressionConstants.VAR_MODEL_CONTEXT, context, ModelContext.class);
LensFocusContext<?> focusContext = context.getFocusContext();
if (focusContext != null) {
variables.put(ExpressionConstants.VAR_FOCUS, focusContext.getObjectAny(), focusContext.getObjectDefinition());
} else {
variables.put(ExpressionConstants.VAR_FOCUS, null, FocusType.class);
}
ModelImplUtils.evaluateScript(scriptExpression, context, variables, false, shortDesc, task, result);
LOGGER.trace("Finished evaluation of {}", shortDesc);
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpression in project midpoint by Evolveum.
the class ExecuteScriptExecutor method getParameters.
private Parameters getParameters(ActionExpressionType action, PipelineData input, ExecutionContext context, OperationResult globalResult) throws CommunicationException, ObjectNotFoundException, SchemaException, ScriptExecutionException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
ScriptExpressionEvaluatorType script = expressionHelper.getActionArgument(ScriptExpressionEvaluatorType.class, action, ExecuteScriptActionExpressionType.F_SCRIPT, PARAM_SCRIPT, input, context, null, PARAM_SCRIPT, globalResult);
ItemDefinition<?> outputDefinition;
String outputItemUri = expressionHelper.getSingleArgumentValue(action.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
if (StringUtils.isNotBlank(outputItemUri)) {
outputDefinition = getItemDefinition(outputItemUri);
} else if (action instanceof ExecuteScriptActionExpressionType) {
ExecuteScriptActionExpressionType execute = (ExecuteScriptActionExpressionType) action;
if (execute.getOutputItemName() != null) {
outputDefinition = getItemDefinitionFromItemName(execute.getOutputItemName());
} else if (execute.getOutputTypeName() != null) {
outputDefinition = getItemDefinitionFromTypeName(execute.getOutputTypeName());
} else {
outputDefinition = null;
}
} else {
outputDefinition = null;
}
boolean forWholeInput = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_FOR_WHOLE_INPUT, PARAM_FOR_WHOLE_INPUT, input, context, false, PARAM_FOR_WHOLE_INPUT, globalResult);
boolean quiet = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_QUIET, PARAM_QUIET, input, context, false, PARAM_QUIET, globalResult);
if (script == null) {
throw new IllegalArgumentException("No script provided");
}
// TODO
ExpressionProfile expressionProfile = null;
ScriptExpression scriptExpression;
try {
scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, expressionProfile, expressionFactory, "script", globalResult);
} catch (ExpressionSyntaxException | SecurityViolationException e) {
throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
}
return new Parameters(scriptExpression, outputDefinition, forWholeInput, quiet);
}
Aggregations