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