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