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