Search in sources :

Example 21 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader 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 22 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class MuleServiceManager method wrapServices.

private List<Service> wrapServices(List<Pair<ArtifactClassLoader, Service>> registeredServices) {
    final List<Service> result = new ArrayList<>(registeredServices.size());
    for (Pair<ArtifactClassLoader, Service> pair : registeredServices) {
        final Service serviceProxy = createLifecycleFilterServiceProxy(pair.getSecond());
        result.add(serviceProxy);
    }
    return unmodifiableList(result);
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ArrayList(java.util.ArrayList) Service(org.mule.runtime.api.service.Service)

Example 23 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class ServiceClassLoaderFactoryTestCase method usesClassLoaderLookupPolicy.

@Test
public void usesClassLoaderLookupPolicy() throws Exception {
    final ArtifactClassLoader artifactClassLoader = factory.create(SERVICE_ID, descriptor, getClass().getClassLoader(), lookupPolicy);
    final MuleArtifactClassLoader classLoader = (MuleArtifactClassLoader) artifactClassLoader.getClassLoader();
    final String className = "com.dummy.Foo";
    try {
        classLoader.loadClass(className);
        fail("Able to load an un-existent class");
    } catch (ClassNotFoundException e) {
    // Expected
    }
    verify(lookupPolicy).getClassLookupStrategy(className);
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 24 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class ServiceClassLoaderFactoryTestCase method createsEmptyClassLoader.

@Test
public void createsEmptyClassLoader() throws Exception {
    final ArtifactClassLoader artifactClassLoader = factory.create(SERVICE_ID, descriptor, getClass().getClassLoader(), lookupPolicy);
    final MuleArtifactClassLoader classLoader = (MuleArtifactClassLoader) artifactClassLoader.getClassLoader();
    assertThat(classLoader.getURLs(), equalTo(new URL[0]));
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) URL(java.net.URL) Test(org.junit.Test)

Example 25 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader 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)

Aggregations

ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)58 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)17 URL (java.net.URL)12 ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)12 Pair (org.mule.runtime.api.util.Pair)10 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)10 MuleArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader)10 Service (org.mule.runtime.api.service.Service)8 SmallTest (org.mule.tck.size.SmallTest)8 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)7 Map (java.util.Map)6 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)6 File (java.io.File)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 MuleModule (org.mule.runtime.container.api.MuleModule)5 ContainerOnlyLookupStrategy (org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)5 HashSet (java.util.HashSet)4