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