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.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 * <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 016 */ 017 018package org.nuxeo.ecm.automation.core.operations.traces; 019 020import org.nuxeo.ecm.automation.OperationContext; 021import org.nuxeo.ecm.automation.core.Constants; 022import org.nuxeo.ecm.automation.core.annotations.Context; 023import org.nuxeo.ecm.automation.core.annotations.Operation; 024import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 025import org.nuxeo.ecm.automation.core.annotations.Param; 026import org.nuxeo.ecm.automation.core.trace.Trace; 027import org.nuxeo.ecm.automation.core.trace.TracerFactory; 028import org.nuxeo.ecm.core.api.NuxeoPrincipal; 029import org.nuxeo.runtime.api.Framework; 030 031/** 032 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 033 * @since 5.8 034 */ 035@Operation(id = AutomationTraceGetOperation.ID, category = Constants.CAT_EXECUTION, label = "Traces.getTrace", description = "Retrieve trace associated to a Chain or an Operation", addToStudio = false) 036public class AutomationTraceGetOperation { 037 038 public static final String ID = "Traces.Get"; 039 040 @Param(name = "traceKey", required = false) 041 protected String traceKey = null; 042 043 @Param(name = "index", required = false) 044 protected int index = -1; 045 046 @Context 047 protected OperationContext ctx; 048 049 protected boolean canManageTraces() { 050 NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal(); 051 return principal != null && (principal.isAdministrator()); 052 } 053 054 @OperationMethod 055 public String run() { 056 057 if (canManageTraces()) { 058 return null; 059 } 060 061 TracerFactory tracerFactory = Framework.getLocalService(TracerFactory.class); 062 063 if (traceKey == null) { 064 Trace trace = tracerFactory.getLastErrorTrace(); 065 if (trace != null) { 066 return trace.getFormattedText(); 067 } else { 068 return "no previous error trace found"; 069 } 070 } else { 071 Trace trace = tracerFactory.getTrace(traceKey); 072 if (trace != null) { 073 return trace.getFormattedText(); 074 } else { 075 return "no trace found"; 076 } 077 } 078 } 079}