Search in sources :

Example 1 with SpiServiceRegistry

use of org.mule.runtime.core.api.registry.SpiServiceRegistry in project mule by mulesoft.

the class ExtensionModelDiscoverer method discoverRuntimeExtensionModels.

/**
 * Discover the extension models provided by the runtime.
 *
 * @return {@link Set} of the runtime provided {@link ExtensionModel}s.
 */
public Set<ExtensionModel> discoverRuntimeExtensionModels() {
    final Set<ExtensionModel> extensionModels = new HashSet<>();
    Collection<RuntimeExtensionModelProvider> runtimeExtensionModelProviders = new SpiServiceRegistry().lookupProviders(RuntimeExtensionModelProvider.class, Thread.currentThread().getContextClassLoader());
    for (RuntimeExtensionModelProvider runtimeExtensionModelProvider : runtimeExtensionModelProviders) {
        extensionModels.add(runtimeExtensionModelProvider.createExtensionModel());
    }
    return extensionModels;
}
Also used : RuntimeExtensionModelProvider(org.mule.runtime.core.api.extension.RuntimeExtensionModelProvider) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) HashSet(java.util.HashSet)

Example 2 with SpiServiceRegistry

use of org.mule.runtime.core.api.registry.SpiServiceRegistry in project mule by mulesoft.

the class MuleExtensionModelLoaderManager method start.

/**
 * Will look through SPI every class that implements the {@code providerClass} and if there are repeated IDs, it will
 * collect them all to throw an exception with the detailed message.
 * <p/>
 * The exception, if thrown, will have the following message:
 * <pre>
 *   There are several loaders that return the same ID when looking up providers for 'org.mule.runtime.module.artifact.ExtensionModelLoader'. Full error list:
 *   ID [some-id] is being returned by the following classes [org.foo.FooLoader, org.bar.BarLoader]
 *   ID [another-id] is being returned by the following classes [org.foo2.FooLoader2, org.bar2.BarLoader2]
 * </pre>
 *
 * @throws IllegalStateException if there are loaders with repeated IDs.
 */
@Override
public void start() throws MuleException {
    final Class<ExtensionModelLoader> providerClass = ExtensionModelLoader.class;
    final SpiServiceRegistry spiServiceRegistry = new SpiServiceRegistry();
    final ClassLoader classLoader = containerClassLoader.getClassLoader();
    final Collection<ExtensionModelLoader> extensionModelLoaders = spiServiceRegistry.lookupProviders(providerClass, classLoader);
    final StringBuilder sb = new StringBuilder();
    extensionModelLoaders.stream().collect(groupingBy(ExtensionModelLoader::getId)).entrySet().stream().filter(entry -> entry.getValue().size() > 1).forEach(entry -> {
        // At this point we are sure there are at least 2 classes that return the same ID, we will append it to the builder
        final String classes = entry.getValue().stream().map(extensionModelLoader -> extensionModelLoader.getClass().getName()).collect(Collectors.joining(", "));
        sb.append(lineSeparator()).append("ID [").append(entry.getKey()).append("] is being returned by the following classes [").append(classes).append("]");
    });
    if (isNotBlank(sb.toString())) {
        throw new MuleRuntimeException(createStaticMessage(format("There are several loaders that return the same identifier when looking up providers for '%s'. Full error list: %s", providerClass.getName(), sb.toString())));
    }
    extensionModelLoaders.stream().forEach(extensionModelLoader -> this.extensionModelLoaders.put(extensionModelLoader.getId(), extensionModelLoader));
    if (logger.isDebugEnabled()) {
        logger.debug("ExtensionModelLoader registered identifiers: {}", printExtensionModelLoaderIDs());
    }
}
Also used : Optional.empty(java.util.Optional.empty) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) System.lineSeparator(java.lang.System.lineSeparator) Logger(org.slf4j.Logger) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Optional.of(java.util.Optional.of) Collection(java.util.Collection) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) LoggerFactory(org.slf4j.LoggerFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ExtensionModelLoaderManager(org.mule.runtime.module.extension.internal.loader.ExtensionModelLoaderManager) Preconditions.checkNotNull(org.mule.runtime.api.util.Preconditions.checkNotNull) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) LoaderDescriber(org.mule.runtime.deployment.model.api.plugin.LoaderDescriber) MuleException(org.mule.runtime.api.exception.MuleException) Map(java.util.Map) Optional(java.util.Optional) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry)

