Skip to content

Commit

Permalink
Minor refactoring and correctly setting thread-local variables for bu…
Browse files Browse the repository at this point in the history
…lk action script execution.
  • Loading branch information
semancik committed Aug 5, 2016
1 parent 1f4821f commit 941b86c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 Evolveum
* Copyright (c) 2013-2016 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,6 +52,10 @@ public static <F extends ObjectType> void pushLensContext(LensContext<F> ctx) {
stack = new ArrayDeque<LensContext<ObjectType>>();
lensContextStackTl.set(stack);
}
if (ctx == null) {
// Deque cannot hold null elements. So we need to create a placeholder
ctx = new LensContextPlaceholder<>(null);
}
stack.push((LensContext<ObjectType>)ctx);
}

Expand All @@ -65,7 +69,12 @@ public static <F extends ObjectType> LensContext<F> getLensContext() {
if (stack == null) {
return null;
}
return (LensContext<F>) stack.peek();
LensContext<F> ctx = (LensContext<F>) stack.peek();
if (ctx instanceof LensContextPlaceholder) {
return null;
} else {
return ctx;
}
}

public static void pushCurrentResult(OperationResult result) {
Expand Down
Expand Up @@ -485,7 +485,7 @@ private void evaluateScriptingHook(LensContext context, HookType hookType,
}
variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, focus);

LensUtil.evaluateScript(scriptExpression, context, variables, shortDesc, task, result);
Utils.evaluateScript(scriptExpression, context, variables, false, shortDesc, task, result);
}

private <F extends ObjectType> void processInitialToPrimary(LensContext<F> context, Task task, OperationResult result) {
Expand Down
Expand Up @@ -595,23 +595,6 @@ public static <F extends ObjectType> void moveTriggers(LensProjectionContext pro
}
}

public static <V extends PrismValue, F extends ObjectType> void evaluateScript(
ScriptExpression scriptExpression, LensContext<F> lensContext, ExpressionVariables variables, String shortDesc, Task task, OperationResult parentResult) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
ModelExpressionThreadLocalHolder.pushLensContext(lensContext);
ModelExpressionThreadLocalHolder.pushCurrentResult(parentResult);
ModelExpressionThreadLocalHolder.pushCurrentTask(task);
try {
scriptExpression.evaluate(variables, ScriptExpressionReturnTypeType.SCALAR, false, shortDesc, task, parentResult);
} finally {
ModelExpressionThreadLocalHolder.popLensContext();
ModelExpressionThreadLocalHolder.popCurrentResult();
ModelExpressionThreadLocalHolder.popCurrentTask();
// if (lensContext.getDebugListener() != null) {
// lensContext.getDebugListener().afterScriptEvaluation(lensContext, scriptExpression);
// }
}
}

public static Object getIterationVariableValue(LensProjectionContext accCtx) {
Integer iterationOld = null;
PrismObject<ShadowType> shadowCurrent = accCtx.getObjectCurrent();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2014 Evolveum
* Copyright (c) 2010-2016 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,7 +81,7 @@ public TaskRunResult run(Task task) {
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.FINISHED);
} catch (ScriptExecutionException|SecurityViolationException|SchemaException e) {
result.recordFatalError("Couldn't execute script: " + e.getMessage(), e);
LoggingUtils.logException(LOGGER, "Couldn't execute script", e);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute script", e);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
} finally {
task.storeOperationStats();
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.evolveum.midpoint.model.common.expression.script.ScriptExpressionFactory;
import com.evolveum.midpoint.model.impl.scripting.Data;
import com.evolveum.midpoint.model.impl.scripting.ExecutionContext;
import com.evolveum.midpoint.model.impl.util.Utils;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismObject;
Expand Down Expand Up @@ -162,7 +163,9 @@ private Object executeScript(ScriptExpression scriptExpression, Item inputItem,
variables.addVariableDefinition(ExpressionConstants.VAR_INPUT, inputItem);
variables.addVariableDefinition(ExpressionConstants.VAR_PRISM_CONTEXT, prismContext);
ExpressionUtil.addActorVariable(variables, securityEnforcer);
List<?> rv = scriptExpression.evaluate(variables, ScriptExpressionReturnTypeType.SCALAR, true, "script action", context.getTask(), result);

List<?> rv = Utils.evaluateScript(scriptExpression, null, variables, true, "script action", context.getTask(), result);

if (rv == null || rv.size() == 0) {
return null;
} else if (rv.size() == 1) {
Expand Down
Expand Up @@ -21,7 +21,9 @@
import com.evolveum.midpoint.common.refinery.RefinedResourceSchema;
import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.model.common.expression.ExpressionVariables;
import com.evolveum.midpoint.model.common.expression.script.ScriptExpression;
import com.evolveum.midpoint.model.impl.ModelConstants;
import com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder;
import com.evolveum.midpoint.model.impl.importer.ObjectImporter;
import com.evolveum.midpoint.model.impl.lens.LensContext;
import com.evolveum.midpoint.model.impl.lens.LensFocusContext;
Expand All @@ -46,6 +48,7 @@
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.Handler;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
Expand Down Expand Up @@ -73,9 +76,6 @@ public final class Utils {
private static final String OPERATION_RESOLVE_REFERENCE = ObjectImporter.class.getName()
+ ".resolveReference";

private Utils() {
}

@Deprecated // use RepositoryService.objectSearchIterative instead
public static <T extends ObjectType> void searchIterative(RepositoryService repositoryService, Class<T> type, ObjectQuery query,
Handler<PrismObject<T>> handler, int blockSize, OperationResult opResult) throws SchemaException {
Expand Down Expand Up @@ -610,5 +610,21 @@ public static PrismReferenceValue getAditTarget(ObjectDelta<? extends ObjectType
return targetRef;
}

public static <V extends PrismValue, F extends ObjectType> List<V> evaluateScript(
ScriptExpression scriptExpression, LensContext<F> lensContext, ExpressionVariables variables, boolean useNew, String shortDesc, Task task, OperationResult parentResult) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
ModelExpressionThreadLocalHolder.pushLensContext(lensContext);
ModelExpressionThreadLocalHolder.pushCurrentResult(parentResult);
ModelExpressionThreadLocalHolder.pushCurrentTask(task);
try {
return scriptExpression.evaluate(variables, ScriptExpressionReturnTypeType.SCALAR, useNew, shortDesc, task, parentResult);
} finally {
ModelExpressionThreadLocalHolder.popLensContext();
ModelExpressionThreadLocalHolder.popCurrentResult();
ModelExpressionThreadLocalHolder.popCurrentTask();
// if (lensContext.getDebugListener() != null) {
// lensContext.getDebugListener().afterScriptEvaluation(lensContext, scriptExpression);
// }
}
}

}

0 comments on commit 941b86c

Please sign in to comment.