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:com.googlecode.fascinator.portal.security.FascinatorWebSecurityExpressionHandler.java

@Override
public EvaluationContext createEvaluationContext(Authentication authentication, FilterInvocation fi) {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    SecurityExpressionRoot root;/*from   w  w w  . ja  v  a 2  s  .co m*/
    root = new FascinatorWebSecurityExpressionRoot(authentication, fi, storage, accessControl);
    root.setTrustResolver(trustResolver);
    root.setRoleHierarchy(roleHierarchy);

    ctx.setRootObject(root);

    return ctx;
}

From source file:net.asfun.jangod.base.Context.java

public Object getAttribute(String expression) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new ReflectivePropertyAccessor());
    context.addPropertyAccessor(new BeanFactoryAccessor());
    context.addPropertyAccessor(new MapAccessor());
    Expression parseExpression = null;
    try {/* w ww .  j av  a 2s . c om*/
        parseExpression = parser.parseExpression(expression);
    } catch (ParseException e) {
        // ignore parsing problem, might be jangod token
        return null;
    }
    try {
        return parseExpression.getValue(context, sessionBindings);
    } catch (EvaluationException e) {
        // ignore. try global application global bindings
    }
    try {
        return parseExpression.getValue(context, application.globalBindings);
    } catch (EvaluationException e) {
        return null;
    }
}

From source file:fr.acxio.tools.agia.ConfigurationTest.java

@Test
public void testSimpleNodeFactory() throws Exception {
    StandardEvaluationContext aContext = new StandardEvaluationContext();
    FieldSet aData = new DefaultFieldSet(new String[] { "V1", "V2", "V3" }, new String[] { "C1", "C2", "C3" });
    aContext.setVariable("in", aData.getProperties());
    NodeList aNodeList = defaultNodeFactory.getNodes(aContext);
    assertNotNull(aNodeList);//from w w w.j  av a2s .co m
    assertEquals(5, aNodeList.size());
}

From source file:org.springdata.ehcache.mapping.BasicEhcachePersistentEntity.java

@SuppressWarnings("unchecked")
public BasicEhcachePersistentEntity(TypeInformation<T> typeInformation) {

    super(typeInformation);

    this.parser = new SpelExpressionParser();
    this.context = new StandardEvaluationContext();

    Class<T> rawType = typeInformation.getType();

    if (rawType.isAnnotationPresent(Entity.class)) {
        Entity annotation = rawType.getAnnotation(Entity.class);
        this.cacheName = StringUtils.hasText(annotation.cacheName()) ? annotation.cacheName() : null;
    } else {//w  w  w  . j  a  v a2s  .  c  o m
        this.cacheName = null;
    }

    if (this.cacheName == null) {
        throw new BeanInitializationException(
                "entity class " + rawType + " does not specify cache name by Cache annotation");
    }

    if (DataSerializable.class.isAssignableFrom(rawType)) {
        serializer = new DataSerializer<T>();
        deserializer = new DataDeserializer<T>(rawType);
    } else {
        serializer = (Serializer<T>) new DefaultSerializer();
        deserializer = (Deserializer<T>) new DefaultDeserializer();
    }

}

From source file:app.web.InterpreterController.java

@RequestMapping("/interpreter/run")
public ModelAndView run(@RequestParam("script") String script) {
    Transaction transaction = Db.getSession().beginTransaction();
    // Groovy Shell
    //        Binding binding = new Binding();
    //        binding.setVariable("userService", userService); // FIXME: go through ApplicationContext or try to use SpEl instead?
    //        Object result = new GroovyShell(binding).parse(script).run();
    // Groovy Interpreter
    //        Object result = GroovyInterpreter.run(script);
    // SpEl/*from   w ww . j  ava2s .com*/
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setBeanResolver(new BeanFactoryResolver(beanFactory));
    Object result = parser.parseExpression(script).getValue(ctx);
    //        Db.flush(); // can not flush here, we get org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    transaction.commit();

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("script", script);
    map.put("result", result != null ? result.toString().replaceAll("\n", "<br/>").replaceAll(" ", "&nbsp;")
            : Void.class.getSimpleName());
    return new ModelAndView("/interpreter", map);
}

From source file:io.twipple.springframework.data.clusterpoint.mapping.BasicClusterpointPersistentEntity.java

