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