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

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

Introduction

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

Prototype

public StandardEvaluationContext() 

Source Link

Document

Create a StandardEvaluationContext with a null root object.

Usage

From source file:it.geosolutions.geoserver.sira.security.expression.ExpressionRuleEngine.java

/**
 * Constructor.// w  w  w  .  ja  va  2  s.  c o m
 */
public ExpressionRuleEngine() {
    this.evalContext = new StandardEvaluationContext();
    this.parser = new SpelExpressionParser();
}

From source file:com.oneops.controller.cms.ExpressionEvaluator.java

private EvaluationContext getEvaluationContext(CmsWorkOrderBase woBase) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    if (woBase instanceof CmsWorkOrder) {
        CmsRfcCISimple rfcSimple = cmsUtil.custRfcCI2RfcCISimple(((CmsWorkOrder) woBase).getRfcCi());
        context.setRootObject(rfcSimple);
    } else if (woBase instanceof CmsActionOrder) {
        CmsCISimple ciSimple = cmsUtil.custCI2CISimple(((CmsActionOrder) woBase).getCi(),
                CmsConstants.ATTR_VALUE_TYPE_DF, true);
        context.setRootObject(ciSimple);
    }//from  w w w . java 2 s .  c o m
    return context;
}

From source file:com.arondor.common.reflection.parser.spring.BeanPropertyParser.java

public ElementConfiguration parseProperty(Object value) {
    LOGGER.debug("value : " + value);
    if (value instanceof TypedStringValue) {
        TypedStringValue stringValue = (TypedStringValue) value;
        if (stringValue.getTargetTypeName() != null) {
            return getEnumObjectConfiguration(stringValue);
        } else {/*  w  w w .  j  a  va2 s  . c o m*/
            PrimitiveConfiguration primitiveConfiguration = objectConfigurationFactory
                    .createPrimitiveConfiguration();
            if (useSPEL(stringValue)) {
                ExpressionParser parser = new SpelExpressionParser();
                String expAsStringWithToken = stringValue.getValue().trim();
                String expAsString = expAsStringWithToken.substring(2, expAsStringWithToken.length() - 1)
                        .trim();
                LOGGER.trace("This property is a SPEL expression: " + expAsString);

                // String regex = "systemProperties\\['([^\\s]+)'\\]";

                Expression exp = parser.parseExpression(expAsString);
                StandardEvaluationContext sec = null;
                if (sec == null) {
                    sec = new StandardEvaluationContext();
                    sec.addPropertyAccessor(new EnvironmentAccessor());
                    sec.addPropertyAccessor(new BeanExpressionContextAccessor());
                    sec.addPropertyAccessor(new BeanFactoryAccessor());
                    sec.addPropertyAccessor(new MapAccessor());
                }
                primitiveConfiguration.setValue(String.valueOf(exp.getValue()));
            } else {
                LOGGER.trace("This property is NOT a SPEL expression: " + stringValue.getValue());
                primitiveConfiguration.setValue(stringValue.getValue());
            }
            return primitiveConfiguration;
        }
    } else if (value instanceof RuntimeBeanReference) {
        RuntimeBeanReference beanReference = (RuntimeBeanReference) value;
        ReferenceConfiguration referenceConfiguration = objectConfigurationFactory
                .createReferenceConfiguration();
        referenceConfiguration.setReferenceName(beanReference.getBeanName());
        return referenceConfiguration;
    } else if (value instanceof ManagedList<?>) {
        return parseValueList((ManagedList<?>) value);
    } else if (value instanceof ManagedMap<?, ?>) {
        return parseValueMap((ManagedMap<?, ?>) value);
    } else if (value instanceof BeanDefinitionHolder) {
        BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) value;
        return parseBeanDefinition(beanDefinitionHolder.getBeanDefinition());
    } else {
        throw new UnsupportedOperationException("The type of property value is not suppported : " + value
                + " (class : " + value.getClass().getName() + ")");
    }
}

From source file:com.qcadoo.model.internal.ExpressionServiceImpl.java

@Override
public String getValue(final Entity entity, final String expression, final Locale locale) {
    if (StringUtils.isEmpty(expression) || "null".equals(expression)) {
        LOG.warn("Calculating empty expressions");
        return null;
    }//www. j  a  v  a 2  s.  c o m

    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(expression);
    EvaluationContext context = new StandardEvaluationContext();

    if (entity != null) {
        Map<String, Object> values = getValuesForEntity(entity, locale, 2);

        for (Map.Entry<String, Object> entry : values.entrySet()) {
            context.setVariable(entry.getKey(), entry.getValue());
        }
    }

    String value = null;

    try {
        value = String.valueOf(exp.getValue(context));

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calculating value of expression \"" + expression + "\" for " + entity + " : " + value);
        }
    } catch (SpelEvaluationException e) {
        if (SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE.equals(e.getMessageCode())) {
            return "";
        } else {
            LOG.error("Error while calculating value of expression \"" + expression + "\" for " + entity, e);
            value = "!!!";
        }
    }

    if (StringUtils.isEmpty(value) || "null".equals(value)) {
        return null;
    } else {
        return translate(value, locale);
    }
}

