| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
| 4 | * |
| 5 | * "Hilbert II" is free software; you can redistribute |
| 6 | * it and/or modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | package org.qedeq.kernel.bo.service.internal; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.qedeq.base.io.Parameters; |
| 24 | import org.qedeq.base.trace.Trace; |
| 25 | import org.qedeq.kernel.bo.module.InternalKernelServices; |
| 26 | import org.qedeq.kernel.bo.module.KernelQedeqBo; |
| 27 | import org.qedeq.kernel.bo.service.basis.InternalModuleServicePlugin; |
| 28 | import org.qedeq.kernel.bo.service.basis.ModuleServicePlugin; |
| 29 | |
| 30 | /** |
| 31 | * Manage all known plugins. |
| 32 | */ |
| 33 | public class PluginManager { |
| 34 | |
| 35 | /** This class. */ |
| 36 | private static final Class CLASS = PluginManager.class; |
| 37 | |
| 38 | /** Maps plugin ids to plugins. */ |
| 39 | private final Map id2plugin = new HashMap(); |
| 40 | |
| 41 | /** Stores all plugins. */ |
| 42 | private final List plugins = new ArrayList(); |
| 43 | |
| 44 | /** Basic kernel properties. */ |
| 45 | private final InternalKernelServices kernel; |
| 46 | |
| 47 | /** |
| 48 | * Constructor. |
| 49 | * |
| 50 | * @param kernel Kernel access. |
| 51 | */ |
| 52 | public PluginManager(final InternalKernelServices kernel) { |
| 53 | this.kernel = kernel; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get plugin with given id. |
| 58 | * |
| 59 | * @param id Plugin id. |
| 60 | * @return Registered plugin. Might be <code>null</code>.. |
| 61 | */ |
| 62 | public synchronized ModuleServicePlugin getPlugin(final String id) { |
| 63 | return (ModuleServicePlugin) id2plugin.get(id); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get all registered (non internal) plugins. |
| 68 | * |
| 69 | * @return Registered plugins. Internal plugins are not included. |
| 70 | */ |
| 71 | synchronized ModuleServicePlugin[] getNonInternalPlugins() { |
| 72 | final List result = new ArrayList(plugins.size()); |
| 73 | for (int i = 0; i < plugins.size(); i++) { |
| 74 | if (!(plugins.get(i) instanceof InternalModuleServicePlugin)) { |
| 75 | result.add(plugins.get(i)); |
| 76 | } |
| 77 | } |
| 78 | return (ModuleServicePlugin[]) result.toArray(new ModuleServicePlugin[] {}); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Add a plugin.. |
| 83 | * |
| 84 | * @param pluginClass Class that extends {@link ModuleServicePlugin} to add. |
| 85 | * A plugin with same name can not be added twice. |
| 86 | * Must not be <code>null</code>. |
| 87 | * @throws RuntimeException Plugin addition failed. |
| 88 | */ |
| 89 | synchronized void addPlugin(final String pluginClass) { |
| 90 | final String method = "addPlugin"; |
| 91 | ModuleServicePlugin plugin = null; |
| 92 | try { |
| 93 | Class cl = Class.forName(pluginClass); |
| 94 | plugin = (ModuleServicePlugin) cl.newInstance(); |
| 95 | } catch (ClassNotFoundException e) { |
| 96 | Trace.fatal(CLASS, this, method, "Plugin class not in class path: " + pluginClass, e); |
| 97 | throw new RuntimeException(e); |
| 98 | } catch (InstantiationException e) { |
| 99 | Trace.fatal(CLASS, this, method, "Plugin class could not be istanciated: " |
| 100 | + pluginClass, e); |
| 101 | throw new RuntimeException(e); |
| 102 | } catch (IllegalAccessException e) { |
| 103 | Trace.fatal(CLASS, this, method, |
| 104 | "Programming error, access for instantiation failed for plugin: " + pluginClass, |
| 105 | e); |
| 106 | throw new RuntimeException(e); |
| 107 | } catch (RuntimeException e) { |
| 108 | Trace.fatal(CLASS, this, method, |
| 109 | "Programming error, instantiation failed for plugin: " + pluginClass, e); |
| 110 | throw new RuntimeException(e); |
| 111 | } |
| 112 | addPlugin(plugin); |
| 113 | // set default plugin values for not yet set parameters |
| 114 | final Parameters parameters = kernel.getConfig().getServiceEntries(plugin); |
| 115 | plugin.setDefaultValuesForEmptyPluginParameters(parameters); |
| 116 | kernel.getConfig().setServiceKeyValues(plugin, parameters); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add a plugin. |
| 121 | * |
| 122 | * @param plugin Plugin to add. A plugin with same name can not be added twice. |
| 123 | * Must not be <code>null</code>. |
| 124 | * @throws RuntimeException Plugin addition failed. |
| 125 | */ |
| 126 | synchronized void addPlugin(final ModuleServicePlugin plugin) { |
| 127 | if (id2plugin.get(plugin.getServiceId()) != null) { |
| 128 | final ModuleServicePlugin oldPlugin = (ModuleServicePlugin) id2plugin.get(plugin.getServiceId()); |
| 129 | final RuntimeException e = new IllegalArgumentException("plugin with that name already added: " |
| 130 | + oldPlugin.getServiceId() + ": " + plugin.getServiceDescription()); |
| 131 | Trace.fatal(CLASS, this, "addPlugin", "Programing error", e); |
| 132 | throw e; |
| 133 | } |
| 134 | id2plugin.put(plugin.getServiceId(), plugin); |
| 135 | plugins.add(plugin); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Clear all plugin errors and warnings on an QEDEQ module. |
| 140 | * |
| 141 | * @param qedeq Clear reults for this module. |
| 142 | */ |
| 143 | public void clearAllPluginResults(final KernelQedeqBo qedeq) { |
| 144 | qedeq.clearAllPluginErrorsAndWarnings(); |
| 145 | } |
| 146 | |
| 147 | } |