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.RunnerFeature;
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")
047@Deploy("org.nuxeo.ecm.actions")
048@Deploy("org.nuxeo.ecm.automation.features")
049@Deploy("org.nuxeo.ecm.automation.scripting")
050@Deploy("org.nuxeo.ecm.platform.query.api")
051@Deploy("org.nuxeo.runtime.management")
052public class AutomationFeature implements RunnerFeature {
053
054    protected final OperationContextProvider contextProvider = new OperationContextProvider();
055
056    protected final TracerProvider tracerProvider = new TracerProvider();
057
058    protected OperationContext context;
059
060    protected TracerFactory tracerFactory;
061
062    protected OperationCallback tracer;
063
064    protected CoreFeature coreFeature;
065
066    public class OperationContextProvider implements Provider<OperationContext> {
067
068        @Override
069        public OperationContext get() {
070            return getContext();
071        }
072
073    }
074
075    class TracerProvider implements Provider<OperationCallback> {
076
077        @Override
078        public OperationCallback get() {
079            return getTracer();
080        }
081
082    }
083
084    protected OperationContext getContext() {
085        if (context == null) {
086            CoreSession session = coreFeature.getCoreSession();
087            context = new OperationContext(session);
088            if (tracer != null) {
089                context.setCallback(tracer);
090            }
091        }
092        return context;
093    }
094
095    protected OperationCallback getTracer() {
096        if (tracer == null) {
097            tracer = tracerFactory.newTracer();
098            if (context != null) {
099                context.setCallback(tracer);
100            }
101        }
102        return tracer;
103    }
104
105    @Override
106    public void configure(FeaturesRunner runner, Binder binder) {
107        binder.bind(OperationContext.class).toProvider(contextProvider).in(AutomationScope.INSTANCE);
108        binder.bind(OperationCallback.class).toProvider(tracerProvider).in(AutomationScope.INSTANCE);
109        coreFeature = runner.getFeature(CoreFeature.class);
110        tracerFactory = Framework.getService(TracerFactory.class);
111    }
112
113    @Override
114    public void beforeSetup(FeaturesRunner runner) {
115        AutomationScope.INSTANCE.enter();
116    }
117
118    @Override
119    public void afterTeardown(FeaturesRunner runner) {
120        AutomationScope.INSTANCE.exit();
121        if (context != null) {
122            context.close();
123        }
124        context = null;
125        tracer = null;
126        tracerFactory.clearTraces();
127    }
128}