use of org.mule.runtime.api.meta.model.construct.HasConstructModels in project mule by mulesoft.
the class ConfigurationBasedElementModelFactory method createIdentifiedElement.
private DslElementModel createIdentifiedElement(ComponentConfiguration configuration) {
final ComponentIdentifier identifier = configuration.getIdentifier();
Optional<Map.Entry<ExtensionModel, DslSyntaxResolver>> entry = resolvers.entrySet().stream().filter(e -> e.getKey().getXmlDslModel().getPrefix().equals(identifier.getNamespace())).findFirst();
if (!entry.isPresent()) {
return null;
}
currentExtension = entry.get().getKey();
dsl = entry.get().getValue();
Reference<DslElementModel> elementModel = new Reference<>();
new ExtensionWalker() {
@Override
protected void onConfiguration(ConfigurationModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
DslElementModel.Builder<ConfigurationModel> element = createElementModel(model, elementDsl, configuration);
addConnectionProvider(model, dsl, element, configuration);
elementModel.set(element.build());
stop();
}
});
}
@Override
protected void onConstruct(HasConstructModels owner, ConstructModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
elementModel.set(createElementModel(model, elementDsl, configuration).build());
stop();
}
});
}
@Override
protected void onOperation(HasOperationModels owner, OperationModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
elementModel.set(createElementModel(model, elementDsl, configuration).build());
stop();
}
});
}
@Override
protected void onSource(HasSourceModels owner, SourceModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
elementModel.set(createElementModel(model, elementDsl, configuration).build());
stop();
}
});
}
}.walk(currentExtension);
if (elementModel.get() == null) {
resolveBasedOnTypes(configuration).ifPresent(elementModel::set);
}
return elementModel.get();
}
use of org.mule.runtime.api.meta.model.construct.HasConstructModels in project mule by mulesoft.
the class DeclarationBasedElementModelFactory method create.
public <T> Optional<DslElementModel<T>> create(ElementDeclaration declaration) {
setupCurrentExtensionContext(declaration.getDeclaringExtension());
final Function<NamedObject, Boolean> equalsName = (named) -> named.getName().equals(declaration.getName());
if (declaration instanceof TopLevelParameterDeclaration) {
return createFromType((TopLevelParameterDeclaration) declaration);
}
Reference<DslElementModel> elementModel = new Reference<>();
new ExtensionWalker() {
@Override
protected void onConfiguration(ConfigurationModel model) {
if (equalsName.apply(model) && declaration instanceof ConfigurationElementDeclaration) {
elementModel.set(createConfigurationElement(model, (ConfigurationElementDeclaration) declaration));
stop();
}
}
@Override
protected void onOperation(HasOperationModels owner, OperationModel model) {
if (equalsName.apply(model) && declaration instanceof OperationElementDeclaration) {
elementModel.set(createComponentElement(model, (OperationElementDeclaration) declaration));
stop();
}
}
@Override
protected void onConstruct(HasConstructModels owner, ConstructModel model) {
if (equalsName.apply(model) && declaration instanceof ConstructElementDeclaration) {
elementModel.set(createComponentElement(model, (ConstructElementDeclaration) declaration));
stop();
}
}
@Override
protected void onSource(HasSourceModels owner, SourceModel model) {
if (equalsName.apply(model) && declaration instanceof SourceElementDeclaration) {
elementModel.set(createComponentElement(model, (SourceElementDeclaration) declaration));
stop();
}
}
}.walk(currentExtension);
if (LOGGER.isDebugEnabled() && elementModel.get() == null) {
LOGGER.debug(format("No model found with name [%s] of type [%s] for extension [%s]", declaration.getName(), declaration.getClass().getName(), declaration.getDeclaringExtension()));
}
return Optional.ofNullable(elementModel.get());
}
use of org.mule.runtime.api.meta.model.construct.HasConstructModels in project mule by mulesoft.
the class DefaultXmlArtifactDeclarationLoader method getComponentDeclaringWalker.
private ExtensionWalker getComponentDeclaringWalker(final Consumer<ComponentElementDeclaration> declarationConsumer, final ConfigLine line, final ElementDeclarer extensionElementsDeclarer) {
final DslSyntaxResolver dsl = resolvers.get(getNamespace(line));
return new ExtensionWalker() {
@Override
protected void onOperation(HasOperationModels owner, OperationModel model) {
if (!model.getName().equals(TRANSFORM_IDENTIFIER)) {
declareComponentModel(line, model, extensionElementsDeclarer::newOperation).ifPresent(declarer -> {
declarationConsumer.accept((ComponentElementDeclaration) declarer.getDeclaration());
stop();
});
} else {
declareTransform(model);
}
}
@Override
protected void onSource(HasSourceModels owner, SourceModel model) {
declareComponentModel(line, model, extensionElementsDeclarer::newSource).ifPresent(declarer -> {
final DslElementSyntax elementDsl = dsl.resolve(model);
model.getSuccessCallback().ifPresent(cb -> declareParameterizedComponent(cb, elementDsl, declarer, line.getConfigAttributes(), line.getChildren()));
model.getErrorCallback().ifPresent(cb -> declareParameterizedComponent(cb, elementDsl, declarer, line.getConfigAttributes(), line.getChildren()));
declarationConsumer.accept((ComponentElementDeclaration) declarer.getDeclaration());
stop();
});
}
@Override
protected void onConstruct(HasConstructModels owner, ConstructModel model) {
declareComponentModel(line, model, extensionElementsDeclarer::newConstruct).ifPresent(declarer -> {
declarationConsumer.accept((ComponentElementDeclaration) declarer.getDeclaration());
stop();
});
}
private void declareTransform(ComponentModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
if (model.getName().equals(TRANSFORM_IDENTIFIER) && elementDsl.getElementName().equals(line.getIdentifier())) {
ComponentElementDeclarer transform = extensionElementsDeclarer.newOperation(TRANSFORM_IDENTIFIER);
line.getChildren().stream().filter(c -> c.getIdentifier().equals("message")).findFirst().ifPresent(messageConfig -> {
ParameterGroupElementDeclarer messageGroup = newParameterGroup("Message");
messageConfig.getChildren().stream().filter(c -> c.getIdentifier().equals("set-payload")).findFirst().ifPresent(payloadConfig -> {
ParameterObjectValue.Builder payload = newObjectValue();
populateTransformScriptParameter(payloadConfig, payload);
messageGroup.withParameter("setPayload", payload.build());
});
messageConfig.getChildren().stream().filter(c -> c.getIdentifier().equals("set-attributes")).findFirst().ifPresent(attributesConfig -> {
ParameterObjectValue.Builder attributes = newObjectValue();
populateTransformScriptParameter(attributesConfig, attributes);
messageGroup.withParameter("setAttributes", attributes.build());
});
transform.withParameterGroup(messageGroup.getDeclaration());
});
line.getChildren().stream().filter(c -> c.getIdentifier().equals("variables")).findFirst().ifPresent(variablesConfig -> {
ParameterGroupElementDeclarer variablesGroup = newParameterGroup("Set Variables");
ParameterListValue.Builder variables = newListValue();
variablesConfig.getChildren().forEach(variableConfig -> {
ParameterObjectValue.Builder variable = newObjectValue();
variable.withParameter(TRANSFORM_VARIABLE_NAME, variableConfig.getConfigAttributes().get(TRANSFORM_VARIABLE_NAME).getValue());
populateTransformScriptParameter(variableConfig, variable);
variables.withValue(variable.build());
});
transform.withParameterGroup(variablesGroup.withParameter("setVariables", variables.build()).getDeclaration());
});
line.getConfigAttributes().values().forEach(a -> transform.withCustomParameter(a.getName(), a.getValue()));
declarationConsumer.accept((ComponentElementDeclaration) transform.getDeclaration());
stop();
}
}
};
}
use of org.mule.runtime.api.meta.model.construct.HasConstructModels in project mule by mulesoft.
the class ExtensionModelHelper method findComponentModel.
/**
* Finds a {@link org.mule.runtime.api.meta.model.ComponentModel} within the provided set of {@link ExtensionModel}s by a
* {@link ComponentIdentifier}.
*
* @param componentIdentifier the identifier to use for the search.
* @return the found {@link org.mule.runtime.api.meta.model.ComponentModel} or {@link Optional#empty()} if it couldn't be found.
*/
public Optional<? extends org.mule.runtime.api.meta.model.ComponentModel> findComponentModel(ComponentIdentifier componentIdentifier) {
try {
return extensionComponentModelByComponentIdentifier.get(componentIdentifier, () -> {
String componentName = toCamelCase(componentIdentifier.getName(), COMPONENT_NAME_SEPARATOR);
for (ExtensionModel extensionModel : extensionsModels) {
if (extensionModel.getXmlDslModel().getPrefix().equals(componentIdentifier.getNamespace())) {
List<HasOperationModels> operationModelsProviders = ImmutableList.<HasOperationModels>builder().add(extensionModel).addAll(extensionModel.getConfigurationModels()).build();
List<HasSourceModels> sourceModelsProviders = ImmutableList.<HasSourceModels>builder().add(extensionModel).addAll(extensionModel.getConfigurationModels()).build();
List<HasConstructModels> constructModelsProviders = singletonList(extensionModel);
Optional<? extends org.mule.runtime.api.meta.model.ComponentModel> componentModel = resolveModel(operationModelsProviders, sourceModelsProviders, constructModelsProviders, componentName);
// TODO MULE-13894 remove this once unified extensionModel names to use camelCase (see smart connectors and crafted declared extesion models)
if (!componentModel.isPresent()) {
componentModel = resolveModel(operationModelsProviders, sourceModelsProviders, constructModelsProviders, componentIdentifier.getName());
}
return componentModel;
}
}
return empty();
});
} catch (ExecutionException e) {
throw new MuleRuntimeException(e);
}
}
use of org.mule.runtime.api.meta.model.construct.HasConstructModels in project mule by mulesoft.
the class DefaultXmlArtifactDeclarationLoader method declareElement.
private void declareElement(final ConfigLine configLine, final ArtifactDeclarer artifactDeclarer) {
final ExtensionModel ownerExtension = getExtensionModel(configLine);
final ElementDeclarer extensionElementsDeclarer = forExtension(ownerExtension.getName());
final DslSyntaxResolver dsl = resolvers.get(getNamespace(configLine));
Reference<Boolean> alreadyDeclared = new Reference<>(false);
new ExtensionWalker() {
@Override
protected void onConstruct(HasConstructModels owner, ConstructModel model) {
declareComponentModel(configLine, model, extensionElementsDeclarer::newConstruct).ifPresent(declarer -> {
getDeclaredName(configLine).ifPresent(((ConstructElementDeclarer) declarer)::withRefName);
artifactDeclarer.withGlobalElement((GlobalElementDeclaration) declarer.getDeclaration());
alreadyDeclared.set(true);
stop();
});
}
@Override
protected void onConfiguration(ConfigurationModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
if (elementDsl.getElementName().equals(configLine.getIdentifier())) {
ConfigurationElementDeclarer configurationDeclarer = extensionElementsDeclarer.newConfiguration(model.getName());
getDeclaredName(configLine).ifPresent(configurationDeclarer::withRefName);
Map<String, SimpleConfigAttribute> attributes = configLine.getConfigAttributes().values().stream().filter(a -> !a.getName().equals(NAME_ATTRIBUTE_NAME)).collect(toMap(SimpleConfigAttribute::getName, a -> a));
List<ConfigLine> configComplexParameters = configLine.getChildren().stream().filter(config -> declareAsConnectionProvider(ownerExtension, model, configurationDeclarer, config, extensionElementsDeclarer)).collect(toList());
declareParameterizedComponent(model, elementDsl, configurationDeclarer, attributes, configComplexParameters);
artifactDeclarer.withGlobalElement(configurationDeclarer.getDeclaration());
alreadyDeclared.set(true);
stop();
}
}
}.walk(ownerExtension);
if (!alreadyDeclared.get()) {
ownerExtension.getTypes().stream().filter(type -> dsl.resolve(type).map(typeDsl -> typeDsl.getElementName().equals(configLine.getIdentifier())).orElse(false)).findFirst().ifPresent(type -> {
TopLevelParameterDeclarer topLevelParameter = extensionElementsDeclarer.newGlobalParameter(configLine.getIdentifier());
getDeclaredName(configLine).ifPresent(topLevelParameter::withRefName);
type.accept(getParameterDeclarerVisitor(configLine, dsl.resolve(type).get(), value -> topLevelParameter.withValue((ParameterObjectValue) value)));
artifactDeclarer.withGlobalElement(topLevelParameter.getDeclaration());
});
}
}
Aggregations