Search in sources :

Example 1 with ExtensionModelLoader

use of org.mule.runtime.extension.api.loader.ExtensionModelLoader 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 2 with ExtensionModelLoader

use of org.mule.runtime.extension.api.loader.ExtensionModelLoader in project mule by mulesoft.

the class MuleExtensionModelLoaderManagerTestCase method noExtensionModelLoaderRegistered.

@Test
public void noExtensionModelLoaderRegistered() throws MuleException {
    URLClassLoader emptyClassLoader = new URLClassLoader(new URL[0], getLauncherClassLoader(this.getClass().getClassLoader()));
    when(containerClassLoader.getClassLoader()).thenReturn(emptyClassLoader);
    MuleExtensionModelLoaderManager manager = new MuleExtensionModelLoaderManager(containerClassLoader);
    manager.start();
    Optional<ExtensionModelLoader> loader = manager.getExtensionModelLoader(loaderDescriber);
    assertThat(loader.isPresent(), is(false));
}
Also used : URLClassLoader(java.net.URLClassLoader) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with ExtensionModelLoader

use of org.mule.runtime.extension.api.loader.ExtensionModelLoader 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 ExtensionModelLoader

use of org.mule.runtime.extension.api.loader.ExtensionModelLoader in project mule by mulesoft.

the class MuleExtensionModelLoaderManagerTestCase method getExtensionModelLoaderRegistered.

@Test
public void getExtensionModelLoaderRegistered() throws Exception {
    File serviceFolder = new File(temporaryFolder.newFolder("META-INF"), "services");
    assertThat(serviceFolder.mkdirs(), is(true));
    File serviceFile = new File(serviceFolder, ExtensionModelLoader.class.getCanonicalName());
    try (BufferedWriter writer = newBufferedWriter(Paths.get(serviceFile.toURI()))) {
        writer.write(TestExtensionModelLoader.class.getName());
        writer.newLine();
    }
    when(containerClassLoader.getClassLoader()).thenReturn(new URLClassLoader(new URL[] { temporaryFolder.getRoot().toURI().toURL() }, this.getClass().getClassLoader()));
    MuleExtensionModelLoaderManager manager = new MuleExtensionModelLoaderManager(containerClassLoader);
    manager.start();
    Optional<ExtensionModelLoader> loader = manager.getExtensionModelLoader(loaderDescriber);
    assertThat(loader.isPresent(), is(true));
    assertThat(loader.get(), instanceOf(TestExtensionModelLoader.class));
}
Also used : URLClassLoader(java.net.URLClassLoader) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) File(java.io.File) URL(java.net.URL) Files.newBufferedWriter(java.nio.file.Files.newBufferedWriter) BufferedWriter(java.io.BufferedWriter) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 5 with ExtensionModelLoader

use of org.mule.runtime.extension.api.loader.ExtensionModelLoader in project mule by mulesoft.

the class DefaultExtensionSchemaGeneratorTestCase method data.

