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