001/*
002 * (C) Copyright 2015 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;
020
021import java.lang.reflect.Method;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.automation.AutomationService;
028import org.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.OperationDocumentation;
030import org.nuxeo.ecm.automation.OperationException;
031import org.nuxeo.ecm.automation.OperationType;
032import org.nuxeo.ecm.automation.core.impl.InvokableMethod;
033
034/**
035 * @since 7.2
036 */
037public class ScriptingOperationTypeImpl implements OperationType {
038
039    protected final AutomationScriptingServiceImpl scripting;
040
041    protected final AutomationService automation;
042
043    protected final ScriptingOperationDescriptor desc;
044
045    protected final InvokableMethod method;
046
047    public ScriptingOperationTypeImpl(AutomationScriptingServiceImpl scripting, AutomationService automation,
048            ScriptingOperationDescriptor desc) {
049        this.scripting = scripting;
050        this.automation = automation;
051        this.desc = desc;
052        this.method = runMethod(this, desc.getInputType());
053    }
054
055    @Override
056    public String getContributingComponent() {
057        return "org.nuxeo.automation.scripting.internals.AutomationScriptingComponent";
058    }
059
060    @Override
061    public OperationDocumentation getDocumentation() {
062        OperationDocumentation doc = new OperationDocumentation(getId());
063        doc.label = getId();
064        doc.category = desc.getCategory();
065        doc.description = desc.getDescription();
066        doc.params = desc.getParams();
067        doc.signature = new String[] { desc.getInputType(), desc.getOutputType() };
068        doc.aliases = desc.getAliases();
069        return doc;
070    }
071
072    @Override
073    public String getId() {
074        return desc.getId();
075    }
076
077    @Override
078    public String[] getAliases() {
079        return desc.getAliases();
080    }
081
082    @Override
083    public Object newInstance(OperationContext ctx, Map<String, Object> args) throws OperationException {
084        Map<String, Object> params = new HashMap<>(args);
085        scripting.paramsInjector.inject(params, ctx, desc);
086        return new ScriptingOperationImpl(desc.source, ctx, params);
087    }
088
089    @Override
090    public Class<?> getType() {
091        return ScriptingOperationImpl.class;
092    }
093
094    @Override
095    public String getInputType() {
096        return desc.getInputType();
097    }
098
099    @Override
100    public AutomationService getService() {
101        return automation;
102    }
103
104    @Override
105    public InvokableMethod[] getMethodsMatchingInput(Class<?> in) {
106        return new InvokableMethod[] { method };
107    }
108
109    @Override
110    public List<InvokableMethod> getMethods() {
111        return Collections.singletonList(method);
112    }
113
114    /**
115     * Returns the right {@code run} method according to whether the input type exists or not.
116     */
117    protected static InvokableMethod runMethod(ScriptingOperationTypeImpl op, String inputType) {
118        try {
119            Method method = inputType == null ? ScriptingOperationImpl.class.getMethod("run")
120                    : ScriptingOperationImpl.class.getMethod("run", Object.class);
121            return new InvokableMethod(op, method);
122        } catch (ReflectiveOperationException cause) {
123            throw new Error("Cannot reference run method of " + ScriptingOperationImpl.class);
124        }
125    }
126
127}