Example usage for org.springframework.integration.config.xml IntegrationNamespaceUtils setValueIfAttributeDefined

List of usage examples for org.springframework.integration.config.xml IntegrationNamespaceUtils setValueIfAttributeDefined

Introduction

In this page you can find the example usage for org.springframework.integration.config.xml IntegrationNamespaceUtils setValueIfAttributeDefined.

Prototype

public static void setValueIfAttributeDefined(BeanDefinitionBuilder builder, Element element,
        String attributeName) 

Source Link

Document

Configures the provided bean definition builder with a property value corresponding to the attribute whose name is provided if that attribute is defined in the given element.

Usage

From source file:biz.c24.io.spring.integration.config.TransformerParser.java

@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    // Required by schema
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "transform-class");

    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "always-return-array");

    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "target-class");

}

From source file:biz.c24.io.spring.integration.config.XPathHeaderEnricherParser.java

@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    ManagedMap<String, Object> headers = new ManagedMap<String, Object>();
    this.processHeaders(element, headers, parserContext);
    builder.addConstructorArgValue(headers);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-overwrite");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "should-skip-nulls");
}

From source file:sipackage.config.xml.SIAdapterUpperPrefixInboundChannelAdapterParser.java

protected BeanMetadataElement parseSource(Element element, ParserContext parserContext) {

    final BeanDefinitionBuilder siAdapterLowerPrefixPollingChannelAdapterBuilder = BeanDefinitionBuilder
            .genericBeanDefinition(SIAdapterUpperPrefixPollingChannelAdapter.class);

    final BeanDefinitionBuilder siAdapterLowerPrefixExecutorBuilder = SIAdapterUpperPrefixParserUtils
            .getSIAdapterUpperPrefixExecutorBuilder(element, parserContext);

    IntegrationNamespaceUtils.setValueIfAttributeDefined(siAdapterLowerPrefixExecutorBuilder, element,
            "example-property");

    final BeanDefinition siAdapterLowerPrefixExecutorBuilderBeanDefinition = siAdapterLowerPrefixExecutorBuilder
            .getBeanDefinition();/* ww w.  ja va 2 s.c  o m*/
    final String channelAdapterId = this.resolveId(element,
            siAdapterLowerPrefixPollingChannelAdapterBuilder.getRawBeanDefinition(), parserContext);
    final String siAdapterLowerPrefixExecutorBeanName = channelAdapterId + ".siAdapterLowerPrefixExecutor";

    parserContext.registerBeanComponent(new BeanComponentDefinition(
            siAdapterLowerPrefixExecutorBuilderBeanDefinition, siAdapterLowerPrefixExecutorBeanName));

    siAdapterLowerPrefixPollingChannelAdapterBuilder
            .addConstructorArgReference(siAdapterLowerPrefixExecutorBeanName);

    return siAdapterLowerPrefixPollingChannelAdapterBuilder.getBeanDefinition();
}

From source file:biz.c24.io.spring.integration.config.XPathTransformerParser.java

@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    String expression = element.getAttribute("xpath-statement");
    String expressionRef = element.getAttribute("xpath-statement-ref");
    boolean hasRef = StringUtils.hasText(expressionRef);
    Assert.isTrue(hasRef ^ StringUtils.hasText(expression),
            "Exactly one of the 'xpath-statement' or 'xpath-statement-ref' attributes is required.");
    if (hasRef) {
        builder.addConstructorArgReference(expressionRef);
    } else {/*  www  . ja va2s  . com*/
        builder.addConstructorArgValue(expression);
    }

    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "evaluation-type");
}

From source file:sipackage.config.xml.SIAdapterUpperPrefixOutboundChannelAdapterParser.java

