Search in sources :

Example 16 with ExtensionDeclarer

use of org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer in project mule by mulesoft.

the class XmlExtensionLoaderDelegate method createTnsExtensionModel.

/**
 * Transforms the current <module/> by stripping out the <body/>'s content, so that there are not parsing errors, to generate
 * a simpler {@link ExtensionModel} if there are references to the TNS prefix defined by the {@link #XMLNS_TNS}.
 *
 * @param resource <module/>'s resource
 * @param extensions complete list of extensions the current module depends on
 * @return an {@link ExtensionModel} if there's a {@link #XMLNS_TNS} defined, {@link Optional#empty()} otherwise
 * @throws IOException if it fails reading the resource
 */
private Optional<ExtensionModel> createTnsExtensionModel(URL resource, Set<ExtensionModel> extensions) throws IOException {
    ExtensionModel result = null;
    final ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
    try {
        final Source xslt = new StreamSource(getClass().getClassLoader().getResourceAsStream(TRANSFORMATION_FOR_TNS_RESOURCE));
        final Source moduleToTransform = new StreamSource(resource.openStream());
        TransformerFactory.newInstance().newTransformer(xslt).transform(moduleToTransform, new StreamResult(resultStream));
    } catch (TransformerException e) {
        throw new MuleRuntimeException(createStaticMessage(format("There was an issue transforming the stream for the resource %s while trying to remove the content of the <body> element to generate an XSD", resource.getFile())), e);
    }
    final Document transformedModuleDocument = schemaValidatingDocumentLoader(NoOpXmlErrorHandler::new).loadDocument(extensions, resource.getFile(), new ByteArrayInputStream(resultStream.toByteArray()));
    if (StringUtils.isNotBlank(transformedModuleDocument.getDocumentElement().getAttribute(XMLNS_TNS))) {
        final ExtensionDeclarer extensionDeclarer = new ExtensionDeclarer();
        loadModuleExtension(extensionDeclarer, resource, transformedModuleDocument, extensions, true);
        result = createExtensionModel(extensionDeclarer);
    }
    return Optional.ofNullable(result);
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) StreamSource(javax.xml.transform.stream.StreamSource) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Document(org.w3c.dom.Document) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException)

Example 17 with ExtensionDeclarer

use of org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer in project mule by mulesoft.

the class TestConnectionFailuresTestCase method mockedExtension.

private ExtensionModel mockedExtension(final String name, final String config, final String connectionProvider) {
    final ExtensionDeclarer extensionDeclarer = new ExtensionDeclarer();
    extensionDeclarer.named(name).onVersion("4.0.0").fromVendor("MuleSoft testcase").withCategory(Category.COMMUNITY).withConfig(config).withConnectionProvider(connectionProvider).withConnectionManagementType(ConnectionManagementType.NONE);
    return new ExtensionModelFactory().create(new DefaultExtensionLoadingContext(extensionDeclarer, currentThread().getContextClassLoader(), new NullDslResolvingContext()));
}
Also used : DefaultExtensionLoadingContext(org.mule.runtime.extension.internal.loader.DefaultExtensionLoadingContext) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) NullDslResolvingContext(org.mule.runtime.internal.dsl.NullDslResolvingContext) ExtensionModelFactory(org.mule.runtime.extension.internal.loader.ExtensionModelFactory)

Example 18 with ExtensionDeclarer

use of org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer in project mule by mulesoft.

the class BooleanParameterDeclarationEnricherTestCase method setUp.

@Before
public void setUp() {
    ExtensionDeclarer declarer = new DefaultJavaModelLoaderDelegate(HeisenbergExtension.class, getProductVersion()).declare(new DefaultExtensionLoadingContext(getClass().getClassLoader(), getDefault(emptySet())));
    new BooleanParameterDeclarationEnricher().enrich(new DefaultExtensionLoadingContext(declarer, this.getClass().getClassLoader(), getDefault(emptySet())));
    declaration = declarer.getDeclaration();
}
Also used : DefaultExtensionLoadingContext(org.mule.runtime.extension.internal.loader.DefaultExtensionLoadingContext) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) DefaultJavaModelLoaderDelegate(org.mule.runtime.module.extension.internal.loader.java.DefaultJavaModelLoaderDelegate) HeisenbergExtension(org.mule.test.heisenberg.extension.HeisenbergExtension) Before(org.junit.Before)

Example 19 with ExtensionDeclarer

use of org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer in project mule by mulesoft.

the class SubTypesDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    ExtensionDeclarer declarer = extensionLoadingContext.getExtensionDeclarer();
    ExtensionDeclaration extensionDeclaration = declarer.getDeclaration();
    Optional<ExtensionTypeDescriptorModelProperty> implementingType = extensionDeclaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
    if (!implementingType.isPresent()) {
        return;
    }
    Type type = implementingType.get().getType();
    List<AnnotationValueFetcher<SubTypeMapping>> typeMappings = parseRepeatableAnnotation(type, SubTypeMapping.class, c -> ((SubTypesMapping) c).value());
    if (!typeMappings.isEmpty()) {
        declareSubTypesMapping(declarer, typeMappings, extensionDeclaration.getName());
    }
}
Also used : Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) AnnotationValueFetcher(org.mule.runtime.module.extension.api.loader.java.type.AnnotationValueFetcher)

Example 20 with ExtensionDeclarer

use of org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer in project mule by mulesoft.

