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 java.util.Collection;
19  import java.util.HashSet;
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  /**
25   * This object manages all the configured commands.
26   * 
27   * @author Massimiliano Ziccardi
28   */
29  public final class CommandRepository {
30  	/**
31  	 * Contains all the commands. The KEY is the command name, while the value.
32  	 * is the {@link CommandDefinition}.
33  	 */
34  	private final Map<String, CommandDefinition> commandDefinitionsMap = new ConcurrentHashMap<String, CommandDefinition>();
35  
36  	/**
37  	 * Adds a new command definition to the repository.
38  	 * 
39  	 * @param commandDef
40  	 *            The command definition to be added
41  	 */
42  	public void addCommandDefinition(final CommandDefinition commandDef) {
43  		commandDefinitionsMap.put(commandDef.getName(), commandDef);
44  	}
45  
46  	/**
47  	 * Remove the given command definition from the command repository object.
48  	 * 
49  	 * @param commandDef
50  	 *            the command definition to be removed
51  	 */
52  	public void removeCommandDefinition(final CommandDefinition commandDef) {
53  		commandDefinitionsMap.remove(commandDef.getName());
54  	}
55  
56  	/**
57  	 * Returns all the command definition that involves the given plugin.
58  	 * 
59  	 * @param pluginName
60  	 *            the name of the plugin we are interested in
61  	 * @return all the command definition that involves the given plugin
62  	 */
63  	public Set<CommandDefinition> getAllCommandDefinition(
64  			final String pluginName) {
65  
66  		Set<CommandDefinition> res = new HashSet<CommandDefinition>();
67  
68  		for (CommandDefinition cd : commandDefinitionsMap.values()) {
69  			if (cd.getPluginName().equals(pluginName)) {
70  				res.add(cd);
71  			}
72  		}
73  
74  		return res;
75  	}
76  
77  	/**
78  	 * Returns the named command definition.
79  	 * 
80  	 * @param commandName
81  	 *            The command name
82  	 * @return The command definition associated with <code>sName</code>.
83  	 *         <code>null</code> if not found.
84  	 */
85  	public CommandDefinition getCommand(final String commandName) {
86  		return commandDefinitionsMap.get(commandName);
87  	}
88  
89  	/**
90  	 * @return all the installed commands.
91  	 */
92  	public Collection<CommandDefinition> getAllCommands() {
93  		return commandDefinitionsMap.values();
94  	}
95  }