use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType 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.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType in project midpoint by Evolveum.
the class ScriptExpressionEvaluatorFactory method createEvaluator.
/* (non-Javadoc)
* @see com.evolveum.midpoint.common.expression.ExpressionEvaluatorFactory#createEvaluator(javax.xml.bind.JAXBElement, com.evolveum.midpoint.prism.ItemDefinition)
*/
@Override
public <V extends PrismValue, D extends ItemDefinition> ExpressionEvaluator<V, D> createEvaluator(Collection<JAXBElement<?>> evaluatorElements, D outputDefinition, String contextDescription, Task task, OperationResult result) throws SchemaException {
if (evaluatorElements.size() > 1) {
throw new SchemaException("More than one evaluator specified in " + contextDescription);
}
JAXBElement<?> evaluatorElement = evaluatorElements.iterator().next();
Object evaluatorElementObject = evaluatorElement.getValue();
if (!(evaluatorElementObject instanceof ScriptExpressionEvaluatorType)) {
throw new IllegalArgumentException("Script expression cannot handle elements of type " + evaluatorElementObject.getClass().getName());
}
ScriptExpressionEvaluatorType scriptType = (ScriptExpressionEvaluatorType) evaluatorElementObject;
ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptType, outputDefinition, contextDescription);
return new ScriptExpressionEvaluator<>(scriptType, scriptExpression, securityEnforcer);
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType in project midpoint by Evolveum.
the class AbstractScriptTest method evaluateStringExpression.
private List<PrismPropertyValue<String>> evaluateStringExpression(String fileName, String testName, VariablesMap variables) throws ObjectNotFoundException, CommunicationException, SecurityViolationException, SchemaException, IOException, ExpressionEvaluationException, ConfigurationException {
ScriptExpressionEvaluatorType scriptType = parseScriptType(fileName);
OperationResult opResult = createOperationResult();
return evaluateExpression(scriptType, DOMUtil.XSD_STRING, true, variables, testName, opResult);
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType 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);
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType in project midpoint by Evolveum.
the class TestModelExpressions method testGetManagersOids.
@Test
public void testGetManagersOids() throws Exception {
// GIVEN
OperationResult result = createOperationResult();
String shortTestName = getTestNameShort();
PrismObject<UserType> chef = repositoryService.getObject(UserType.class, CHEF_OID, null, result);
ScriptExpressionEvaluatorType scriptType = parseScriptType("expression-" + shortTestName + ".xml");
PrismPropertyDefinition<String> outputDefinition = getPrismContext().definitionFactory().createPropertyDefinition(PROPERTY_NAME, DOMUtil.XSD_STRING);
ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptType, outputDefinition, MiscSchemaUtil.getExpressionProfile(), expressionFactory, shortTestName, result);
VariablesMap variables = createVariables(ExpressionConstants.VAR_USER, chef, chef.getDefinition());
// WHEN
List<PrismPropertyValue<String>> scriptOutputs = evaluate(scriptExpression, variables, false, shortTestName, null, result);
// THEN
display("Script output", scriptOutputs);
assertEquals("Unexpected number of script outputs", 3, scriptOutputs.size());
Set<String> oids = new HashSet<>();
oids.add(scriptOutputs.get(0).getValue());
oids.add(scriptOutputs.get(1).getValue());
oids.add(scriptOutputs.get(2).getValue());
Set<String> expectedOids = new HashSet<>(Arrays.asList(CHEESE_OID, CHEESE_JR_OID, LECHUCK_OID));
assertEquals("Unexpected script output", expectedOids, oids);
}
Aggregations