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

    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-channel");
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "request-timeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "reply-timeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "requires-reply");

    List<Element> subElements = DomUtils.getChildElementsByTagName(element, "property");
    if (!CollectionUtils.isEmpty(subElements)) {
      ManagedMap<String, Object> expressions = new ManagedMap<String, Object>();
      for (Element subElement : subElements) {
        String name = subElement.getAttribute("name");
        BeanDefinition beanDefinition =
            IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression(
                "value", "expression", parserContext, subElement, true);
        expressions.put(name, beanDefinition);
      }
      builder.addPropertyValue("propertyExpressions", expressions);
    }

    subElements = DomUtils.getChildElementsByTagName(element, "header");
    if (!CollectionUtils.isEmpty(subElements)) {
      ManagedMap<String, Object> expressions = new ManagedMap<String, Object>();
      for (Element subElement : subElements) {
        String name = subElement.getAttribute("name");
        BeanDefinition expressionDefinition =
            IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression(
                "value", "expression", parserContext, subElement, true);
        BeanDefinitionBuilder valueProcessorBuilder =
            BeanDefinitionBuilder.genericBeanDefinition(
                IntegrationNamespaceUtils.BASE_PACKAGE
                    + ".transformer.support.ExpressionEvaluatingHeaderValueMessageProcessor");
        valueProcessorBuilder
            .addConstructorArgValue(expressionDefinition)
            .addConstructorArgValue(subElement.getAttribute("type"));
        IntegrationNamespaceUtils.setValueIfAttributeDefined(
            valueProcessorBuilder, subElement, "overwrite");
        expressions.put(name, valueProcessorBuilder.getBeanDefinition());
      }
      builder.addPropertyValue("headerExpressions", expressions);
    }

    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "should-clone-payload");

    String requestPayloadExpression = element.getAttribute("request-payload-expression");

    if (StringUtils.hasText(requestPayloadExpression)) {
      BeanDefinitionBuilder expressionBuilder =
          BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
      expressionBuilder.addConstructorArgValue(requestPayloadExpression);
      builder.addPropertyValue("requestPayloadExpression", expressionBuilder.getBeanDefinition());
    }

    return builder;
  }
  @Override
  protected BeanMetadataElement parseSource(Element element, ParserContext parserContext) {

    final BeanDefinitionBuilder jpaPollingChannelAdapterBuilder =
        BeanDefinitionBuilder.genericBeanDefinition(JpaPollingChannelAdapter.class);

    final BeanDefinitionBuilder jpaExecutorBuilder =
        JpaParserUtils.getJpaExecutorBuilder(element, parserContext);

    String maxNumberOfResults = element.getAttribute("max-number-of-results");
    boolean hasMaxNumberOfResults = StringUtils.hasText(maxNumberOfResults);

    String maxResults = element.getAttribute("max-results");
    boolean hasMaxResults = StringUtils.hasText(maxResults);

    if (hasMaxNumberOfResults) {
      parserContext
          .getReaderContext()
          .warning("'max-number-of-results' is deprecated in favor of 'max-results'", element);
      if (hasMaxResults) {
        parserContext
            .getReaderContext()
            .error("'max-number-of-results' and 'max-results' are mutually exclusive", element);
      } else {
        element.setAttribute("max-results", maxNumberOfResults);
      }
    }

    BeanDefinition definition =
        IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression(
            "max-results", "max-results-expression", parserContext, element, false);
    if (definition != null) {
      jpaExecutorBuilder.addPropertyValue("maxResultsExpression", definition);
    }
    IntegrationNamespaceUtils.setValueIfAttributeDefined(
        jpaExecutorBuilder, element, "delete-after-poll");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(
        jpaExecutorBuilder, element, "flush-after-delete", "flush");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(
        jpaExecutorBuilder, element, "delete-in-batch");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(
        jpaExecutorBuilder, element, "expect-single-result");
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(
        jpaExecutorBuilder, element, "parameter-source");

    final BeanDefinition jpaExecutorBuilderBeanDefinition = jpaExecutorBuilder.getBeanDefinition();
    final String channelAdapterId =
        this.resolveId(
            element, jpaPollingChannelAdapterBuilder.getRawBeanDefinition(), parserContext);
    final String jpaExecutorBeanName = channelAdapterId + ".jpaExecutor";

    parserContext.registerBeanComponent(
        new BeanComponentDefinition(jpaExecutorBuilderBeanDefinition, jpaExecutorBeanName));

    jpaPollingChannelAdapterBuilder.addConstructorArgReference(jpaExecutorBeanName);

    return jpaPollingChannelAdapterBuilder.getBeanDefinition();
  }
  @Override
  protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder =
        BeanDefinitionBuilder.genericBeanDefinition(RedisCollectionPopulatingMessageHandler.class);

    String redisTemplateRef = element.getAttribute("redis-template");
    String connectionFactory = element.getAttribute("connection-factory");
    if (StringUtils.hasText(redisTemplateRef) && StringUtils.hasText(connectionFactory)) {
      parserContext
          .getReaderContext()
          .error("Only one of 'redis-template' or 'connection-factory'" + " is allowed", element);
    }

    if (StringUtils.hasText(redisTemplateRef)) {
      builder.addConstructorArgReference(redisTemplateRef);
    } else {
      if (!StringUtils.hasText(connectionFactory)) {
        connectionFactory = "redisConnectionFactory";
      }
      builder.addConstructorArgReference(connectionFactory);
    }

    boolean atLeastOneRequired = false;
    RootBeanDefinition expressionDef =
        IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression(
            "key", "key-expression", parserContext, element, atLeastOneRequired);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "collection-type");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(
        builder, element, "extract-payload-elements");

    if (expressionDef != null) {
      builder.addConstructorArgValue(expressionDef);
    }

    String mapKeyExpression = element.getAttribute("map-key-expression");
    if (StringUtils.hasText(mapKeyExpression)) {
      RootBeanDefinition mapKeyExpressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
      mapKeyExpressionDef.getConstructorArgumentValues().addGenericArgumentValue(mapKeyExpression);
      builder.addPropertyValue("mapKeyExpression", mapKeyExpressionDef);
    }

    return builder.getBeanDefinition();
  }
