This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch camel-3.4.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.4.x by this push: new 6efd6eb CAMEL-15840: avoid keeping duplicate copies of the configuration object in camel-aws2-sns (#4593) 6efd6eb is described below commit 6efd6eb9ab096efab6047cebef274afe9ffaf685 Author: Otavio Rodolfo Piske <orpi...@users.noreply.github.com> AuthorDate: Wed Nov 11 17:14:11 2020 +0100 CAMEL-15840: avoid keeping duplicate copies of the configuration object in camel-aws2-sns (#4593) --- .../camel/component/aws2/sns/Sns2Component.java | 72 ++++++++++++++++++---- .../camel/component/aws2/sns/Sns2Endpoint.java | 6 +- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Component.java b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Component.java index 614c5b6..98cb0d0 100644 --- a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Component.java +++ b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Component.java @@ -18,6 +18,7 @@ package org.apache.camel.component.aws2.sns; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; @@ -34,7 +35,7 @@ import software.amazon.awssdk.services.sns.SnsClient; public class Sns2Component extends DefaultComponent { private static final Logger LOG = LoggerFactory.getLogger(Sns2Component.class); - + @Metadata private Sns2Configuration configuration = new Sns2Configuration(); @@ -50,31 +51,78 @@ public class Sns2Component extends DefaultComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - if (remaining == null || remaining.trim().length() == 0) { throw new IllegalArgumentException("Topic name must be specified."); } - Sns2Configuration configuration = this.configuration != null ? this.configuration.copy() : new Sns2Configuration(); + + if (containsTransientParameters(parameters)) { + Map<String, Object> transientParameters = getTransientParameters(parameters); + + setProperties(getCamelContext(), this, transientParameters); + } + + configuration = this.configuration != null ? this.configuration : new Sns2Configuration(); + Sns2Endpoint endpoint = new Sns2Endpoint(uri, this, configuration); + + Map<String, Object> nonTransientParameters = getNonTransientParameters(parameters); + + setProperties(endpoint, nonTransientParameters); + if (remaining.startsWith("arn:")) { - String[] parts = remaining.split(":"); - if (parts.length != 6 || !parts[2].equals("sns")) { - throw new IllegalArgumentException("Topic arn must be in format arn:aws:sns:region:account:name."); - } - configuration.setTopicArn(remaining); - configuration.setRegion(Region.of(parts[3]).toString()); + parseRemaining(remaining); } else { configuration.setTopicName(remaining); + LOG.debug("Created the endpoint with topic {}", configuration.getTopicName()); } - Sns2Endpoint endpoint = new Sns2Endpoint(uri, this, configuration); - setProperties(endpoint, parameters); + checkAndSetRegistryClient(configuration, endpoint); - if (configuration.getAmazonSNSClient() == null && (configuration.getAccessKey() == null || configuration.getSecretKey() == null)) { + + if (configuration.getAmazonSNSClient() == null + && (configuration.getAccessKey() == null || configuration.getSecretKey() == null)) { throw new IllegalArgumentException("AmazonSNSClient or accessKey and secretKey must be specified"); } return endpoint; } + /* + This method, along with getTransientParameters, getNonTransientParameters and validateParameters handle transient + parameters. Transient parameters, in this sense, means temporary parameters passed to the URI, that should + no be directly set on the endpoint because they apply to a different lifecycle in the component/endpoint creation. + For example, the "configuration" parameter is used to set a different Component/Endpoint configuration class other + than the one provided by Camel. Because the configuration object is required to configure these objects, it must + be used earlier in the life cycle ... and not later as part of the transport setup. Therefore, transient. + */ + private boolean containsTransientParameters(Map<String, Object> parameters) { + return parameters.containsKey("configuration"); + } + + private Map<String, Object> getNonTransientParameters(Map<String, Object> parameters) { + return parameters.entrySet().stream().filter(k -> !k.getKey().equals("configuration")) + .collect(Collectors.toMap(k -> k.getKey(), k -> k.getValue())); + } + + private Map<String, Object> getTransientParameters(Map<String, Object> parameters) { + return parameters.entrySet().stream().filter(k -> k.getKey().equals("configuration")) + .collect(Collectors.toMap(k -> k.getKey(), k -> k.getValue())); + } + + @Override + protected void validateParameters(String uri, Map<String, Object> parameters, String optionPrefix) { + super.validateParameters(uri, getNonTransientParameters(parameters), optionPrefix); + } + + private void parseRemaining(String remaining) { + String[] parts = remaining.split(":"); + if (parts.length != 6 || !parts[2].equals("sns")) { + throw new IllegalArgumentException("Topic arn must be in format arn:aws:sns:region:account:name."); + } + configuration.setTopicArn(remaining); + configuration.setRegion(Region.of(parts[3]).toString()); + + LOG.debug("Created the endpoint with topic arn {}", configuration.getTopicArn()); + } + public Sns2Configuration getConfiguration() { return configuration; } diff --git a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java index 7f94cf7..0ed589e 100644 --- a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java +++ b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java @@ -66,7 +66,7 @@ public class Sns2Endpoint extends DefaultEndpoint implements HeaderFilterStrateg @Metadata(required = true) private String topicNameOrArn; // to support component docs @UriParam - private Sns2Configuration configuration; + private final Sns2Configuration configuration; @UriParam private HeaderFilterStrategy headerFilterStrategy; @@ -185,10 +185,6 @@ public class Sns2Endpoint extends DefaultEndpoint implements HeaderFilterStrateg return configuration; } - public void setConfiguration(Sns2Configuration configuration) { - this.configuration = configuration; - } - public void setSNSClient(SnsClient snsClient) { this.snsClient = snsClient; }