| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
| 3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
| 4 | ||
| 5 | All rights reserved. | |
| 6 | ||
| 7 | Redistribution and use in source and binary forms, with or without | |
| 8 | modification, are permitted provided that the following conditions are met: | |
| 9 | ||
| 10 | 1. Redistributions of source code must retain the above copyright notice, this | |
| 11 | list of conditions and the following disclaimer. | |
| 12 | ||
| 13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 14 | this list of conditions and the following disclaimer in the documentation | |
| 15 | and/or other materials provided with the distribution. | |
| 16 | ||
| 17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
| 18 | may be used to endorse or promote products derived from this software without | |
| 19 | specific prior written permission. | |
| 20 | ||
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
| 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
| 28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | ||
| 33 | package org.chwf.plugin; | |
| 34 | ||
| 35 | import org.chwf.config.Config; | |
| 36 | import org.chwf.config.ConfigFactory; | |
| 37 | import org.chwf.i18n.Message; | |
| 38 | ||
| 39 | /** | |
| 40 | * Class with plugin utility methods. | |
| 41 | * | |
| 42 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 43 | */ | |
| 44 | 1 | public abstract class Plugin { |
| 45 | ||
| 46 | /** Message key. */ | |
| 47 | private static final String MESSAGE_INVALID_PLUGIN_TYPE = | |
| 48 | "INVALID_PLUGIN_TYPE"; | |
| 49 | ||
| 50 | /** Message key. */ | |
| 51 | private static final String MESSAGE_NO_PUBLIC_PLUGIN_CONSTRUCTOR = | |
| 52 | "NO_PUBLIC_PLUGIN_CONSTRUCTOR"; | |
| 53 | ||
| 54 | /** Message key. */ | |
| 55 | private static final String MESSAGE_PLUGIN_CLASS_NOT_FOUND = | |
| 56 | "PLUGIN_CLASS_NOT_FOUND"; | |
| 57 | ||
| 58 | /** Message key. */ | |
| 59 | private static final String MESSAGE_PLUGIN_CONSTRUCTOR_FAILED = | |
| 60 | "PLUGIN_CONSTRUCTOR_FAILED"; | |
| 61 | ||
| 62 | /** | |
| 63 | * Utility method to initialize plugin singletons.<p> | |
| 64 | * | |
| 65 | * @param rootClass The root class (superclass) of the plugin. | |
| 66 | * @param pluginID The plugin configuration id (key). Will lookup the config | |
| 67 | * value pluginID + ".class". | |
| 68 | * @param defaultPlugin The default plugin class for this plugin. | |
| 69 | * @return The plugin object. | |
| 70 | */ | |
| 71 | public static Object initPlugin( | |
| 72 | Class rootClass, | |
| 73 | String pluginID, | |
| 74 | String defaultPlugin) { | |
| 75 | ||
| 76 | 9 | Config config = ConfigFactory.getConfig(rootClass); |
| 77 | 9 | String pluginClass = config.get(pluginID + ".class", defaultPlugin); |
| 78 | try { | |
| 79 | 9 | Object plugin = Class.forName(pluginClass).newInstance(); |
| 80 | 6 | if (!rootClass.isInstance(plugin)) { |
| 81 | 1 | Object[] args = { pluginID, pluginClass, rootClass.getName()}; |
| 82 | 1 | throw genEx(MESSAGE_INVALID_PLUGIN_TYPE, args); |
| 83 | } | |
| 84 | 5 | return plugin; |
| 85 | } catch (ClassNotFoundException ex) { | |
| 86 | 1 | Object[] args = { pluginID, pluginClass }; |
| 87 | 1 | throw genEx(MESSAGE_PLUGIN_CLASS_NOT_FOUND, args); |
| 88 | } catch (IllegalAccessException ex) { | |
| 89 | 1 | Object[] args = { pluginID, pluginClass }; |
| 90 | 1 | throw genEx(MESSAGE_NO_PUBLIC_PLUGIN_CONSTRUCTOR, args); |
| 91 | } catch (InstantiationException ex) { | |
| 92 | 0 | Object[] args = { pluginID, pluginClass, ex }; |
| 93 | 0 | throw genEx(MESSAGE_PLUGIN_CONSTRUCTOR_FAILED, args); |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Construct error message. | |
| 99 | * | |
| 100 | * @param msg The message. | |
| 101 | * @param args The message arguments. | |
| 102 | * @return The exception containing the message. | |
| 103 | */ | |
| 104 | private static IllegalStateException genEx(String msg, Object[] args) { | |
| 105 | 3 | Message message = Message.getMessage(msg, args, Plugin.class); |
| 106 | 3 | return new IllegalStateException(message.toString()); |
| 107 | } | |
| 108 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |