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