the class XmlExtensionLoaderDelegate method loadModuleExtension.

private void loadModuleExtension(ExtensionDeclarer declarer, URL resource, Document moduleDocument, Set<ExtensionModel> extensions, boolean comesFromTNS) {
    final ComponentModel moduleModel = getModuleComponentModel(resource, moduleDocument);
    if (!moduleModel.getIdentifier().equals(MODULE_IDENTIFIER)) {
        throw new MuleRuntimeException(createStaticMessage(format("The root element of a module must be '%s', but found '%s'", MODULE_IDENTIFIER.toString(), moduleModel.getIdentifier().toString())));
    }
    final String name = moduleModel.getParameters().get(MODULE_NAME);
    // TODO(fernandezlautaro): MULE-11010 remove version from ExtensionModel
    final String version = "4.0.0";
    final String category = moduleModel.getParameters().get(CATEGORY);
    final String vendor = moduleModel.getParameters().get(VENDOR);
    final XmlDslModel xmlDslModel = getXmlDslModel(moduleModel, name, version);
    final String description = getDescription(moduleModel);
    final String xmlnsTnsValue = moduleModel.getParameters().get(XMLNS_TNS);
    if (xmlnsTnsValue != null && !xmlDslModel.getNamespace().equals(xmlnsTnsValue)) {
        throw new MuleRuntimeException(createStaticMessage(format("The %s attribute value of the module must be '%s', but found '%s'", XMLNS_TNS, xmlDslModel.getNamespace(), xmlnsTnsValue)));
    }
    fillDeclarer(declarer, name, version, category, vendor, xmlDslModel, description);
    declarer.withModelProperty(getXmlExtensionModelProperty(moduleModel, xmlDslModel));
    DirectedGraph<String, DefaultEdge> directedGraph = new DefaultDirectedGraph<>(DefaultEdge.class);
    // loading public operations
    final Optional<ConfigurationDeclarer> configurationDeclarer = loadPropertiesFrom(declarer, moduleModel, extensions);
    final HasOperationDeclarer hasOperationDeclarer = configurationDeclarer.isPresent() ? configurationDeclarer.get() : declarer;
    loadOperationsFrom(hasOperationDeclarer, moduleModel, directedGraph, xmlDslModel, OperationVisibility.PUBLIC);
    // loading private operations
    if (comesFromTNS) {
        // when parsing for the TNS, we need the <operation/>s to be part of the extension model to validate the XML properly
        loadOperationsFrom(hasOperationDeclarer, moduleModel, directedGraph, xmlDslModel, OperationVisibility.PRIVATE);
    } else {
        // when parsing for the macro expansion, the <operation/>s will be left in the PrivateOperationsModelProperty model property
        final ExtensionDeclarer temporalDeclarer = new ExtensionDeclarer();
        fillDeclarer(temporalDeclarer, name, version, category, vendor, xmlDslModel, description);
        loadOperationsFrom(temporalDeclarer, moduleModel, directedGraph, xmlDslModel, OperationVisibility.PRIVATE);
        final ExtensionModel result = createExtensionModel(temporalDeclarer);
        declarer.withModelProperty(new PrivateOperationsModelProperty(result.getOperationModels()));
    }
    final CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<>(directedGraph);
    final Set<String> cycles = cycleDetector.findCycles();
    if (!cycles.isEmpty()) {
        throw new MuleRuntimeException(createStaticMessage(format(CYCLIC_OPERATIONS_ERROR, new TreeSet(cycles))));
    }
}
Also used : DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) ConfigurationDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclarer) HasOperationDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasOperationDeclarer) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) DefaultEdge(org.jgrapht.graph.DefaultEdge) CycleDetector(org.jgrapht.alg.CycleDetector) PrivateOperationsModelProperty(org.mule.runtime.config.internal.dsl.model.extension.xml.property.PrivateOperationsModelProperty) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) TreeSet(java.util.TreeSet) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) XmlDslModel(org.mule.runtime.api.meta.model.XmlDslModel)

Aggregations

ExtensionDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer)35 ExtensionDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration)15 Test (org.junit.Test)14 SmallTest (org.mule.tck.size.SmallTest)14 DefaultExtensionLoadingContext (org.mule.runtime.extension.internal.loader.DefaultExtensionLoadingContext)8 MetadataType (org.mule.metadata.api.model.MetadataType)6 ExtensionTypeDescriptorModelProperty (org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty)6 Before (org.junit.Before)5 OperationDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration)5 HeisenbergExtension (org.mule.test.heisenberg.extension.HeisenbergExtension)5 Declarer (org.mule.runtime.api.meta.model.declaration.fluent.Declarer)4 ExtensionParameter (org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter)4 Type (org.mule.runtime.module.extension.api.loader.java.type.Type)4 ObjectType (org.mule.metadata.api.model.ObjectType)3 ConfigurationDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclaration)3 HasOperationDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.HasOperationDeclarer)3 IllegalModelDefinitionException (org.mule.runtime.extension.api.exception.IllegalModelDefinitionException)3 ExtensionLoadingContext (org.mule.runtime.extension.api.loader.ExtensionLoadingContext)3 DefaultJavaModelLoaderDelegate (org.mule.runtime.module.extension.internal.loader.java.DefaultJavaModelLoaderDelegate)3 ImplementingMethodModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.ImplementingMethodModelProperty)3