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
047        final OperationType typeof;
048
049        final LinkedList<Call> calls = new LinkedList<Call>();
050
051        OperationException error;
052
053        Context(Call parent, OperationType oftype) {
054            this.parent = parent;
055            typeof = oftype;
056        }
057
058    }
059
060    protected Tracer(TracerFactory factory) {
061        this.factory = factory;
062    }
063
064    protected void pushContext(OperationType chain) {
065        stack.push(new Context(stack.isEmpty() ? null : stack.peek().calls.peekLast(), chain));
066    }
067
068    protected void popContext() {
069        Context context = stack.pop();
070        Trace trace = factory.newTrace(context.parent, context.typeof, context.calls,
071                context.calls.getLast().getOutput(), context.error);
072        if (stack.isEmpty()) {
073            factory.onTrace(trace);
074        } else {
075            context.parent.nested.add(trace);
076        }
077    }
078
079    @Override
080    public void onChainEnter(OperationType chain) {
081        pushContext(chain);
082    }
083
084    @Override
085    public void onChainExit() {
086        popContext();
087    }
088
089    @Override
090    public void onOperationEnter(OperationContext context, OperationType type, InvokableMethod method,
091            Map<String, Object> params) {
092        stack.peek().calls.add(factory.newCall(stack.peek().typeof, context, type, method, params));
093    }
094
095    @Override
096    public void onOperationExit(Object output) {
097        stack.peek().calls.peekLast().details.output = output;
098    }
099
100    @Override
101    public OperationException onError(OperationException error) {
102        return stack.peek().error = error;
103    }
104
105}