From 1ddd73655878a146cd99d2fd2746504fe33acee5 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Thu, 20 Aug 2015 11:25:17 -0400 Subject: [PATCH] NIFI-854: Ensure that controller services are included in template if any processor (or other service) in the template references it, regardless of how many levels deep the component's ProcessGroup is --- .../web/api/dto/ControllerServiceDTO.java | 27 +++++++++++++++++ .../apache/nifi/web/util/SnippetUtils.java | 29 ++++++++++--------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/ControllerServiceDTO.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/ControllerServiceDTO.java index 3abd7f2e0c..659be91389 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/ControllerServiceDTO.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/ControllerServiceDTO.java @@ -201,4 +201,31 @@ public class ControllerServiceDTO extends NiFiComponentDTO { public void setValidationErrors(Collection validationErrors) { this.validationErrors = validationErrors; } + + @Override + public int hashCode() { + final String id = getId(); + return 37 + 3 * ((id == null) ? 0 : id.hashCode()); + } + + @Override + public boolean equals(final Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + + if (obj.getClass() != ControllerServiceDTO.class) { + return false; + } + + final ControllerServiceDTO other = (ControllerServiceDTO) obj; + if (getId() == null || other.getId() == null) { + return false; + } + + return getId().equals(other.getId()); + } } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java index 55982bd6aa..5f74a0e071 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java @@ -190,17 +190,23 @@ public final class SnippetUtils { } if (includeControllerServices) { - addControllerServicesToSnippet(snippetDto); + Set controllerServices = snippetDto.getControllerServices(); + if (controllerServices == null) { + controllerServices = new HashSet<>(); + snippetDto.setControllerServices(controllerServices); + } + + addControllerServicesToSnippet(snippetDto, controllerServices); } return snippetDto; } - private void addControllerServicesToSnippet(final FlowSnippetDTO snippetDto) { + private void addControllerServicesToSnippet(final FlowSnippetDTO snippetDto, final Set destinationSet) { final Set processors = snippetDto.getProcessors(); if (processors != null) { for (final ProcessorDTO processorDto : processors) { - addControllerServicesToSnippet(snippetDto, processorDto); + addControllerServicesToSnippet(snippetDto, processorDto, destinationSet); } } @@ -209,13 +215,13 @@ public final class SnippetUtils { for (final ProcessGroupDTO processGroupDto : childGroups) { final FlowSnippetDTO childGroupDto = processGroupDto.getContents(); if (childGroupDto != null) { - addControllerServicesToSnippet(childGroupDto); + addControllerServicesToSnippet(childGroupDto, destinationSet); } } } } - private void addControllerServicesToSnippet(final FlowSnippetDTO snippet, final ProcessorDTO processorDto) { + private void addControllerServicesToSnippet(final FlowSnippetDTO snippet, final ProcessorDTO processorDto, final Set destinationSet) { final ProcessorConfigDTO configDto = processorDto.getConfig(); if (configDto == null) { return; @@ -236,25 +242,20 @@ public final class SnippetUtils { if (propertyDescriptorDto != null && propertyDescriptorDto.getIdentifiesControllerService() != null) { final ControllerServiceNode serviceNode = flowController.getControllerServiceNode(propValue); if (serviceNode != null) { - addControllerServicesToSnippet(snippet, serviceNode); + addControllerServicesToSnippet(snippet, serviceNode, destinationSet); } } } } } - private void addControllerServicesToSnippet(final FlowSnippetDTO snippet, final ControllerServiceNode serviceNode) { + private void addControllerServicesToSnippet(final FlowSnippetDTO snippet, final ControllerServiceNode serviceNode, final Set destinationSet) { if (isServicePresent(serviceNode.getIdentifier(), snippet.getControllerServices())) { return; } final ControllerServiceDTO serviceNodeDto = dtoFactory.createControllerServiceDto(serviceNode); - Set existingServiceDtos = snippet.getControllerServices(); - if (existingServiceDtos == null) { - existingServiceDtos = new HashSet<>(); - snippet.setControllerServices(existingServiceDtos); - } - existingServiceDtos.add(serviceNodeDto); + destinationSet.add(serviceNodeDto); for (final Map.Entry entry : serviceNode.getProperties().entrySet()) { final PropertyDescriptor descriptor = entry.getKey(); @@ -270,7 +271,7 @@ public final class SnippetUtils { final boolean alreadyPresent = isServicePresent(referencedNodeId, snippet.getControllerServices()); if (!alreadyPresent) { - addControllerServicesToSnippet(snippet, referencedNode); + addControllerServicesToSnippet(snippet, referencedNode, destinationSet); } } }