use of org.mule.runtime.api.service.Service in project mule by mulesoft.
the class ReflectionServiceResolver method resolveServices.
@Override
public List<Pair<ArtifactClassLoader, Service>> resolveServices(List<Pair<ArtifactClassLoader, ServiceProvider>> serviceProviders) throws ServiceResolutionError {
List<DependencyAwareServiceProvider> dependencyAwareServiceProviders = createDependencyAwareServiceProviders(serviceProviders);
Map<Class<? extends Service>, Pair<ArtifactClassLoader, ServiceDefinition>> registeredServices = new LinkedHashMap<>();
List<DependencyAwareServiceProvider> unresolvedServiceProviders = new LinkedList<>(dependencyAwareServiceProviders);
List<DependencyAwareServiceProvider> resolvedServiceProviders = new LinkedList<>();
boolean continueResolution = true;
while (continueResolution) {
int initialResolvedCount = resolvedServiceProviders.size();
List<DependencyAwareServiceProvider> pendingUnresolvedServices = new LinkedList<>();
for (DependencyAwareServiceProvider dependencyAwareServiceProvider : unresolvedServiceProviders) {
List<ServiceDefinition> serviceDefinitions = registeredServices.values().stream().map(pair -> pair.getSecond()).collect(toList());
if (isResolvedService(dependencyAwareServiceProvider, serviceDefinitions)) {
serviceProviderResolutionHelper.injectInstance(dependencyAwareServiceProvider.serviceProvider, serviceDefinitions);
for (ServiceDefinition serviceDefinition : dependencyAwareServiceProvider.providedServices()) {
registeredServices.put(serviceDefinition.getServiceClass(), new Pair<>(dependencyAwareServiceProvider.getArtifactClassLoader(), serviceDefinition));
}
resolvedServiceProviders.add(dependencyAwareServiceProvider);
} else {
pendingUnresolvedServices.add(dependencyAwareServiceProvider);
}
}
// Will try to resolve the services that are still unresolved
unresolvedServiceProviders = pendingUnresolvedServices;
continueResolution = resolvedServiceProviders.size() > initialResolvedCount;
}
if (!unresolvedServiceProviders.isEmpty()) {
final Set<Class<? extends Service>> dependencies = new HashSet<>();
for (DependencyAwareServiceProvider dependencyAwareServiceProvider : unresolvedServiceProviders) {
dependencies.addAll(dependencyAwareServiceProvider.getDependencies());
}
throw new ServiceResolutionError("Unable to resolve core service dependencies. Missing some of: " + dependencies);
}
List<Pair<ArtifactClassLoader, Service>> servicePairs = new ArrayList<>();
for (Pair<ArtifactClassLoader, ServiceDefinition> pair : registeredServices.values()) {
servicePairs.add(new Pair<>(pair.getFirst(), pair.getSecond().getService()));
}
return servicePairs;
}
use of org.mule.runtime.api.service.Service in project mule by mulesoft.
the class MuleServiceManager method wrapServices.
private List<Service> wrapServices(List<Pair<ArtifactClassLoader, Service>> registeredServices) {
final List<Service> result = new ArrayList<>(registeredServices.size());
for (Pair<ArtifactClassLoader, Service> pair : registeredServices) {
final Service serviceProxy = createLifecycleFilterServiceProxy(pair.getSecond());
result.add(serviceProxy);
}
return unmodifiableList(result);
}
use of org.mule.runtime.api.service.Service in project mule by mulesoft.
the class MuleServiceManagerTestCase method stopsServicesAfterStartFail.
@Test
public void stopsServicesAfterStartFail() throws Exception {
StartableStoppableService firstService = mock(StartableStoppableService.class);
StartableStoppableService secondService = mock(StartableStoppableService.class);
Exception firstStartFailure = new RuntimeException();
doThrow(firstStartFailure).when(firstService).start();
final ServiceDiscoverer serviceDiscoverer = mock(ServiceDiscoverer.class);
final List<Pair<ArtifactClassLoader, Service>> services = new ArrayList<>();
Pair<ArtifactClassLoader, Service> service1Pairs = new Pair(mock(ArtifactClassLoader.class), firstService);
Pair<ArtifactClassLoader, Service> service2Pairs = new Pair(mock(ArtifactClassLoader.class), secondService);
services.add(service1Pairs);
services.add(service2Pairs);
when(serviceDiscoverer.discoverServices()).thenReturn(services);
final MuleServiceManager muleServiceManager = new MuleServiceManager(serviceDiscoverer);
try {
muleServiceManager.start();
fail();
} catch (Exception e) {
assertThat(e.getCause(), sameInstance(firstStartFailure));
}
muleServiceManager.stop();
verify(secondService, never()).stop();
}
use of org.mule.runtime.api.service.Service in project mule by mulesoft.
the class MuleServiceManagerTestCase method startServices.
@Test
public void startServices() throws Exception {
final ServiceDiscoverer serviceDiscoverer = mock(ServiceDiscoverer.class);
final List<Pair<ArtifactClassLoader, Service>> services = new ArrayList<>();
Pair<ArtifactClassLoader, Service> service1 = new Pair(mock(ArtifactClassLoader.class), mock(StartableService.class));
Pair<ArtifactClassLoader, Service> service2 = new Pair(mock(ArtifactClassLoader.class), mock(StartableService.class));
services.add(service1);
services.add(service2);
when(serviceDiscoverer.discoverServices()).thenReturn(services);
final MuleServiceManager muleServiceManager = new MuleServiceManager(serviceDiscoverer);
muleServiceManager.start();
InOrder inOrder = inOrder(service1.getSecond(), service2.getSecond());
inOrder.verify((StartableService) service1.getSecond()).start();
inOrder.verify((StartableService) service2.getSecond()).start();
}
use of org.mule.runtime.api.service.Service in project mule by mulesoft.
the class BasicRuntimeServicesConfigurationBuilder method doConfigure.
@Override
protected void doConfigure(MuleContext muleContext) throws Exception {
new SpiServiceRegistry().lookupProviders(Service.class, BasicRuntimeServicesConfigurationBuilder.class.getClassLoader()).forEach(service -> {
try {
startIfNeeded(service);
registerObject(muleContext, service.getName(), service);
} catch (MuleException e) {
throw new MuleRuntimeException(e);
}
});
DefaultExpressionLanguageFactoryService weaveExpressionExecutor = new WeaveDefaultExpressionLanguageFactoryService();
registerObject(muleContext, weaveExpressionExecutor.getName(), weaveExpressionExecutor);
}
Aggregations