1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38
39
40
41 public final class PluginProxy extends PluginBase {
42
43
44
45 private final IPluginInterface proxiedPlugin;
46
47
48
49
50 private final PluginDefinition proxyedPluginDefinition;
51
52
53
54
55
56 private Group mainOptionsGroup = null;
57
58
59
60
61 private final String description;
62
63
64
65
66
67
68
69
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
88
89
90
91 public Collection<PluginOption> getOptions() {
92 return proxyedPluginDefinition.getOptions();
93 }
94
95
96
97
98
99
100
101
102
103
104 public ReturnValue execute(final String[] argsAry)
105 throws BadThresholdException {
106
107 try {
108 HelpFormatter hf = new HelpFormatter();
109
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
136 return new ReturnValue(Status.UNKNOWN, e.getMessage());
137 }
138 }
139
140
141
142
143
144
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
163 hf.setDivider(sbDivider.toString());
164 hf.setPrintWriter(out);
165 hf.print();
166
167 }
168
169
170
171
172
173 public void printHelp() {
174 printHelp(new PrintWriter(System.out));
175 }
176
177
178
179
180
181
182
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 }