001/*
002 * (C) Copyright 2015-2016 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 *     Thierry Delprat <tdelprat@nuxeo.com>
018 */
019package org.nuxeo.automation.scripting.internals.operation;
020
021import java.util.Map;
022
023import javax.script.ScriptException;
024
025import org.nuxeo.automation.scripting.api.AutomationScriptingService;
026import org.nuxeo.automation.scripting.internals.ScriptOperationContext;
027import org.nuxeo.automation.scripting.internals.WrapperHelper;
028import org.nuxeo.ecm.automation.OperationException;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @since 7.2
034 */
035public class ScriptingOperationImpl {
036
037    protected final ScriptOperationContext ctx;
038
039    protected final Map<String, Object> args;
040
041    protected final String source;
042
043    public ScriptingOperationImpl(String source, ScriptOperationContext ctx, Map<String, Object> args)
044            throws ScriptException {
045        this.ctx = ctx;
046        this.args = args;
047        this.source = source;
048    }
049
050    public Object run(Object input) throws Exception {
051        try {
052            AutomationScriptingService scriptingService = Framework.getService(AutomationScriptingService.class);
053            scriptingService.setOperationContext(ctx);
054            ScriptingOperationInterface itf = scriptingService.getInterface(ScriptingOperationInterface.class, source,
055                    ctx.getCoreSession());
056            input = wrapArgsAndInput(input, args);
057            return unwrapResult(itf.run(input, args));
058        } catch (ScriptException e) {
059            throw new OperationException(e);
060        } finally {
061            if (ctx.get(Constants.VAR_IS_CHAIN) != null && !(Boolean) ctx.get(Constants.VAR_IS_CHAIN)) {
062                ctx.deferredDispose();
063            }
064        }
065    }
066
067    protected Object wrapArgsAndInput(Object input, Map<String, Object> args) {
068        args.replaceAll((key, value) -> WrapperHelper.wrap(value, ctx.getCoreSession()));
069        return WrapperHelper.wrap(input, ctx.getCoreSession());
070    }
071
072    protected Object unwrapResult(Object res) {
073        // Unwrap Context
074        ctx.replaceAll((key, value) -> WrapperHelper.unwrap(value));
075        // Unwrap Result
076        return WrapperHelper.unwrap(res);
077    }
078
079}