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.TracerFactory;
027import org.nuxeo.ecm.core.api.NuxeoPrincipal;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
032 * @since 5.8
033 */
034@Operation(id = AutomationTraceToggleOperation.ID, category = Constants.CAT_EXECUTION, label = "Traces.toggleRecording", description = "Toggle Automation call tracing (you can set the 'enableTrace' parameter if you want to explicitly set the traceEnable value", addToStudio = false)
035public class AutomationTraceToggleOperation {
036
037    public static final String ID = "Traces.ToggleRecording";
038
039    @Param(name = "enableTrace", required = false)
040    protected Boolean enableTrace = null;
041
042    @Context
043    protected OperationContext ctx;
044
045    protected boolean canManageTraces() {
046        NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal();
047        return principal != null && (principal.isAdministrator());
048    }
049
050    @OperationMethod
051    public boolean run() {
052        TracerFactory tracerFactory = Framework.getLocalService(TracerFactory.class);
053        if (canManageTraces()) {
054            if (enableTrace == null) {
055                tracerFactory.toggleRecording();
056            } else {
057                if (enableTrace != tracerFactory.getRecordingState()) {
058                    tracerFactory.toggleRecording();
059                }
060            }
061        }
062        return tracerFactory.getRecordingState();
063    }
064}