[1014] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | * For questions, comments etc, |
---|
| 19 | * use the website at http://jeti.jabberstudio.org |
---|
| 20 | * or mail/IM me at jeti@jabber.org |
---|
| 21 | * |
---|
| 22 | * Created on 26-dec-2003 |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | package nu.fw.jeti.plugins; |
---|
| 26 | |
---|
| 27 | import java.lang.ref.WeakReference; |
---|
| 28 | import java.lang.reflect.Method; |
---|
| 29 | import java.text.MessageFormat; |
---|
| 30 | import java.util.*; |
---|
| 31 | import javax.xml.parsers.SAXParser; |
---|
| 32 | import nu.fw.jeti.backend.Start; |
---|
| 33 | import nu.fw.jeti.jabber.Backend; |
---|
| 34 | import nu.fw.jeti.util.I18N; |
---|
| 35 | import nu.fw.jeti.util.Preferences; |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * @author E.S. de Boer |
---|
| 39 | * |
---|
| 40 | */ |
---|
| 41 | public class PluginsInfo |
---|
| 42 | { |
---|
[1097] | 43 | private static Backend backend; |
---|
[1014] | 44 | private static Map loadedPlugins = new HashMap(); |
---|
| 45 | public static Map loadedPreferencePanels = new HashMap(); |
---|
| 46 | private static Map pluginInstances = new HashMap(); |
---|
[1097] | 47 | private static String pluginsJava; |
---|
[1014] | 48 | |
---|
[1097] | 49 | public PluginsInfo( Backend backend, Start start ) |
---|
[1014] | 50 | { |
---|
[1097] | 51 | PluginsInfo.backend = backend; |
---|
| 52 | PluginsInfo.pluginsJava = nu.fw.jeti.applet.Jeti.JAVAPLUGINS; |
---|
[1014] | 53 | |
---|
[1097] | 54 | /** |
---|
| 55 | * Carregando plugins: |
---|
| 56 | * |
---|
| 57 | * nu.fw.jeti.plugins.<NOME_do_Plugin>.Plugin.init( backend ); |
---|
| 58 | * loadedPlugins.put("<NOME_do_Plugin>", nu.fw.jeti.plugins.<NOME_do_Plugin>.Plugin.class); |
---|
| 59 | * |
---|
| 60 | * Exemplo : Plugin xhtml |
---|
| 61 | * nu.fw.jeti.plugins.xhtml.Plugin.init( backend ); |
---|
| 62 | * loadedPlugins.put("xhtml", nu.fw.jeti.plugins.xhtml.Plugin.class); |
---|
| 63 | */ |
---|
[1014] | 64 | |
---|
[1097] | 65 | String[] plugins = pluginsJava.split(","); |
---|
[1014] | 66 | |
---|
[1097] | 67 | for(int i = 0 ; i < plugins.length; i++) |
---|
| 68 | PluginsInfo.loadPlugin(plugins[i].toString()); |
---|
[1014] | 69 | } |
---|
[1097] | 70 | |
---|
[1014] | 71 | public static boolean isPluginLoaded(String name) |
---|
| 72 | { |
---|
| 73 | return loadedPlugins.containsKey(name); |
---|
| 74 | } |
---|
| 75 | |
---|
[1097] | 76 | private static void loadPlugin(String name) |
---|
[1014] | 77 | { |
---|
[1097] | 78 | Class loadedClass = nu.fw.jeti.applet.Jeti.getPlugin(name); |
---|
[1014] | 79 | |
---|
| 80 | try |
---|
[1097] | 81 | { |
---|
| 82 | //Init plugin |
---|
[1014] | 83 | Method m = loadedClass.getMethod("init",new Class[]{Backend.class}); |
---|
| 84 | m.invoke(null,new Object[]{backend}); |
---|
[1097] | 85 | } |
---|
| 86 | catch(Exception e) |
---|
[1014] | 87 | { |
---|
| 88 | e.printStackTrace(); |
---|
| 89 | return; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | loadedPlugins.put(name, loadedClass); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | public static void unloadPlugin(String name) |
---|
| 96 | { |
---|
| 97 | Class loadedClass = (Class)loadedPlugins.remove(name); |
---|
[1097] | 98 | |
---|
| 99 | if (loadedClass == null) |
---|
| 100 | return; |
---|
| 101 | |
---|
| 102 | System.out.println(I18N.gettext("main.pluginsInfo.Plugin_removing") + " : " + name); |
---|
| 103 | |
---|
| 104 | loadedPreferencePanels.remove(name); |
---|
| 105 | |
---|
[1014] | 106 | List list = (List) pluginInstances.remove(name); |
---|
[1097] | 107 | |
---|
[1014] | 108 | try |
---|
[1097] | 109 | { |
---|
| 110 | //unload cleaning |
---|
[1014] | 111 | Method m = loadedClass.getMethod("unload",new Class[]{Backend.class}); |
---|
| 112 | m.invoke(null,new Object[]{backend}); |
---|
[1097] | 113 | } |
---|
| 114 | catch(NoSuchMethodException e) |
---|
| 115 | { |
---|
| 116 | System.out.println("ERRO : " + name + " " + I18N.gettext("main.pluginsInfo.has_no_remove")); |
---|
| 117 | } |
---|
[1014] | 118 | catch(Exception e) |
---|
| 119 | { |
---|
| 120 | e.printStackTrace(); |
---|
| 121 | } |
---|
| 122 | |
---|
[1097] | 123 | if ( list == null) |
---|
| 124 | return; |
---|
| 125 | |
---|
[1014] | 126 | for (Iterator i = list.iterator(); i.hasNext();) |
---|
[1097] | 127 | { |
---|
| 128 | //unload loaded plugins |
---|
[1014] | 129 | Plugins plugin = (Plugins)((WeakReference)i.next()).get(); |
---|
[1097] | 130 | |
---|
| 131 | if ( plugin != null ) |
---|
| 132 | plugin.unload(); |
---|
[1014] | 133 | } |
---|
[1097] | 134 | |
---|
| 135 | System.out.println( "Removed plugin : " + name ); |
---|
[1014] | 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Gets a single instance of a plugin, the plugin has to have the static getInstance() method |
---|
| 140 | * @param name name of the plugin |
---|
| 141 | * @return instance of the plugin |
---|
| 142 | */ |
---|
| 143 | public static Object getPluginInstance(String name) |
---|
| 144 | { |
---|
| 145 | Object o = null; |
---|
| 146 | try |
---|
| 147 | { |
---|
| 148 | Class loadedClass = (Class) loadedPlugins.get(name); |
---|
| 149 | try |
---|
| 150 | { |
---|
| 151 | Method m = loadedClass.getMethod("getInstance",null); |
---|
| 152 | o = m.invoke(null,null); |
---|
[1097] | 153 | } |
---|
| 154 | catch(NoSuchMethodException e) |
---|
| 155 | { |
---|
| 156 | System.out.println(name + " has no instance method"); |
---|
| 157 | } |
---|
[1014] | 158 | catch(Exception e) |
---|
| 159 | { |
---|
| 160 | e.printStackTrace(); |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | catch (Exception ie) |
---|
| 164 | { |
---|
| 165 | ie.printStackTrace(); |
---|
| 166 | return null; |
---|
| 167 | } |
---|
[1097] | 168 | |
---|
[1014] | 169 | return o; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | public static Plugins newPluginInstance(String name) |
---|
| 173 | { |
---|
| 174 | Object o = null; |
---|
[1097] | 175 | |
---|
[1014] | 176 | try |
---|
| 177 | { |
---|
| 178 | o = ((Class) loadedPlugins.get(name)).newInstance(); |
---|
| 179 | addInstance(name,o); |
---|
| 180 | } |
---|
| 181 | catch (Exception ie) |
---|
| 182 | { |
---|
| 183 | ie.printStackTrace(); |
---|
| 184 | return null; |
---|
| 185 | } |
---|
[1097] | 186 | |
---|
[1014] | 187 | return (Plugins) o; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | private static void addInstance(String name, Object object) |
---|
| 191 | { |
---|
| 192 | List list = (List) pluginInstances.get(name); |
---|
[1097] | 193 | |
---|
[1014] | 194 | if (list == null) |
---|
| 195 | { |
---|
| 196 | list = new LinkedList(); |
---|
| 197 | pluginInstances.put(name, list); |
---|
| 198 | } |
---|
[1097] | 199 | |
---|
[1014] | 200 | list.add(new WeakReference(object)); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | public static String getAbout() |
---|
| 204 | { |
---|
| 205 | StringBuffer buffer = new StringBuffer(); |
---|
[1097] | 206 | |
---|
[1014] | 207 | for(Iterator i = loadedPlugins.entrySet().iterator();i.hasNext();) |
---|
| 208 | { |
---|
| 209 | Map.Entry entry = (Map.Entry)i.next(); |
---|
| 210 | String text = null; |
---|
| 211 | try |
---|
| 212 | { |
---|
| 213 | text = (String) ((Class)entry.getValue()).getField("ABOUT").get(null); |
---|
| 214 | } |
---|
[1097] | 215 | catch (Exception e1){} |
---|
| 216 | |
---|
| 217 | if( text != null) |
---|
| 218 | buffer.append("\n - " + (String)entry.getKey() + '\n' + text); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | return buffer.toString(); |
---|
[1014] | 222 | } |
---|
| 223 | |
---|
[1097] | 224 | // Remove plugins |
---|
[1014] | 225 | public void exit() |
---|
| 226 | { |
---|
[1097] | 227 | String[] temp = new String[loadedPlugins.size()]; |
---|
[1014] | 228 | loadedPlugins.keySet().toArray(temp); |
---|
[1097] | 229 | |
---|
[1014] | 230 | for(int i=0;i<temp.length;i++) |
---|
| 231 | { |
---|
| 232 | unloadPlugin(temp[i]); |
---|
| 233 | } |
---|
[1097] | 234 | |
---|
| 235 | System.out.println(I18N.gettext("main.pluginsInfo.plugins_unloaded")); |
---|
[1014] | 236 | } |
---|
| 237 | } |
---|
[1097] | 238 | |
---|
[1014] | 239 | /* |
---|
| 240 | * Overrides for emacs |
---|
| 241 | * Local variables: |
---|
| 242 | * tab-width: 4 |
---|
| 243 | * End: |
---|
| 244 | */ |
---|