Skip to content

Commit

Permalink
Velocity script executor
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Jul 9, 2016
1 parent 3ee2041 commit cd4e4d2
Show file tree
Hide file tree
Showing 24 changed files with 946 additions and 156 deletions.
Expand Up @@ -79,16 +79,16 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {
return (T) ((Boolean)rawValue);
}
if (expectedType == Boolean.class && rawValue instanceof String) {
return (T) (Boolean)Boolean.parseBoolean(((String)rawValue));
return (T) (Boolean)Boolean.parseBoolean((((String)rawValue)).trim());
}
if (expectedType == Boolean.class && rawValue instanceof PolyString) {
return (T) (Boolean)Boolean.parseBoolean(((PolyString)rawValue).toString());
return (T) (Boolean)Boolean.parseBoolean((rawValue.toString().trim()));
}
if (expectedType == boolean.class && rawValue instanceof String) {
return (T) (Boolean)Boolean.parseBoolean(((String)rawValue));
return (T) (Boolean)Boolean.parseBoolean(((String)rawValue).trim());
}
if (expectedType == boolean.class && rawValue instanceof PolyString) {
return (T) (Boolean)Boolean.parseBoolean(((PolyString)rawValue).toString());
return (T) (Boolean)Boolean.parseBoolean((rawValue).toString().trim());
}
if (expectedType == String.class && rawValue instanceof Boolean) {
return (T) rawValue.toString();
Expand All @@ -99,10 +99,10 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {
return (T)((Integer)rawValue);
}
if (expectedType == Integer.class && rawValue instanceof String) {
return (T) (Integer)Integer.parseInt(((String)rawValue));
return (T) (Integer)Integer.parseInt(((String)rawValue).trim());
}
if (expectedType == int.class && rawValue instanceof String) {
return (T) (Integer)Integer.parseInt(((String)rawValue));
return (T) (Integer)Integer.parseInt(((String)rawValue).trim());
}
if (expectedType == String.class && rawValue instanceof Integer) {
return (T) rawValue.toString();
Expand All @@ -112,13 +112,13 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {
}

if (expectedType == long.class && rawValue instanceof Long) {
return (T)((Long)rawValue);
return (T)(rawValue);
}
if (expectedType == Long.class && rawValue instanceof String) {
return (T) (Long)Long.parseLong(((String)rawValue));
return (T) (Long)Long.parseLong(((String)rawValue).trim());
}
if (expectedType == long.class && rawValue instanceof String) {
return (T) (Long)Long.parseLong(((String)rawValue));
return (T) (Long)Long.parseLong(((String)rawValue).trim());
}
if (expectedType == String.class && rawValue instanceof Long) {
return (T) rawValue.toString();
Expand All @@ -128,10 +128,10 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {
return (T)((Float)rawValue);
}
if (expectedType == Float.class && rawValue instanceof String) {
return (T) (Float)Float.parseFloat(((String)rawValue));
return (T) (Float)Float.parseFloat(((String)rawValue).trim());
}
if (expectedType == float.class && rawValue instanceof String) {
return (T) (Float)Float.parseFloat(((String)rawValue));
return (T) (Float)Float.parseFloat(((String)rawValue).trim());
}
if (expectedType == String.class && rawValue instanceof Float) {
return (T) rawValue.toString();
Expand All @@ -141,10 +141,10 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {
return (T)((Double)rawValue);
}
if (expectedType == Double.class && rawValue instanceof String) {
return (T) (Double)Double.parseDouble(((String)rawValue));
return (T) (Double)Double.parseDouble(((String)rawValue).trim());
}
if (expectedType == double.class && rawValue instanceof String) {
return (T) (Double)Double.parseDouble(((String)rawValue));
return (T) (Double)Double.parseDouble(((String)rawValue).trim());
}
if (expectedType == String.class && rawValue instanceof Float) {
return (T) rawValue.toString();
Expand All @@ -164,17 +164,17 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {
}

if (expectedType == BigInteger.class && rawValue instanceof String) {
return (T) new BigInteger(((String)rawValue));
return (T) new BigInteger(((String)rawValue).trim());
}
if (expectedType == String.class && rawValue instanceof BigInteger) {
return (T) ((BigInteger)rawValue).toString();
return (T) rawValue.toString().trim();
}

if (expectedType == BigDecimal.class && rawValue instanceof String) {
return (T) new BigDecimal(((String)rawValue));
return (T) new BigDecimal(((String)rawValue).trim());
}
if (expectedType == String.class && rawValue instanceof BigDecimal) {
return (T) ((BigDecimal)rawValue).toString();
return (T) ((BigDecimal)rawValue).toString().trim();
}

if (expectedType == PolyString.class && rawValue instanceof String) {
Expand Down Expand Up @@ -224,15 +224,15 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {

// XML Enums (JAXB)
if (expectedType.isEnum() && expectedType.getAnnotation(XmlEnum.class) != null && rawValue instanceof String) {
return XmlTypeConverter.toXmlEnum(expectedType, (String)rawValue);
return XmlTypeConverter.toXmlEnum(expectedType, ((String)rawValue).trim());
}
if (expectedType == String.class && rawValue.getClass().isEnum() && rawValue.getClass().getAnnotation(XmlEnum.class) != null) {
return (T) XmlTypeConverter.fromXmlEnum(rawValue);
}

// Java Enums
if (expectedType.isEnum() && rawValue instanceof String) {
return (T) Enum.valueOf((Class<Enum>)expectedType, (String)rawValue);
return (T) Enum.valueOf((Class<Enum>)expectedType, ((String)rawValue).trim());
}
if (expectedType == String.class && rawValue.getClass().isEnum()) {
return (T) rawValue.toString();
Expand All @@ -243,7 +243,7 @@ public static <T> T convert(Class<T> expectedType, Object rawValue) {
return (T) rawValue;
}
if (expectedType == QName.class && rawValue instanceof String){
return (T) QNameUtil.uriToQName((String)rawValue);
return (T) QNameUtil.uriToQName(((String)rawValue).trim());
}

throw new IllegalArgumentException("Expected "+expectedType+" type, but got "+rawValue.getClass());
Expand Down
4 changes: 4 additions & 0 deletions model/model-common/pom.xml
Expand Up @@ -112,6 +112,10 @@
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
Expand Down
Expand Up @@ -22,27 +22,14 @@
import java.util.Map;
import java.util.Map.Entry;

import javax.script.Bindings;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.model.common.expression.functions.BasicExpressionFunctions;
import com.evolveum.midpoint.model.common.expression.functions.BasicExpressionFunctionsXPath;
import com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary;
import com.evolveum.midpoint.model.common.expression.functions.LogExpressionFunctions;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.Structured;
import com.evolveum.midpoint.prism.Visitable;
import com.evolveum.midpoint.prism.Visitor;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.crypto.EncryptionException;
import com.evolveum.midpoint.prism.crypto.Protector;
import com.evolveum.midpoint.prism.delta.ItemDelta;
Expand All @@ -51,6 +38,7 @@
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.prism.path.NameItemPathSegment;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.query.AllFilter;
import com.evolveum.midpoint.prism.query.ExpressionWrapper;
import com.evolveum.midpoint.prism.query.InOidFilter;
Expand Down Expand Up @@ -243,6 +231,67 @@ public static Object resolvePath(ItemPath path, ExpressionVariables variables, O
}
}

public static Object convertVariableValue(Object originalValue, String variableName, ObjectResolver objectResolver,
String contextDescription, Task task, OperationResult result) throws ExpressionSyntaxException, ObjectNotFoundException {
if (originalValue instanceof ObjectReferenceType) {
try {
originalValue = resolveReference((ObjectReferenceType)originalValue, objectResolver, variableName,
contextDescription, task, result);
} catch (SchemaException e) {
throw new ExpressionSyntaxException("Schema error during variable "+variableName+" resolution in "+contextDescription+": "+e.getMessage(), e);
}
}
if (originalValue instanceof PrismObject<?>) {
return ((PrismObject<?>)originalValue).asObjectable();
}
if (originalValue instanceof PrismContainerValue<?>) {
return ((PrismContainerValue<?>)originalValue).asContainerable();
}
if (originalValue instanceof PrismPropertyValue<?>) {
return ((PrismPropertyValue<?>)originalValue).getValue();
}
if (originalValue instanceof PrismProperty<?>) {
PrismProperty<?> prop = (PrismProperty<?>)originalValue;
PrismPropertyDefinition<?> def = prop.getDefinition();
if (def != null) {
if (def.isSingleValue()) {
return prop.getRealValue();
} else {
return prop.getRealValues();
}
} else {
return prop.getValues();
}
}
return originalValue;
}

public static Map<String,Object> prepareScriptVariables(ExpressionVariables variables, ObjectResolver objectResolver,
Collection<FunctionLibrary> functions,
String contextDescription, Task task, OperationResult result) throws ExpressionSyntaxException, ObjectNotFoundException {
Map<String,Object> scriptVariables = new HashMap<>();
// Functions
if (functions != null) {
for (FunctionLibrary funcLib: functions) {
scriptVariables.put(funcLib.getVariableName(), funcLib.getGenericFunctions());
}
}
// Variables
if (variables != null) {
for (Entry<QName, Object> variableEntry: variables.entrySet()) {
if (variableEntry.getKey() == null) {
// This is the "root" node. We have no use for it in JSR223, just skip it
continue;
}
String variableName = variableEntry.getKey().getLocalPart();
Object variableValue = ExpressionUtil.convertVariableValue(variableEntry.getValue(), variableName, objectResolver, contextDescription, task, result);
scriptVariables.put(variableName, variableValue);
}
}
return scriptVariables;
}


private static PrismObject<?> resolveReference(ObjectReferenceType ref, ObjectResolver objectResolver,
String varDesc, String contextDescription, Task task, OperationResult result)
throws SchemaException, ObjectNotFoundException {
Expand Down Expand Up @@ -813,4 +862,32 @@ public static <D extends ItemDefinition> Object convertToOutputValue(String stri
}
}

public static <T> boolean isEmpty(T val) {
if (val == null) {
return true;
}
if (val instanceof String && ((String)val).isEmpty()) {
return true;
}
if (val instanceof PolyString && ((PolyString)val).isEmpty()) {
return true;
}
return false;
}

public static <T, V extends PrismValue> V convertToPrismValue(T value, ItemDefinition definition, String contextDescription, PrismContext prismContext) throws ExpressionEvaluationException {
if (definition instanceof PrismReferenceDefinition) {
return (V) ((ObjectReferenceType) value).asReferenceValue();
} else if (definition instanceof PrismContainerDefinition) {
try {
prismContext.adopt((Containerable) value);
((Containerable) value).asPrismContainerValue().applyDefinition(definition);
} catch (SchemaException e) {
throw new ExpressionEvaluationException(e.getMessage() + " " + contextDescription, e);
}
return (V) ((Containerable) value).asPrismContainerValue();
} else {
return (V) new PrismPropertyValue<>(value);
}
}
}

0 comments on commit cd4e4d2

Please sign in to comment.