@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {

    final BeanDefinitionBuilder siAdapterLowerPrefixOutboundChannelAdapterBuilder = BeanDefinitionBuilder
            .genericBeanDefinition(SIAdapterUpperPrefixOutboundGateway.class);
    final BeanDefinitionBuilder siAdapterLowerPrefixExecutorBuilder = SIAdapterUpperPrefixParserUtils
            .getSIAdapterUpperPrefixExecutorBuilder(element, parserContext);

    IntegrationNamespaceUtils.setValueIfAttributeDefined(siAdapterLowerPrefixExecutorBuilder, element,
            "example-property");

    final BeanDefinition siAdapterLowerPrefixExecutorBuilderBeanDefinition = siAdapterLowerPrefixExecutorBuilder
            .getBeanDefinition();//from w w  w .  ja va2s  .c  o  m
    final String channelAdapterId = this.resolveId(element,
            siAdapterLowerPrefixOutboundChannelAdapterBuilder.getRawBeanDefinition(), parserContext);
    final String siAdapterLowerPrefixExecutorBeanName = channelAdapterId + ".siAdapterLowerPrefixExecutor";

    parserContext.registerBeanComponent(new BeanComponentDefinition(
            siAdapterLowerPrefixExecutorBuilderBeanDefinition, siAdapterLowerPrefixExecutorBeanName));

    siAdapterLowerPrefixOutboundChannelAdapterBuilder
            .addConstructorArgReference(siAdapterLowerPrefixExecutorBeanName);
    siAdapterLowerPrefixOutboundChannelAdapterBuilder.addPropertyValue("producesReply", Boolean.FALSE);

    return siAdapterLowerPrefixOutboundChannelAdapterBuilder.getBeanDefinition();

}

From source file:biz.c24.io.spring.integration.config.XPathHeaderEnricherParser.java

protected void processHeaders(Element element, ManagedMap<String, Object> headers,
        ParserContext parserContext) {/*from  www  . j  ava  2s. co m*/
    Object source = parserContext.extractSource(element);
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element headerElement = (Element) node;
            String elementName = node.getLocalName();
            if ("header".equals(elementName)) {
                BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
                        "biz.c24.io.spring.integration.transformer.C24XPathHeaderEnricher$XPathExpressionEvaluatingHeaderValueMessageProcessor");
                String expressionString = headerElement.getAttribute("xpath-statement");
                String expressionRef = headerElement.getAttribute("xpath-statement-ref");
                boolean isExpressionString = StringUtils.hasText(expressionString);
                boolean isExpressionRef = StringUtils.hasText(expressionRef);
                if (!(isExpressionString ^ isExpressionRef)) {
                    parserContext.getReaderContext().error(
                            "Exactly one of the 'xpath-statement' or 'xpath-statement-ref' attributes is required.",
                            source);
                }
                if (isExpressionString) {
                    builder.addConstructorArgValue(expressionString);
                } else {
                    builder.addConstructorArgReference(expressionRef);
                }
                IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, headerElement, "evaluation-type");
                IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, headerElement, "overwrite");
                String headerName = headerElement.getAttribute("name");
                headers.put(headerName, builder.getBeanDefinition());
            }
        }
    }
}

From source file:org.springframework.integration.aws.sqs.config.AmazonSQSInboundChannelAdapterParser.java

