View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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              // TODO Auto-generated catch block
72              e.printStackTrace();
73          }
74      }
75  }