@Parameterized.Parameters(name = "{1}")
public static Collection<Object[]> data() {
    final ClassLoader classLoader = DefaultExtensionSchemaGeneratorTestCase.class.getClassLoader();
    final ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
    when(serviceRegistry.lookupProviders(DeclarationEnricher.class, classLoader)).thenReturn(asList(new JavaXmlDeclarationEnricher()));
    final List<SchemaGeneratorTestUnit> extensions;
    extensions = asList(newTestUnit(javaLoader, MapConnector.class, "map.xsd"), newTestUnit(javaLoader, ListConnector.class, "list.xsd"), newTestUnit(javaLoader, TestConnector.class, "basic.xsd"), newTestUnit(javaLoader, StringListConnector.class, "string-list.xsd"), newTestUnit(javaLoader, GlobalPojoConnector.class, "global-pojo.xsd"), newTestUnit(javaLoader, GlobalInnerPojoConnector.class, "global-inner-pojo.xsd"), newTestUnit(javaLoader, VeganExtension.class, "vegan.xsd"), newTestUnit(javaLoader, PetStoreConnector.class, "petstore.xsd"), newTestUnit(javaLoader, MetadataExtension.class, "metadata.xsd"), newTestUnit(javaLoader, HeisenbergExtension.class, "heisenberg.xsd"), newTestUnit(javaLoader, SubstitutionGroupExtension.class, "substitutiongroup.xsd"), newTestUnit(javaLoader, TransactionalExtension.class, "tx-ext.xsd"), newTestUnit(javaLoader, SubTypesMappingConnector.class, "subtypes.xsd"), newTestUnit(javaLoader, MarvelExtension.class, "marvel.xsd"), newTestUnit(soapLoader, FootballSoapExtension.class, "soap.xsd"), newTestUnit(soapLoader, RickAndMortyExtension.class, "ram.xsd"), newTestUnit(javaLoader, TypedValueExtension.class, "typed-value.xsd"), newTestUnit(javaLoader, TestOAuthExtension.class, "test-oauth.xsd"), newTestUnit(javaLoader, WeaveFunctionExtension.class, "test-fn.xsd"), newTestUnit(javaLoader, ValuesExtension.class, "values.xsd"), newTestUnit(javaLoader, ReconnectionExtension.class, "reconnection-extension.xsd"));
    BiFunction<Class<?>, ExtensionModelLoader, ExtensionModel> createExtensionModel = (extension, loader) -> {
        ExtensionModel model = loadExtension(extension, loader);
        if (extensionModels.put(model.getName(), model) != null) {
            throw new IllegalArgumentException(format("Extension names must be unique. Name [%s] for extension [%s] was already used", model.getName(), extension.getName()));
        }
        return model;
    };
    return extensions.stream().map(e -> new Object[] { createExtensionModel.apply(e.getExtensionClass(), e.getLoader()), e.getFileName() }).collect(toList());
}
Also used : IOUtils.getResourceAsUrl(org.mule.runtime.core.api.util.IOUtils.getResourceAsUrl) TestOAuthExtension(org.mule.test.oauth.TestOAuthExtension) FootballSoapExtension(org.mule.test.soap.extension.FootballSoapExtension) BiFunction(java.util.function.BiFunction) MapConnector(org.mule.runtime.module.extension.internal.runtime.connectivity.basic.MapConnector) MetadataExtension(org.mule.test.metadata.extension.MetadataExtension) SmallTest(org.mule.tck.size.SmallTest) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) DslResolvingContext(org.mule.runtime.api.dsl.DslResolvingContext) ServiceRegistry(org.mule.runtime.core.api.registry.ServiceRegistry) DeclarationEnricher(org.mule.runtime.extension.api.loader.DeclarationEnricher) Parameterized(org.junit.runners.Parameterized) DefaultJavaExtensionModelLoader(org.mule.runtime.module.extension.api.loader.java.DefaultJavaExtensionModelLoader) Collection(java.util.Collection) Set(java.util.Set) String.format(java.lang.String.format) List(java.util.List) ExtensionSchemaGenerator(org.mule.runtime.extension.api.dsl.syntax.resources.spi.ExtensionSchemaGenerator) ListConnector(org.mule.runtime.module.extension.internal.runtime.connectivity.basic.ListConnector) Optional(java.util.Optional) IOUtils.getResourceAsString(org.mule.runtime.core.api.util.IOUtils.getResourceAsString) DslResolvingContext.getDefault(org.mule.runtime.api.dsl.DslResolvingContext.getDefault) FileUtils.stringToFile(org.mule.runtime.core.api.util.FileUtils.stringToFile) MarvelExtension(org.mule.test.marvel.MarvelExtension) Mockito.mock(org.mockito.Mockito.mock) MuleManifest.getProductVersion(org.mule.runtime.core.api.config.MuleManifest.getProductVersion) RickAndMortyExtension(org.mule.test.ram.RickAndMortyExtension) StringListConnector(org.mule.runtime.module.extension.internal.runtime.connectivity.basic.StringListConnector) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) TypeCatalog(org.mule.runtime.api.meta.type.TypeCatalog) WeaveFunctionExtension(org.mule.test.function.extension.WeaveFunctionExtension) SoapExtensionModelLoader(org.mule.runtime.module.extension.soap.api.loader.SoapExtensionModelLoader) GlobalInnerPojoConnector(org.mule.runtime.module.extension.internal.runtime.connectivity.basic.GlobalInnerPojoConnector) AbstractMuleTestCase(org.mule.tck.junit4.AbstractMuleTestCase) GlobalPojoConnector(org.mule.runtime.module.extension.internal.runtime.connectivity.basic.GlobalPojoConnector) TestConnector(org.mule.runtime.module.extension.internal.runtime.connectivity.basic.TestConnector) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) HeisenbergExtension(org.mule.test.heisenberg.extension.HeisenbergExtension) LinkedHashSet(java.util.LinkedHashSet) TransactionalExtension(org.mule.test.transactional.TransactionalExtension) TypedValueExtension(org.mule.test.typed.value.extension.extension.TypedValueExtension) Before(org.junit.Before) DefaultExtensionSchemaGenerator(org.mule.runtime.module.extension.internal.capability.xml.schema.DefaultExtensionSchemaGenerator) ValuesExtension(org.mule.test.values.extension.ValuesExtension) ImmutableSet.copyOf(com.google.common.collect.ImmutableSet.copyOf) Optional.ofNullable(java.util.Optional.ofNullable) PetStoreConnector(org.mule.test.petstore.extension.PetStoreConnector) SubstitutionGroupExtension(org.mule.test.substitutiongroup.extension.SubstitutionGroupExtension) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) VERSION(org.mule.runtime.module.extension.api.loader.AbstractJavaExtensionModelLoader.VERSION) VeganExtension(org.mule.test.vegan.extension.VeganExtension) File(java.io.File) ExtensionsTestUtils.compareXML(org.mule.test.module.extension.internal.util.ExtensionsTestUtils.compareXML) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) Collectors.toList(java.util.stream.Collectors.toList) ReconnectionExtension(org.mule.extension.test.extension.reconnection.ReconnectionExtension) SchemaGeneratorTestUnit.newTestUnit(org.mule.runtime.module.extension.internal.capability.xml.DefaultExtensionSchemaGeneratorTestCase.SchemaGeneratorTestUnit.newTestUnit) TYPE_PROPERTY_NAME(org.mule.runtime.module.extension.api.loader.AbstractJavaExtensionModelLoader.TYPE_PROPERTY_NAME) SubTypesMappingConnector(org.mule.test.subtypes.extension.SubTypesMappingConnector) JavaXmlDeclarationEnricher(org.mule.runtime.module.extension.internal.loader.enricher.JavaXmlDeclarationEnricher) JavaXmlDeclarationEnricher(org.mule.runtime.module.extension.internal.loader.enricher.JavaXmlDeclarationEnricher) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) DefaultJavaExtensionModelLoader(org.mule.runtime.module.extension.api.loader.java.DefaultJavaExtensionModelLoader) SoapExtensionModelLoader(org.mule.runtime.module.extension.soap.api.loader.SoapExtensionModelLoader) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) ServiceRegistry(org.mule.runtime.core.api.registry.ServiceRegistry)

Aggregations

ExtensionModelLoader (org.mule.runtime.extension.api.loader.ExtensionModelLoader)7 Test (org.junit.Test)5 SmallTest (org.mule.tck.size.SmallTest)5 Map (java.util.Map)4 File (java.io.File)3 String.format (java.lang.String.format)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Before (org.junit.Before)3 RunWith (org.junit.runner.RunWith)3 DslResolvingContext (org.mule.runtime.api.dsl.DslResolvingContext)3 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)3 ServiceRegistry (org.mule.runtime.core.api.registry.ServiceRegistry)3 IOException (java.io.IOException)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 Optional (java.util.Optional)2 BiFunction (java.util.function.BiFunction)2 Collectors.toList (java.util.stream.Collectors.toList)2 Parameterized (org.junit.runners.Parameterized)2