This is an automated email from the ASF dual-hosted git repository.
nicoloboschi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 3efdc9af3be [improve][pulsar-shell] Use singleton custom command
factories (#20064)
3efdc9af3be is described below
commit 3efdc9af3bede77b018146caf1cd11730701ba4f
Author: Ayman Khalil <[email protected]>
AuthorDate: Wed Apr 12 06:09:23 2023 -0700
[improve][pulsar-shell] Use singleton custom command factories (#20064)
---
.../pulsar/admin/cli/PulsarAdminToolTest.java | 31 ++++++++++++++++++++++
.../apache/pulsar/admin/cli/PulsarAdminTool.java | 10 ++-----
2 files changed, 33 insertions(+), 8 deletions(-)
diff --git
a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
index ccae1b11765..2fe7b0236f0 100644
---
a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
+++
b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
@@ -29,8 +29,11 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
+import static org.testng.AssertJUnit.assertNotNull;
+
import com.beust.jcommander.JCommander;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
@@ -45,12 +48,14 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.admin.cli.extensions.CustomCommandFactory;
import org.apache.pulsar.admin.cli.utils.SchemaExtractor;
import org.apache.pulsar.client.admin.Bookies;
import org.apache.pulsar.client.admin.BrokerStats;
@@ -2438,6 +2443,32 @@ public class PulsarAdminToolTest {
}
+ @Test
+ public void customCommandsFactoryImmutable() throws Exception {
+ File narFile = new File(PulsarAdminTool.class.getClassLoader()
+
.getResource("cliextensions/customCommands-nar.nar").getFile());
+ log.info("NAR FILE is {}", narFile);
+
+ PulsarAdminBuilder builder = mock(PulsarAdminBuilder.class);
+ PulsarAdmin admin = mock(PulsarAdmin.class);
+ when(builder.build()).thenReturn(admin);
+ Topics topics = mock(Topics.class);
+ when(admin.topics()).thenReturn(topics);
+ TopicStats topicStats = mock(TopicStats.class);
+ when(topics.getStats(anyString())).thenReturn(topicStats);
+ when(topicStats.toString()).thenReturn("MOCK-TOPIC-STATS");
+
+ Properties properties = new Properties();
+ properties.put("webServiceUrl", "http://localhost:2181");
+ properties.put("cliExtensionsDirectory",
narFile.getParentFile().getAbsolutePath());
+ properties.put("customCommandFactories", "dummy");
+ PulsarAdminTool tool = new PulsarAdminTool(properties);
+ List<CustomCommandFactory> customCommandFactories =
tool.customCommandFactories;
+ assertNotNull(customCommandFactories);
+ tool.run(split("-h"));
+ assertSame(tool.customCommandFactories, customCommandFactories);
+ }
+
@Test
public void testHelpFlag() {
PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
diff --git
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java
index ca0a8a055cf..c06016be438 100644
---
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java
+++
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java
@@ -25,7 +25,6 @@ import com.beust.jcommander.Parameter;
import com.google.common.annotations.VisibleForTesting;
import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -49,7 +48,7 @@ public class PulsarAdminTool {
private static int lastExitCode = Integer.MIN_VALUE;
- protected List<CustomCommandFactory> customCommandFactories = new
ArrayList();
+ protected final List<CustomCommandFactory> customCommandFactories;
protected Map<String, Class<?>> commandMap;
protected JCommander jcommander;
protected RootParams rootParams;
@@ -101,6 +100,7 @@ public class PulsarAdminTool {
public PulsarAdminTool(Properties properties) throws Exception {
this.properties = properties;
+ customCommandFactories =
CustomCommandFactoryProvider.createCustomCommandFactories(properties);
rootParams = new RootParams();
// fallback to previous-version serviceUrl property to maintain
backward-compatibility
initRootParamsFromProperties(properties);
@@ -169,7 +169,6 @@ public class PulsarAdminTool {
return properties;
}
};
- loadCustomCommandFactories();
for (CustomCommandFactory factory : customCommandFactories) {
List<CustomCommandGroup> customCommandGroups =
factory.commandGroups(context);
@@ -191,11 +190,6 @@ public class PulsarAdminTool {
}
}
- private void loadCustomCommandFactories() throws Exception {
- customCommandFactories =
CustomCommandFactoryProvider.createCustomCommandFactories(properties);
- }
-
-
private void addCommand(Map.Entry<String, Class<?>> c,
Supplier<PulsarAdmin> admin) throws Exception {
// To remain backwards compatibility for "source" and "sink" commands
// TODO eventually remove this