@Override
protected BeanMetadataElement parseSource(Element element, ParserContext parserContext) {
    String awsCredentials = registerAmazonWSCredentials(element, parserContext);
    //Mandated at xsd level, so has to be present
    String sqsQueue = element.getAttribute(SQS_QUEUE);
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(AmazonSQSMessageSource.class)
            .addConstructorArgReference(awsCredentials).addConstructorArgValue(sqsQueue);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IS_TRANSACTIONAL);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, MAX_REDELIVERY_ATTEMPTS);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, SQS_OPERATIONS);
    String messageTransformerRef = element.getAttribute(MESSAGE_TRANSFORMER);
    boolean hasMessageTransformerRef = StringUtils.hasText(messageTransformerRef);

    boolean hasSnsHeaderPrefix = false;
    String snsNotificationAttribute = element.getAttribute(CHECK_SNS_NOTIFICATION);
    boolean checkSnsNotification = StringUtils.hasText(snsNotificationAttribute);
    String snsHeaderPrefix = null;
    if (checkSnsNotification) {
        snsHeaderPrefix = element.getAttribute(SNS_HEADER_PREFIX);
        hasSnsHeaderPrefix = StringUtils.hasText(snsHeaderPrefix);
    }//from   w  w  w  .  ja  v a  2 s  . c  o m

    if (!element.hasAttribute(SQS_OPERATIONS)) {
        BeanDefinitionBuilder sqsOperationsBuilder = BeanDefinitionBuilder
                .genericBeanDefinition(AmazonSQSOperationsImpl.class)
                .addConstructorArgReference(awsCredentials);
        if (hasMessageTransformerRef) {
            sqsOperationsBuilder.addPropertyReference("messageTransformer", messageTransformerRef);
        }

        if (checkSnsNotification) {
            sqsOperationsBuilder.addPropertyValue("checkSnsNotification", true);
            if (hasSnsHeaderPrefix) {
                sqsOperationsBuilder.addPropertyValue("snsHeaderPrefix", snsHeaderPrefix);
            }
        }

        //sqs_operations attribute not defined, register the default one
        String operationsBean = BeanDefinitionReaderUtils.registerWithGeneratedName(
                sqsOperationsBuilder.getBeanDefinition(), parserContext.getRegistry());
        builder.addPropertyReference("sqsOperations", operationsBean);
    } else {
        if (hasMessageTransformerRef) {
            //This means, we have a reference to both sqs operations and message transformer provided
            throw new BeanDefinitionStoreException(
                    "Both the attributes,  \"sqs-operations\" and \"message-transformer\" are "
                            + "not supported together. Consider injecting the messageTransformer in the sqsOperation's bean definition"
                            + " and provide \"sqs-operations\" attribute only");
        }
        if (checkSnsNotification) {
            logger.warn("check-sns-notification and sns-header-prefix attributes are supported"
                    + " only when default implementation of sqs operations is used");
        }

    }
    return builder.getBeanDefinition();
}

From source file:org.springframework.integration.config.xml.ChainParser.java

@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MessageHandlerChain.class);

    if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE))) {
        logger.info("It is useful to provide an explicit 'id' attribute on 'chain' elements "
                + "to simplify the identification of child elements in logs etc.");
    }/* ww  w  . j  ava 2 s. c om*/

    String chainHandlerId = this.resolveId(element, builder.getRawBeanDefinition(), parserContext);
    List<BeanMetadataElement> handlerList = new ManagedList<BeanMetadataElement>();
    Set<String> handlerBeanNameSet = new HashSet<String>();
    NodeList children = element.getChildNodes();

    int childOrder = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE && !"poller".equals(child.getLocalName())) {
            BeanMetadataElement childBeanMetadata = this.parseChild(chainHandlerId, (Element) child,
                    childOrder++, parserContext, builder.getBeanDefinition());
            if (childBeanMetadata instanceof RuntimeBeanReference) {
                String handlerBeanName = ((RuntimeBeanReference) childBeanMetadata).getBeanName();
                if (!handlerBeanNameSet.add(handlerBeanName)) {
                    parserContext.getReaderContext().error("A bean definition is already registered for "
                            + "beanName: '" + handlerBeanName + "' within the current <chain>.", element);
                    return null;
                }
            }
            if ("gateway".equals(child.getLocalName())) {
                BeanDefinitionBuilder gwBuilder = BeanDefinitionBuilder.genericBeanDefinition(
                        IntegrationNamespaceUtils.BASE_PACKAGE + ".gateway.RequestReplyMessageHandlerAdapter");
                gwBuilder.addConstructorArgValue(childBeanMetadata);
                handlerList.add(gwBuilder.getBeanDefinition());
            } else {
                handlerList.add(childBeanMetadata);
            }
        }
    }
    builder.addPropertyValue("handlers", handlerList);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "phase");
    return builder;
}

From source file:org.springframework.integration.config.xml.PointToPointChannelParser.java

