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.ReturnValue;
20  import it.jnrpe.commands.CommandDefinition;
21  import it.jnrpe.commands.CommandInvoker;
22  import it.jnrpe.commands.CommandOption;
23  import it.jnrpe.commands.CommandRepository;
24  import it.jnrpe.plugins.IPluginRepository;
25  
26  import java.io.IOException;
27  
28  import jline.console.ConsoleReader;
29  
30  import org.apache.commons.lang.StringUtils;
31  
32  public class CommandConsoleCommand extends ConsoleCommand {
33  
34  	public final static String NAME = "command:";
35  
36  	private final String commandName;
37  	private final CommandRepository commandRepository;
38  	private final IPluginRepository pluginRepository;
39  
40  	public CommandConsoleCommand(ConsoleReader consoleReader, JNRPE jnrpe,
41  			String commandName, IPluginRepository pr, CommandRepository cr) {
42  		super(consoleReader, jnrpe);
43  		this.commandName = commandName;
44  		this.pluginRepository = pr;
45  		this.commandRepository = cr;
46  	}
47  
48  	public boolean execute(String[] args) throws Exception {
49  		ReturnValue retVal = new CommandInvoker(pluginRepository,
50  				commandRepository, true, null).invoke(commandName, args);
51  
52  		if (retVal == null) {
53  			getConsole()
54  					.println(
55  							"An error has occurred executing the plugin. Null result received.");
56  		} else {
57  			getConsole().println(retVal.getMessage());
58  		}
59  
60  		return false;
61  	}
62  
63  	public String getCommandLine() {
64  		CommandDefinition commandDef = commandRepository
65  				.getCommand(commandName);
66  		StringBuffer opts;
67  
68  		if (commandDef.getArgs() != null) {
69  			opts = new StringBuffer(commandDef.getArgs()).append(" ");
70  		} else {
71  			opts = new StringBuffer(" ");
72  		}
73  
74  		for (CommandOption opt : commandDef.getOptions()) {
75  			opts.append(opt.getValue());
76  		}
77  
78  		String params = opts + StringUtils.join(commandDef.getOptions(), " ");
79  
80  		return getName() + " " + params;
81  	}
82  
83  	public void printHelp() throws IOException {
84  		getConsole().println("Command Line : " + getCommandLine());
85  	}
86  
87  	public String getName() {
88  		return NAME + commandName;
89  	}
90  
91  }