Example usage for org.springframework.context.expression EnvironmentAccessor EnvironmentAccessor

List of usage examples for org.springframework.context.expression EnvironmentAccessor EnvironmentAccessor

Introduction

In this page you can find the example usage for org.springframework.context.expression EnvironmentAccessor EnvironmentAccessor.

Prototype

EnvironmentAccessor

Source Link

Usage

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 {//from  w  ww  . j a  v  a 2  s.co  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: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 {/*from   ww  w.java 2 s  .c  om*/
        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.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport.java

/**
 * Constructs, configures and initializes a new instance of an {@link EvaluationContext}.
 *
 * @param beanFactory reference to the Spring {@link BeanFactory}.
 * @return a new {@link EvaluationContext}.
 * @see org.springframework.beans.factory.BeanFactory
 * @see org.springframework.expression.EvaluationContext
 * @see #getBeanFactory()/*from www.  j  a  v  a2  s .c  o m*/
 */
protected EvaluationContext newEvaluationContext(BeanFactory beanFactory) {

    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();

    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
    evaluationContext.addPropertyAccessor(new EnvironmentAccessor());
    evaluationContext.addPropertyAccessor(new MapAccessor());
    evaluationContext.setTypeLocator(new StandardTypeLocator(getBeanClassLoader()));

    configureTypeConverter(evaluationContext, beanFactory);

    return evaluationContext;
}