@Override
protected BeanDefinitionBuilder buildBeanDefinition(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = null;
    Element queueElement = null;/*from  ww w. ja v  a 2s. c  om*/

    // configure a queue-based channel if any queue sub-element is defined
    if ((queueElement = DomUtils.getChildElementByTagName(element, "queue")) != null) {
        builder = BeanDefinitionBuilder.genericBeanDefinition(QueueChannel.class);
        boolean hasStoreRef = this.parseStoreRef(builder, queueElement, element.getAttribute(ID_ATTRIBUTE));
        boolean hasQueueRef = this.parseQueueRef(builder, queueElement);
        if (!hasStoreRef) {
            boolean hasCapacity = this.parseQueueCapacity(builder, queueElement);
            if (hasCapacity && hasQueueRef) {
                parserContext.getReaderContext().error("The 'capacity' attribute is not allowed"
                        + " when providing a 'ref' to a custom queue.", element);
            }
        }
        if (hasStoreRef && hasQueueRef) {
            parserContext.getReaderContext().error("The 'message-store' attribute is not allowed"
                    + " when providing a 'ref' to a custom queue.", element);
        }
    } else if ((queueElement = DomUtils.getChildElementByTagName(element, "priority-queue")) != null) {
        builder = BeanDefinitionBuilder.genericBeanDefinition(PriorityChannel.class);
        this.parseQueueCapacity(builder, queueElement);
        String comparatorRef = queueElement.getAttribute("comparator");
        if (StringUtils.hasText(comparatorRef)) {
            builder.addConstructorArgReference(comparatorRef);
        }
    } else if ((queueElement = DomUtils.getChildElementByTagName(element, "rendezvous-queue")) != null) {
        builder = BeanDefinitionBuilder.genericBeanDefinition(RendezvousChannel.class);
    }

    Element dispatcherElement = DomUtils.getChildElementByTagName(element, "dispatcher");

    // check for the dispatcher attribute (deprecated)
    String dispatcherAttribute = element.getAttribute("dispatcher");
    boolean hasDispatcherAttribute = StringUtils.hasText(dispatcherAttribute);
    if (hasDispatcherAttribute && logger.isWarnEnabled()) {
        logger.warn("The 'dispatcher' attribute on the 'channel' element is deprecated. "
                + "Please use the 'dispatcher' sub-element instead.");
    }

    // verify that a dispatcher is not provided if a queue sub-element exists
    if (queueElement != null && (dispatcherElement != null || hasDispatcherAttribute)) {
        parserContext.getReaderContext().error("The 'dispatcher' attribute or sub-element "
                + "and any queue sub-element are mutually exclusive.", element);
        return null;
    }

    if (queueElement != null) {
        return builder;
    }

    if (dispatcherElement != null && hasDispatcherAttribute) {
        parserContext.getReaderContext()
                .error("The 'dispatcher' attribute and 'dispatcher' "
                        + "sub-element are mutually exclusive. NOTE: the attribute is DEPRECATED. "
                        + "Please use the dispatcher sub-element instead.", element);
        return null;
    }

    if (hasDispatcherAttribute) {
        // this attribute is deprecated, but if set, we need to create a DirectChannel
        // without any LoadBalancerStrategy and the failover flag set to true (default).
        builder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
        if ("failover".equals(dispatcherAttribute)) {
            // round-robin dispatcher is used by default, the "failover" value simply disables it
            builder.addConstructorArgValue(null);
        }
    } else if (dispatcherElement == null) {
        // configure the default DirectChannel with a RoundRobinLoadBalancingStrategy
        builder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
        String maxSubscribers = this.getDefaultMaxSubscribers(parserContext,
                IntegrationNamespaceUtils.DEFAULT_MAX_UNICAST_SUBSCRIBERS_PROPERTY_NAME);
        if (maxSubscribers != null) {
            builder.addPropertyValue("maxSubscribers", maxSubscribers);
        }
    } else {
        // configure either an ExecutorChannel or DirectChannel based on existence of 'task-executor'
        String taskExecutor = dispatcherElement.getAttribute("task-executor");
        if (StringUtils.hasText(taskExecutor)) {
            builder = BeanDefinitionBuilder.genericBeanDefinition(ExecutorChannel.class);
            builder.addConstructorArgReference(taskExecutor);
        } else {
            builder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
        }
        // unless the 'load-balancer' attribute is explicitly set to 'none',
        // configure the default RoundRobinLoadBalancingStrategy
        String loadBalancer = dispatcherElement.getAttribute("load-balancer");
        if ("none".equals(loadBalancer)) {
            builder.addConstructorArgValue(null);
        }
        IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, dispatcherElement, "failover");
        this.setMaxSubscribersProperty(parserContext, builder, dispatcherElement,
                IntegrationNamespaceUtils.DEFAULT_MAX_UNICAST_SUBSCRIBERS_PROPERTY_NAME);
    }
    return builder;
}

