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 *     Sun Seng David TAN <stan@nuxeo.com>
018 *     vpasquier <vpasquier@nuxeo.com>
019 *     slacoin <slacoin@nuxeo.com>
020 */
021package org.nuxeo.ecm.automation.test;
022
023import org.nuxeo.ecm.automation.OperationCallback;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.trace.TracerFactory;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.test.CoreFeature;
028import org.nuxeo.ecm.platform.test.PlatformFeature;
029import org.nuxeo.runtime.api.Framework;
030import org.nuxeo.runtime.test.runner.Deploy;
031import org.nuxeo.runtime.test.runner.Features;
032import org.nuxeo.runtime.test.runner.FeaturesRunner;
033import org.nuxeo.runtime.test.runner.SimpleFeature;
034
035import com.google.inject.Binder;
036import com.google.inject.Provider;
037
038/**
039 * Based on the existing {@link PlatformFeature}, AutomationFeature is a simple feature that includes
040 * org.nuxeo.ecm.automation.core and org.nuxeo.ecm.automation.features bundles.
041 *
042 * @since 5.7
043 * @since 5.6-HF02
044 */
045@Features(PlatformFeature.class)
046@Deploy({ "org.nuxeo.ecm.automation.core", "org.nuxeo.ecm.automation.features", "org.nuxeo.ecm.automation.scripting", "org.nuxeo.ecm.platform.query.api",
047        "org.nuxeo.runtime.management" })
048public class AutomationFeature extends SimpleFeature {
049
050    protected final OperationContextProvider contextProvider = new OperationContextProvider();
051
052    protected final TracerProvider tracerProvider = new TracerProvider();
053
054    protected OperationContext context;
055
056    protected TracerFactory tracerFactory;
057
058    protected OperationCallback tracer;
059
060    protected CoreFeature coreFeature;
061
062    public class OperationContextProvider implements Provider<OperationContext> {
063
064        @Override
065        public OperationContext get() {
066            return getContext();
067        }
068
069    }
070
071    class TracerProvider implements Provider<OperationCallback> {
072
073        @Override
074        public OperationCallback get() {
075            return getTracer();
076        }
077
078    }
079
080    protected OperationContext getContext() {
081        if (context == null) {
082            CoreSession session = coreFeature.getCoreSession();
083            context = new OperationContext(session);
084            if (tracer != null) {
085                context.setCallback(tracer);
086            }
087        }
088        return context;
089    }
090
091    protected OperationCallback getTracer() {
092        if (tracer == null) {
093            tracer = tracerFactory.newTracer();
094            if (context != null) {
095                context.setCallback(tracer);
096            }
097        }
098        return tracer;
099    }
100
101    @Override
102    public void configure(FeaturesRunner runner, Binder binder) {
103        binder.bind(OperationContext.class).toProvider(contextProvider).in(AutomationScope.INSTANCE);
104        binder.bind(OperationCallback.class).toProvider(tracerProvider).in(AutomationScope.INSTANCE);
105        coreFeature = runner.getFeature(CoreFeature.class);
106        tracerFactory = Framework.getLocalService(TracerFactory.class);
107    }
108
109    @Override
110    public void beforeSetup(FeaturesRunner runner) throws Exception {
111        AutomationScope.INSTANCE.enter();
112    }
113
114    @Override
115    public void afterTeardown(FeaturesRunner runner) throws Exception {
116        AutomationScope.INSTANCE.exit();
117        if (context != null) {
118            context.close();
119        }
120        context = null;
121        tracer = null;
122        tracerFactory.clearTraces();
123    }
124}