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 java.util.Collection; 19 import java.util.Map; 20 import java.util.concurrent.ConcurrentHashMap; 21 22 /** 23 * This class represent the repository of all the installed plugins. 24 * 25 * @author Massimiliano Ziccardi 26 * 27 */ 28 public class PluginRepository implements IPluginRepository { 29 /** 30 * Contains all the plugins declared inside this {@link PluginRepository} 31 * instance. The key of the map is the plugin name, while the value is the. 32 * plugin definition itself. 33 */ 34 private final Map<String, PluginDefinition> pluginsDefinitionsMap = new ConcurrentHashMap<String, PluginDefinition>(); 35 36 /** 37 * Adds a plugin definition to this repository. 38 * 39 * @param pluginDef 40 * The plugin definition to be added. 41 */ 42 public final void addPluginDefinition(final PluginDefinition pluginDef) { 43 pluginsDefinitionsMap.put(pluginDef.getName(), pluginDef); 44 } 45 46 /** 47 * Removes a plugin definition from the repository. 48 * 49 * @param pluginDef 50 * The plugin to be removed 51 */ 52 public final void removePluginDefinition(final PluginDefinition pluginDef) { 53 pluginsDefinitionsMap.remove(pluginDef.getName()); 54 } 55 56 /** 57 * Returns the implementation of the plugin identified by the given name. 58 * 59 * @param name 60 * The plugin name 61 * @return the plugin identified by the given name 62 * @throws UnknownPluginException 63 * if no plugin with the given name exists. 64 */ 65 public final IPluginInterface getPlugin(final String name) 66 throws UnknownPluginException { 67 PluginDefinition pluginDef = pluginsDefinitionsMap.get(name); 68 if (pluginDef == null) { 69 throw new UnknownPluginException(name); 70 } 71 72 try { 73 IPluginInterface pluginInterface = pluginDef.getPluginInterface(); 74 75 if (pluginInterface == null) { 76 pluginInterface = pluginDef.getPluginClass().newInstance(); 77 } 78 return new PluginProxy(pluginInterface, pluginDef); 79 } catch (Exception e) { 80 // FIXME : handle this exception 81 e.printStackTrace(); 82 } 83 84 return null; 85 } 86 87 /** 88 * @return all the configured plugins 89 */ 90 public final Collection<PluginDefinition> getAllPlugins() { 91 return pluginsDefinitionsMap.values(); 92 } 93 }