Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart5.png 87% of files have more coverage
32   147   13   5.33
6   74   0.41   6
6     2.17  
1    
 
  PluginManager       Line # 33 32 13 43.2% 0.4318182
 
  (84)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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.control;
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.InternalPluginBo;
27    import org.qedeq.kernel.bo.module.KernelQedeqBo;
28    import org.qedeq.kernel.bo.module.PluginBo;
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  471 toggle public PluginManager(final InternalKernelServices kernel) {
53  471 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  1293 toggle public synchronized PluginBo getPlugin(final String id) {
63  1293 return (PluginBo) 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  0 toggle synchronized PluginBo[] getNonInternalPlugins() {
72  0 final List result = new ArrayList(plugins.size());
73  0 for (int i = 0; i < plugins.size(); i++) {
74  0 if (!(plugins.get(i) instanceof InternalPluginBo)) {
75  0 result.add(plugins.get(i));
76    }
77    }
78  0 return (PluginBo[]) result.toArray(new PluginBo[] {});
79    }
80   
81    /**
82    * Add a plugin..
83    *
84    * @param pluginClass Class that extends {@link PluginBo} 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  4710 toggle synchronized void addPlugin(final String pluginClass) {
90  4710 final String method = "addPlugin";
91  4710 PluginBo plugin = null;
92  4710 try {
93  4710 Class cl = Class.forName(pluginClass);
94  4710 plugin = (PluginBo) cl.newInstance();
95    } catch (ClassNotFoundException e) {
96  0 Trace.fatal(CLASS, this, method, "Plugin class not in class path: " + pluginClass, e);
97  0 throw new RuntimeException(e);
98    } catch (InstantiationException e) {
99  0 Trace.fatal(CLASS, this, method, "Plugin class could not be istanciated: "
100    + pluginClass, e);
101  0 throw new RuntimeException(e);
102    } catch (IllegalAccessException e) {
103  0 Trace.fatal(CLASS, this, method,
104    "Programming error, access for instantiation failed for plugin: " + pluginClass,
105    e);
106  0 throw new RuntimeException(e);
107    } catch (RuntimeException e) {
108  0 Trace.fatal(CLASS, this, method,
109    "Programming error, instantiation failed for plugin: " + pluginClass, e);
110  0 throw new RuntimeException(e);
111    }
112  4710 addPlugin(plugin);
113    // set default plugin values for not yet set parameters
114  4710 final Parameters parameters = kernel.getConfig().getServiceEntries(plugin);
115  4710 plugin.setDefaultValuesForEmptyPluginParameters(parameters);
116  4710 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  4710 toggle synchronized void addPlugin(final PluginBo plugin) {
127  4710 if (id2plugin.get(plugin.getServiceId()) != null) {
128  0 final PluginBo oldPlugin = (PluginBo) id2plugin.get(plugin.getServiceId());
129  0 final RuntimeException e = new IllegalArgumentException("plugin with that name already added: "
130    + oldPlugin.getServiceId() + ": " + plugin.getServiceDescription());
131  0 Trace.fatal(CLASS, this, "addPlugin", "Programing error", e);
132  0 throw e;
133    }
134  4710 id2plugin.put(plugin.getServiceId(), plugin);
135  4710 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  0 toggle public void clearAllPluginResults(final KernelQedeqBo qedeq) {
144  0 qedeq.clearAllPluginErrorsAndWarnings();
145    }
146   
147    }