Skip to content

Commit

Permalink
Cache the parsed result of the HelpCommand and HelpTelnetHandler (#7919)
Browse files Browse the repository at this point in the history
* Cache the parsed result of the HelpCommand and HelpTelnetHandler

* Modify the map type to WeakHashMap
  • Loading branch information
BurningCN authored Jun 1, 2021
1 parent 2755cc8 commit 2a9f25f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

@Cmd(name = "help", summary = "help command", example = {
"help",
"help online"
})
public class Help implements BaseCommand {

private static final String MAIN_HELP = "mainHelp";

private static Map<String, String> processedTable = new WeakHashMap<>();

@Override
public String execute(CommandContext commandContext, String[] args) {
if (args != null && args.length > 0) {
return commandHelp(args[0]);
return processedTable.computeIfAbsent(args[0], commandName -> commandHelp(commandName));
} else {
return mainHelp();
return processedTable.computeIfAbsent(MAIN_HELP, commandName -> mainHelp());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

/**
* HelpTelnetHandler
Expand All @@ -36,38 +38,50 @@ public class HelpTelnetHandler implements TelnetHandler {

private final ExtensionLoader<TelnetHandler> extensionLoader = ExtensionLoader.getExtensionLoader(TelnetHandler.class);

private static final String MAIN_HELP = "mainHelp";

private static Map<String, String> processedTable = new WeakHashMap<>();

@Override
public String telnet(Channel channel, String message) {
if (message.length() > 0) {
if (!extensionLoader.hasExtension(message)) {
return "No such command " + message;
}
TelnetHandler handler = extensionLoader.getExtension(message);
Help help = handler.getClass().getAnnotation(Help.class);
StringBuilder buf = new StringBuilder();
buf.append("Command:\r\n ");
buf.append(message + " " + help.parameter().replace("\r\n", " ").replace("\n", " "));
buf.append("\r\nSummary:\r\n ");
buf.append(help.summary().replace("\r\n", " ").replace("\n", " "));
buf.append("\r\nDetail:\r\n ");
buf.append(help.detail().replace("\r\n", " \r\n").replace("\n", " \n"));
return buf.toString();
return processedTable.computeIfAbsent(message, commandName -> generateForOneCommand(commandName));
} else {
List<List<String>> table = new ArrayList<List<String>>();
List<TelnetHandler> handlers = extensionLoader.getActivateExtension(channel.getUrl(), "telnet");
if (CollectionUtils.isNotEmpty(handlers)) {
for (TelnetHandler handler : handlers) {
Help help = handler.getClass().getAnnotation(Help.class);
List<String> row = new ArrayList<String>();
String parameter = " " + extensionLoader.getExtensionName(handler) + " " + (help != null ? help.parameter().replace("\r\n", " ").replace("\n", " ") : "");
row.add(parameter.length() > 55 ? parameter.substring(0, 55) + "..." : parameter);
String summary = help != null ? help.summary().replace("\r\n", " ").replace("\n", " ") : "";
row.add(summary.length() > 55 ? summary.substring(0, 55) + "..." : summary);
table.add(row);
}
return processedTable.computeIfAbsent(MAIN_HELP, commandName -> generateForAllCommand(channel));
}
}

private String generateForOneCommand(String message) {
if (!extensionLoader.hasExtension(message)) {
return "No such command " + message;
}
TelnetHandler handler = extensionLoader.getExtension(message);
Help help = handler.getClass().getAnnotation(Help.class);
StringBuilder buf = new StringBuilder();
buf.append("Command:\r\n ");
buf.append(message + " " + help.parameter().replace("\r\n", " ").replace("\n", " "));
buf.append("\r\nSummary:\r\n ");
buf.append(help.summary().replace("\r\n", " ").replace("\n", " "));
buf.append("\r\nDetail:\r\n ");
buf.append(help.detail().replace("\r\n", " \r\n").replace("\n", " \n"));
return buf.toString();
}

private String generateForAllCommand(Channel channel) {
List<List<String>> table = new ArrayList<List<String>>();
List<TelnetHandler> handlers = extensionLoader.getActivateExtension(channel.getUrl(), "telnet");
if (CollectionUtils.isNotEmpty(handlers)) {
for (TelnetHandler handler : handlers) {
Help help = handler.getClass().getAnnotation(Help.class);
List<String> row = new ArrayList<String>();
String parameter = " " + extensionLoader.getExtensionName(handler) + " " + (help != null ? help.parameter().replace("\r\n", " ").replace("\n", " ") : "");
row.add(parameter.length() > 55 ? parameter.substring(0, 55) + "..." : parameter);
String summary = help != null ? help.summary().replace("\r\n", " ").replace("\n", " ") : "";
row.add(summary.length() > 55 ? summary.substring(0, 55) + "..." : summary);
table.add(row);
}
return "Please input \"help [command]\" show detail.\r\n" + TelnetUtils.toList(table);
}
return "Please input \"help [command]\" show detail.\r\n" + TelnetUtils.toList(table);
}

}

0 comments on commit 2a9f25f

Please sign in to comment.