001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scripting.bsh;
018
019import java.io.IOException;
020import java.io.StringReader;
021import java.util.Map;
022
023import bsh.EvalError;
024import bsh.Interpreter;
025
026import org.springframework.beans.factory.BeanClassLoaderAware;
027import org.springframework.lang.Nullable;
028import org.springframework.scripting.ScriptCompilationException;
029import org.springframework.scripting.ScriptEvaluator;
030import org.springframework.scripting.ScriptSource;
031
032/**
033 * BeanShell-based implementation of Spring's {@link ScriptEvaluator} strategy interface.
034 *
035 * @author Juergen Hoeller
036 * @since 4.0
037 * @see Interpreter#eval(String)
038 */
039public class BshScriptEvaluator implements ScriptEvaluator, BeanClassLoaderAware {
040
041        @Nullable
042        private ClassLoader classLoader;
043
044
045        /**
046         * Construct a new BshScriptEvaluator.
047         */
048        public BshScriptEvaluator() {
049        }
050
051        /**
052         * Construct a new BshScriptEvaluator.
053         * @param classLoader the ClassLoader to use for the {@link Interpreter}
054         */
055        public BshScriptEvaluator(ClassLoader classLoader) {
056                this.classLoader = classLoader;
057        }
058
059
060        @Override
061        public void setBeanClassLoader(ClassLoader classLoader) {
062                this.classLoader = classLoader;
063        }
064
065
066        @Override
067        @Nullable
068        public Object evaluate(ScriptSource script) {
069                return evaluate(script, null);
070        }
071
072        @Override
073        @Nullable
074        public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
075                try {
076                        Interpreter interpreter = new Interpreter();
077                        interpreter.setClassLoader(this.classLoader);
078                        if (arguments != null) {
079                                for (Map.Entry<String, Object> entry : arguments.entrySet()) {
080                                        interpreter.set(entry.getKey(), entry.getValue());
081                                }
082                        }
083                        return interpreter.eval(new StringReader(script.getScriptAsString()));
084                }
085                catch (IOException ex) {
086                        throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex);
087                }
088                catch (EvalError ex) {
089                        throw new ScriptCompilationException(script, ex);
090                }
091        }
092
093}