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;
}
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;
}
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)));
}
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;
}
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);
}
Aggregations