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.plugins;
17  
18  import it.jnrpe.ICommandLine;
19  import it.jnrpe.ReturnValue;
20  import it.jnrpe.Status;
21  import it.jnrpe.utils.BadThresholdException;
22  
23  import java.io.PrintWriter;
24  import java.util.Collection;
25  
26  import org.apache.commons.cli2.CommandLine;
27  import org.apache.commons.cli2.Group;
28  import org.apache.commons.cli2.OptionException;
29  import org.apache.commons.cli2.builder.GroupBuilder;
30  import org.apache.commons.cli2.commandline.Parser;
31  import org.apache.commons.cli2.util.HelpFormatter;
32  import org.apache.commons.lang.StringUtils;
33  
34  /**
35   * This class was intended to abstract the kind of plugin to execute. Hides
36   * command line parsing from command invoker.
37   * 
38   * @author Massimiliano Ziccardi
39   * 
40   */
41  public final class PluginProxy extends PluginBase {
42  	/**
43  	 * The plugin instance proxied by this object.
44  	 */
45  	private final IPluginInterface proxiedPlugin;
46  
47  	/**
48  	 * The plugin definition of the plugin proxied by this object.
49  	 */
50  	private final PluginDefinition proxyedPluginDefinition;
51  
52  	/**
53  	 * The command line definition as requested by the Apache commons cli.
54  	 * library.
55  	 */
56  	private Group mainOptionsGroup = null;
57  
58  	/**
59  	 * The proxied plugin description.
60  	 */
61  	private final String description;
62  
63  	/**
64  	 * Instantiate a new plugin proxy.
65  	 * 
66  	 * @param plugin
67  	 *            The plugin to be proxied
68  	 * @param pluginDef
69  	 *            The plugin definition of the plugin to be proxied
70  	 */
71  	public PluginProxy(final IPluginInterface plugin,
72  			final PluginDefinition pluginDef) {
73  		proxiedPlugin = plugin;
74  		proxyedPluginDefinition = pluginDef;
75  		description = proxyedPluginDefinition.getDescription();
76  
77  		GroupBuilder gBuilder = new GroupBuilder();
78  
79  		for (PluginOption po : pluginDef.getOptions()) {
80  			gBuilder = gBuilder.withOption(po.toOption());
81  		}
82  
83  		mainOptionsGroup = gBuilder.create();
84  	}
85  
86  	/**
87  	 * Returns a collection of all the options accepted by this plugin.
88  	 * 
89  	 * @return a collection of plugin options.
90  	 */
91  	public Collection<PluginOption> getOptions() {
92  		return proxyedPluginDefinition.getOptions();
93  	}
94  
95  	/**
96  	 * Executes the proxied plugin passing the received arguments as parameters.
97  	 * 
98  	 * @param argsAry
99  	 *            The parameters to be passed to the plugin
100 	 * @return The return value of the plugin.
101 	 * @throws BadThresholdException
102 	 *             -
103 	 */
104 	public ReturnValue execute(final String[] argsAry)
105 			throws BadThresholdException {
106 		// CommandLineParser clp = new PosixParser();
107 		try {
108 			HelpFormatter hf = new HelpFormatter();
109 			// configure a parser
110 			Parser p = new Parser();
111 			p.setGroup(mainOptionsGroup);
112 			p.setHelpFormatter(hf);
113 			CommandLine cl = p.parse(argsAry);
114 			if (getListeners() != null
115 					&& proxiedPlugin instanceof IPluginInterfaceEx) {
116 				((IPluginInterfaceEx) proxiedPlugin)
117 						.addListeners(getListeners());
118 			}
119 
120 			Thread.currentThread().setContextClassLoader(
121 					proxiedPlugin.getClass().getClassLoader());
122 
123 			ReturnValue retValue = proxiedPlugin.execute(new PluginCommandLine(
124 					cl));
125 
126 			if (retValue == null) {
127 				String msg = "Plugin [" + getPluginName() + "] with args ["
128 						+ StringUtils.join(argsAry) + "] returned null";
129 
130 				retValue = new ReturnValue(Status.UNKNOWN, msg);
131 			}
132 
133 			return retValue;
134 		} catch (OptionException e) {
135 			// m_Logger.error("ERROR PARSING PLUGIN ARGUMENTS", e);
136 			return new ReturnValue(Status.UNKNOWN, e.getMessage());
137 		}
138 	}
139 
140 	/**
141 	 * Prints the help related to the plugin to a specified output.
142 	 * 
143 	 * @param out
144 	 *            the writer where the help should be written
145 	 */
146 	public void printHelp(final PrintWriter out) {
147 		HelpFormatter hf = new HelpFormatter();
148 		StringBuffer sbDivider = new StringBuffer("=");
149 		while (sbDivider.length() < hf.getPageWidth()) {
150 			sbDivider.append("=");
151 		}
152 		out.println(sbDivider.toString());
153 		out.println("PLUGIN NAME : " + proxyedPluginDefinition.getName());
154 		if (description != null && description.trim().length() != 0) {
155 			out.println(sbDivider.toString());
156 			out.println("Description : ");
157 			out.println();
158 			out.println(description);
159 		}
160 
161 		hf.setGroup(mainOptionsGroup);
162 		// hf.setHeader(m_pluginDef.getName());
163 		hf.setDivider(sbDivider.toString());
164 		hf.setPrintWriter(out);
165 		hf.print();
166 		// hf.printHelp(m_pluginDef.getName(), m_Options);
167 	}
168 
169 	/**
170 	 * Prints the help related to the plugin to standard output.
171 	 */
172 	// FIXME : the output should be a parameter
173 	public void printHelp() {
174 		printHelp(new PrintWriter(System.out));
175 	}
176 
177 	/**
178 	 * Not used.
179 	 * 
180 	 * @param cl
181 	 *            Not used
182 	 * @return null.
183 	 */
184 	@Override
185 	public ReturnValue execute(final ICommandLine cl) {
186 		return null;
187 	}
188 
189 	@Override
190 	protected String getPluginName() {
191 		return null;
192 	}
193 }