io.dyn.el.ScopedEvaluationContext.java Source code

Java tutorial

Introduction

Here is the source code for io.dyn.el.ScopedEvaluationContext.java

Source

/*
 * Copyright (c) 2011-2012 by 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 io.dyn.el;

import java.util.ArrayList;
import java.util.List;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @author Jon Brisbin <jon@jbrisbin.com>
 */
public class ScopedEvaluationContext extends StandardEvaluationContext {

    private List<EvaluationContext> parentContexts = new ArrayList<>();

    public ScopedEvaluationContext(List<EvaluationContext> parentContexts) {
        this.parentContexts.addAll(parentContexts);
    }

    public ScopedEvaluationContext(EvaluationContext evalCtx) {
        if (null != evalCtx) {
            parentContexts.add(evalCtx);
        }
    }

    public ScopedEvaluationContext(EvaluationContext evalCtx, Object rootObj) {
        super(rootObj);
        if (null != evalCtx) {
            parentContexts.add(evalCtx);
        }
    }

    public List<EvaluationContext> parentContexts() {
        return parentContexts;
    }

    @Override
    public Object lookupVariable(String name) {
        Object o = super.lookupVariable(name);
        if (null == o) {
            for (EvaluationContext ec : parentContexts) {
                o = ec.lookupVariable(name);
                if (null != o) {
                    break;
                }
            }
        }
        return o;
    }

}