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

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

Introduction

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

Prototype

public void setBeanResolver(BeanResolver beanResolver) 

Source Link

Usage

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

@PostConstruct
public void init() {
    if (evalContext == null) {
        evalContext = new StandardEvaluationContext();
    }/*from   www. j av  a  2  s.  c  o  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:org.apache.camel.language.spel.SpelExpression.java

private EvaluationContext createEvaluationContext(Exchange exchange) {
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange));
    if (exchange.getContext() instanceof SpringCamelContext) {
        // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext
        ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext())
                .getApplicationContext();
        evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }//ww w.  java2  s  .  com
    return evaluationContext;
}

From source file:org.jasig.portlet.spring.SpringELProcessor.java

@Override
public String process(String value, PortletRequest request) {
    Map<String, Object> context = getContext(request);

    StandardEvaluationContext sec = new StandardEvaluationContext(context);
    sec.addPropertyAccessor(new MapAccessor());
    sec.addPropertyAccessor(new ReflectivePropertyAccessor());
    sec.addPropertyAccessor(new DefaultPropertyAccessor(PARSER_CONTEXT.getExpressionPrefix(),
            PARSER_CONTEXT.getExpressionSuffix()));
    if (beanResolver != null) {
        sec.setBeanResolver(beanResolver);
    }//from   www .  j  a va2s  . c  om
    SpelExpressionParser parser = new SpelExpressionParser();

    try {
        String processed = parser.parseExpression(value, PARSER_CONTEXT).getValue(sec, String.class);
        return processed;
    } catch (SpelEvaluationException e) {
        throw new EvaluationException("Failed to process string '" + value
                + "'. See nested error message and check your SpEL tokens in your string", e);
    }
}

From source file:org.mule.module.spel.EvaluationContextFactory.java

public EvaluationContext create(MuleMessage message) {
    Assert.notNull(message, "Message cannot be null");

    StandardEvaluationContext simpleContext = new StandardEvaluationContext();

    simpleContext.setVariable(CONTEXT_VAR_NAME, muleContext);
    simpleContext.setVariable(REGISTRY_VAR_NAME, muleRegistry);
    simpleContext.setVariable(MESSAGE_VAR_NAME, message);
    simpleContext.setVariable(ORIGINAL_PAYLOAD_VAR_NAME, message.getOriginalPayload());
    simpleContext.setVariable(PAYLOAD_VAR_NAME, message.getPayload());
    simpleContext.setVariable(ID_VAR_NAME, message.getUniqueId());

    /* systemProperties and systemEnvironment are spring beans already registered
       in the mule registry, and thus available as @systemEnvironment and @systemProperties.
       But this is collateral, so to properly to mimic Spring framework behavior both beans will
       be added as variables. //from  ww w  . ja  v  a2s.c o m
    */

    if (muleRegistry != null) {
        simpleContext.setVariable(SYSTEM_PROPERTIES_VAR_NAME,
                muleRegistry.get(ConfigurableApplicationContext.SYSTEM_PROPERTIES_BEAN_NAME));
        simpleContext.setVariable(ENVIRONMENT_VAR_NAME,
                muleRegistry.get(ConfigurableApplicationContext.SYSTEM_ENVIRONMENT_BEAN_NAME));
    }

    simpleContext.setRootObject(message.getPayload());

    simpleContext.setBeanResolver(beanResolver);
    return simpleContext;
}

From source file:org.apereo.portal.portlet.PortletSpELServiceImpl.java

/**
 * Return a SpEL evaluation context for the supplied portlet request.
 *
 * @param request PortletRequest// w  w w . j a v a 2 s  .c o  m
 * @return SpEL evaluation context for the supplied portlet request
 */
protected EvaluationContext getEvaluationContext(PortletRequest request) {
    Map<String, String> userInfo = (Map<String, String>) request.getAttribute(PortletRequest.USER_INFO);
    final SpELEnvironmentRoot root = new SpELEnvironmentRoot(new PortletWebRequest(request), userInfo);
    final StandardEvaluationContext context = new StandardEvaluationContext(root);
    // Allows for @myBeanName replacements
    context.setBeanResolver(this.beanResolver);
    return context;
}

From source file:org.apereo.portal.spring.spel.PortalSpELServiceImpl.java

@Override
public String getValue(String expressionString, Object spelEnvironment) {
    final StandardEvaluationContext context = new StandardEvaluationContext(spelEnvironment);
    context.setBeanResolver(this.beanResolver);
    final Expression expression = this.parseCachedExpression(expressionString, TemplateParserContext.INSTANCE);
    return expression.getValue(context, String.class);
}

From source file:org.apereo.portal.spring.spel.PortalSpELServiceImpl.java

/**
 * Return a SpEL evaluation context for the supplied web request.
 * //w w  w.  j  av  a 2s.  co  m
 * @param request
 * @return
 */
protected EvaluationContext getEvaluationContext(WebRequest request) {
    final HttpServletRequest httpRequest = this.portalRequestUtils.getOriginalPortalRequest(request);
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(httpRequest);
    final IPerson person = userInstance.getPerson();

    final SpELEnvironmentRoot root = new SpELEnvironmentRoot(request, person);
    final StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.setBeanResolver(this.beanResolver);
    return context;
}

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  ww .j a  v  a 2  s . com*/
    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.data.mongodb.core.convert.MappingMongoConverter.java

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

    final StandardEvaluationContext spelCtx = new StandardEvaluationContext(dbo);
    spelCtx.addPropertyAccessor(DBObjectPropertyAccessor.INSTANCE);

    if (applicationContext != null) {
        spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }/*w w w .  j a  v a  2s .  com*/

    final MappedConstructor constructor = new MappedConstructor(entity, mappingContext);

    SpELAwareParameterValueProvider delegate = new SpELAwareParameterValueProvider(spelExpressionParser,
            spelCtx);
    ParameterValueProvider provider = new DelegatingParameterValueProvider(constructor, dbo, delegate);

    final BeanWrapper<MongoPersistentEntity<S>, S> wrapper = BeanWrapper.create(entity, provider,
            conversionService);

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

            boolean isConstructorProperty = constructor.isConstructorParameter(prop);
            boolean hasValueForProperty = dbo.containsField(prop.getFieldName());

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

            Object obj = getValueInternal(prop, dbo, spelCtx, prop.getSpelExpression());
            wrapper.setProperty(prop, obj, useFieldAccessOnly);
        }
    });

    // 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 {
                wrapper.setProperty(inverseProp, obj);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    return wrapper.getBean();
}

From source file:org.springframework.integration.expression.ExpressionUtils.java

/**
 * Create a {@link StandardEvaluationContext} with a {@link MapAccessor} in its
 * property accessor property and the supplied {@link ConversionService} in its
 * conversionService property./*  w w  w.ja va2s.c  o  m*/
 * @param conversionService the conversion service.
 * @return the evaluation context.
 */
private static StandardEvaluationContext createStandardEvaluationContext(ConversionService conversionService,
        BeanFactory beanFactory) {
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new MapAccessor());
    if (conversionService != null) {
        evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService));
    }
    if (beanFactory != null) {
        evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
    }
    return evaluationContext;
}