aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src
diff options
context:
space:
mode:
authorGravatar Anatoliy Sablin2017-11-06 16:34:39 +0300
committerGravatar Anatoliy Sablin2017-11-06 16:34:39 +0300
commit7fe4c65820d012d684405c56942b01da8e77bd23 (patch)
tree019cddf1546a6d5f7897c64475200705a46c6d10 /juick-commands/src
parentd77ff16de5543b5820ee3b185660a052c5f57a15 (diff)
Added help command.
Diffstat (limited to 'juick-commands/src')
-rw-r--r--juick-commands/src/main/java/com/juick/command/Processor.java19
-rw-r--r--juick-commands/src/main/java/com/juick/command/ShowHelp.java52
2 files changed, 57 insertions, 14 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/Processor.java b/juick-commands/src/main/java/com/juick/command/Processor.java
index 51ce2830..60815d16 100644
--- a/juick-commands/src/main/java/com/juick/command/Processor.java
+++ b/juick-commands/src/main/java/com/juick/command/Processor.java
@@ -11,8 +11,6 @@ import org.springframework.stereotype.Component;
import java.util.Set;
import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-import javax.annotation.PostConstruct;
/**
* @author ma1uta
@@ -26,30 +24,23 @@ public class Processor {
private final Set<Command> commands;
@Setter
private ProtocolListener protocolListener;
- private String help;
@Autowired
public Processor(Set<Command> commands) {
this.commands = commands;
}
- @PostConstruct
- public void init() {
- this.help =
- "HELP - returns this help message.\n" + getCommands().stream().map(Command::help).sorted().collect(Collectors.joining("\n"));
- }
-
public String execute(User sender, String command) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Execute command: {}", command);
}
command = command != null ? command.trim() : "";
- if ("help".equalsIgnoreCase(command)) {
- LOGGER.debug("Show help");
- return getHelp();
- }
Command defaultCommand = null;
+ Command helpCommand = null;
for (Command commandItem : getCommands()) {
+ if (commandItem instanceof ShowHelp) {
+ helpCommand = commandItem;
+ }
Pattern pattern = commandItem.pattern();
if (pattern != null && pattern.matcher(command).matches()) {
if (LOGGER.isDebugEnabled()) {
@@ -68,6 +59,6 @@ public class Processor {
return defaultCommand.execute(sender, getProtocolListener(), command);
}
LOGGER.debug("Command processor not found, return help");
- return getHelp();
+ return helpCommand != null ? helpCommand.execute(sender, getProtocolListener(), command) : "Help not found";
}
}
diff --git a/juick-commands/src/main/java/com/juick/command/ShowHelp.java b/juick-commands/src/main/java/com/juick/command/ShowHelp.java
new file mode 100644
index 00000000..78e01706
--- /dev/null
+++ b/juick-commands/src/main/java/com/juick/command/ShowHelp.java
@@ -0,0 +1,52 @@
+package com.juick.command;
+
+import com.juick.User;
+import com.juick.server.protocol.ProtocolListener;
+import lombok.Getter;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import javax.annotation.PostConstruct;
+
+/**
+ * @author ma1uta
+ */
+@Component
+@Getter
+public class ShowHelp implements Command, ApplicationContextAware {
+
+ private static final Pattern PATTERN = Pattern.compile("^help$", Pattern.CASE_INSENSITIVE);
+
+ private ApplicationContext applicationContext;
+ private String help;
+
+ @PostConstruct
+ public void init() {
+ help = getApplicationContext().getBeansOfType(Command.class).values().stream().map(Command::help).sorted()
+ .collect(Collectors.joining("\n"));
+ }
+
+ @Override
+ public Pattern pattern() {
+ return PATTERN;
+ }
+
+ @Override
+ public String help() {
+ return "HELP - returns this help message.";
+ }
+
+ @Override
+ public String execute(User sender, ProtocolListener protocolListener, String command) {
+ return getHelp();
+ }
+
+ @Override
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+ this.applicationContext = applicationContext;
+ }
+}