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