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.utils.StringUtils;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 /**
25 * Container class for command definition configuration.
26 *
27 * @author Massimiliano Ziccardi
28 */
29 public final class CommandDefinition {
30 /**
31 * The command name.
32 */
33 private final String name;
34 /**
35 * The plugin name.
36 */
37 private final String pluginName;
38
39 /**
40 * The raw list of arguments.
41 */
42 private String argsString = null;
43
44 /**
45 * The list of options related to this command.
46 */
47 private List<CommandOption> optionsList = new ArrayList<CommandOption>();
48
49 /**
50 * Builds and initializes the command definition.
51 *
52 * @param commandName
53 * The command name
54 * @param cmdPluginName
55 * The plugin associated with this command
56 */
57 public CommandDefinition(final String commandName,
58 final String cmdPluginName) {
59 this.name = commandName;
60 this.pluginName = cmdPluginName;
61 }
62
63 /**
64 * Sets the raw arguments of this command.
65 *
66 * @param args
67 * The command line
68 */
69 public void setArgs(final String args) {
70 argsString = args;
71 }
72
73 /**
74 * Returns the command name.
75 *
76 * @return The command name
77 */
78 public String getName() {
79 return name;
80 }
81
82 /**
83 * Returns the name of the plugin associated with this command.
84 *
85 * @return The name of the plugin associated with this command
86 */
87 public String getPluginName() {
88 return pluginName;
89 }
90
91 /**
92 * The raw command line of this command.
93 *
94 * @return The raw command line
95 */
96 public String getArgs() {
97 return argsString;
98 }
99
100 /**
101 * Utility function used to quote the characters.
102 *
103 * @param s
104 * The string to be elaborated
105 * @return The string with the quoted characters
106 */
107 private static String quote(final String s) {
108 if (s.indexOf(' ') != -1) {
109 return "\"" + s + "\"";
110 }
111 return s;
112 }
113
114 /**
115 * Merges the command line definition read from the server config file with.
116 * the values received from check_nrpe and produces a clean command line.
117 *
118 * @return a parsable command line
119 */
120 public String[] getCommandLine() {
121 String[] resAry = null;
122 String[] argsAry =
123 argsString != null ? split(argsString) : new String[0];
124 List<String> argsList = new ArrayList<String>();
125
126 int startIndex = 0;
127
128 for (CommandOption opt : optionsList) {
129 String argName = opt.getName();
130 String argValueString = opt.getValue();
131
132 argsList.add((argName.length() == 1 ? "-" : "--") + argName);
133
134 if (argValueString != null) {
135 argsList.add(quote(argValueString));
136 }
137 }
138
139 resAry = new String[argsAry.length + argsList.size()];
140
141 for (String argString : argsList) {
142 resAry[startIndex++] = argString;
143 }
144
145 // vsRes = new String[args.length + m_vArguments.size()];
146 System.arraycopy(argsAry, 0, resAry, startIndex, argsAry.length);
147
148 return resAry;
149 }
150
151 /**
152 * This method splits the command line separating each command and each
153 * argument.
154 *
155 * @param commandLine
156 * The raw command line
157 * @return the splitted command line.
158 */
159 private static String[] split(final String commandLine) {
160 return StringUtils.split(commandLine, false);
161 }
162
163 /**
164 * Adds an option to the command definition.
165 *
166 * @param arg
167 * The option to be added
168 * @return This object.
169 */
170 public CommandDefinition addArgument(final CommandOption arg) {
171 optionsList.add(arg);
172 return this;
173 }
174
175 /**
176 * @return all the command options.
177 */
178 public Collection<CommandOption> getOptions() {
179 return optionsList;
180 }
181 }