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:org.focusns.common.web.page.engine.widget.WidgetPageEngine.java

private EvaluationContext createEvaluationContext() {
    StandardEvaluationContext evaluationContext = (StandardEvaluationContext) getServletContext()
            .getAttribute(EVALUATION_CONTEXT_KEY);
    if (evaluationContext == null) {
        evaluationContext = new StandardEvaluationContext();
        evaluationContext.addPropertyAccessor(new NavigatorPropertyAccessor());
        evaluationContext.addPropertyAccessor(new ServletPropertyAccessor());
        evaluationContext.addPropertyAccessor(new MapAccessor());
        ///*from   w  ww  .  j  a  va  2 s.  c o  m*/
        getServletContext().setAttribute(EVALUATION_CONTEXT_KEY, evaluationContext);
    }
    return evaluationContext;
}

From source file:com.googlecode.ehcache.annotations.key.SpELCacheKeyGenerator.java

/**
 * Get the {@link EvaluationContext} to use to evaluate the configured {@link Expression}
 *//*ww  w.ja v a 2s.c om*/
protected EvaluationContext getEvaluationContext(MethodInvocation methodInvocation, Object... args) {
    final StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.setBeanResolver(new BeanFactoryResolver(this.beanFactory));
    evaluationContext.setVariable("invocation", methodInvocation);
    evaluationContext.setVariable("args", args);
    evaluationContext.setVariable("key", keyCallbackObject);
    evaluationContext.addMethodResolver(this.methodResolver);
    return evaluationContext;
}

From source file:com.oembedler.moon.graphql.engine.dfs.GraphQLSchemaDfsTraversal.java

public Object evaluateSpElExpression(DfsContext dfsContext, Class<?> implClass, Object instance,
        String spElExpression, Object defaultIfNone) {
    Object defaultValue = defaultIfNone;
    if (StringUtils.hasText(spElExpression)) {
        Expression expression = SPEL_EXPRESSION_PARSER.parseExpression(spElExpression);

        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable(GraphQLConstants.DFS_IMPLEMENTATION_CLASS, implClass);
        context.setVariable(GraphQLConstants.DFS_OBJECT_INSTANCE, instance);
        defaultValue = expression.getValue(context);
    }//  ww  w . j  a  v  a  2 s .com
    return defaultValue;
}

From source file:org.apache.rave.synchronization.SynchronizingAspect.java

private EvaluationContext getEvaluationContext(Class<?> targetClass, Method method, Object[] args) {
    EvaluationContext context = new StandardEvaluationContext();
    if (ArrayUtils.isEmpty(args)) {
        return context;
    }//from   w  w w  .ja  va2  s.  c o m

    Method targetMethod = getTargetMethod(targetClass, method);
    for (int i = 0; i < args.length; i++) {
        context.setVariable("p" + i, args[i]);
    }

    String[] parameterNames = paramNameDiscoverer.getParameterNames(targetMethod);
    if (parameterNames != null) {
        for (int i = 0; i < parameterNames.length; i++) {
            context.setVariable(parameterNames[i], args[i]);
        }
    }

    return context;
}

From source file:org.cloudfoundry.identity.uaa.scim.ScimUserEndpoints.java

@RequestMapping(value = "/Users", method = RequestMethod.GET)
@ResponseBody//from w  ww.  j av a  2 s.  c om
public SearchResults<Map<String, Object>> findUsers(
        @RequestParam(value = "attributes", required = false, defaultValue = "id") String attributesCommaSeparated,
        @RequestParam(required = false, defaultValue = "id pr") String filter,
        @RequestParam(required = false) String sortBy,
        @RequestParam(required = false, defaultValue = "ascending") String sortOrder,
        @RequestParam(required = false, defaultValue = "1") int startIndex,
        @RequestParam(required = false, defaultValue = "100") int count) {

    List<ScimUser> input;
    try {
        input = dao.retrieveUsers(filter, sortBy, sortOrder.equals("ascending"));
    } catch (IllegalArgumentException e) {
        throw new ScimException("Invalid filter expression: [" + filter + "]", HttpStatus.BAD_REQUEST);
    }
    String[] attributes = attributesCommaSeparated.split(",");
    Map<String, Expression> expressions = new LinkedHashMap<String, Expression>();

    for (String attribute : attributes) {

        String spel = attribute.replaceAll("emails\\.(.*)", "emails.![$1]");
        logger.debug("Registering SpEL for attribute: " + spel);

        Expression expression;
        try {
            expression = new SpelExpressionParser().parseExpression(spel);
        } catch (SpelParseException e) {
            throw new ScimException("Invalid attributes: [" + attributesCommaSeparated + "]",
                    HttpStatus.BAD_REQUEST);
        }

        expressions.put(attribute, expression);

    }

    Collection<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
    StandardEvaluationContext context = new StandardEvaluationContext();
    try {
        for (ScimUser user : input.subList(startIndex - 1, startIndex + count - 1)) {
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            for (String attribute : expressions.keySet()) {
                map.put(attribute, expressions.get(attribute).getValue(context, user));
            }
            users.add(map);
        }
    } catch (SpelEvaluationException e) {
        throw new ScimException("Invalid attributes: [" + attributesCommaSeparated + "]",
                HttpStatus.BAD_REQUEST);
    }

    return new SearchResults<Map<String, Object>>(schemas, users, 1, count, input.size());

}

