use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class ValueTupleTransformation method evaluate.
void evaluate() {
try {
if (!combinatorialEvaluation.evaluator.isIncludeNullInputs() && MiscUtil.isAllNull(valuesTuple)) {
// The case that all the sources are null. There is no point executing the expression.
setTraceComment("All sources are null and includeNullInputs is true.");
return;
}
VariablesMap staticVariables = createStaticVariablesFromSources();
recordBeforeTransformation();
augmentStaticVariablesWithInputVariables(staticVariables);
evaluateConditionAndTransformation(staticVariables);
recordTransformationResult();
outputTriple.addAllToSet(outputSet, transformationResult);
} catch (Throwable t) {
result.recordFatalError(t.getMessage(), t);
throw t;
}
}
use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class CustomFunctions method execute.
/**
* This method is invoked by the scripts. It is supposed to be only public method exposed
* by this class.
*/
public <V extends PrismValue, D extends ItemDefinition> Object execute(String functionName, Map<String, Object> params) throws ExpressionEvaluationException {
Validate.notNull(functionName, "Function name must be specified");
ScriptExpressionEvaluationContext ctx = ScriptExpressionEvaluationContext.getThreadLocal();
Task task;
OperationResult result;
if (ctx != null) {
if (ctx.getTask() != null) {
task = ctx.getTask();
} else {
// We shouldn't use task of unknown provenience.
throw new IllegalStateException("No task in ScriptExpressionEvaluationContext for the current thread found");
}
if (ctx.getResult() != null) {
result = ctx.getResult();
} else {
// This situation should never occur anyway.
throw new IllegalStateException("No operation result in ScriptExpressionEvaluationContext for the current thread found");
}
} else {
// This situation should never occur anyway.
throw new IllegalStateException("No ScriptExpressionEvaluationContext for current thread found");
}
List<ExpressionType> functions = library.getFunction().stream().filter(expression -> functionName.equals(expression.getName())).collect(Collectors.toList());
LOGGER.trace("functions {}", functions);
ExpressionType expressionType = MiscUtil.extractSingletonRequired(functions, () -> new ExpressionEvaluationException(functions.size() + " functions named '" + functionName + "' present"), () -> new ExpressionEvaluationException("No function named '" + functionName + "' present"));
LOGGER.trace("function to execute {}", expressionType);
try {
VariablesMap variables = new VariablesMap();
if (MapUtils.isNotEmpty(params)) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
variables.put(entry.getKey(), convertInput(entry, expressionType));
}
}
QName returnType = defaultIfNull(expressionType.getReturnType(), DOMUtil.XSD_STRING);
D outputDefinition = prepareOutputDefinition(returnType, expressionType.getReturnMultiplicity());
String shortDesc = "custom function execute";
Expression<V, D> expression = expressionFactory.makeExpression(expressionType, outputDefinition, expressionProfile, shortDesc, task, result);
ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, shortDesc, task);
PrismValueDeltaSetTriple<V> outputTriple = expression.evaluate(context, result);
LOGGER.trace("Result of the expression evaluation: {}", outputTriple);
if (outputTriple == null) {
return null;
}
Collection<V> nonNegativeValues = outputTriple.getNonNegativeValues();
if (nonNegativeValues.isEmpty()) {
return null;
}
if (outputDefinition.isMultiValue()) {
return PrismValueCollectionsUtil.getRealValuesOfCollection(nonNegativeValues);
}
if (nonNegativeValues.size() > 1) {
throw new ExpressionEvaluationException("Expression returned more than one value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
return nonNegativeValues.iterator().next().getRealValue();
} catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
throw new ExpressionEvaluationException(e.getMessage(), e);
}
}
use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class SandboxTypeCheckingExtension method handleUnresolvedVariableExpression.
@Override
public boolean handleUnresolvedVariableExpression(VariableExpression vexp) {
String variableName = vexp.getName();
ScriptExpressionEvaluationContext context = getContext();
String contextDescription = context.getContextDescription();
if (!isDynamic(vexp)) {
LOGGER.error("Unresolved script variable {} because it is not dynamic, in {}", variableName, contextDescription);
return false;
}
VariablesMap variables = context.getVariables();
if (variables != null) {
TypedValue variableTypedValue = variables.get(variableName);
if (variableTypedValue != null) {
Class variableClass;
try {
variableClass = variableTypedValue.determineClass();
} catch (SchemaException e) {
String msg = "Cannot determine type of variable '" + variableName + "' (" + variableTypedValue + ") in " + contextDescription + ": " + e.getMessage();
LOGGER.error("{}", msg);
throw new IllegalStateException(msg, e);
}
LOGGER.trace("Determine script variable {} as expression variable, class {} in {}", variableName, variableClass, contextDescription);
storeType(vexp, ClassHelper.make(variableClass));
setHandled(true);
return true;
}
}
Collection<FunctionLibrary> functions = context.getFunctions();
if (functions != null) {
for (FunctionLibrary function : functions) {
if (function.getVariableName().equals(variableName)) {
Class functionClass = function.getGenericFunctions().getClass();
LOGGER.trace("Determine script variable {} as function library, class {} in {}", variableName, functionClass, contextDescription);
storeType(vexp, ClassHelper.make(functionClass));
setHandled(true);
return true;
}
}
}
LOGGER.error("Unresolved script variable {} because no declaration for it cannot be found in {}", variableName, contextDescription);
return false;
}
use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class TransformationalAsyncUpdateMessageListener method onMessage.
@Override
public void onMessage(AsyncUpdateMessageType message, AcknowledgementSink acknowledgementSink) {
int messageNumber = messagesSeen.getAndIncrement();
LOGGER.trace("Got message number {}: {}", messageNumber, message);
SecurityContextManager securityContextManager = connectorInstance.getSecurityContextManager();
Authentication oldAuthentication = securityContextManager.getAuthentication();
try {
securityContextManager.setupPreAuthenticatedSecurityContext(authentication);
Task task = connectorInstance.getTaskManager().createTaskInstance(OP_ON_MESSAGE_PREPARATION);
task.setChannel(CHANNEL_ASYNC_UPDATE_URI);
if (authentication != null && authentication.getPrincipal() instanceof MidPointPrincipal) {
task.setOwner(((MidPointPrincipal) authentication.getPrincipal()).getFocus().asPrismObject().clone());
}
Tracer tracer = connectorInstance.getTracer();
OperationResult result = task.getResult();
OperationResultBuilder resultBuilder = OperationResult.createFor(OP_ON_MESSAGE);
try {
ActivityTracingDefinitionType tracing = connectorInstance.getConfiguration().getProcessTracingConfiguration();
if (tracing != null) {
int interval = defaultIfNull(tracing.getInterval(), 1);
boolean matches = interval > 0 && messageNumber % interval == 0;
if (matches) {
task.setTracingProfile(tracing.getTracingProfile());
if (tracing.getTracingPoint().isEmpty()) {
task.addTracingRequest(TracingRootType.ASYNCHRONOUS_MESSAGE_PROCESSING);
} else {
tracing.getTracingPoint().forEach(task::addTracingRequest);
}
}
}
if (task.getTracingRequestedFor().contains(TracingRootType.ASYNCHRONOUS_MESSAGE_PROCESSING)) {
TracingProfileType profile = task.getTracingProfile() != null ? task.getTracingProfile() : tracer.getDefaultProfile();
resultBuilder.tracingProfile(tracer.compileProfile(profile, task.getResult()));
}
// replace task result with the newly-built one
result = resultBuilder.build();
task.setResult(result);
VariablesMap variables = new VariablesMap();
variables.put(VAR_MESSAGE, message, AsyncUpdateMessageType.class);
List<UcfChangeType> changeBeans;
try {
ExpressionType transformExpression = connectorInstance.getTransformExpression();
if (transformExpression != null) {
changeBeans = connectorInstance.getUcfExpressionEvaluator().evaluate(transformExpression, variables, SchemaConstantsGenerated.C_UCF_CHANGE, "computing UCF change from async update", task, result);
} else {
changeBeans = unwrapMessage(message);
}
} catch (RuntimeException | SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
throw new SystemException("Couldn't evaluate message transformation expression: " + e.getMessage(), e);
}
if (changeBeans.isEmpty()) {
acknowledgementSink.acknowledge(true, result);
} else {
AcknowledgementSink aggregatedSink = createAggregatingAcknowledgeSink(acknowledgementSink, changeBeans.size());
for (UcfChangeType changeBean : changeBeans) {
// For this to work reliably, we have to run in a single thread. But that's ok.
// If we receive messages in multiple threads, there is no message ordering.
int changeSequentialNumber = changesProduced.incrementAndGet();
// intentionally in this order - to process changes even after failure
// (if listener wants to fail fast, it can throw an exception)
UcfAsyncUpdateChange change = createChange(changeBean, result, changeSequentialNumber, aggregatedSink);
changeListener.onChange(change, task, result);
}
}
} catch (Exception e) {
LoggingUtils.logUnexpectedException(LOGGER, "Got exception while processing asynchronous message in {}", e, task);
result.recordFatalError(e.getMessage(), e);
int changeSequentialNumber = changesProduced.incrementAndGet();
UcfAsyncUpdateChange change = new UcfAsyncUpdateChange(changeSequentialNumber, UcfErrorState.error(e), acknowledgementSink);
changeListener.onChange(change, task, result);
} finally {
result.computeStatusIfUnknown();
// (Otherwise it captures only the pre-processing activities.)
if (result.isTraced()) {
tracer.storeTrace(task, result, null);
}
}
} finally {
securityContextManager.setupPreAuthenticatedSecurityContext(oldAuthentication);
}
}
use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class CommandLineScriptExecutor method executeScript.
public void executeScript(CommandLineScriptType scriptType, VariablesMap variables, String shortDesc, Task task, OperationResult parentResult) throws IOException, InterruptedException, SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
OperationResult result = parentResult.createSubresult(CommandLineScriptExecutor.class.getSimpleName() + ".run");
if (variables == null) {
variables = new VariablesMap();
}
String expandedCode = expandMacros(scriptType, variables, shortDesc, task, result);
// TODO: later: prepare agruments and environment
String preparedCode = expandedCode.trim();
LOGGER.debug("Prepared shell code: {}", preparedCode);
CommandLineRunner runner = new CommandLineRunner(preparedCode, result);
runner.setExecutionMethod(scriptType.getExecutionMethod());
runner.execute();
result.computeStatus();
}
Aggregations