001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
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 *     http://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 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.core.event.script;
020
021import java.io.IOException;
022import java.io.Reader;
023
024import javax.script.CompiledScript;
025import javax.script.ScriptContext;
026import javax.script.ScriptEngine;
027import javax.script.ScriptException;
028
029/**
030 * Simulates a compiled script for scripts that don't support compilation.
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class FakeCompiledScript extends CompiledScript {
035
036    protected final ScriptEngine engine;
037
038    protected final Script script;
039
040    public FakeCompiledScript(ScriptEngine engine, Script script) {
041        this.script = script;
042        this.engine = engine;
043    }
044
045    @Override
046    public Object eval(ScriptContext arg0) throws ScriptException {
047        try {
048            try (Reader reader = script.getReader()) {
049                return engine.eval(reader, arg0);
050            }
051        } catch (IOException e) {
052            throw new ScriptException(e);
053        }
054    }
055
056    @Override
057    public ScriptEngine getEngine() {
058        return engine;
059    }
060
061}