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

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

Introduction

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

Prototype

public void registerFunction(String name, Method method) 

Source Link

Usage

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);
            }/*from w  w w  .  j a va  2  s .  com*/
        }

        healthBuilder.success(success);
    }
}

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

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

    context.setRootObject(rootObject);//from ww  w  .j a  v  a2 s  .  c  o  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.kuali.rice.krad.datadictionary.DataDictionary.java

/**
 * Sets up the bean post processor and conversion service
 *
 * @param beans - The bean factory for the the dictionary beans
 *///  w  w  w.j  a  va  2 s . c  o  m
public static void setupProcessor(DefaultListableBeanFactory beans) {
    try {
        // UIF post processor that sets component ids
        BeanPostProcessor idPostProcessor = ComponentBeanPostProcessor.class.newInstance();
        beans.addBeanPostProcessor(idPostProcessor);
        beans.setBeanExpressionResolver(new StandardBeanExpressionResolver() {
            @Override
            protected void customizeEvaluationContext(StandardEvaluationContext evalContext) {
                try {
                    evalContext.registerFunction("getService", ExpressionFunctions.class
                            .getDeclaredMethod("getService", new Class[] { String.class }));
                } catch (NoSuchMethodException me) {
                    LOG.error("Unable to register custom expression to data dictionary bean factory", me);
                }
            }
        });

        // special converters for shorthand map and list property syntax
        GenericConversionService conversionService = new GenericConversionService();
        conversionService.addConverter(new StringMapConverter());
        conversionService.addConverter(new StringListConverter());

        beans.setConversionService(conversionService);
    } catch (Exception e1) {
        throw new DataDictionaryException(
                "Cannot create component decorator post processor: " + e1.getMessage(), e1);
    }
}

From source file:org.kuali.rice.krad.uif.service.impl.ExpressionEvaluatorServiceImpl.java

/**
 * Registers custom functions for el expressions with the given context
 *
 * @param context - context instance to register functions to
 */// w  w w . j ava2s .  c om
protected void addCustomFunctions(StandardEvaluationContext context) {
    try {
        // TODO: possibly reflect ExpressionFunctions and add automatically
        context.registerFunction("isAssignableFrom", ExpressionFunctions.class
                .getDeclaredMethod("isAssignableFrom", new Class[] { Class.class, Class.class }));
        context.registerFunction("empty",
                ExpressionFunctions.class.getDeclaredMethod("empty", new Class[] { Object.class }));
        context.registerFunction("getName",
                ExpressionFunctions.class.getDeclaredMethod("getName", new Class[] { Class.class }));
        context.registerFunction("getParm", ExpressionFunctions.class.getDeclaredMethod("getParm",
                new Class[] { String.class, String.class, String.class }));
        context.registerFunction("getParmInd", ExpressionFunctions.class.getDeclaredMethod("getParmInd",
                new Class[] { String.class, String.class, String.class }));
        context.registerFunction("hasPerm", ExpressionFunctions.class.getDeclaredMethod("hasPerm",
                new Class[] { String.class, String.class }));
        context.registerFunction("hasPermDtls", ExpressionFunctions.class.getDeclaredMethod("hasPermDtls",
                new Class[] { String.class, String.class, Map.class, Map.class }));
        context.registerFunction("hasPermTmpl", ExpressionFunctions.class.getDeclaredMethod("hasPermTmpl",
                new Class[] { String.class, String.class, Map.class, Map.class }));
    } catch (NoSuchMethodException e) {
        LOG.error("Custom function for el expressions not found: " + e.getMessage());
        throw new RuntimeException("Custom function for el expressions not found: " + e.getMessage(), e);
    }
}

From source file:org.kuali.rice.krad.uif.view.DefaultExpressionEvaluator.java

/**
 * Registers custom functions for el expressions with the given context
 *
 * @param context - context instance to register functions to
 *///from w  w  w.  j a va  2  s . c  o  m
protected void addCustomFunctions(StandardEvaluationContext context) {
    context.registerFunction("isAssignableFrom", isAssignableFrom);
    context.registerFunction("empty", empty);
    context.registerFunction("emptyList", emptyList);
    context.registerFunction("getService", getService);
    context.registerFunction("listContains", listContains);
    context.registerFunction("getName", getName);
    context.registerFunction("getParam", getParam);
    context.registerFunction("getParamAsBoolean", getParamAsBoolean);
    context.registerFunction("hasPerm", hasPerm);
    context.registerFunction("hasPermDtls", hasPermDtls);
    context.registerFunction("hasPermTmpl", hasPermTmpl);
    context.registerFunction("sequence", sequence);
    context.registerFunction("getDataObjectKey", getDataObjectKey);
}

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(//  ww  w  .  j a v a2 s.  co  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));
}