Search in sources :

Example 1 with ServiceResolutionError

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;
}
Also used : ServiceDescriptor(org.mule.runtime.module.service.internal.artifact.ServiceDescriptor) ServiceResolutionError(org.mule.runtime.module.service.api.discoverer.ServiceResolutionError) SuffixFileFilter(org.apache.commons.io.filefilter.SuffixFileFilter) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 2 with ServiceResolutionError

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);
        }
    }
}
Also used : Inject(javax.inject.Inject) Field(java.lang.reflect.Field) ServiceResolutionError(org.mule.runtime.module.service.api.discoverer.ServiceResolutionError)

Example 3 with ServiceResolutionError

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;
}
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 4 with ServiceResolutionError

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

Example 5 with ServiceResolutionError

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();
}
Also used : ServiceResolutionError(org.mule.runtime.module.service.api.discoverer.ServiceResolutionError) Test(org.junit.Test)

Aggregations

ServiceResolutionError (org.mule.runtime.module.service.api.discoverer.ServiceResolutionError)5 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 Pair (org.mule.runtime.api.util.Pair)2 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 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 Inject (javax.inject.Inject)1 SuffixFileFilter (org.apache.commons.io.filefilter.SuffixFileFilter)1 Service (org.mule.runtime.api.service.Service)1 ServiceDefinition (org.mule.runtime.api.service.ServiceDefinition)1