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