Example 3 with SpiServiceRegistry

use of org.mule.runtime.core.api.registry.SpiServiceRegistry in project mule by mulesoft.

the class CraftedExtensionModelLoaderTestCase method spiDiscoverable.

@Test
public void spiDiscoverable() throws Exception {
    ServiceRegistry registry = new SpiServiceRegistry();
    Optional<ExtensionModelLoader> craftedLoader = registry.lookupProviders(ExtensionModelLoader.class, classLoader).stream().filter(p -> p instanceof CraftedExtensionModelLoader).findAny();
    assertThat(craftedLoader.isPresent(), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) Assert.assertThat(org.junit.Assert.assertThat) AbstractMuleTestCase(org.mule.tck.junit4.AbstractMuleTestCase) SmallTest(org.mule.tck.size.SmallTest) Map(java.util.Map) ExpectedException.none(org.junit.rules.ExpectedException.none) DslResolvingContext(org.mule.runtime.api.dsl.DslResolvingContext) ExtensionLoadingDelegate(org.mule.runtime.extension.api.loader.ExtensionLoadingDelegate) ServiceRegistry(org.mule.runtime.core.api.registry.ServiceRegistry) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) COMMUNITY(org.mule.runtime.api.meta.Category.COMMUNITY) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) Test(org.junit.Test) TYPE_PROPERTY_NAME(org.mule.runtime.module.extension.api.loader.java.CraftedExtensionModelLoader.TYPE_PROPERTY_NAME) CraftedExtensionModelLoader(org.mule.runtime.module.extension.api.loader.java.CraftedExtensionModelLoader) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) MuleVersion(org.mule.runtime.api.meta.MuleVersion) Rule(org.junit.Rule) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Optional(java.util.Optional) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) CraftedExtensionModelLoader(org.mule.runtime.module.extension.api.loader.java.CraftedExtensionModelLoader) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) CraftedExtensionModelLoader(org.mule.runtime.module.extension.api.loader.java.CraftedExtensionModelLoader) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) ServiceRegistry(org.mule.runtime.core.api.registry.ServiceRegistry) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 4 with SpiServiceRegistry

use of org.mule.runtime.core.api.registry.SpiServiceRegistry in project mule by mulesoft.

the class DefaultXmlArtifactDeclarationLoader method loadArtifactConfig.

private ConfigLine loadArtifactConfig(String name, InputStream resource) {
    checkArgument(resource != null, "The given application was not found as resource");
    Document document = noValidationDocumentLoader().loadDocument(context.getExtensions(), name, resource);
    return new XmlApplicationParser(new XmlApplicationServiceRegistry(new SpiServiceRegistry(), context), resolveContextArtifactPluginClassLoaders()).parse(document.getDocumentElement()).orElseThrow(() -> new MuleRuntimeException(createStaticMessage("Could not load load a Configuration from the given resource")));
}
Also used : XmlApplicationParser(org.mule.runtime.config.api.dsl.processor.xml.XmlApplicationParser) XmlApplicationServiceRegistry(org.mule.runtime.config.api.dsl.processor.xml.XmlApplicationServiceRegistry) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Document(org.w3c.dom.Document) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry)

Example 5 with SpiServiceRegistry

use of org.mule.runtime.core.api.registry.SpiServiceRegistry 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

SpiServiceRegistry (org.mule.runtime.core.api.registry.SpiServiceRegistry)8 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)3 ExtensionModelLoader (org.mule.runtime.extension.api.loader.ExtensionModelLoader)3 Map (java.util.Map)2 Optional (java.util.Optional)2 Test (org.junit.Test)2 MuleException (org.mule.runtime.api.exception.MuleException)2 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)2 XmlApplicationParser (org.mule.runtime.config.api.dsl.processor.xml.XmlApplicationParser)2 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 String.format (java.lang.String.format)1 System.lineSeparator (java.lang.System.lineSeparator)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Optional.empty (java.util.Optional.empty)1 Optional.of (java.util.Optional.of)1