public BasicClusterpointPersistentEntity(@NotNull TypeInformation<T> information,
        Comparator<ClusterpointPersistentProperty> comparator) {
    super(information, comparator);

    context = new StandardEvaluationContext();
}

From source file:org.formiz.core.expr.spel.ElContext.java

public ElContext() {
    ctx = new Context();
    spelContext = new StandardEvaluationContext();
}

From source file:org.craftercms.core.util.template.impl.spel.SpELStringTemplateCompiler.java

@PostConstruct
public void init() {
    if (evalContext == null) {
        evalContext = new StandardEvaluationContext();
    }//from   w  w w  . jav  a  2  s .co  m

    if (evalContext instanceof StandardEvaluationContext) {
        StandardEvaluationContext standardEvalContext = (StandardEvaluationContext) evalContext;
        // PropertyAccessor used when the model is a BeanFactory.
        standardEvalContext.addPropertyAccessor(new BeanFactoryAccessor());
        if (beanFactory != null) {
            if (standardEvalContext.getBeanResolver() == null) {
                standardEvalContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
            }
            if (standardEvalContext.getTypeLocator() == null) {
                standardEvalContext.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader()));
            }
            if (standardEvalContext.getTypeConverter() == null) {
                ConversionService conversionService = beanFactory.getConversionService();
                if (conversionService != null) {
                    standardEvalContext.setTypeConverter(new StandardTypeConverter(conversionService));
                }
            }
        }
    }
}

From source file:com.qpark.eip.core.spring.PayloadLogger.java

/** Creates the {@link PayloadLogger}. */
public PayloadLogger() {
    final StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    standardEvaluationContext.addPropertyAccessor(new MapAccessor());
    this.evaluationContext = standardEvaluationContext;
    this.expression = EXPRESSION_PARSER.parseExpression("payload");
}

From source file:reactor.spring.context.ConsumerBeanPostProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
    ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
        @Override// w ww .ja  v a  2  s. com
        public void doWith(final Method method) throws IllegalArgumentException, IllegalAccessException {
            Annotation anno = AnnotationUtils.findAnnotation(method, On.class);
            if (null == anno) {
                return;
            }

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

            On onAnno = (On) anno;

            Object reactorObj = null;
            if (StringUtils.hasText(onAnno.reactor())) {
                Expression reactorExpr = expressionParser.parseExpression(onAnno.reactor());
                reactorObj = reactorExpr.getValue(evalCtx);
            }

            Object selObj;
            if (StringUtils.hasText(onAnno.selector())) {
                try {
                    Expression selectorExpr = expressionParser.parseExpression(onAnno.selector());
                    selObj = selectorExpr.getValue(evalCtx);
                } catch (EvaluationException e) {
                    selObj = Fn.$(onAnno.selector());
                }
            } else {
                selObj = Fn.$(method.getName());
            }

            Consumer<Event<Object>> handler = new Consumer<Event<Object>>() {
                Class<?>[] argTypes = method.getParameterTypes();

                @Override
                public void accept(Event<Object> ev) {
                    if (argTypes.length == 0) {
                        ReflectionUtils.invokeMethod(method, bean);
                        return;
                    }

                    if (!argTypes[0].isAssignableFrom(ev.getClass())
                            && conversionService.canConvert(ev.getClass(), argTypes[0])) {
                        ReflectionUtils.invokeMethod(method, bean, conversionService.convert(ev, argTypes[0]));
                    } else {
                        ReflectionUtils.invokeMethod(method, bean, ev);
                        return;
                    }

                    if (null == ev.getData() || argTypes[0].isAssignableFrom(ev.getData().getClass())) {
                        ReflectionUtils.invokeMethod(method, bean, ev.getData());
                        return;
                    }

                    if (conversionService.canConvert(ev.getData().getClass(), argTypes[0])) {
                        ReflectionUtils.invokeMethod(method, bean,
                                conversionService.convert(ev.getData(), argTypes[0]));
                        return;
                    }

                    throw new IllegalArgumentException(
                            "Cannot invoke method " + method + " passing parameter " + ev.getData());
                }
            };

            if (!(selObj instanceof Selector)) {
                throw new IllegalArgumentException(selObj + ", referred to by the expression '"
                        + onAnno.selector() + "', is not a Selector");
            }
            if (null == reactorObj) {
                throw new IllegalStateException("Cannot register handler with null Reactor");
            } else {
                ((Reactor) reactorObj).on((Selector) selObj, handler);
            }
        }
    });
    return bean;
}