INT-3598: Disambiguate MQTT Namespace Constructor

JIRA: https://jira.spring.io/browse/INT-3598

- Force the first two constructor args to be of type `String`.
- When no client factory, add one in the parser rather than selecing an alternate ctor.

Conflicts:
	spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/config/xml/MqttParserUtils.java
	spring-integration-mqtt/src/test/java/org/springframework/integration/mqtt/config/xml/MqttMessageDrivenChannelAdapterParserTests-context.xml
	spring-integration-mqtt/src/test/java/org/springframework/integration/mqtt/config/xml/MqttMessageDrivenChannelAdapterParserTests.java

Remove test for unsupported `empty topics` functionality
This commit is contained in:
Gary Russell
2015-01-08 11:16:43 -05:00
committed by Artem Bilan
parent 3fbd863fcb
commit 2231f1d68f
4 changed files with 29 additions and 6 deletions

View File

@@ -40,7 +40,7 @@ public class MqttMessageDrivenChannelAdapterParser extends AbstractChannelAdapte
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition(MqttPahoMessageDrivenChannelAdapter.class);
MqttParserUtils.parseCommon(element, builder);
MqttParserUtils.parseCommon(element, builder, parserContext);
builder.addConstructorArgValue(element.getAttribute("topics"));
builder.addPropertyReference("outputChannel", channelName);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-channel");

View File

@@ -49,7 +49,7 @@ public class MqttOutboundChannelAdapterParser extends AbstractOutboundChannelAda
final BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MqttPahoMessageHandler.class);
MqttParserUtils.parseCommon(element, builder);
MqttParserUtils.parseCommon(element, builder, parserContext);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-topic");
if (StringUtils.hasText(element.getAttribute("converter")) &&
(StringUtils.hasText(element.getAttribute("default-qos")) ||

View File

@@ -18,13 +18,17 @@ package org.springframework.integration.mqtt.config.xml;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.util.StringUtils;
/**
* Contains various utility methods for parsing Mqtt Adapter
* specific namesspace elements as well as for the generation of the the
* specific namespace elements as well as for the generation of the the
* respective {@link BeanDefinition}s.
*
* @author Gary Russell
@@ -38,13 +42,30 @@ public final class MqttParserUtils {
throw new AssertionError();
}
public static void parseCommon(Element element, BeanDefinitionBuilder builder) {
builder.addConstructorArgValue(element.getAttribute("url"));
public static void parseCommon(Element element, BeanDefinitionBuilder builder, ParserContext parserContext) {
ValueHolder holder;
int n = 0;
String url = element.getAttribute("url");
if (StringUtils.hasText(url)) {
builder.addConstructorArgValue(url);
holder = builder.getRawBeanDefinition().getConstructorArgumentValues().getIndexedArgumentValues().get(n++);
holder.setType("java.lang.String");
}
builder.addConstructorArgValue(element.getAttribute("client-id"));
holder = builder.getRawBeanDefinition().getConstructorArgumentValues().getIndexedArgumentValues().get(n++);
holder.setType("java.lang.String");
String clientFactory = element.getAttribute("client-factory");
if (StringUtils.hasText(clientFactory)) {
builder.addConstructorArgReference(clientFactory);
}
else {
if (!StringUtils.hasText(url)) {
parserContext.getReaderContext().error("If no 'url' attribute is provided, a 'client-factory' " +
"(with serverURIs) is required", element);
}
builder.addConstructorArgValue(new RootBeanDefinition(DefaultMqttPahoClientFactory.class));
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "phase");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;