From source file:org.finra.dm.app.security.SecurityHelper.java

/**
 * Checks whether security is enabled based on SpEL expression defined in environment.
 * /*w  w w.  j  a  va  2s .com*/
 * @param request {@link HttpServletRequest} to determine whether security is enabled.
 * @return true if security is enabled, false if disabled.
 */
public boolean isSecurityEnabled(HttpServletRequest request) {
    Boolean isSecurityEnabled = true;

    String enableSecuritySpelExpression = configurationHelper
            .getProperty(ConfigurationValue.SECURITY_ENABLED_SPEL_EXPRESSION);

    if (StringUtils.isNotBlank(enableSecuritySpelExpression)) {
        EvaluationContext evaluationContext = new StandardEvaluationContext();
        evaluationContext.setVariable("request", request);

        Expression expression = expressionParser.parseExpression(enableSecuritySpelExpression);

        isSecurityEnabled = expression.getValue(evaluationContext, Boolean.class);
    }

    return isSecurityEnabled;
}

From source file:org.metaeffekt.dcc.commons.mapping.AbstractPropertyExpression.java

@SuppressWarnings("unchecked")
protected <T> T evaluateExpression(String originalExpression, String value, Class<T> type) {
    final ExpressionParser parser = new SpelExpressionParser();

    try {//  w  ww .  j  a v a  2 s  .  c o m
        final StandardEvaluationContext context = new StandardEvaluationContext();
        context.setRootObject(propertiesHolder);
        context.addPropertyAccessor(new EnvironmentAccessor());
        context.setVariable(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, STANDARD_ENVIRONMENT);
        context.setVariable(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, STANDARD_ENVIRONMENT);

        final Expression expression = parser.parseExpression(value, PARSER_CONTEXT);

        return expression.getValue(context, type);
    } catch (ParseException | EvaluationException e) {
        LOG.debug(String.format("Failed to evaluate expression %s, and with replaced properties %s",
                originalExpression, value), e);
    }

    if (type == String.class) {
        return (T) value;
    }

    return null;
}

From source file:org.springframework.amqp.rabbit.stocks.context.RefreshScope.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    beanFactory.registerScope(name, this);
    setSerializationId(beanFactory);/* ww  w .j a v a  2  s.  c o  m*/

    this.beanFactory = beanFactory;

    evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a BeanDefinitionRegistry, so RefreshScope cannot be used.");
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
        // Replace this or any of its inner beans with scoped proxy if it
        // has this scope
        boolean scoped = name.equals(definition.getScope());
        Scopifier scopifier = new Scopifier(registry, name, proxyTargetClass, scoped);
        scopifier.visitBeanDefinition(definition);
        if (scoped) {
            createScopedProxy(beanName, definition, registry, proxyTargetClass);
        }
    }

}

From source file:org.springframework.batch.core.partition.gemfire.RemoteScope.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    beanFactory.registerScope(name, this);

    this.beanFactory = beanFactory;
    evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a BeanDefinitionRegistry, so RefreshScope cannot be used.");
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    String id = this.id;
    if (id == null) {
        String names = Arrays.asList(registry.getBeanDefinitionNames()).toString();
        logger.debug("Generating bean factory id from names: " + names);
        id = UUID.nameUUIDFromBytes(names.getBytes()).toString();
        logger.debug("Generated bean factory id: " + id);
    }//w w  w  .  j a  v  a  2 s . co m

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a DefaultListableBeanFactory, so RefreshScope cannot be used.");
    ((DefaultListableBeanFactory) beanFactory).setSerializationId(id);

    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
        // Replace this or any of its inner beans with scoped proxy if it
        // has this scope
        boolean scoped = name.equals(definition.getScope());
        Scopifier scopifier = new Scopifier(registry, name, proxyTargetClass, scoped);
        scopifier.visitBeanDefinition(definition);
        if (scoped) {
            createScopedProxy(beanName, definition, registry, proxyTargetClass);
        }
    }

}

From source file:org.springframework.cloud.context.scope.GenericScope.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    beanFactory.registerScope(name, this);
    setSerializationId(beanFactory);//  ww w.  j a  v a  2 s.  c o  m

    this.beanFactory = beanFactory;

    evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());

    if (!autoProxy) {
        // No need to try and create proxies
        return;
    }

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a BeanDefinitionRegistry, so RefreshScope cannot be used.");
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
        // Replace this or any of its inner beans with scoped proxy if it
        // has this scope
        boolean scoped = name.equals(definition.getScope());
        Scopifier scopifier = new Scopifier(registry, name, proxyTargetClass, scoped);
        scopifier.visitBeanDefinition(definition);
        if (scoped) {
            createScopedProxy(beanName, definition, registry, proxyTargetClass);
        }
    }

}