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 */ 020package org.nuxeo.ecm.automation.core.trace; 021 022import java.io.ByteArrayOutputStream; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.List; 026 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.automation.OperationException; 029import org.nuxeo.ecm.automation.OperationType; 030import org.nuxeo.runtime.api.Framework; 031 032/** 033 * @since 5.7.3 034 */ 035public class Trace { 036 037 protected final Call parent; 038 039 protected final OperationType chain; 040 041 protected final OperationException error; 042 043 protected final Object output; 044 045 protected final List<Call> operations; 046 047 Trace(Call parent, OperationType chain, List<Call> operations) { 048 this.parent = parent; 049 // If chain doesn't exist, this should be one operation call 050 this.chain = chain != null ? chain : operations.get(0).getType(); 051 this.operations = new ArrayList<Call>(operations); 052 output = null; 053 error = null; 054 } 055 056 Trace(Call parent, OperationType chain, List<Call> calls, OperationException error) { 057 this.parent = parent; 058 // If chain doesn't exist, this should be one operation call 059 this.chain = chain != null ? chain : calls.get(0).getType(); 060 operations = new ArrayList<Call>(calls); 061 output = null; 062 this.error = error; 063 } 064 065 Trace(Call parent, OperationType chain, List<Call> calls, Object output) { 066 this.parent = parent; 067 // If chain doesn't exist, this should be one operation call 068 this.chain = chain != null ? chain : calls.get(0).getType(); 069 operations = new ArrayList<Call>(calls); 070 this.output = output; 071 error = null; 072 } 073 074 public Call getParent() { 075 return parent; 076 } 077 078 public OperationType getChain() { 079 return chain; 080 } 081 082 public OperationException getError() { 083 return error; 084 } 085 086 public Object getOutput() { 087 return output; 088 } 089 090 public List<Call> getCalls() { 091 return operations; 092 } 093 094 public String getFormattedText() { 095 TracerFactory tracerFactory = Framework.getLocalService(TracerFactory.class); 096 ByteArrayOutputStream out = new ByteArrayOutputStream(); 097 try { 098 if (tracerFactory.getRecordingState()) { 099 new TracePrinter(out).print(this); 100 } else { 101 new TracePrinter(out).litePrint(this); 102 } 103 } catch (IOException e) { 104 LogFactory.getLog(Trace.class).error("Cannot print trace of " + chain.getId(), e); 105 return chain.getId(); 106 } 107 return out.toString(); 108 } 109}