use of org.mule.runtime.module.service.api.discoverer.ServiceResolutionError in project mule by mulesoft.
the class FileSystemServiceProviderDiscoverer method getServiceDescriptors.
private List<ServiceDescriptor> getServiceDescriptors(ServiceDescriptorFactory serviceDescriptorFactory) throws ServiceResolutionError {
List<ServiceDescriptor> foundServices = new LinkedList<>();
for (String serviceFile : getServicesFolder().list(new SuffixFileFilter(".jar"))) {
final File tempFolder = new File(getServicesTempFolder(), getBaseName(serviceFile));
try {
unzip(new File(getServicesFolder(), serviceFile), tempFolder);
} catch (IOException e) {
throw new ServiceResolutionError("Error processing service JAR file", e);
}
final ServiceDescriptor serviceDescriptor = serviceDescriptorFactory.create(tempFolder, empty());
foundServices.add(serviceDescriptor);
}
return foundServices;
}
use of org.mule.runtime.module.service.api.discoverer.ServiceResolutionError in project mule by mulesoft.
the class ReflectionServiceProviderResolutionHelper method injectInstance.
@Override
public void injectInstance(ServiceProvider serviceProvider, Collection<ServiceDefinition> resolvedServices) throws ServiceResolutionError {
for (Field field : getAllFields(serviceProvider.getClass(), withAnnotation(Inject.class))) {
Class<?> dependencyType = field.getType();
final Object dependency = lookupService(resolvedServices, dependencyType);
if (dependency == null) {
throw new ServiceResolutionError(format("Cannot find a service to inject into field '%s' of service provider '%s'", field.getName(), serviceProvider.getClass().getName()));
}
try {
field.setAccessible(true);
field.set(serviceProvider, dependency);
} catch (Exception e) {
throw new ServiceResolutionError(format("Could not inject dependency on field %s of type %s", field.getName(), dependencyType.getClass().getName()), e);
}
}
}
use of org.mule.runtime.module.service.api.discoverer.ServiceResolutionError 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.module.service.api.discoverer.ServiceResolutionError in project mule by mulesoft.
the class DefaultServiceDiscovererTestCase method propagatesServiceResolutionErrors.
@Test(expected = ServiceResolutionError.class)
public void propagatesServiceResolutionErrors() throws Exception {
final List<Pair<ArtifactClassLoader, ServiceProvider>> serviceProviders = new ArrayList<>();
when(serviceProviderDiscoverer.discover()).thenReturn(serviceProviders);
when(serviceResolver.resolveServices(serviceProviders)).thenThrow(new ServiceResolutionError("Error"));
serviceDiscoverer.discoverServices();
}
use of org.mule.runtime.module.service.api.discoverer.ServiceResolutionError in project mule by mulesoft.
the class DefaultServiceDiscovererTestCase method propagatesDiscoverErrors.
@Test(expected = ServiceResolutionError.class)
public void propagatesDiscoverErrors() throws Exception {
when(serviceProviderDiscoverer.discover()).thenThrow(new ServiceResolutionError("Error"));
serviceDiscoverer.discoverServices();
}
Aggregations