GH-3306: Remove setters from registration object

Fixes https://github.com/spring-projects/spring-integration/issues/3306

To prevent API misuse (one can set ID after doing a registration which
will do nothing actually), it is safer to remove set* methods from
`StandardIntegrationFlowRegistration` object.
Providing some related refactoring like making fields 'final', 
adjusting usages to keep correct behavior of the builder.
This commit is contained in:
Artem Vozhdayenko
2020-09-25 21:48:52 +03:00
committed by GitHub
parent a62a7d1ddd
commit a8430c12a6
3 changed files with 31 additions and 56 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2020 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.
@@ -47,6 +47,7 @@ import org.springframework.messaging.MessageChannel;
*
* @author Artem Bilan
* @author Gary Russell
* @author Artem Vozhdayenko
*
* @since 5.0
*
@@ -118,36 +119,18 @@ public interface IntegrationFlowContext {
*/
String getId();
/**
* Set the flow id.
* @param id the id.
*/
void setId(String id);
/**
* Return the flow.
* @return the flow.
*/
IntegrationFlow getIntegrationFlow();
/**
* Set the integration flow.
* @param integrationFlow the flow.
*/
void setIntegrationFlow(IntegrationFlow integrationFlow);
/**
* Return the flow input channel.
* @return the channel.
*/
MessageChannel getInputChannel();
/**
* Set the flow context.
* @param integrationFlowContext the context.
*/
void setIntegrationFlowContext(IntegrationFlowContext integrationFlowContext);
/**
* Obtain a {@link MessagingTemplate} with its default destination set to the input channel
* of the {@link IntegrationFlow}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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.
@@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
* @author Artem Bilan
* @author Gary Russell
* @author Alexander Shaklein
* @author Artem Vozhdayenko
*
* @since 5.1
*
@@ -90,9 +91,9 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
return Boolean.TRUE.equals(this.useFlowIdAsPrefix.get(flowId));
}
private void register(StandardIntegrationFlowRegistrationBuilder builder) {
IntegrationFlow integrationFlow = builder.integrationFlowRegistration.getIntegrationFlow();
String flowId = builder.integrationFlowRegistration.getId();
private IntegrationFlowRegistration register(StandardIntegrationFlowRegistrationBuilder builder) {
IntegrationFlow integrationFlow = builder.integrationFlow;
String flowId = builder.id;
Lock registerBeanLock = null;
try {
if (flowId == null) {
@@ -115,15 +116,18 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
}
}
builder.integrationFlowRegistration.setIntegrationFlow(integrationFlow);
builder.integrationFlow = integrationFlow;
final String theFlowId = flowId;
builder.additionalBeans.forEach((key, value) -> registerBean(key, value, theFlowId));
IntegrationFlowRegistration registration = new StandardIntegrationFlowRegistration(integrationFlow, this, flowId);
if (builder.autoStartup) {
builder.integrationFlowRegistration.start();
registration.start();
}
this.registry.put(flowId, builder.integrationFlowRegistration);
this.registry.put(flowId, registration);
return registration;
}
private IntegrationFlow registerFlowBean(IntegrationFlow flow, String flowId, @Nullable Object source) {
@@ -257,7 +261,9 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
private final Map<Object, String> additionalBeans = new HashMap<>();
private final IntegrationFlowRegistration integrationFlowRegistration;
private IntegrationFlow integrationFlow;
private String id;
private boolean autoStartup = true;
@@ -267,9 +273,7 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
private Object source;
StandardIntegrationFlowRegistrationBuilder(IntegrationFlow integrationFlow) {
this.integrationFlowRegistration = new StandardIntegrationFlowRegistration(integrationFlow);
this.integrationFlowRegistration.setBeanFactory(StandardIntegrationFlowContext.this.beanFactory);
this.integrationFlowRegistration.setIntegrationFlowContext(StandardIntegrationFlowContext.this);
this.integrationFlow = integrationFlow;
}
/**
@@ -282,7 +286,7 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
*/
@Override
public StandardIntegrationFlowRegistrationBuilder id(String id) {
this.integrationFlowRegistration.setId(id);
this.id = id;
return this;
}
@@ -345,14 +349,14 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
*/
@Override
public IntegrationFlowRegistration register() {
String id = this.integrationFlowRegistration.getId();
Assert.state(!this.idAsPrefix || StringUtils.hasText(id),
Assert.state(!this.idAsPrefix || StringUtils.hasText(this.id),
"An 'id' must be present to use 'useFlowIdAsPrefix'");
if (this.idAsPrefix) {
StandardIntegrationFlowContext.this.useFlowIdAsPrefix.put(id, this.idAsPrefix);
StandardIntegrationFlowContext.this.useFlowIdAsPrefix.put(this.id, this.idAsPrefix);
}
StandardIntegrationFlowContext.this.register(this);
return this.integrationFlowRegistration;
IntegrationFlowRegistration registration = StandardIntegrationFlowContext.this.register(this);
registration.setBeanFactory(StandardIntegrationFlowContext.this.beanFactory);
return registration;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2020 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.
@@ -32,6 +32,7 @@ import org.springframework.messaging.MessageChannel;
*
* @author Artem Bilan
* @author Gary Russell
* @author Artem Vozhdayenko
*
* @since 5.1
*
@@ -39,11 +40,11 @@ import org.springframework.messaging.MessageChannel;
*/
class StandardIntegrationFlowRegistration implements IntegrationFlowRegistration {
private IntegrationFlow integrationFlow;
private final IntegrationFlow integrationFlow;
private IntegrationFlowContext integrationFlowContext;
private final IntegrationFlowContext integrationFlowContext;
private String id;
private final String id;
private MessageChannel inputChannel;
@@ -51,8 +52,10 @@ class StandardIntegrationFlowRegistration implements IntegrationFlowRegistration
private ConfigurableListableBeanFactory beanFactory;
StandardIntegrationFlowRegistration(IntegrationFlow integrationFlow) {
StandardIntegrationFlowRegistration(IntegrationFlow integrationFlow, IntegrationFlowContext integrationFlowContext, String id) {
this.integrationFlow = integrationFlow;
this.integrationFlowContext = integrationFlowContext;
this.id = id;
}
@Override
@@ -60,21 +63,6 @@ class StandardIntegrationFlowRegistration implements IntegrationFlowRegistration
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
@Override
public void setIntegrationFlowContext(IntegrationFlowContext integrationFlowContext) {
this.integrationFlowContext = integrationFlowContext;
}
@Override
public void setId(String id) {
this.id = id;
}
@Override
public void setIntegrationFlow(IntegrationFlow integrationFlow) {
this.integrationFlow = integrationFlow;
}
@Override
public String getId() {
return this.id;