org.springframework.integration.expression.ExpressionUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.springframework.integration.expression.ExpressionUtils.java

Source

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.integration.expression;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.expression.MapAccessor;
import org.springframework.core.convert.ConversionService;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.integration.context.IntegrationContextUtils;

/**
 * Utility class with static methods for helping with establishing environments for
 * SpEL expressions.
 *
 * @author Gary Russell
 * @author Oleg Zhurakousky
 * @author Artem Bilan
 * @since 2.2
 */
public abstract class ExpressionUtils {

    private static final Log logger = LogFactory.getLog(ExpressionUtils.class);

    /**
     * Create a {@link StandardEvaluationContext} with a {@link MapAccessor} in its
     * property accessor property and the supplied {@link ConversionService} in its
     * conversionService property.
     * @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;
    }

    /**
     * Used to create a context with no BeanFactory, usually in tests.
     * @return The evaluation context.
     */
    public static StandardEvaluationContext createStandardEvaluationContext() {
        return doCreateContext(null);
    }

    /**
     * Obtains the context from the beanFactory if not null; emits a warning if the beanFactory
     * is null.
     * @param beanFactory
     * @return The evaluation context.
     */
    public static StandardEvaluationContext createStandardEvaluationContext(BeanFactory beanFactory) {
        if (beanFactory == null) {
            logger.warn("Creating EvaluationContext with no beanFactory", new RuntimeException("No beanfactory"));
        }
        return doCreateContext(beanFactory);
    }

    private static StandardEvaluationContext doCreateContext(BeanFactory beanFactory) {
        ConversionService conversionService = null;
        StandardEvaluationContext evaluationContext = null;
        if (beanFactory != null) {
            evaluationContext = IntegrationContextUtils.getEvaluationContext(beanFactory);
        }
        if (evaluationContext == null) {
            if (beanFactory != null) {
                conversionService = IntegrationContextUtils.getConversionService(beanFactory);
            }
            evaluationContext = createStandardEvaluationContext(conversionService, beanFactory);
        }
        return evaluationContext;
    }

}