1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.server.console;
17
18 import it.jnrpe.JNRPE;
19 import it.jnrpe.commands.CommandRepository;
20 import it.jnrpe.plugins.IPluginRepository;
21
22 import java.io.IOException;
23
24 import jline.console.ConsoleReader;
25 import jline.console.history.MemoryHistory;
26
27 public class JNRPEConsole {
28
29 private final JNRPE jnrpeInstance;
30 private final IPluginRepository pluginRepository;
31 private final CommandRepository commandRepository;
32
33 public JNRPEConsole(final JNRPE jnrpe, final IPluginRepository pr, final CommandRepository cr) {
34 jnrpeInstance = jnrpe;
35 pluginRepository = pr;
36 commandRepository = cr;
37 }
38
39 protected String highlight(final String msg) {
40 if (msg == null){
41 throw new IllegalArgumentException("Message can't be null");
42 }
43
44 return new StringBuffer("\u001B[1m")
45 .append(msg)
46 .append("\u001B[0m").toString();
47 }
48
49 public void start() {
50 try {
51 boolean exit = false;
52 ConsoleReader console = new ConsoleReader();
53 console.setPrompt("JNRPE> ");
54 console.setHistory(new MemoryHistory());
55
56 console.addCompleter(new CommandCompleter(pluginRepository, commandRepository));
57
58 while (!exit) {
59 String commandLine = console.readLine();
60 if (commandLine == null || commandLine.trim().length() == 0) {
61 continue;
62 }
63 try {
64 exit = CommandExecutor.getInstance(console, jnrpeInstance, pluginRepository, commandRepository).executeCommand(commandLine);
65 } catch (Exception e) {
66 console.println(highlight("ERROR: ") + e.getMessage());
67 }
68 }
69
70 } catch (IOException e) {
71
72 e.printStackTrace();
73 }
74 }
75 }