001/*
002 * (C) Copyright 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 *     vpasquier <vpasquier@nuxeo.com>
018 *     slacoin <slacoin@nuxeo.com>
019 */
020
021package org.nuxeo.ecm.automation.core.trace;
022
023import java.util.LinkedList;
024import java.util.Map;
025import java.util.Stack;
026
027import org.nuxeo.ecm.automation.OperationCallback;
028import org.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.OperationException;
030import org.nuxeo.ecm.automation.OperationType;
031import org.nuxeo.ecm.automation.core.impl.InvokableMethod;
032
033/**
034 * Automation Abstract tracer recording all automation execution traces.
035 *
036 * @since 5.9.1
037 */
038public class Tracer implements OperationCallback {
039
040    protected final TracerFactory factory;
041
042    protected Stack<Context> stack = new Stack<Context>();
043
044    class Context {
045        final Call parent;
046        final OperationType typeof;
047        final LinkedList<Call> calls = new LinkedList<Call>();
048        OperationException error;
049
050        Context(Call parent, OperationType oftype) {
051            this.parent = parent;
052            typeof = oftype;
053        }
054
055    }
056
057    protected Tracer(TracerFactory factory) {
058        this.factory = factory;
059    }
060
061    protected void pushContext(OperationType chain) {
062        stack.push(new Context(stack.isEmpty() ? null : stack.peek().calls.peekLast(), chain));
063    }
064
065    protected void popContext() {
066        Context context = stack.pop();
067        Trace trace = factory.newTrace(context.parent, context.typeof, context.calls, context.calls.getLast().getOutput(), context.error);
068        if (stack.isEmpty()) {
069            factory.onTrace(trace);
070        } else {
071            context.parent.nested.add(trace);
072        }
073    }
074
075    @Override
076    public void onChainEnter(OperationType chain) {
077        pushContext(chain);
078    }
079
080    @Override
081    public void onChainExit() {
082        popContext();
083    }
084
085    @Override
086    public void onOperationEnter(OperationContext context, OperationType type, InvokableMethod method,
087            Map<String, Object> params) {
088        stack.peek().calls.add(factory.newCall(stack.peek().typeof, context, type, method, params));
089    }
090
091    @Override
092    public void onOperationExit(Object output) {
093        stack.peek().calls.peekLast().details.output = output;
094    }
095
096    @Override
097    public OperationException onError(OperationException error) {
098        return stack.peek().error = error;
099    }
100
101}