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