Example usage for org.springframework.expression.spel.support StandardEvaluationContext setVariable

List of usage examples for org.springframework.expression.spel.support StandardEvaluationContext setVariable

Introduction

In this page you can find the example usage for org.springframework.expression.spel.support StandardEvaluationContext setVariable.

Prototype

@Override
    public void setVariable(@Nullable String name, @Nullable Object value) 

Source Link

Usage

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

private <S extends Object> S read(final MongoPersistentEntity<S> entity, final DBObject dbo) {

    final StandardEvaluationContext spelCtx = new StandardEvaluationContext();
    if (null != applicationContext) {
        spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }//from w w w  .  jav a  2s  .  c  o m
    if (!(dbo instanceof BasicDBList)) {
        String[] keySet = dbo.keySet().toArray(new String[] {});
        for (String key : keySet) {
            spelCtx.setVariable(key, dbo.get(key));
        }
    }

    final List<String> ctorParamNames = new ArrayList<String>();
    final MongoPersistentProperty idProperty = entity.getIdProperty();
    final S instance = constructInstance(entity, new PreferredConstructor.ParameterValueProvider() {
        @SuppressWarnings("unchecked")
        public <T> T getParameterValue(PreferredConstructor.Parameter<T> parameter) {
            String name = parameter.getName();
            TypeInformation<T> type = parameter.getType();
            Class<T> rawType = parameter.getRawType();
            String key = idProperty == null ? name
                    : idProperty.getName().equals(name) ? idProperty.getKey() : name;
            Object obj = dbo.get(key);

            ctorParamNames.add(name);
            if (obj instanceof DBRef) {
                return read(type, ((DBRef) obj).fetch());
            } else if (obj instanceof BasicDBList) {
                BasicDBList objAsDbList = (BasicDBList) obj;
                List<?> l = unwrapList(objAsDbList, type);
                return conversionService.convert(l, rawType);
            } else if (obj instanceof DBObject) {
                return read(type, ((DBObject) obj));
            } else if (null != obj && obj.getClass().isAssignableFrom(rawType)) {
                return (T) obj;
            } else if (null != obj) {
                return conversionService.convert(obj, rawType);
            }

            return null;
        }
    }, spelCtx);

    // Set properties not already set in the constructor
    entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            boolean isConstructorProperty = ctorParamNames.contains(prop.getName());
            boolean hasValueForProperty = dbo.containsField(prop.getKey());

            if (!hasValueForProperty || isConstructorProperty) {
                return;
            }

            Object obj = getValueInternal(prop, dbo, spelCtx, prop.getSpelExpression());
            try {
                setProperty(instance, prop, obj, useFieldAccessOnly);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    // Handle associations
    entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
        public void doWithAssociation(Association<MongoPersistentProperty> association) {
            MongoPersistentProperty inverseProp = association.getInverse();
            Object obj = getValueInternal(inverseProp, dbo, spelCtx, inverseProp.getSpelExpression());
            try {
                setProperty(instance, inverseProp, obj);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    return instance;
}

From source file:org.springframework.integration.gateway.GatewayMethodInboundMessageMapper.java

private StandardEvaluationContext createMethodInvocationEvaluationContext(Object[] arguments) {
    StandardEvaluationContext context = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
    context.setVariable("args", arguments);
    context.setVariable("method", this.method.getName());
    return context;
}

From source file:org.springframework.integration.handler.support.MessagingMethodInvokerHelper.java

private void prepareEvaluationContext() throws Exception {
    StandardEvaluationContext context = getEvaluationContext(false);
    Class<?> targetType = AopUtils.getTargetClass(this.targetObject);
    if (this.method != null) {
        context.registerMethodFilter(targetType, new FixedMethodFilter(this.method));
        if (this.expectedType != null) {
            Assert.state(/*from  w  w  w .j  a  v a  2s . c o  m*/
                    context.getTypeConverter().canConvert(TypeDescriptor.valueOf((this.method).getReturnType()),
                            this.expectedType),
                    "Cannot convert to expected type (" + this.expectedType + ") from " + this.method);
        }
    } else {
        AnnotatedMethodFilter filter = new AnnotatedMethodFilter(this.annotationType, this.methodName,
                this.requiresReply);
        Assert.state(canReturnExpectedType(filter, targetType, context.getTypeConverter()),
                "Cannot convert to expected type (" + this.expectedType + ") from " + this.method);
        context.registerMethodFilter(targetType, filter);
    }
    context.setVariable("target", this.targetObject);
    context.registerFunction("requiredHeader",
            ParametersWrapper.class.getDeclaredMethod("getHeader", Map.class, String.class));
}

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

private void prepareEvaluationContext(StandardEvaluationContext context, Object method,
        Class<? extends Annotation> annotationType) {
    Class<?> targetType = AopUtils.getTargetClass(this.targetObject);
    if (method instanceof Method) {
        context.registerMethodFilter(targetType, new FixedMethodFilter((Method) method));
        if (expectedType != null) {
            Assert.state(//w w w . ja va 2s.  c om
                    context.getTypeConverter().canConvert(
                            TypeDescriptor.valueOf(((Method) method).getReturnType()),
                            TypeDescriptor.valueOf(expectedType)),
                    "Cannot convert to expected type (" + expectedType + ") from " + method);
        }
    } else if (method == null || method instanceof String) {
        AnnotatedMethodFilter filter = new AnnotatedMethodFilter(annotationType, (String) method,
                this.requiresReply);
        Assert.state(canReturnExpectedType(filter, targetType, context.getTypeConverter()),
                "Cannot convert to expected type (" + expectedType + ") from " + method);
        context.registerMethodFilter(targetType, filter);
    }
    context.setVariable("target", targetObject);
}