geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinmeil...@apache.org
Subject [geode] branch develop updated: GEODE-4858: move the utility methods out of ClusterConfigurationService (#1822)
Date Thu, 19 Apr 2018 20:47:51 GMT
This is an automated email from the ASF dual-hosted git repository.

jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 2c96f1a  GEODE-4858: move the utility methods out of ClusterConfigurationService
(#1822)
2c96f1a is described below

commit 2c96f1adc043045cef194069f1917db718ebd2cd
Author: jinmeiliao <jiliao@pivotal.io>
AuthorDate: Thu Apr 19 13:47:46 2018 -0700

    GEODE-4858: move the utility methods out of ClusterConfigurationService (#1822)
    
    * GEODE-4858: move the utility methods out of ClusterConfigurationService
---
 .../jdbc/internal/cli/AlterConnectionCommand.java  |  5 +-
 .../jdbc/internal/cli/AlterMappingCommand.java     |  5 +-
 .../internal/cli/DescribeConnectionCommand.java    |  3 +-
 .../jdbc/internal/cli/DescribeMappingCommand.java  |  3 +-
 .../internal/cli/DestroyConnectionCommand.java     |  4 +-
 .../jdbc/internal/cli/DestroyMappingCommand.java   |  3 +-
 .../internal/cli/AlterConnectionCommandTest.java   | 19 ++++---
 .../jdbc/internal/cli/AlterMappingCommandTest.java | 18 ++++---
 .../cli/DescribeConnectionCommandTest.java         | 10 ++--
 .../internal/cli/DescribeMappingCommandTest.java   |  6 ++-
 .../geode/cache/configuration/CacheElement.java    | 53 ++++++++++++++++++
 .../distributed/ClusterConfigurationService.java   | 62 ++--------------------
 .../cli/commands/CreateJndiBindingCommand.java     |  3 +-
 .../cli/commands/DestroyJndiBindingCommand.java    |  3 +-
 .../cli/commands/CreateJndiBindingCommandTest.java | 25 +++++----
 .../commands/DestroyJndiBindingCommandTest.java    |  9 ----
 16 files changed, 124 insertions(+), 107 deletions(-)

diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommand.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommand.java
index 0781b24..73bd30d 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommand.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommand.java
@@ -20,6 +20,7 @@ import java.util.Set;
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.connectors.jdbc.internal.configuration.ConnectorService;
 import org.apache.geode.distributed.ClusterConfigurationService;
 import org.apache.geode.distributed.DistributedMember;
@@ -83,7 +84,7 @@ public class AlterConnectionCommand extends GfshCommand {
       if (service == null) {
         throw new EntityNotFoundException("connection with name '" + name + "' does not exist.");
       }
-      ConnectorService.Connection conn = ccService.findIdentifiable(service.getConnection(),
name);
+      ConnectorService.Connection conn = CacheElement.findElement(service.getConnection(),
name);
       if (conn == null) {
         throw new EntityNotFoundException("connection with name '" + name + "' does not exist.");
       }
@@ -105,7 +106,7 @@ public class AlterConnectionCommand extends GfshCommand {
           results.stream().filter(CliFunctionResult::isSuccessful).findAny().get();
       ConnectorService.Connection mergedConnection =
           (ConnectorService.Connection) successResult.getResultObject();
-      ccService.removeFromList(service.getConnection(), name);
+      CacheElement.removeElement(service.getConnection(), name);
       service.getConnection().add(mergedConnection);
       ccService.saveCustomCacheElement("cluster", service);
       persisted = true;
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommand.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommand.java
index c51a265..ec56281 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommand.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommand.java
@@ -20,6 +20,7 @@ import java.util.Set;
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.connectors.jdbc.internal.configuration.ConnectorService;
 import org.apache.geode.distributed.ClusterConfigurationService;
 import org.apache.geode.distributed.DistributedMember;
@@ -90,7 +91,7 @@ public class AlterMappingCommand extends InternalGfshCommand {
         throw new EntityNotFoundException("mapping with name '" + regionName + "' does not
exist.");
       }
       ConnectorService.RegionMapping mapping =
-          ccService.findIdentifiable(service.getRegionMapping(), regionName);
+          CacheElement.findElement(service.getRegionMapping(), regionName);
       if (mapping == null) {
         throw new EntityNotFoundException("mapping with name '" + regionName + "' does not
exist.");
       }
@@ -112,7 +113,7 @@ public class AlterMappingCommand extends InternalGfshCommand {
           results.stream().filter(CliFunctionResult::isSuccessful).findAny().get();
       ConnectorService.RegionMapping mergedMapping =
           (ConnectorService.RegionMapping) successResult.getResultObject();
-      ccService.removeFromList(service.getRegionMapping(), connectionName);
+      CacheElement.removeElement(service.getRegionMapping(), connectionName);
       service.getRegionMapping().add(mergedMapping);
       ccService.saveCustomCacheElement("cluster", service);
       persisted = true;
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommand.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommand.java
index 4b6751f..07ea5c8 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommand.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommand.java
@@ -26,6 +26,7 @@ import org.apache.logging.log4j.Logger;
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.connectors.jdbc.internal.configuration.ConnectorService;
 import org.apache.geode.distributed.ClusterConfigurationService;
 import org.apache.geode.distributed.DistributedMember;
@@ -95,7 +96,7 @@ public class DescribeConnectionCommand extends InternalGfshCommand {
       throw new EntityNotFoundException("connection named '" + name + "' not found");
     }
     ConnectorService.Connection connection =
-        ccService.findIdentifiable(service.getConnection(), name);
+        CacheElement.findElement(service.getConnection(), name);
     if (connection == null) {
       throw new EntityNotFoundException("connection named '" + name + "' not found");
     }
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommand.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommand.java
index b6948b2..49fd133 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommand.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommand.java
@@ -25,6 +25,7 @@ import java.util.List;
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.connectors.jdbc.internal.configuration.ConnectorService;
 import org.apache.geode.distributed.ClusterConfigurationService;
 import org.apache.geode.distributed.DistributedMember;
@@ -91,7 +92,7 @@ public class DescribeMappingCommand extends InternalGfshCommand {
       throw new EntityNotFoundException("mapping for region '" + regionName + "' not found");
     }
     ConnectorService.RegionMapping mapping =
-        ccService.findIdentifiable(service.getRegionMapping(), regionName);
+        CacheElement.findElement(service.getRegionMapping(), regionName);
     if (mapping == null) {
       throw new EntityNotFoundException("mapping for region '" + regionName + "' not found");
     }
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyConnectionCommand.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyConnectionCommand.java
index 8474b75..295c625 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyConnectionCommand.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyConnectionCommand.java
@@ -20,6 +20,7 @@ import java.util.Set;
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.connectors.jdbc.internal.configuration.ConnectorService;
 import org.apache.geode.distributed.ClusterConfigurationService;
 import org.apache.geode.distributed.DistributedMember;
@@ -61,8 +62,7 @@ public class DestroyConnectionCommand extends InternalGfshCommand {
       ConnectorService service =
           ccService.getCustomCacheElement("cluster", "connector-service", ConnectorService.class);
       if (service != null) {
-        ConnectorService.Connection conn =
-            ccService.findIdentifiable(service.getConnection(), name);
+        ConnectorService.Connection conn = CacheElement.findElement(service.getConnection(),
name);
         service.getConnection().remove(conn);
         ccService.saveCustomCacheElement("cluster", service);
         persisted = true;
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java
index b0e90fd..8877b78 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java
@@ -20,6 +20,7 @@ import java.util.Set;
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.connectors.jdbc.internal.configuration.ConnectorService;
 import org.apache.geode.distributed.ClusterConfigurationService;
 import org.apache.geode.distributed.DistributedMember;
@@ -63,7 +64,7 @@ public class DestroyMappingCommand extends InternalGfshCommand {
       ConnectorService service =
           ccService.getCustomCacheElement("cluster", "connector-service", ConnectorService.class);
       if (service != null) {
-        ccService.removeFromList(service.getRegionMapping(), regionName);
+        CacheElement.removeElement(service.getRegionMapping(), regionName);
         ccService.saveCustomCacheElement("cluster", service);
         persisted = true;
       }
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommandTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommandTest.java
index 207e199..fa85ae8 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommandTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterConnectionCommandTest.java
@@ -47,6 +47,9 @@ public class AlterConnectionCommandTest {
   private List<CliFunctionResult> results;
   private CliFunctionResult result;
   private ClusterConfigurationService ccService;
+  private ConnectorService.Connection connection;
+  private List<ConnectorService.Connection> connections;
+  private ConnectorService connectorService;
 
   @ClassRule
   public static GfshParserRule gfsh = new GfshParserRule();
@@ -63,6 +66,10 @@ public class AlterConnectionCommandTest {
     when(result.getStatus()).thenReturn("message");
     ccService = mock(InternalClusterConfigurationService.class);
     doReturn(ccService).when(command).getConfigurationService();
+    connectorService = mock(ConnectorService.class);
+    connections = new ArrayList<>();
+    connection = new ConnectorService.Connection();
+    connection.setName("name");
   }
 
   @Test
@@ -106,7 +113,6 @@ public class AlterConnectionCommandTest {
 
   @Test
   public void whenCCServiceIsRunningAndNoConnectionFound() {
-    ConnectorService connectorService = mock(ConnectorService.class);
     when(ccService.getCustomCacheElement(any(), any(), any())).thenReturn(connectorService);
     gfsh.executeAndAssertThat(command, COMMAND).statusIsError()
         .containsOutput("connection with name 'name' does not exist.");
@@ -116,11 +122,9 @@ public class AlterConnectionCommandTest {
   @Test
   public void noSuccessfulResult() {
     // connection found in CC
-    ConnectorService connectorService = mock(ConnectorService.class);
     when(ccService.getCustomCacheElement(any(), any(), any())).thenReturn(connectorService);
-    when(ccService.findIdentifiable(any(), any()))
-        .thenReturn(mock(ConnectorService.Connection.class));
-
+    when(connectorService.getConnection()).thenReturn(connections);
+    connections.add(connection);
     // result is not successful
     when(result.isSuccessful()).thenReturn(false);
     results.add(result);
@@ -132,10 +136,9 @@ public class AlterConnectionCommandTest {
   @Test
   public void successfulResult() {
     // connection found in CC
-    ConnectorService connectorService = mock(ConnectorService.class);
     when(ccService.getCustomCacheElement(any(), any(), any())).thenReturn(connectorService);
-    when(ccService.findIdentifiable(any(), any()))
-        .thenReturn(mock(ConnectorService.Connection.class));
+    when(connectorService.getConnection()).thenReturn(connections);
+    connections.add(connection);
 
     // result is not successful
     when(result.isSuccessful()).thenReturn(true);
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommandTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommandTest.java
index 9df6885..8d36497 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommandTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/AlterMappingCommandTest.java
@@ -47,6 +47,9 @@ public class AlterMappingCommandTest {
   private List<CliFunctionResult> results;
   private CliFunctionResult result;
   private ClusterConfigurationService ccService;
+  private ConnectorService.RegionMapping mapping;
+  private List<ConnectorService.RegionMapping> mappings;
+  private ConnectorService connectorService;
 
   @ClassRule
   public static GfshParserRule gfsh = new GfshParserRule();
@@ -63,6 +66,10 @@ public class AlterMappingCommandTest {
     when(result.getStatus()).thenReturn("message");
     ccService = mock(InternalClusterConfigurationService.class);
     doReturn(ccService).when(command).getConfigurationService();
+    connectorService = mock(ConnectorService.class);
+    mappings = new ArrayList<>();
+    mapping = new ConnectorService.RegionMapping();
+    mapping.setRegionName("region");
   }
 
   @Test
@@ -123,11 +130,9 @@ public class AlterMappingCommandTest {
   @Test
   public void noSuccessfulResult() {
     // mapping found in CC
-    ConnectorService connectorService = mock(ConnectorService.class);
     when(ccService.getCustomCacheElement(any(), any(), any())).thenReturn(connectorService);
-    when(ccService.findIdentifiable(any(), any()))
-        .thenReturn(mock(ConnectorService.RegionMapping.class));
-
+    when(connectorService.getRegionMapping()).thenReturn(mappings);
+    mappings.add(mapping);
     // result is not successful
     when(result.isSuccessful()).thenReturn(false);
     results.add(result);
@@ -139,10 +144,9 @@ public class AlterMappingCommandTest {
   @Test
   public void successfulResult() {
     // mapping found in CC
-    ConnectorService connectorService = mock(ConnectorService.class);
     when(ccService.getCustomCacheElement(any(), any(), any())).thenReturn(connectorService);
-    when(ccService.findIdentifiable(any(), any()))
-        .thenReturn(mock(ConnectorService.RegionMapping.class));
+    when(connectorService.getRegionMapping()).thenReturn(mappings);
+    mappings.add(mapping);
 
     // result is not successful
     when(result.isSuccessful()).thenReturn(true);
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandTest.java
index 1b99234..e922d71 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandTest.java
@@ -20,7 +20,9 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.List;
 
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -81,14 +83,16 @@ public class DescribeConnectionCommandTest {
   @Test
   public void successfulResult() {
     // connection found in CC
-    ConnectorService.Connection connection = new ConnectorService.Connection("name1", "url1",
+    ConnectorService.Connection connection = new ConnectorService.Connection("name", "url1",
         "user1", "password1", "param1:value1,param2:value2");
+    List<ConnectorService.Connection> connections = new ArrayList<>();
+    connections.add(connection);
 
     ConnectorService connectorService = mock(ConnectorService.class);
     when(ccService.getCustomCacheElement(any(), any(), any())).thenReturn(connectorService);
-    when(ccService.findIdentifiable(any(), any())).thenReturn(connection);
+    when(connectorService.getConnection()).thenReturn(connections);
 
-    gfsh.executeAndAssertThat(command, COMMAND).statusIsSuccess().containsOutput("name",
"name1")
+    gfsh.executeAndAssertThat(command, COMMAND).statusIsSuccess().containsOutput("name",
"name")
         .containsOutput("url", "url1").containsOutput("user", "user1")
         .containsOutput("password", "********").containsOutput("param1", "value1")
         .containsOutput("param2", "value2");
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommandTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommandTest.java
index 3acdd32..25559b8 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommandTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeMappingCommandTest.java
@@ -20,7 +20,9 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.List;
 
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -90,8 +92,10 @@ public class DescribeMappingCommandTest {
         .add(new ConnectorService.RegionMapping.FieldMapping("field2", "value2"));
 
     ConnectorService connectorService = mock(ConnectorService.class);
+    List<ConnectorService.RegionMapping> mappings = new ArrayList<>();
+    when(connectorService.getRegionMapping()).thenReturn(mappings);
+    mappings.add(mapping);
     when(ccService.getCustomCacheElement(any(), any(), any())).thenReturn(connectorService);
-    when(ccService.findIdentifiable(any(), any())).thenReturn(mapping);
 
     gfsh.executeAndAssertThat(command, COMMAND).statusIsSuccess().containsOutput("region",
"region")
         .containsOutput("connection", "name1").containsOutput("table", "table1")
diff --git a/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheElement.java
b/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheElement.java
index 922d064..e0f1777 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheElement.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheElement.java
@@ -18,10 +18,63 @@
 package org.apache.geode.cache.configuration;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.geode.annotations.Experimental;
 import org.apache.geode.lang.Identifiable;
 
 @Experimental
 public interface CacheElement extends Identifiable<String>, Serializable {
+
+  static <T extends CacheElement> T findElement(List<T> list, String id) {
+    return list.stream().filter(o -> o.getId().equals(id)).findFirst().orElse(null);
+  }
+
+  static <T extends CacheElement> void removeElement(List<T> list, String id)
{
+    list.removeIf(t -> t.getId().equals(id));
+  }
+
+  static RegionConfig findRegionConfiguration(CacheConfig cacheConfig, String regionPath)
{
+    return findElement(cacheConfig.getRegion(), regionPath);
+  }
+
+  static <T extends CacheElement> List<T> findCustomCacheElements(CacheConfig
cacheConfig,
+      Class<T> classT) {
+    List<T> newList = new ArrayList<>();
+    // streaming won't work here, because it's trying to cast element into CacheElement
+    for (Object element : cacheConfig.getCustomCacheElements()) {
+      if (classT.isInstance(element)) {
+        newList.add(classT.cast(element));
+      }
+    }
+    return newList;
+  }
+
+  static <T extends CacheElement> T findCustomCacheElement(CacheConfig cacheConfig,
+      String elementId, Class<T> classT) {
+    return findElement(findCustomCacheElements(cacheConfig, classT), elementId);
+  }
+
+  static <T extends CacheElement> List<T> findCustomRegionElements(CacheConfig
cacheConfig,
+      String regionPath, Class<T> classT) {
+    List<T> newList = new ArrayList<>();
+    RegionConfig regionConfig = findRegionConfiguration(cacheConfig, regionPath);
+    if (regionConfig == null) {
+      return newList;
+    }
+
+    // streaming won't work here, because it's trying to cast element into CacheElement
+    for (Object element : regionConfig.getCustomRegionElements()) {
+      if (classT.isInstance(element)) {
+        newList.add(classT.cast(element));
+      }
+    }
+    return newList;
+  }
+
+  static <T extends CacheElement> T findCustomRegionElement(CacheConfig cacheConfig,
+      String regionPath, String elementId, Class<T> classT) {
+    return findElement(findCustomRegionElements(cacheConfig, regionPath, classT), elementId);
+  }
 }
diff --git a/geode-core/src/main/java/org/apache/geode/distributed/ClusterConfigurationService.java
b/geode-core/src/main/java/org/apache/geode/distributed/ClusterConfigurationService.java
index 2033f0d..2681145 100644
--- a/geode-core/src/main/java/org/apache/geode/distributed/ClusterConfigurationService.java
+++ b/geode-core/src/main/java/org/apache/geode/distributed/ClusterConfigurationService.java
@@ -17,16 +17,16 @@
 
 package org.apache.geode.distributed;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import static org.apache.geode.cache.configuration.CacheElement.findCustomCacheElement;
+import static org.apache.geode.cache.configuration.CacheElement.findCustomRegionElement;
+import static org.apache.geode.cache.configuration.CacheElement.findRegionConfiguration;
+
 import java.util.function.UnaryOperator;
 
 import org.apache.geode.annotations.Experimental;
 import org.apache.geode.cache.configuration.CacheConfig;
 import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.cache.configuration.RegionConfig;
-import org.apache.geode.lang.Identifiable;
 import org.apache.geode.management.internal.cli.exceptions.EntityNotFoundException;
 
 @Experimental
@@ -144,59 +144,5 @@ public interface ClusterConfigurationService {
   }
 
 
-  default <T extends Identifiable<String>> T findIdentifiable(List<T> list,
String id) {
-    return list.stream().filter(o -> o.getId().equals(id)).findFirst().orElse(null);
-  }
-
-  default <T extends Identifiable<String>> void removeFromList(List<T>
list, String id) {
-    for (Iterator<T> iter = list.listIterator(); iter.hasNext();) {
-      if (iter.next().getId().equals(id)) {
-        iter.remove();
-      }
-    }
-  }
-
-  default RegionConfig findRegionConfiguration(CacheConfig cacheConfig, String regionPath)
{
-    return findIdentifiable(cacheConfig.getRegion(), regionPath);
-  }
-
-  default <T extends CacheElement> List<T> findCustomCacheElements(CacheConfig
cacheConfig,
-      Class<T> classT) {
 
-    List<T> newList = new ArrayList<>();
-    // streaming won't work here, because it's trying to cast element into CacheElement
-    for (Object element : cacheConfig.getCustomCacheElements()) {
-      if (classT.isInstance(element)) {
-        newList.add(classT.cast(element));
-      }
-    }
-    return newList;
-  }
-
-  default <T extends CacheElement> T findCustomCacheElement(CacheConfig cacheConfig,
-      String elementId, Class<T> classT) {
-    return findIdentifiable(findCustomCacheElements(cacheConfig, classT), elementId);
-  }
-
-  default <T extends CacheElement> List<T> findCustomRegionElements(CacheConfig
cacheConfig,
-      String regionPath, Class<T> classT) {
-    List<T> newList = new ArrayList<>();
-    RegionConfig regionConfig = findRegionConfiguration(cacheConfig, regionPath);
-    if (regionConfig == null) {
-      return newList;
-    }
-
-    // streaming won't work here, because it's trying to cast element into CacheElement
-    for (Object element : regionConfig.getCustomRegionElements()) {
-      if (classT.isInstance(element)) {
-        newList.add(classT.cast(element));
-      }
-    }
-    return newList;
-  }
-
-  default <T extends CacheElement> T findCustomRegionElement(CacheConfig cacheConfig,
-      String regionPath, String elementId, Class<T> classT) {
-    return findIdentifiable(findCustomRegionElements(cacheConfig, regionPath, classT), elementId);
-  }
 }
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommand.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommand.java
index 2900bdb..e940dc9 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommand.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommand.java
@@ -26,6 +26,7 @@ import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
 import org.apache.geode.cache.configuration.CacheConfig;
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.cache.configuration.JndiBindingsType;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.distributed.internal.InternalClusterConfigurationService;
@@ -154,7 +155,7 @@ public class CreateJndiBindingCommand extends InternalGfshCommand {
       CacheConfig cacheConfig = service.getCacheConfig("cluster");
       if (cacheConfig != null) {
         JndiBindingsType.JndiBinding existing =
-            service.findIdentifiable(cacheConfig.getJndiBindings(), jndiName);
+            CacheElement.findElement(cacheConfig.getJndiBindings(), jndiName);
         if (existing != null) {
           throw new EntityExistsException(
               CliStrings.format("Jndi binding with jndi-name \"{0}\" already exists.", jndiName),
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommand.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommand.java
index 569cf67..e1540dc 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommand.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommand.java
@@ -22,6 +22,7 @@ import java.util.Set;
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheElement;
 import org.apache.geode.cache.configuration.JndiBindingsType;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.distributed.internal.InternalClusterConfigurationService;
@@ -62,7 +63,7 @@ public class DestroyJndiBindingCommand extends InternalGfshCommand {
     if (service != null) {
       service.updateCacheConfig("cluster", cc -> {
         List<JndiBindingsType.JndiBinding> bindings = cc.getJndiBindings();
-        JndiBindingsType.JndiBinding binding = service.findIdentifiable(bindings, jndiName);
+        JndiBindingsType.JndiBinding binding = CacheElement.findElement(bindings, jndiName);
         if (binding == null) {
           throw new EntityNotFoundException(
               CliStrings.format("Jndi binding with jndi-name \"{0}\" does not exist.", jndiName),
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommandTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommandTest.java
index 5f0e4ff..642ce6d 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommandTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CreateJndiBindingCommandTest.java
@@ -21,6 +21,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -58,6 +59,8 @@ public class CreateJndiBindingCommandTest {
 
   private CreateJndiBindingCommand command;
   private InternalCache cache;
+  JndiBindingsType.JndiBinding binding;
+  List<JndiBindingsType.JndiBinding> bindings;
 
   private static String COMMAND = "create jndi-binding ";
 
@@ -66,6 +69,10 @@ public class CreateJndiBindingCommandTest {
     cache = mock(InternalCache.class);
     command = spy(CreateJndiBindingCommand.class);
     doReturn(cache).when(command).getCache();
+
+    binding = new JndiBindingsType.JndiBinding();
+    binding.setJndiName("name");
+    bindings = new ArrayList<>();
   }
 
   @Test
@@ -101,11 +108,11 @@ public class CreateJndiBindingCommandTest {
     InternalClusterConfigurationService clusterConfigService =
         mock(InternalClusterConfigurationService.class);
     CacheConfig cacheConfig = mock(CacheConfig.class);
-    JndiBindingsType.JndiBinding existingBinding = mock(JndiBindingsType.JndiBinding.class);
+    when(cacheConfig.getJndiBindings()).thenReturn(bindings);
+    bindings.add(binding);
 
     doReturn(clusterConfigService).when(command).getConfigurationService();
     doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any());
-    doReturn(existingBinding).when(clusterConfigService).findIdentifiable(any(), any());
 
     gfsh.executeAndAssertThat(command,
         COMMAND + " --type=SIMPLE --name=name --jdbc-driver-class=driver --connection-url=url")
@@ -118,11 +125,11 @@ public class CreateJndiBindingCommandTest {
     InternalClusterConfigurationService clusterConfigService =
         mock(InternalClusterConfigurationService.class);
     CacheConfig cacheConfig = mock(CacheConfig.class);
-    JndiBindingsType.JndiBinding existingBinding = mock(JndiBindingsType.JndiBinding.class);
 
     doReturn(clusterConfigService).when(command).getConfigurationService();
     doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any());
-    doReturn(existingBinding).when(clusterConfigService).findIdentifiable(any(), any());
+    when(cacheConfig.getJndiBindings()).thenReturn(bindings);
+    bindings.add(binding);
 
     gfsh.executeAndAssertThat(command,
         COMMAND
@@ -136,11 +143,11 @@ public class CreateJndiBindingCommandTest {
     InternalClusterConfigurationService clusterConfigService =
         mock(InternalClusterConfigurationService.class);
     CacheConfig cacheConfig = mock(CacheConfig.class);
-    JndiBindingsType.JndiBinding existingBinding = mock(JndiBindingsType.JndiBinding.class);
+    when(cacheConfig.getJndiBindings()).thenReturn(bindings);
+    bindings.add(binding);
 
     doReturn(clusterConfigService).when(command).getConfigurationService();
     doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any());
-    doReturn(existingBinding).when(clusterConfigService).findIdentifiable(any(), any());
 
     gfsh.executeAndAssertThat(command,
         COMMAND
@@ -153,11 +160,11 @@ public class CreateJndiBindingCommandTest {
     InternalClusterConfigurationService clusterConfigService =
         mock(InternalClusterConfigurationService.class);
     CacheConfig cacheConfig = mock(CacheConfig.class);
-    JndiBindingsType.JndiBinding existingBinding = mock(JndiBindingsType.JndiBinding.class);
+    when(cacheConfig.getJndiBindings()).thenReturn(bindings);
+    bindings.add(binding);
 
     doReturn(clusterConfigService).when(command).getConfigurationService();
     doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any());
-    doReturn(existingBinding).when(clusterConfigService).findIdentifiable(any(), any());
 
     gfsh.executeAndAssertThat(command,
         COMMAND
@@ -185,7 +192,6 @@ public class CreateJndiBindingCommandTest {
     doReturn(Collections.emptySet()).when(command).findMembers(any(), any());
     doReturn(clusterConfigService).when(command).getConfigurationService();
     doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any());
-    doReturn(null).when(clusterConfigService).findIdentifiable(any(), any());
 
     gfsh.executeAndAssertThat(command,
         COMMAND + " --type=SIMPLE --name=name --jdbc-driver-class=driver --connection-url=url")
@@ -251,7 +257,6 @@ public class CreateJndiBindingCommandTest {
     doReturn(clusterConfigService).when(command).getConfigurationService();
     doReturn(results).when(command).executeAndGetFunctionResult(any(), any(), any());
     doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any());
-    doReturn(null).when(clusterConfigService).findIdentifiable(any(), any());
 
     gfsh.executeAndAssertThat(command,
         COMMAND
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommandTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommandTest.java
index bcc8d25..86a1458 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommandTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyJndiBindingCommandTest.java
@@ -39,7 +39,6 @@ import org.mockito.ArgumentCaptor;
 
 import org.apache.geode.cache.Region;
 import org.apache.geode.cache.configuration.CacheConfig;
-import org.apache.geode.cache.configuration.JndiBindingsType;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.distributed.internal.InternalClusterConfigurationService;
 import org.apache.geode.internal.cache.InternalCache;
@@ -86,28 +85,24 @@ public class DestroyJndiBindingCommandTest {
 
   @Test
   public void returnsErrorIfBindingDoesNotExistAndIfExistsUnspecified() {
-    when(ccService.findIdentifiable(any(), any())).thenReturn(null);
     gfsh.executeAndAssertThat(command, COMMAND + " --name=name").statusIsError()
         .containsOutput("does not exist.");
   }
 
   @Test
   public void skipsIfBindingDoesNotExistAndIfExistsSpecified() {
-    when(ccService.findIdentifiable(any(), any())).thenReturn(null);
     gfsh.executeAndAssertThat(command, COMMAND + " --name=name --if-exists").statusIsSuccess()
         .containsOutput("does not exist.");
   }
 
   @Test
   public void skipsIfBindingDoesNotExistAndIfExistsSpecifiedTrue() {
-    when(ccService.findIdentifiable(any(), any())).thenReturn(null);
     gfsh.executeAndAssertThat(command, COMMAND + " --name=name --if-exists=true").statusIsSuccess()
         .containsOutput("does not exist.");
   }
 
   @Test
   public void returnsErrorIfBindingDoesNotExistAndIfExistsSpecifiedFalse() {
-    when(ccService.findIdentifiable(any(), any())).thenReturn(null);
     gfsh.executeAndAssertThat(command, COMMAND + " --name=name --if-exists=false").statusIsError()
         .containsOutput("does not exist.");
   }
@@ -125,8 +120,6 @@ public class DestroyJndiBindingCommandTest {
   public void whenNoMembersFoundAndClusterConfigRunningThenUpdateClusterConfig() {
     doNothing().when(ccService).updateCacheConfig(any(), any());
     doReturn(Collections.emptySet()).when(command).findMembers(any(), any());
-    when(ccService.findIdentifiable(any(), any()))
-        .thenReturn(mock(JndiBindingsType.JndiBinding.class));
 
     gfsh.executeAndAssertThat(command, COMMAND + " --name=name").statusIsSuccess()
         .containsOutput(
@@ -182,8 +175,6 @@ public class DestroyJndiBindingCommandTest {
     doReturn(members).when(command).findMembers(any(), any());
     doReturn(results).when(command).executeAndGetFunctionResult(any(), any(), any());
     doNothing().when(ccService).updateCacheConfig(any(), any());
-    when(ccService.findIdentifiable(any(), any()))
-        .thenReturn(mock(JndiBindingsType.JndiBinding.class));
 
     gfsh.executeAndAssertThat(command, COMMAND + " --name=name").statusIsSuccess()
         .tableHasColumnOnlyWithValues("Member", "server1")

-- 
To stop receiving notification emails like this one, please contact
jinmeiliao@apache.org.

Mime
View raw message