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.commands;
17  
18  import it.jnrpe.JNRPELIB;
19  import it.jnrpe.ReturnValue;
20  import it.jnrpe.Status;
21  import it.jnrpe.events.EventsUtil;
22  import it.jnrpe.events.IJNRPEEventListener;
23  import it.jnrpe.events.LogEvent;
24  import it.jnrpe.plugins.IPluginRepository;
25  import it.jnrpe.plugins.PluginProxy;
26  
27  import java.util.Collection;
28  import java.util.regex.Matcher;
29  
30  /**
31   * This class is used to invoke a command.
32   * 
33   * @author Massimiliano Ziccardi
34   * 
35   */
36  public final class CommandInvoker {
37  	/**
38  	 * <code>true</code> if the variable parameters ($ARG?$) must be
39  	 * interpolated.
40  	 */
41  	private final boolean acceptParams;
42  
43  	/**
44  	 * The plugin repository to be used to find the plugins.
45  	 */
46  	private final IPluginRepository pluginRepository;
47  
48  	/**
49  	 * The command repository to be used to find the commands.
50  	 */
51  	private final CommandRepository commandRepository;
52  
53  	/**
54  	 * The listeners.
55  	 */
56  	private final Collection<IJNRPEEventListener> listenersList;
57  
58  	/**
59  	 * Builds and initializes the {@link CommandInvoker} object.
60  	 * 
61  	 * @param pluginRepo
62  	 *            The plugin repository containing all the plugins that must be
63  	 *            used by this invoker.
64  	 * @param commandRepo
65  	 *            The command repository containing all the commands that must
66  	 *            be used by this invoker.
67  	 * @param listeners
68  	 *            All the listeners
69  	 */
70  	public CommandInvoker(final IPluginRepository pluginRepo,
71  			final CommandRepository commandRepo, final boolean acceptParams,
72  			final Collection<IJNRPEEventListener> listeners) {
73  		this.acceptParams = acceptParams;
74  		pluginRepository = pluginRepo;
75  		commandRepository = commandRepo;
76  		listenersList = listeners;
77  	}
78  
79  	/**
80  	 * This method executes built in commands or builds a CommandDefinition to.
81  	 * execute external commands (plugins). The methods also expands the $ARG?$
82  	 * macros.
83  	 * 
84  	 * @param commandName
85  	 *            The name of the command, as configured in the server
86  	 *            configuration XML
87  	 * @param argsAry
88  	 *            The arguments to pass to the command as configured in the
89  	 *            server configuration XML (with the $ARG?$ macros)
90  	 * @return The result of the command
91  	 */
92  	public ReturnValue invoke(final String commandName, final String[] argsAry) {
93  		if ("_NRPE_CHECK".equals(commandName)) {
94  			return new ReturnValue(Status.OK, JNRPELIB.VERSION);
95  		}
96  
97  		CommandDefinition cd = commandRepository.getCommand(commandName);
98  
99  		if (cd == null) {
100 			return new ReturnValue(Status.UNKNOWN, "Bad command");
101 		}
102 
103 		return invoke(cd, argsAry);
104 	}
105 
106 	/**
107 	 * This method executes external commands (plugins) The methods also expands
108 	 * the $ARG?$ macros.
109 	 * 
110 	 * @param cd
111 	 *            The command definition
112 	 * @param argsAry
113 	 *            The arguments to pass to the command as configured in the
114 	 *            server configuration XML (with the $ARG?$ macros)
115 	 * @return The result of the command
116 	 */
117 	public ReturnValue invoke(final CommandDefinition cd, final String[] argsAry) {
118 		String pluginName = cd.getPluginName();
119 		try {
120 
121 			String[] commandLine = cd.getCommandLine();
122 
123 			if (acceptParams) {
124 				for (int j = 0; commandLine != null && j < commandLine.length; j++) {
125 					for (int i = 0; i < argsAry.length; i++) {
126 						commandLine[j] = commandLine[j].replaceAll(
127 								"\\$[Aa][Rr][Gg]" + (i + 1) + "\\$",
128 								Matcher.quoteReplacement(argsAry[i]));
129 					}
130 				}
131 			}
132 
133 			PluginProxy plugin = (PluginProxy) pluginRepository
134 					.getPlugin(pluginName);
135 
136 			if (plugin == null) {
137 				EventsUtil.sendEvent(listenersList, this, LogEvent.INFO,
138 						"Unable to instantiate plugin named " + pluginName);
139 				return new ReturnValue(Status.UNKNOWN,
140 						"Error instantiating plugin '" + pluginName
141 								+ "' : bad plugin name?");
142 			}
143 
144 			plugin.addListeners(listenersList);
145 
146 			if (commandLine != null) {
147 				return plugin.execute(commandLine);
148 			} else {
149 				return plugin.execute(new String[0]);
150 			}
151 		} catch (Throwable thr) {
152 			return new ReturnValue(Status.UNKNOWN, "Plugin [" + pluginName
153 					+ "] execution error: " + thr.getMessage());
154 		}
155 	}
156 }