use of org.mule.runtime.api.service.ServiceDefinition 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.ServiceDefinition in project mule by mulesoft.
the class ReflectionServiceProviderResolutionHelperTestCase method injectsDependencies.
@Test
public void injectsDependencies() throws Exception {
final InjectableServiceProvider serviceProvider = new InjectableServiceProvider();
List<ServiceDefinition> resolvedServices = new ArrayList<>();
FooService fooService = mock(FooService.class);
resolvedServices.add(new ServiceDefinition(FooService.class, fooService));
providerResolutionHelper.injectInstance(serviceProvider, resolvedServices);
assertThat(serviceProvider.fooService, is(fooService));
}
use of org.mule.runtime.api.service.ServiceDefinition in project mule by mulesoft.
the class ReflectionServiceResolverTestCase method createBarServiceProvider.
private Pair<ArtifactClassLoader, ServiceProvider> createBarServiceProvider(BarService barService) {
ServiceProvider barServiceProvider = mock(ServiceProvider.class);
final List<ServiceDefinition> barServices = new ArrayList<>();
barServices.add(new ServiceDefinition(BarService.class, barService));
when(barServiceProvider.providedServices()).thenReturn(barServices);
return new Pair<>(barServiceClassLoader, barServiceProvider);
}
use of org.mule.runtime.api.service.ServiceDefinition in project mule by mulesoft.
the class ReflectionServiceResolverTestCase method createFooServiceProvider.
private Pair<ArtifactClassLoader, ServiceProvider> createFooServiceProvider(FooService fooService) {
ServiceProvider fooServiceProvider = mock(ServiceProvider.class);
final List<ServiceDefinition> fooServices = new ArrayList<>();
fooServices.add(new ServiceDefinition(FooService.class, fooService));
when(fooServiceProvider.providedServices()).thenReturn(fooServices);
return new Pair<>(fooServiceClassLoader, fooServiceProvider);
}
Aggregations