Search in sources :

Example 1 with ServiceProvider

use of org.mule.runtime.api.service.ServiceProvider in project mule by mulesoft.

the class FileSystemServiceProviderDiscoverer method createServiceProviders.

private List<Pair<ArtifactClassLoader, ServiceProvider>> createServiceProviders(List<ServiceDescriptor> serviceDescriptors, ArtifactClassLoaderFactory<ServiceDescriptor> serviceClassLoaderFactory) throws ServiceResolutionError {
    List<Pair<ArtifactClassLoader, ServiceProvider>> serviceProviders = new LinkedList<>();
    for (ServiceDescriptor serviceDescriptor : serviceDescriptors) {
        final ArtifactClassLoader serviceClassLoader = serviceClassLoaderFactory.create(getServiceArtifactId(serviceDescriptor), serviceDescriptor, apiClassLoader.getClassLoader(), apiClassLoader.getClassLoaderLookupPolicy());
        final ServiceProvider serviceProvider = instantiateServiceProvider(serviceClassLoader.getClassLoader(), serviceDescriptor.getServiceProviderClassName());
        serviceProviders.add(new Pair<>(serviceClassLoader, serviceProvider));
    }
    return serviceProviders;
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ServiceDescriptor(org.mule.runtime.module.service.internal.artifact.ServiceDescriptor) ServiceProvider(org.mule.runtime.api.service.ServiceProvider) LinkedList(java.util.LinkedList) Pair(org.mule.runtime.api.util.Pair)

Example 2 with ServiceProvider

use of org.mule.runtime.api.service.ServiceProvider 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 3 with ServiceProvider

use of org.mule.runtime.api.service.ServiceProvider in project mule by mulesoft.

the class FileSystemServiceProviderDiscovererTestCase method discoversServices.

@Test
public void discoversServices() throws Exception {
    installService("fooService", FooServiceProvider.class);
    installService("barService", BarServiceProvider.class);
    ArtifactClassLoader serviceClassLoader = mock(ArtifactClassLoader.class);
    when(serviceClassLoaderFactory.create(argThat(any(String.class)), argThat(any(ServiceDescriptor.class)), argThat(any(ClassLoader.class)), argThat(any(ClassLoaderLookupPolicy.class)))).thenReturn(serviceClassLoader);
    final FileSystemServiceProviderDiscoverer serviceProviderDiscoverer = new FileSystemServiceProviderDiscoverer(containerClassLoader, serviceClassLoaderFactory, descriptorLoaderRepository, builder());
    final List<Pair<ArtifactClassLoader, ServiceProvider>> serviceProvidersPairs = serviceProviderDiscoverer.discover();
    List<ServiceProvider> serviceProviders = serviceProvidersPairs.stream().map(Pair::getSecond).collect(Collectors.toList());
    assertThat(serviceProviders.size(), equalTo(2));
    assertThat(serviceProviders, hasItem(instanceOf(FooServiceProvider.class)));
    assertThat(serviceProviders, hasItem(instanceOf(BarServiceProvider.class)));
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ServiceProvider(org.mule.runtime.api.service.ServiceProvider) Pair(org.mule.runtime.api.util.Pair) Test(org.junit.Test)

Example 4 with ServiceProvider

use of org.mule.runtime.api.service.ServiceProvider in project mule by mulesoft.

the class IsolatedServiceProviderDiscoverer method discover.

@Override
public List<Pair<ArtifactClassLoader, ServiceProvider>> discover() throws ServiceResolutionError {
    List<Pair<ArtifactClassLoader, ServiceProvider>> serviceProviders = new LinkedList<>();
    for (Object serviceArtifactClassLoader : serviceArtifactClassLoaders) {
        try {
            final ServiceProvider serviceProvider;
            String artifactName = (String) serviceArtifactClassLoader.getClass().getMethod("getArtifactId").invoke(serviceArtifactClassLoader);
            ClassLoader classLoader = (ClassLoader) serviceArtifactClassLoader.getClass().getMethod("getClassLoader").invoke(serviceArtifactClassLoader);
            serviceProvider = instantiateServiceProvider(classLoader, artifactName);
            // TODO MULE-12254 - Remove null which is needed in order to avoid class cast exceptions
            serviceProviders.add(new Pair(null, serviceProvider));
        } catch (Exception e) {
            throw new IllegalStateException("Couldn't discover service from class loader: " + serviceArtifactClassLoader, e);
        }
    }
    return serviceProviders;
}
Also used : ServiceProvider(org.mule.runtime.api.service.ServiceProvider) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) LinkedList(java.util.LinkedList) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Pair(org.mule.runtime.api.util.Pair)

Example 5 with ServiceProvider

use of org.mule.runtime.api.service.ServiceProvider 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)

Aggregations

ServiceProvider (org.mule.runtime.api.service.ServiceProvider)6 Pair (org.mule.runtime.api.util.Pair)6 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 ServiceDefinition (org.mule.runtime.api.service.ServiceDefinition)3 Collection (java.util.Collection)1 Comparator.comparing (java.util.Comparator.comparing)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)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 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 Service (org.mule.runtime.api.service.Service)1 Preconditions.checkArgument (org.mule.runtime.api.util.Preconditions.checkArgument)1 ClassUtils.withContextClassLoader (org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader)1 ServiceResolutionError (org.mule.runtime.module.service.api.discoverer.ServiceResolutionError)1