From source file:org.springframework.integration.jdbc.config.StoredProcParserUtils.java

/**
 * Create a new {@link BeanDefinitionBuilder} for the class {@link StoredProcExecutor}.
 * Initialize the wrapped {@link StoredProcExecutor} with common properties.
 *
 * @param element Must not be Null//  ww  w .ja v a 2 s .  com
 * @param parserContext Must not be Null
 * @return The {@link BeanDefinitionBuilder} for the {@link StoredProcExecutor}
 */
public static BeanDefinitionBuilder getStoredProcExecutorBuilder(final Element element,
        final ParserContext parserContext) {

    Assert.notNull(element, "The provided element must not be Null.");
    Assert.notNull(parserContext, "The provided parserContext must not be Null.");

    final String dataSourceRef = element.getAttribute("data-source");

    final BeanDefinitionBuilder storedProcExecutorBuilder = BeanDefinitionBuilder
            .genericBeanDefinition(StoredProcExecutor.class);
    storedProcExecutorBuilder.addConstructorArgReference(dataSourceRef);

    final String storedProcedureName = element.getAttribute("stored-procedure-name");
    final String storedProcedureNameExpression = element.getAttribute("stored-procedure-name-expression");
    boolean hasStoredProcedureName = StringUtils.hasText(storedProcedureName);
    boolean hasStoredProcedureNameExpression = StringUtils.hasText(storedProcedureNameExpression);

    if (!(hasStoredProcedureName ^ hasStoredProcedureNameExpression)) {
        parserContext.getReaderContext().error(
                "Exactly one of 'stored-procedure-name' or 'stored-procedure-name-expression' is required",
                element);
    }

    BeanDefinitionBuilder expressionBuilder;
    if (hasStoredProcedureNameExpression) {
        expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
        expressionBuilder.addConstructorArgValue(storedProcedureNameExpression);
    } else {
        expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(LiteralExpression.class);
        expressionBuilder.addConstructorArgValue(storedProcedureName);
    }
    storedProcExecutorBuilder.addPropertyValue("storedProcedureNameExpression",
            expressionBuilder.getBeanDefinition());

    IntegrationNamespaceUtils.setValueIfAttributeDefined(storedProcExecutorBuilder, element,
            "ignore-column-meta-data");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(storedProcExecutorBuilder, element,
            "jdbc-call-operations-cache-size");

    final ManagedList<BeanDefinition> procedureParameterList = StoredProcParserUtils
            .getProcedureParameterBeanDefinitions(element, parserContext);
    final ManagedList<BeanDefinition> sqlParameterDefinitionList = StoredProcParserUtils
            .getSqlParameterDefinitionBeanDefinitions(element, parserContext);

    if (!procedureParameterList.isEmpty()) {
        storedProcExecutorBuilder.addPropertyValue("procedureParameters", procedureParameterList);
    }
    if (!sqlParameterDefinitionList.isEmpty()) {
        storedProcExecutorBuilder.addPropertyValue("sqlParameters", sqlParameterDefinitionList);
    }

    return storedProcExecutorBuilder;

}