/**
  * If the given parse operation is nested inside an instance of {@link
  * TemplateBeanDefinitionParser.TemplateComponentDefinition}, return the template bean
  * configuration associated with that component. Otherwise return null.
  */
 public static DefaultListableBeanFactory findEnclosingTemplateFactory(ParserContext context) {
   if (context.isNested()) {
     // TODO: support deeper nesting.  this logic breaks completely with bt:persister-chain.
     CompositeComponentDefinition parent = context.getContainingComponent();
     if (parent instanceof TemplateComponentDefinition)
       return ((TemplateComponentDefinition) parent).getTemplateFactory();
   }
   return null;
 }
  @Override
  protected String resolveId(
      Element element, AbstractBeanDefinition definition, ParserContext parserContext)
      throws BeanDefinitionStoreException {
    if (parserContext.isNested()) {
      throw new IllegalStateException(
          "Email sender should be global and cannot be nested within other tags!");
    }

    return ScumdNamespaceHandler.EMAIL_SENDER_ID;
  }
  @Override
  protected AbstractBeanDefinition doParse(
      Element element, ParserContext parserContext, String channelName) {
    if (parserContext.isNested()) {
      if (channelName != null) {
        String elementDescription = IntegrationNamespaceUtils.createElementDescription(element);
        parserContext
            .getReaderContext()
            .error(
                "The 'channel' attribute isn't allowed for "
                    + elementDescription
                    + " when it is used as a nested element,"
                    + " e.g. inside a <chain/>",
                element);
      }
      AbstractBeanDefinition consumerBeanDefinition = this.parseConsumer(element, parserContext);
      this.configureRequestHandlerAdviceChain(element, parserContext, consumerBeanDefinition, null);
      return consumerBeanDefinition;
    }
    BeanDefinitionBuilder builder =
        BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class);
    Element pollerElement = DomUtils.getChildElementByTagName(element, "poller");
    BeanComponentDefinition handlerBeanComponentDefinition =
        this.doParseAndRegisterConsumer(element, parserContext);
    builder.addPropertyReference("handler", handlerBeanComponentDefinition.getBeanName());
    IntegrationNamespaceUtils.checkAndConfigureFixedSubscriberChannel(
        element, parserContext, channelName, handlerBeanComponentDefinition.getBeanName());
    if (pollerElement != null) {
      if (!StringUtils.hasText(channelName)) {
        parserContext
            .getReaderContext()
            .error(
                "outbound channel adapter with a 'poller' requires a 'channel' to poll", element);
      }
      IntegrationNamespaceUtils.configurePollerMetadata(pollerElement, builder, parserContext);
    }
    builder.addPropertyValue("inputChannelName", channelName);

    this.configureRequestHandlerAdviceChain(
        element, parserContext, handlerBeanComponentDefinition.getBeanDefinition(), builder);

    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;
    }
  }