001/*
002 * (C) Copyright 2006-2017 Nuxeo (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 *     bstefanescu
018 */
019package org.nuxeo.runtime.test.runner;
020
021import java.util.Arrays;
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.Map;
025import java.util.Set;
026
027import org.junit.Rule;
028import org.junit.rules.MethodRule;
029import org.nuxeo.runtime.RuntimeServiceEvent;
030import org.nuxeo.runtime.RuntimeServiceListener;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.model.ComponentManager;
033import org.nuxeo.runtime.test.RuntimeHarnessImpl;
034import org.nuxeo.runtime.test.runner.HotDeployer.ActionHandler;
035
036import com.google.inject.Binder;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041@Features({ MDCFeature.class, ConditionalIgnoreRule.Feature.class, RandomBug.Feature.class })
042public class RuntimeFeature implements RunnerFeature {
043
044    protected RuntimeHarness harness;
045
046    protected RuntimeDeployment deployment;
047
048    protected HotDeployer deployer;
049
050    /**
051     * Providers contributed by other features to override the default service provider used for a nuxeo service.
052     */
053    protected final Map<Class<?>, ServiceProvider<?>> serviceProviders;
054
055    public RuntimeFeature() {
056        serviceProviders = new HashMap<>();
057    }
058
059    public <T> void addServiceProvider(ServiceProvider<T> provider) {
060        serviceProviders.put(provider.getServiceClass(), provider);
061    }
062
063    public RuntimeHarness getHarness() {
064        return harness;
065    }
066
067    @Override
068    public void initialize(FeaturesRunner runner) throws Exception {
069        harness = new RuntimeHarnessImpl(runner.getTargetTestClass());
070        deployment = RuntimeDeployment.onTest(runner);
071        deployer = new HotDeployer(runner, harness);
072    }
073
074    public HotDeployer registerHandler(ActionHandler handler) {
075        return deployer.addHandler(handler);
076    }
077
078    public boolean unregisterHandler(ActionHandler handler) {
079        return deployer.removeHandler(handler);
080    }
081
082    @SuppressWarnings({ "unchecked", "rawtypes" })
083    @Override
084    public void configure(FeaturesRunner runner, Binder binder) {
085        binder.bind(RuntimeHarness.class).toInstance(getHarness());
086        binder.bind(HotDeployer.class).toInstance(deployer);
087        for (String svc : Framework.getRuntime().getComponentManager().getServices()) {
088            try {
089                Class clazz = Thread.currentThread().getContextClassLoader().loadClass(svc);
090                ServiceProvider<?> provider = serviceProviders.get(clazz);
091                if (provider == null) {
092                    provider = new ServiceProvider(clazz);
093                }
094                binder.bind(clazz).toProvider(provider).in(provider.getScope());
095            } catch (Exception e) {
096                throw new RuntimeException("Failed to bind service: " + svc, e);
097            }
098        }
099    }
100
101    @Override
102    public void start(final FeaturesRunner runner) throws Exception {
103        Framework.addListener(new RuntimeServiceListener() {
104
105            @Override
106            public void handleEvent(RuntimeServiceEvent event) {
107                if (event.id != RuntimeServiceEvent.RUNTIME_ABOUT_TO_START) {
108                    return;
109                }
110                Framework.removeListener(this);
111                blacklistComponents(runner);
112            }
113        });
114
115        harness.start();
116        deployment.deploy(runner, harness);
117    }
118
119    @Override
120    public void stop(FeaturesRunner runner) throws Exception {
121        harness.stop();
122    }
123
124    @Rule
125    public MethodRule onMethodDeployment() {
126        return RuntimeDeployment.onMethod();
127    }
128
129    protected void blacklistComponents(FeaturesRunner aRunner) {
130        BlacklistComponent config = aRunner.getConfig(BlacklistComponent.class);
131        if (config.value().length == 0) {
132            return;
133        }
134        final ComponentManager manager = Framework.getRuntime().getComponentManager();
135        Set<String> blacklist = new HashSet<>(manager.getBlacklist());
136        blacklist.addAll(Arrays.asList(config.value()));
137        manager.setBlacklist(blacklist);
138    }
139
140    @Override
141    public void beforeRun(FeaturesRunner runner) throws Exception {
142        // this will make a snapshot of the component registry and will start the components
143        harness.fireFrameworkStarted();
144    }
145
146}