Example usage for org.springframework.expression Expression getValue

List of usage examples for org.springframework.expression Expression getValue

Introduction

In this page you can find the example usage for org.springframework.expression Expression getValue.

Prototype

@Nullable
Object getValue(EvaluationContext context) throws EvaluationException;

Source Link

Document

Evaluate this expression in the provided context and return the result of evaluation.

Usage

From source file:org.springframework.cloud.function.deployer.ApplicationRunner.java

public Object evaluate(String expression, Object root, Object... attrs) {
    Expression parsed = new SpelExpressionParser(this.config).parseExpression(expression);
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.setTypeLocator(this.typeLocator);
    if (attrs.length % 2 != 0) {
        throw new IllegalArgumentException("Context attributes must be name, value pairs");
    }/* ww  w.  j a  va2s .  co m*/
    for (int i = 0; i < attrs.length / 2; i++) {
        String name = (String) attrs[2 * i];
        Object value = attrs[2 * i + 1];
        context.setVariable(name, value);
    }
    return parsed.getValue(context);
}

From source file:org.springframework.cloud.function.deployer.ApplicationRunner.java

private void runContext(String mainClass, Map<String, String> properties, String... args) {
    Expression parsed = new SpelExpressionParser().parseExpression("run(#main,#properties,#args)");
    StandardEvaluationContext context = this.app;
    context.setVariable("main", mainClass);
    context.setVariable("properties", properties);
    context.setVariable("args", args);
    parsed.getValue(context);
}

From source file:org.springframework.cloud.function.deployer.ApplicationRunner.java

private void closeContext() {
    if (this.app != null) {
        Expression parsed = new SpelExpressionParser().parseExpression("close()");
        parsed.getValue(this.app);
        this.app = null;
    }//from  w  ww.jav  a  2 s.  co m
}

From source file:org.springframework.cloud.stream.app.tasklaunchrequest.DataFlowTaskLaunchRequestAutoConfiguration.java

private List<String> evaluateArgExpressions(Message message, Map<String, String> argExpressions) {
    List<String> results = new LinkedList<>();
    argExpressions.forEach((k, v) -> {
        Expression expression = argExpressionsMap.get(k);
        Assert.notNull(expression, String.format("expression %s cannot be null!", v));
        Object val = expression.getValue(message);
        results.add(String.format("%s=%s", k, val));
    });/*from w ww  .  j  av  a  2s  .com*/
    return results;
}

From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java

@SuppressWarnings({ "unchecked" })
protected Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx,
        String spelExpr) {/*from   w w w .java2 s  .com*/

    Object o;
    if (null != spelExpr) {
        Expression x = spelExpressionParser.parseExpression(spelExpr);
        o = x.getValue(ctx);
    } else {

        Object dbObj = dbo.get(prop.getKey());

        if (dbObj == null) {
            return null;
        }

        Class<?> propertyType = prop.getType();
        Class<?> customTarget = getCustomTarget(dbObj.getClass(), propertyType);

        if (customTarget != null) {
            return conversionService.convert(dbObj, propertyType);
        }

        if (dbObj instanceof DBRef) {
            dbObj = ((DBRef) dbObj).fetch();
        }
        if (dbObj instanceof DBObject) {
            if (prop.isMap() && dbObj instanceof DBObject) {

                // We have to find a potentially stored class to be used first.
                Class<?> toType = findTypeToBeUsed((DBObject) dbObj);
                Map<String, Object> m = new LinkedHashMap<String, Object>();

                for (Map.Entry<String, Object> entry : ((Map<String, Object>) ((DBObject) dbObj).toMap())
                        .entrySet()) {
                    if (entry.getKey().equals(CUSTOM_TYPE_KEY)) {
                        continue;
                    }
                    if (null != entry.getValue() && entry.getValue() instanceof DBObject) {
                        m.put(entry.getKey(), read((null != toType ? toType : prop.getMapValueType()),
                                (DBObject) entry.getValue()));
                    } else {
                        m.put(entry.getKey(), entry.getValue());
                    }
                }
                return m;
            } else if (prop.isArray() && dbObj instanceof BasicDBObject
                    && ((DBObject) dbObj).keySet().size() == 0) {
                // It's empty
                return Array.newInstance(prop.getComponentType(), 0);
            } else if (prop.isCollection() && dbObj instanceof BasicDBList) {
                BasicDBList dbObjList = (BasicDBList) dbObj;
                List<Object> items = new LinkedList<Object>();
                for (int i = 0; i < dbObjList.size(); i++) {
                    Object dbObjItem = dbObjList.get(i);
                    if (dbObjItem instanceof DBRef) {
                        items.add(read(prop.getComponentType(), ((DBRef) dbObjItem).fetch()));
                    } else if (dbObjItem instanceof DBObject) {
                        items.add(read(prop.getComponentType(), (DBObject) dbObjItem));
                    } else {
                        items.add(dbObjItem);
                    }
                }
                List<Object> itemsToReturn = new LinkedList<Object>();
                for (Object obj : items) {
                    itemsToReturn.add(obj);
                }
                return itemsToReturn;
            }

            Class<?> toType = findTypeToBeUsed((DBObject) dbObj);

            // It's a complex object, have to read it in
            if (toType != null) {
                dbo.removeField(CUSTOM_TYPE_KEY);
                o = read(toType, (DBObject) dbObj);
            } else {
                o = read(mappingContext.getPersistentEntity(prop.getTypeInformation()), (DBObject) dbObj);
            }
        } else {
            o = dbObj;
        }
    }
    return o;
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

@SuppressWarnings("unchecked")
protected Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx,
        String spelExpr) {/*w  w  w .  j  a v  a2  s.  c  o  m*/

    Object o;
    if (null != spelExpr) {
        Expression x = spelExpressionParser.parseExpression(spelExpr);
        o = x.getValue(ctx);
    } else {

        Object sourceValue = dbo.get(prop.getFieldName());

        if (sourceValue == null) {
            return null;
        }

        Class<?> propertyType = prop.getType();

        if (conversions.hasCustomReadTarget(sourceValue.getClass(), propertyType)) {
            return conversionService.convert(sourceValue, propertyType);
        }

        if (sourceValue instanceof DBRef) {
            sourceValue = ((DBRef) sourceValue).fetch();
        }
        if (sourceValue instanceof DBObject) {
            if (prop.isMap()) {
                return readMap(prop.getTypeInformation(), (DBObject) sourceValue);
            } else if (prop.isArray() && sourceValue instanceof BasicDBObject
                    && ((DBObject) sourceValue).keySet().size() == 0) {
                // It's empty
                return Array.newInstance(prop.getComponentType(), 0);
            } else if (prop.isCollectionLike() && sourceValue instanceof BasicDBList) {
                return readCollectionOrArray(
                        (TypeInformation<? extends Collection<?>>) prop.getTypeInformation(),
                        (BasicDBList) sourceValue);
            }

            TypeInformation<?> toType = typeMapper.readType((DBObject) sourceValue, prop.getTypeInformation());

            // It's a complex object, have to read it in
            if (toType != null) {
                // TODO: why do we remove the type?
                // dbo.removeField(CUSTOM_TYPE_KEY);
                o = read(toType, (DBObject) sourceValue);
            } else {
                o = read(mappingContext.getPersistentEntity(prop.getTypeInformation()), (DBObject) sourceValue);
            }
        } else {
            o = sourceValue;
        }
    }
    return o;
}

From source file:org.springframework.integration.util.AbstractExpressionEvaluator.java

protected Object evaluateExpression(Expression expression) {
    return expression.getValue(this.getEvaluationContext());
}