GEODE-4913: gfsh start server cmd is not recognizing local properties…

… and cache.xml, instead using default

  * Reverted having a default cluster xml, having a default xml
    in the cluster config caused a default cache server to be
    launched, which caused the sighted issue.

  * Instead cluster config is initialized only when needed.
diff --git a/geode-core/src/main/java/org/apache/geode/distributed/internal/ClusterConfigurationService.java b/geode-core/src/main/java/org/apache/geode/distributed/internal/ClusterConfigurationService.java
index 37a76a9..6a06d06 100644
--- a/geode-core/src/main/java/org/apache/geode/distributed/internal/ClusterConfigurationService.java
+++ b/geode-core/src/main/java/org/apache/geode/distributed/internal/ClusterConfigurationService.java
@@ -176,6 +176,13 @@
     return sharedConfigDls;
   }
 
+  public String generateDefaultXml() {
+    StringWriter sw = new StringWriter();
+    PrintWriter pw = new PrintWriter(sw);
+    CacheXmlGenerator.generateDefault(pw);
+    return sw.toString();
+  }
+
   /**
    * Adds/replaces the xml entity in the shared configuration we don't need to trigger the change
    * listener for this modification, so it's ok to operate on the original configuration object
@@ -194,10 +201,7 @@
         }
         String xmlContent = configuration.getCacheXmlContent();
         if (xmlContent == null || xmlContent.isEmpty()) {
-          StringWriter sw = new StringWriter();
-          PrintWriter pw = new PrintWriter(sw);
-          CacheXmlGenerator.generateDefault(pw);
-          xmlContent = sw.toString();
+          xmlContent = generateDefaultXml();
         }
         try {
           final Document doc = XmlUtils.createAndUpgradeDocumentFromXml(xmlContent);
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 c8459f7..1c726ad 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
@@ -194,7 +194,11 @@
 
     Configuration config = getSharedConfiguration().getConfiguration("cluster");
 
-    Document document = XmlUtils.createDocumentFromXml(config.getCacheXmlContent());
+    String xmlContent = config.getCacheXmlContent();
+    if (xmlContent == null || xmlContent.isEmpty()) {
+      xmlContent = getSharedConfiguration().generateDefaultXml();
+    }
+    Document document = XmlUtils.createDocumentFromXml(xmlContent);
     NodeList jndiBindings = document.getElementsByTagName("jndi-binding");
 
     if (jndiBindings == null || jndiBindings.getLength() == 0) {
@@ -214,7 +218,11 @@
     // cluster group config should always be present
     Configuration config = getSharedConfiguration().getConfiguration("cluster");
 
-    Document document = XmlUtils.createDocumentFromXml(config.getCacheXmlContent());
+    String xmlContent = config.getCacheXmlContent();
+    if (xmlContent == null || xmlContent.isEmpty()) {
+      xmlContent = getSharedConfiguration().generateDefaultXml();
+    }
+    Document document = XmlUtils.createDocumentFromXml(xmlContent);
     Node cacheNode = document.getElementsByTagName("cache").item(0);
 
     NodeList jndiBindingsNode = document.getElementsByTagName("jndi-bindings");
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/Configuration.java b/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/Configuration.java
index 132b8a3..b9059b7 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/Configuration.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/Configuration.java
@@ -73,14 +73,6 @@
     this.propertiesFileName = configName + ".properties";
     this.gemfireProperties = new Properties();
     this.jarNames = new HashSet<String>();
-    this.cacheXmlContent = generateInitialXmlContent();
-  }
-
-  private String generateInitialXmlContent() {
-    StringWriter sw = new StringWriter();
-    PrintWriter pw = new PrintWriter(sw);
-    CacheXmlGenerator.generateDefault(pw);
-    return sw.toString();
   }
 
   public String getCacheXmlContent() {
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/StartServerWithXmlDUnitTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/StartServerWithXmlDUnitTest.java
new file mode 100644
index 0000000..5146ef4
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/StartServerWithXmlDUnitTest.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package org.apache.geode.internal.cache;
+
+import static org.apache.geode.distributed.ConfigurationProperties.CACHE_XML_FILE;
+import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.test.dunit.VM;
+import org.apache.geode.test.dunit.rules.ClusterStartupRule;
+import org.apache.geode.test.dunit.rules.MemberVM;
+import org.apache.geode.test.junit.categories.DistributedTest;
+import org.apache.geode.util.test.TestUtil;
+
+@Category(DistributedTest.class)
+public class StartServerWithXmlDUnitTest {
+
+  private VM server;
+  private MemberVM locator;
+
+  @Rule
+  public ClusterStartupRule cluster = new ClusterStartupRule();
+
+  @Before
+  public void before() throws Exception {
+    locator = cluster.startLocatorVM(0);
+
+    Properties props = new Properties();
+    String locators = "localhost[" + locator.getPort() + "]";
+    props.setProperty(LOCATORS, locators);
+    String cacheXmlPath = TestUtil.getResourcePath(getClass(), "CacheServerWithZeroPort.xml");
+    props.setProperty(CACHE_XML_FILE, cacheXmlPath);
+
+    server = cluster.getVM(1);
+
+    server.invoke(() -> {
+      CacheServerLauncher.setServerBindAddress("localhost");
+      CacheServerLauncher.setDisableDefaultServer(false);
+      CacheFactory cf = new CacheFactory(props);
+      Cache cache = cf.create();
+    });
+  }
+
+  @Test
+  public void startServerWithXMLNotToStartDefaultCacheServer() {
+    // Verify that when there is a declarative cache server then we dont launch default server
+    server.invoke(() -> {
+      assertThat(GemFireCacheImpl.getInstance().getCacheServers().size()).isEqualTo(1);
+    });
+  }
+}
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 59acb0e..0e3d700 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
@@ -16,6 +16,7 @@
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doCallRealMethod;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -166,6 +167,7 @@
     Region configRegion = mock(Region.class);
 
     doReturn(configRegion).when(clusterConfigService).getConfigurationRegion();
+    doCallRealMethod().when(clusterConfigService).generateDefaultXml();
     doReturn(null).when(configRegion).put(any(), any());
     doReturn(clusterConfig).when(clusterConfigService).getConfiguration("cluster");
     doReturn(Collections.emptySet()).when(command).findMembers(any(), any());
@@ -188,6 +190,7 @@
     ClusterConfigurationService clusterConfigService = mock(ClusterConfigurationService.class);
 
     doReturn(clusterConfig).when(clusterConfigService).getConfiguration("cluster");
+    doCallRealMethod().when(clusterConfigService).generateDefaultXml();
     doReturn(Collections.emptySet()).when(command).findMembers(any(), any());
     doReturn(clusterConfigService).when(command).getSharedConfiguration();
 
@@ -203,6 +206,7 @@
     Region configRegion = mock(Region.class);
 
     doReturn(configRegion).when(clusterConfigService).getConfigurationRegion();
+    doCallRealMethod().when(clusterConfigService).generateDefaultXml();
     doReturn(null).when(configRegion).put(any(), any());
     doReturn(clusterConfig).when(clusterConfigService).getConfiguration("cluster");
     doReturn(Collections.emptySet()).when(command).findMembers(any(), any());
@@ -225,6 +229,7 @@
     Region configRegion = mock(Region.class);
 
     doReturn(configRegion).when(clusterConfigService).getConfigurationRegion();
+    doCallRealMethod().when(clusterConfigService).generateDefaultXml();
     doReturn(null).when(configRegion).put(any(), any());
     doReturn(clusterConfig).when(clusterConfigService).getConfiguration("cluster");
     doReturn(Collections.emptySet()).when(command).findMembers(any(), any());
diff --git a/geode-core/src/test/resources/org/apache/geode/internal/cache/CacheServerWithZeroPort.xml b/geode-core/src/test/resources/org/apache/geode/internal/cache/CacheServerWithZeroPort.xml
new file mode 100644
index 0000000..01d6633
--- /dev/null
+++ b/geode-core/src/test/resources/org/apache/geode/internal/cache/CacheServerWithZeroPort.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<cache
+        xmlns="http://geode.apache.org/schema/cache"
+        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+        xsi:schemaLocation="http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd"
+        version="1.0">
+    <cache-server port="0"/>
+</cache>