Search in sources :

Example 1 with Service

use of org.mule.runtime.api.service.Service 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 2 with Service

use of org.mule.runtime.api.service.Service 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 3 with Service

use of org.mule.runtime.api.service.Service in project mule by mulesoft.

the class MuleServiceManagerTestCase method stopsServicesAfterStartFail.

@Test
public void stopsServicesAfterStartFail() throws Exception {
    StartableStoppableService firstService = mock(StartableStoppableService.class);
    StartableStoppableService secondService = mock(StartableStoppableService.class);
    Exception firstStartFailure = new RuntimeException();
    doThrow(firstStartFailure).when(firstService).start();
    final ServiceDiscoverer serviceDiscoverer = mock(ServiceDiscoverer.class);
    final List<Pair<ArtifactClassLoader, Service>> services = new ArrayList<>();
    Pair<ArtifactClassLoader, Service> service1Pairs = new Pair(mock(ArtifactClassLoader.class), firstService);
    Pair<ArtifactClassLoader, Service> service2Pairs = new Pair(mock(ArtifactClassLoader.class), secondService);
    services.add(service1Pairs);
    services.add(service2Pairs);
    when(serviceDiscoverer.discoverServices()).thenReturn(services);
    final MuleServiceManager muleServiceManager = new MuleServiceManager(serviceDiscoverer);
    try {
        muleServiceManager.start();
        fail();
    } catch (Exception e) {
        assertThat(e.getCause(), sameInstance(firstStartFailure));
    }
    muleServiceManager.stop();
    verify(secondService, never()).stop();
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ArrayList(java.util.ArrayList) Service(org.mule.runtime.api.service.Service) ServiceDiscoverer(org.mule.runtime.module.service.api.discoverer.ServiceDiscoverer) Pair(org.mule.runtime.api.util.Pair) Test(org.junit.Test)

Example 4 with Service

use of org.mule.runtime.api.service.Service in project mule by mulesoft.

the class MuleServiceManagerTestCase method startServices.

@Test
public void startServices() throws Exception {
    final ServiceDiscoverer serviceDiscoverer = mock(ServiceDiscoverer.class);
    final List<Pair<ArtifactClassLoader, Service>> services = new ArrayList<>();
    Pair<ArtifactClassLoader, Service> service1 = new Pair(mock(ArtifactClassLoader.class), mock(StartableService.class));
    Pair<ArtifactClassLoader, Service> service2 = new Pair(mock(ArtifactClassLoader.class), mock(StartableService.class));
    services.add(service1);
    services.add(service2);
    when(serviceDiscoverer.discoverServices()).thenReturn(services);
    final MuleServiceManager muleServiceManager = new MuleServiceManager(serviceDiscoverer);
    muleServiceManager.start();
    InOrder inOrder = inOrder(service1.getSecond(), service2.getSecond());
    inOrder.verify((StartableService) service1.getSecond()).start();
    inOrder.verify((StartableService) service2.getSecond()).start();
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) InOrder(org.mockito.InOrder) ArrayList(java.util.ArrayList) Service(org.mule.runtime.api.service.Service) ServiceDiscoverer(org.mule.runtime.module.service.api.discoverer.ServiceDiscoverer) Pair(org.mule.runtime.api.util.Pair) Test(org.junit.Test)

Example 5 with Service

use of org.mule.runtime.api.service.Service in project mule by mulesoft.

the class BasicRuntimeServicesConfigurationBuilder method doConfigure.

@Override
protected void doConfigure(MuleContext muleContext) throws Exception {
    new SpiServiceRegistry().lookupProviders(Service.class, BasicRuntimeServicesConfigurationBuilder.class.getClassLoader()).forEach(service -> {
        try {
            startIfNeeded(service);
            registerObject(muleContext, service.getName(), service);
        } catch (MuleException e) {
            throw new MuleRuntimeException(e);
        }
    });
    DefaultExpressionLanguageFactoryService weaveExpressionExecutor = new WeaveDefaultExpressionLanguageFactoryService();
    registerObject(muleContext, weaveExpressionExecutor.getName(), weaveExpressionExecutor);
}
Also used : WeaveDefaultExpressionLanguageFactoryService(org.mule.weave.v2.el.WeaveDefaultExpressionLanguageFactoryService) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) DefaultExpressionLanguageFactoryService(org.mule.runtime.api.el.DefaultExpressionLanguageFactoryService) WeaveDefaultExpressionLanguageFactoryService(org.mule.weave.v2.el.WeaveDefaultExpressionLanguageFactoryService) Service(org.mule.runtime.api.service.Service) DefaultExpressionLanguageFactoryService(org.mule.runtime.api.el.DefaultExpressionLanguageFactoryService) WeaveDefaultExpressionLanguageFactoryService(org.mule.weave.v2.el.WeaveDefaultExpressionLanguageFactoryService) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) MuleException(org.mule.runtime.api.exception.MuleException)

Aggregations

Service (org.mule.runtime.api.service.Service)12 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)8 ArrayList (java.util.ArrayList)7 Pair (org.mule.runtime.api.util.Pair)6 Test (org.junit.Test)5 ServiceDiscoverer (org.mule.runtime.module.service.api.discoverer.ServiceDiscoverer)5 InOrder (org.mockito.InOrder)3 InvocationHandler (java.lang.reflect.InvocationHandler)2 MetadataInvocationHandler (org.mule.runtime.container.internal.MetadataInvocationHandler)2 Collection (java.util.Collection)1 Comparator.comparing (java.util.Comparator.comparing)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 DefaultExpressionLanguageFactoryService (org.mule.runtime.api.el.DefaultExpressionLanguageFactoryService)1 MuleException (org.mule.runtime.api.exception.MuleException)1