From source file:cz.jirutka.validator.spring.SpELAssertValidator.java

private StandardEvaluationContext createEvaluationContext(Object rootObject) {
    StandardEvaluationContext context = new StandardEvaluationContext();

    context.setRootObject(rootObject);/*from   w  w  w. j a  v  a2s . co  m*/
    context.setTypeConverter(TYPE_CONVERTER);

    if (beanFactory != null) {
        context.setBeanResolver(new BeanFactoryResolver(beanFactory));
    }
    if (!functions.isEmpty()) {
        for (Method helper : functions) {
            context.registerFunction(helper.getName(), helper);
        }
        LOG.trace(inspectFunctions(context));
    }

    return context;
}

From source file:org.arrow.service.DefaultExecutionService.java

/**
 * {@inheritDoc}/*ww w.  j ava  2s . c  o  m*/
 */
@Override
public Object evaluateExpression(String expression) {
    ExpressionParser parser = new SpelExpressionParser();

    StandardEvaluationContext ec = new StandardEvaluationContext();
    ec.setBeanResolver(new BeanFactoryResolver(context));
    Expression expr = parser.parseExpression(expression);

    return expr.getValue(ec);
}

From source file:com.oembedler.moon.graphql.engine.execute.GraphQLAbstractRxExecutionStrategy.java

protected Observable<Double> calculateFieldComplexity(ExecutionContext executionContext,
        GraphQLObjectType parentType, List<Field> fields, Observable<Double> childScore) {
    return childScore.flatMap(aDouble -> {
        Observable<Double> result = Observable.just(aDouble + NODE_SCORE);
        GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType,
                fields.get(0));/*from   w  w  w  .java 2  s  .  c om*/
        if (fieldDef != null) {
            GraphQLFieldDefinitionWrapper graphQLFieldDefinitionWrapper = getGraphQLFieldDefinitionWrapper(
                    fieldDef);
            if (graphQLFieldDefinitionWrapper != null) {
                Expression expression = graphQLFieldDefinitionWrapper.getComplexitySpelExpression();
                if (expression != null) {
                    Map<String, Object> argumentValues = valuesResolver.getArgumentValues(
                            fieldDef.getArguments(), fields.get(0).getArguments(),
                            executionContext.getVariables());
                    StandardEvaluationContext context = new StandardEvaluationContext();
                    context.setVariable(GraphQLConstants.EXECUTION_COMPLEXITY_CHILD_SCORE, aDouble);
                    if (argumentValues != null)
                        context.setVariables(argumentValues);
                    result = Observable.just(expression.getValue(context, Double.class));
                }
            }
        }
        return addComplexityCheckObservable(executionContext, result);
    });
}

From source file:io.gravitee.gateway.services.healthcheck.EndpointHealthCheck.java

private void validateAssertions(final HealthStatus.Builder healthBuilder, final HealthCheckResponse response) {
    healthBuilder.success().status(response.getStatus());

    // Run assertions
    if (healthCheck.getExpectation().getAssertions() != null) {
        Iterator<String> assertionIterator = healthCheck.getExpectation().getAssertions().iterator();
        boolean success = true;
        while (success && assertionIterator.hasNext()) {
            String assertion = assertionIterator.next();
            ExpressionParser parser = new SpelExpressionParser();
            Expression expr = parser.parseExpression(assertion);

            StandardEvaluationContext context = new StandardEvaluationContext();
            context.registerFunction("jsonPath",
                    BeanUtils.resolveSignature("evaluate", JsonPathFunction.class));

            context.setVariable("response", response);

            success = expr.getValue(context, boolean.class);

            if (!success) {
                healthBuilder.message("Assertion can not be verified : " + assertion);
            }/* ww  w.ja v a  2s.  com*/
        }

        healthBuilder.success(success);
    }
}

From source file:org.craftercms.commons.ebus.config.EBusBeanAutoConfiguration.java

private <T> T expression(String selector, Object bean) {
    if (selector == null) {
        return null;
    }//  w  w  w  .  ja  v  a2s. c o  m

    StandardEvaluationContext evalCtx = new StandardEvaluationContext();
    evalCtx.setRootObject(bean);
    evalCtx.setBeanResolver(beanResolver);

    return (T) expressionParser.parseExpression(selector).getValue(evalCtx);
}

From source file:guru.qas.martini.DefaultMixologist.java

protected Collection<Martini> getMartinis(Expression expression) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    List<MethodResolver> methodResolvers = context.getMethodResolvers();
    ArrayList<MethodResolver> modifiedList = Lists.newArrayList(methodResolvers);
    modifiedList.add(new TagResolver(this.context, categories));
    context.setMethodResolvers(modifiedList);

    ImmutableList<Martini> martinis = getMartinis();
    List<Martini> matches = Lists.newArrayListWithCapacity(martinis.size());
    for (Martini martini : martinis) {
        Boolean match = expression.getValue(context, martini, Boolean.class);
        if (null != match && match) {
            matches.add(martini);/*from  w w  w.  j a  va  2  s .co m*/
        }
    }
    return matches;
}