PluginManager.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.bo.service;
017 
018 import java.util.ArrayList;
019 import java.util.HashMap;
020 import java.util.List;
021 import java.util.Map;
022 
023 import org.qedeq.base.io.Parameters;
024 import org.qedeq.base.trace.Trace;
025 import org.qedeq.kernel.bo.module.InternalKernelServices;
026 import org.qedeq.kernel.bo.module.InternalPluginBo;
027 import org.qedeq.kernel.bo.module.KernelQedeqBo;
028 import org.qedeq.kernel.bo.module.PluginBo;
029 
030 /**
031  * Manage all known plugins.
032  */
033 public class PluginManager {
034 
035     /** This class. */
036     private static final Class CLASS = PluginManager.class;
037 
038     /** Maps plugin ids to plugins. */
039     private final Map id2plugin = new HashMap();
040 
041     /** Stores all plugins. */
042     private final List plugins = new ArrayList();
043 
044     /** Basic kernel properties.  */
045     private final InternalKernelServices kernel;
046 
047     /**
048      * Constructor.
049      *
050      @param   kernel          Kernel access.
051      */
052     public PluginManager(final InternalKernelServices kernel) {
053         this.kernel = kernel;
054     }
055 
056     /**
057      * Get plugin with given id.
058      *
059      @param   id  Plugin id.
060      @return  Registered plugin. Might be <code>null</code>..
061      */
062     public synchronized PluginBo getPlugin(final String id) {
063         return (PluginBoid2plugin.get(id);
064     }
065 
066     /**
067      * Get all registered (non internal) plugins.
068      *
069      @return  Registered plugins. Internal plugins are not included.
070      */
071     synchronized PluginBo[] getNonInternalPlugins() {
072         final List result = new ArrayList(plugins.size());
073         for (int i = 0; i < plugins.size(); i++) {
074             if (!(plugins.get(iinstanceof InternalPluginBo)) {
075                 result.add(plugins.get(i));
076             }
077         }
078         return (PluginBo[]) result.toArray(new PluginBo[] {});
079     }
080 
081     /**
082      * Add a plugin..
083      *
084      @param   pluginClass Class that extends {@link PluginBo} to add.
085      *                      A plugin with same name can not be added twice.
086      *                      Must not be <code>null</code>.
087      @throws  RuntimeException    Plugin addition failed.
088      */
089     synchronized void addPlugin(final String pluginClass) {
090         final String method = "addPlugin";
091         PluginBo plugin = null;
092         try {
093             Class cl = Class.forName(pluginClass);
094             plugin = (PluginBocl.newInstance();
095         catch (ClassNotFoundException e) {
096             Trace.fatal(CLASS, this, method, "Plugin class not in class path: " + pluginClass, e);
097             throw new RuntimeException(e);
098         catch (InstantiationException e) {
099             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().getPluginEntries(plugin);
115         plugin.setDefaultValuesForEmptyPluginParameters(parameters);
116         kernel.getConfig().setPluginKeyValues(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 PluginBo plugin) {
127         if (id2plugin.get(plugin.getPluginId()) != null) {
128             final PluginBo oldPlugin = (PluginBoid2plugin.get(plugin.getPluginId());
129             final RuntimeException e = new IllegalArgumentException("plugin with that name already added: "
130                     + oldPlugin.getPluginId() ": " + plugin.getPluginDescription());
131             Trace.fatal(CLASS, this, "addPlugin""Programing error", e);
132             throw e;
133         }
134         id2plugin.put(plugin.getPluginId(), 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 }