001/*
002 * (C) Copyright 2006-2013 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.automation.core.impl;
020
021import java.lang.reflect.InvocationTargetException;
022import java.lang.reflect.Method;
023import java.util.Arrays;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.OperationException;
030import org.nuxeo.ecm.automation.OperationType;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.Blobs;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentModelList;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public class InvokableMethod implements Comparable<InvokableMethod> {
041
042    protected static final Log log = LogFactory.getLog(InvokableMethod.class);
043
044    public static final int VOID_PRIORITY = 1;
045
046    public static final int ADAPTABLE_PRIORITY = 2;
047
048    public static final int ISTANCE_OF_PRIORITY = 3;
049
050    public static final int EXACT_MATCH_PRIORITY = 4;
051
052    // priorities from 1 to 16 are reserved for internal use.
053    public static final int USER_PRIORITY = 16;
054
055    protected OperationType op;
056
057    protected Method method;
058
059    protected Class<?> produce;
060
061    protected Class<?> consume;
062
063    protected int priority;
064
065    public InvokableMethod(OperationType op, Method method, OperationMethod anno) {
066        produce = method.getReturnType();
067        Class<?>[] p = method.getParameterTypes();
068        if (p.length > 1) {
069            throw new IllegalArgumentException("Operation method must accept at most one argument: " + method);
070        }
071        // if produce is Void => a control operation
072        // if (produce == Void.TYPE) {
073        // throw new IllegalArgumentException("Operation method must return a
074        // value: "+method);
075        // }
076        this.op = op;
077        this.method = method;
078        priority = anno.priority();
079        if (priority > 0) {
080            priority += USER_PRIORITY;
081        }
082        consume = p.length == 0 ? Void.TYPE : p[0];
083    }
084
085    public InvokableMethod(OperationType op, Method method) {
086        produce = method.getReturnType();
087        Class<?>[] p = method.getParameterTypes();
088        if (p.length > 1) {
089            throw new IllegalArgumentException("Operation method must accept at most one argument: " + method);
090        }
091        this.op = op;
092        this.method = method;
093        if (priority > 0) {
094            priority += USER_PRIORITY;
095        }
096        String inputType = this.op.getInputType();
097        if (inputType != null) {
098            switch (inputType) {
099            case "document":
100                consume = DocumentModel.class;
101                break;
102            case "documents":
103                consume = DocumentModelList.class;
104                break;
105            case "blob":
106                consume = Blob.class;
107                break;
108            case "blobs":
109                consume = Blobs.class;
110                break;
111            default:
112                consume = Object.class;
113                break;
114            }
115        } else {
116            consume = p.length == 0 ? Void.TYPE : p[0];
117        }
118    }
119
120    public boolean isIterable() {
121        return false;
122    }
123
124    public int getPriority() {
125        return priority;
126    }
127
128    public OperationType getOperation() {
129        return op;
130    }
131
132    public final Class<?> getOutputType() {
133        return produce;
134    }
135
136    public final Class<?> getInputType() {
137        return consume;
138    }
139
140    /**
141     * Return 0 for no match.
142     */
143    public int inputMatch(Class<?> in) {
144        if (consume == in) {
145            return priority > 0 ? priority : EXACT_MATCH_PRIORITY;
146        }
147        if (consume.isAssignableFrom(in)) {
148            return priority > 0 ? priority : ISTANCE_OF_PRIORITY;
149        }
150        if (op.getService().isTypeAdaptable(in, consume)) {
151            return priority > 0 ? priority : ADAPTABLE_PRIORITY;
152        }
153        if (consume == Void.TYPE) {
154            return priority > 0 ? priority : VOID_PRIORITY;
155        }
156        return 0;
157    }
158
159    protected Object doInvoke(OperationContext ctx, Map<String, Object> args, Object input) throws OperationException,
160            ReflectiveOperationException {
161        Object target = op.newInstance(ctx, args);
162        if (consume == Void.TYPE) {
163            // preserve last output for void methods
164            Object out = method.invoke(target);
165            return produce == Void.TYPE ? input : out;
166        } else {
167            if (input != null && !consume.isAssignableFrom(input.getClass())) {
168                // try to adapt
169                input = op.getService().getAdaptedValue(ctx, input, consume);
170            }
171            return method.invoke(target, input);
172        }
173    }
174
175    public Object invoke(OperationContext ctx, Map<String, Object> args) throws OperationException {
176        try {
177            return doInvoke(ctx, args, ctx.getInput());
178        } catch (OperationException e) {
179            throw e;
180        } catch (InvocationTargetException e) {
181            Throwable t = e.getTargetException();
182            if (t instanceof OperationException) {
183                throw (OperationException) t;
184            } else {
185                String exceptionMessage = "Failed to invoke operation " + op.getId();
186                if (op.getAliases() != null && op.getAliases().length > 0) {
187                    exceptionMessage += " with aliases " + Arrays.toString(op.getAliases());
188                }
189                throw new OperationException(exceptionMessage, t);
190            }
191        } catch (ReflectiveOperationException e) {
192            String exceptionMessage = "Failed to invoke operation " + op.getId();
193            if (op.getAliases() != null && op.getAliases().length > 0) {
194                exceptionMessage += " with aliases " + Arrays.toString(op.getAliases());
195            }
196            throw new OperationException(exceptionMessage, e);
197        }
198    }
199
200    @Override
201    public String toString() {
202        return getClass().getSimpleName() + "(" + method + ", " + priority + ")";
203    }
204
205    @Override
206    // used for methods of the same class, so ignore the class
207    public int compareTo(InvokableMethod o) {
208        // compare on name
209        int cmp = method.getName().compareTo(o.method.getName());
210        if (cmp != 0) {
211            return cmp;
212        }
213        // same name, compare on parameter types
214        Class<?>[] pt = method.getParameterTypes();
215        Class<?>[] opt = o.method.getParameterTypes();
216        // smaller length first
217        cmp = pt.length - opt.length;
218        if (cmp != 0) {
219            return cmp;
220        }
221        // compare parameter classes lexicographically
222        for (int i = 0; i < pt.length; i++) {
223            cmp = pt[i].getName().compareTo(opt[i].getName());
224            if (cmp != 0) {
225                return cmp;
226            }
227        }
228        return 0;
229    }
230
231    public Method getMethod() {
232        return method;
233    }
234
235    public Class<?> getProduce() {
236        return produce;
237    }
238
239    public Class<?> getConsume() {
240        return consume;
241    }
242}