Пример #4
0
  @Override
  @SuppressWarnings("rawtypes")
  public BeanDefinition parse(final Element element, ParserContext parserContext) {
    boolean isNested = parserContext.isNested();

    final Map<String, Object> gatewayAttributes = new HashMap<String, Object>();
    gatewayAttributes.put("name", element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE));
    gatewayAttributes.put(
        "defaultPayloadExpression", element.getAttribute("default-payload-expression"));
    gatewayAttributes.put(
        "defaultRequestChannel",
        element.getAttribute(isNested ? "request-channel" : "default-request-channel"));
    gatewayAttributes.put(
        "defaultReplyChannel",
        element.getAttribute(isNested ? "reply-channel" : "default-reply-channel"));
    gatewayAttributes.put("errorChannel", element.getAttribute("error-channel"));
    gatewayAttributes.put("asyncExecutor", element.getAttribute("async-executor"));
    gatewayAttributes.put("mapper", element.getAttribute("mapper"));
    gatewayAttributes.put(
        "defaultReplyTimeout",
        element.getAttribute(isNested ? "reply-timeout" : "default-reply-timeout"));
    gatewayAttributes.put(
        "defaultRequestTimeout",
        element.getAttribute(isNested ? "request-timeout" : "default-request-timeout"));

    List<Element> headerElements = DomUtils.getChildElementsByTagName(element, "default-header");
    if (!CollectionUtils.isEmpty(headerElements)) {
      List<Map<String, Object>> headers = new ArrayList<Map<String, Object>>(headerElements.size());
      for (Element e : headerElements) {
        Map<String, Object> header = new HashMap<String, Object>();
        header.put("name", e.getAttribute("name"));
        header.put("value", e.getAttribute("value"));
        header.put("expression", e.getAttribute("expression"));
        headers.add(header);
      }
      gatewayAttributes.put("defaultHeaders", headers.toArray(new Map[headers.size()]));
    }

    List<Element> methodElements = DomUtils.getChildElementsByTagName(element, "method");
    if (!CollectionUtils.isEmpty(methodElements)) {
      Map<String, BeanDefinition> methodMetadataMap = new ManagedMap<String, BeanDefinition>();
      for (Element methodElement : methodElements) {
        String methodName = methodElement.getAttribute("name");
        BeanDefinitionBuilder methodMetadataBuilder =
            BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodMetadata.class);
        methodMetadataBuilder.addPropertyValue(
            "requestChannelName", methodElement.getAttribute("request-channel"));
        methodMetadataBuilder.addPropertyValue(
            "replyChannelName", methodElement.getAttribute("reply-channel"));
        methodMetadataBuilder.addPropertyValue(
            "requestTimeout", methodElement.getAttribute("request-timeout"));
        methodMetadataBuilder.addPropertyValue(
            "replyTimeout", methodElement.getAttribute("reply-timeout"));

        boolean hasMapper = StringUtils.hasText(element.getAttribute("mapper"));
        Assert.state(
            !hasMapper || !StringUtils.hasText(element.getAttribute("payload-expression")),
            "'payload-expression' is not allowed when a 'mapper' is provided");

        IntegrationNamespaceUtils.setValueIfAttributeDefined(
            methodMetadataBuilder, methodElement, "payload-expression");

        List<Element> invocationHeaders =
            DomUtils.getChildElementsByTagName(methodElement, "header");
        if (!CollectionUtils.isEmpty(invocationHeaders)) {
          Assert.state(!hasMapper, "header elements are not allowed when a 'mapper' is provided");

          Map<String, Object> headerExpressions = new ManagedMap<String, Object>();
          for (Element headerElement : invocationHeaders) {
            BeanDefinition expressionDef =
                IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression(
                    "value", "expression", parserContext, headerElement, true);

            headerExpressions.put(headerElement.getAttribute("name"), expressionDef);
          }
          methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions);
        }
        methodMetadataMap.put(methodName, methodMetadataBuilder.getBeanDefinition());
      }

      gatewayAttributes.put("methods", methodMetadataMap);
    }

    gatewayAttributes.put("serviceInterface", element.getAttribute("service-interface"));

    BeanDefinitionHolder gatewayHolder = this.registrar.parse(gatewayAttributes);
    if (isNested) {
      return gatewayHolder.getBeanDefinition();
    } else {
      BeanDefinitionReaderUtils.registerBeanDefinition(gatewayHolder, parserContext.getRegistry());
      return null;
    }
  }