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.plugins.IPluginInterface;
21  import it.jnrpe.plugins.IPluginRepository;
22  import it.jnrpe.plugins.PluginOption;
23  import it.jnrpe.plugins.PluginProxy;
24  import it.jnrpe.plugins.UnknownPluginException;
25  
26  import java.io.ByteArrayOutputStream;
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  
30  import jline.console.ConsoleReader;
31  
32  import org.apache.commons.cli2.Group;
33  import org.apache.commons.cli2.Option;
34  import org.apache.commons.cli2.builder.ArgumentBuilder;
35  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
36  import org.apache.commons.cli2.builder.GroupBuilder;
37  import org.apache.commons.cli2.commandline.Parser;
38  import org.apache.commons.cli2.util.HelpFormatter;
39  
40  public class PluginCommand extends ConsoleCommand {
41  
42  	public final static String NAME = "plugin:";
43  
44  	private final String pluginName;
45  	private final IPluginRepository pluginRepository;
46  
47  	private final IPluginInterface plugin;
48  
49  	public PluginCommand(ConsoleReader consoleReader, JNRPE jnrpe,
50  			String pluginName, IPluginRepository pr)
51  			throws UnknownPluginException {
52  		super(consoleReader, jnrpe);
53  		this.pluginName = pluginName;
54  		this.pluginRepository = pr;
55  		this.plugin = pr.getPlugin(pluginName);
56  	}
57  
58  	public boolean execute(String[] args) throws Exception {
59  
60  		Parser p = new Parser();
61  		p.setGroup(getCommandLineGroup());
62  		try {
63  			p.parse(args);
64  		} catch (Exception e) {
65  			getConsole().println();
66  			// getConsole().println("\u001B[1mERROR:\u001B[0m " +
67  			// e.getMessage());
68  			getConsole().println(highlight("ERROR: ") + e.getMessage());
69  			getConsole().println();
70  
71  			printHelp();
72  			return false;
73  		}
74  		PluginProxy plugin = (PluginProxy) pluginRepository
75  				.getPlugin(pluginName);
76  
77  		ReturnValue retVal = plugin.execute(args);
78  
79  		getConsole().println(retVal.getMessage());
80  		return false;
81  	}
82  
83  	public String getName() {
84  		return NAME + pluginName;
85  	}
86  
87  	private Option toOption(PluginOption po) {
88  		DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
89  
90  		oBuilder.withShortName(po.getOption())
91  				.withDescription(po.getDescription())
92  				.withRequired(po.getRequired().equalsIgnoreCase("true"));
93  
94  		if (po.getLongOpt() != null) {
95  			oBuilder.withLongName(po.getLongOpt());
96  		}
97  
98  		if (po.hasArgs()) {
99  			ArgumentBuilder aBuilder = new ArgumentBuilder();
100 
101 			if (po.getArgName() != null) {
102 				aBuilder.withName(po.getArgName());
103 			}
104 
105 			if (po.getArgsOptional()) {
106 				aBuilder.withMinimum(0);
107 			}
108 
109 			if (po.getArgsCount() != null) {
110 				aBuilder.withMaximum(po.getArgsCount());
111 			} else {
112 				aBuilder.withMaximum(1);
113 			}
114 
115 			if (po.getValueSeparator() != null
116 					&& po.getValueSeparator().length() != 0) {
117 				aBuilder.withInitialSeparator(po.getValueSeparator().charAt(0));
118 				aBuilder.withSubsequentSeparator(po.getValueSeparator().charAt(
119 						0));
120 			}
121 			oBuilder.withArgument(aBuilder.create());
122 		}
123 
124 		return oBuilder.create();
125 	}
126 
127 	private Group getCommandLineGroup() {
128 		PluginProxy pp = (PluginProxy) plugin;
129 		GroupBuilder gBuilder = new GroupBuilder();
130 
131 		for (PluginOption po : pp.getOptions()) {
132 			gBuilder = gBuilder.withOption(toOption(po));
133 		}
134 
135 		return gBuilder.create();
136 	}
137 
138 	public String getCommandLine() {
139 		ByteArrayOutputStream bout = new ByteArrayOutputStream();
140 
141 		Group g = getCommandLineGroup();
142 		HelpFormatter hf = new HelpFormatter(null, null, null, getConsole()
143 				.getTerminal().getWidth());
144 		hf.setGroup(g);
145 		hf.setPrintWriter(new PrintWriter(bout));
146 		hf.printUsage();
147 
148 		String usage = new String(bout.toByteArray());
149 
150 		String[] lines = usage.split("\\n");
151 
152 		StringBuffer res = new StringBuffer();
153 
154 		for (int i = 1; i < lines.length; i++) {
155 			res.append(lines[i]);
156 		}
157 
158 		return res.toString();
159 	}
160 
161 	public void printHelp() throws IOException {
162 		Group g = getCommandLineGroup();
163 
164 		ByteArrayOutputStream bout = new ByteArrayOutputStream();
165 		HelpFormatter hf = new HelpFormatter("  ", null, null, getConsole()
166 				.getTerminal().getWidth());
167 		hf.setGroup(g);
168 
169 		PrintWriter pw = new PrintWriter(bout);
170 		hf.setPrintWriter(pw);
171 		hf.printHelp();
172 
173 		pw.flush();
174 
175 		// getConsole().println("\u001B[1mCommand Line:\u001B[0m ");
176 		getConsole().println(highlight("Command Line: "));
177 		getConsole().println("  " + getName() + " " + getCommandLine());
178 		getConsole().println(highlight("Usage:"));
179 		getConsole().println(new String(bout.toByteArray()));
180 
181 	}
182 
183 }