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