001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     vpasquier <vpasquier@nuxeo.com>
016 *     slacoin <slacoin@nuxeo.com>
017 */
018package org.nuxeo.ecm.automation.core.trace;
019
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.automation.OperationException;
027import org.nuxeo.ecm.automation.OperationType;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * @since 5.7.3
032 */
033public class Trace {
034
035    protected final Call parent;
036
037    protected final OperationType chain;
038
039    protected final OperationException error;
040
041    protected final Object output;
042
043    protected final List<Call> operations;
044
045    Trace(Call parent, OperationType chain, List<Call> operations) {
046        this.parent = parent;
047        // If chain doesn't exist, this should be one operation call
048        this.chain = chain != null ? chain : operations.get(0).getType();
049        this.operations = new ArrayList<Call>(operations);
050        output = null;
051        error = null;
052    }
053
054    Trace(Call parent, OperationType chain, List<Call> calls, OperationException error) {
055        this.parent = parent;
056        // If chain doesn't exist, this should be one operation call
057        this.chain = chain != null ? chain : calls.get(0).getType();
058        operations = new ArrayList<Call>(calls);
059        output = null;
060        this.error = error;
061    }
062
063    Trace(Call parent, OperationType chain, List<Call> calls, Object output) {
064        this.parent = parent;
065        // If chain doesn't exist, this should be one operation call
066        this.chain = chain != null ? chain : calls.get(0).getType();
067        operations = new ArrayList<Call>(calls);
068        this.output = output;
069        error = null;
070    }
071
072    public Call getParent() {
073        return parent;
074    }
075
076    public OperationType getChain() {
077        return chain;
078    }
079
080    public OperationException getError() {
081        return error;
082    }
083
084    public Object getOutput() {
085        return output;
086    }
087
088    public List<Call> getCalls() {
089        return operations;
090    }
091
092    public String getFormattedText() {
093        TracerFactory tracerFactory = Framework.getLocalService(TracerFactory.class);
094        ByteArrayOutputStream out = new ByteArrayOutputStream();
095        try {
096            if (tracerFactory.getRecordingState()) {
097                new TracePrinter(out).print(this);
098            } else {
099                new TracePrinter(out).litePrint(this);
100            }
101        } catch (IOException e) {
102            LogFactory.getLog(Trace.class).error("Cannot print trace of " + chain.getId(), e);
103            return chain.getId();
104        }
105        return out.toString();
106    }
107}