Search in sources :

Example 1 with ServiceDefinition

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;
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ServiceResolutionError(org.mule.runtime.module.service.api.discoverer.ServiceResolutionError) Collection(java.util.Collection) Set(java.util.Set) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) ServiceDefinition(org.mule.runtime.api.service.ServiceDefinition) Service(org.mule.runtime.api.service.Service) ServiceProvider(org.mule.runtime.api.service.ServiceProvider) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Map(java.util.Map) Comparator.comparing(java.util.Comparator.comparing) Pair(org.mule.runtime.api.util.Pair) LinkedList(java.util.LinkedList) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ServiceResolutionError(org.mule.runtime.module.service.api.discoverer.ServiceResolutionError) ArrayList(java.util.ArrayList) Service(org.mule.runtime.api.service.Service) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ServiceDefinition(org.mule.runtime.api.service.ServiceDefinition) Pair(org.mule.runtime.api.util.Pair) HashSet(java.util.HashSet)

Example 2 with ServiceDefinition

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));
}
Also used : ArrayList(java.util.ArrayList) ServiceDefinition(org.mule.runtime.api.service.ServiceDefinition) Test(org.junit.Test)

Example 3 with ServiceDefinition

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);
}
Also used : ServiceProvider(org.mule.runtime.api.service.ServiceProvider) ArrayList(java.util.ArrayList) ServiceDefinition(org.mule.runtime.api.service.ServiceDefinition) Pair(org.mule.runtime.api.util.Pair)

Example 4 with ServiceDefinition

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);
}
Also used : ServiceProvider(org.mule.runtime.api.service.ServiceProvider) ArrayList(java.util.ArrayList) ServiceDefinition(org.mule.runtime.api.service.ServiceDefinition) Pair(org.mule.runtime.api.util.Pair)

Aggregations

ArrayList (java.util.ArrayList)4 ServiceDefinition (org.mule.runtime.api.service.ServiceDefinition)4 ServiceProvider (org.mule.runtime.api.service.ServiceProvider)3 Pair (org.mule.runtime.api.util.Pair)3 Collection (java.util.Collection)1 Comparator.comparing (java.util.Comparator.comparing)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 Test (org.junit.Test)1 Service (org.mule.runtime.api.service.Service)1 Preconditions.checkArgument (org.mule.runtime.api.util.Preconditions.checkArgument)1 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)1 ServiceResolutionError (org.mule.runtime.module.service.api.discoverer.ServiceResolutionError)1