/* * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org * or mail/IM me at jeti@jabber.org * * Created on 26-dec-2003 */ package nu.fw.jeti.plugins; import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.text.MessageFormat; import java.util.*; import javax.xml.parsers.SAXParser; import nu.fw.jeti.backend.Start; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Preferences; /** * @author E.S. de Boer * */ public class PluginsInfo { private static Backend backend; private static Map loadedPlugins = new HashMap(); public static Map loadedPreferencePanels = new HashMap(); private static Map pluginInstances = new HashMap(); private static String pluginsJava; public PluginsInfo( Backend backend, Start start ) { PluginsInfo.backend = backend; PluginsInfo.pluginsJava = nu.fw.jeti.applet.Jeti.JAVAPLUGINS; /** * Carregando plugins: * * nu.fw.jeti.plugins..Plugin.init( backend ); * loadedPlugins.put("", nu.fw.jeti.plugins..Plugin.class); * * Exemplo : Plugin xhtml * nu.fw.jeti.plugins.xhtml.Plugin.init( backend ); * loadedPlugins.put("xhtml", nu.fw.jeti.plugins.xhtml.Plugin.class); */ String[] plugins = pluginsJava.split(","); for(int i = 0 ; i < plugins.length; i++) PluginsInfo.loadPlugin(plugins[i].toString()); } public static boolean isPluginLoaded(String name) { return loadedPlugins.containsKey(name); } private static void loadPlugin(String name) { Class loadedClass = nu.fw.jeti.applet.Jeti.getPlugin(name); try { //Init plugin Method m = loadedClass.getMethod("init",new Class[]{Backend.class}); m.invoke(null,new Object[]{backend}); } catch(Exception e) { e.printStackTrace(); return; } loadedPlugins.put(name, loadedClass); } public static void unloadPlugin(String name) { Class loadedClass = (Class)loadedPlugins.remove(name); if (loadedClass == null) return; System.out.println(I18N.gettext("main.pluginsInfo.Plugin_removing") + " : " + name); loadedPreferencePanels.remove(name); List list = (List) pluginInstances.remove(name); try { //unload cleaning Method m = loadedClass.getMethod("unload",new Class[]{Backend.class}); m.invoke(null,new Object[]{backend}); } catch(NoSuchMethodException e) { System.out.println("ERRO : " + name + " " + I18N.gettext("main.pluginsInfo.has_no_remove")); } catch(Exception e) { e.printStackTrace(); } if ( list == null) return; for (Iterator i = list.iterator(); i.hasNext();) { //unload loaded plugins Plugins plugin = (Plugins)((WeakReference)i.next()).get(); if ( plugin != null ) plugin.unload(); } System.out.println( "Removed plugin : " + name ); } /** * Gets a single instance of a plugin, the plugin has to have the static getInstance() method * @param name name of the plugin * @return instance of the plugin */ public static Object getPluginInstance(String name) { Object o = null; try { Class loadedClass = (Class) loadedPlugins.get(name); try { Method m = loadedClass.getMethod("getInstance",null); o = m.invoke(null,null); } catch(NoSuchMethodException e) { System.out.println(name + " has no instance method"); } catch(Exception e) { e.printStackTrace(); } } catch (Exception ie) { ie.printStackTrace(); return null; } return o; } public static Plugins newPluginInstance(String name) { Object o = null; try { o = ((Class) loadedPlugins.get(name)).newInstance(); addInstance(name,o); } catch (Exception ie) { ie.printStackTrace(); return null; } return (Plugins) o; } private static void addInstance(String name, Object object) { List list = (List) pluginInstances.get(name); if (list == null) { list = new LinkedList(); pluginInstances.put(name, list); } list.add(new WeakReference(object)); } public static String getAbout() { StringBuffer buffer = new StringBuffer(); for(Iterator i = loadedPlugins.entrySet().iterator();i.hasNext();) { Map.Entry entry = (Map.Entry)i.next(); String text = null; try { text = (String) ((Class)entry.getValue()).getField("ABOUT").get(null); } catch (Exception e1){} if( text != null) buffer.append("\n - " + (String)entry.getKey() + '\n' + text); } return buffer.toString(); } // Remove plugins public void exit() { String[] temp = new String[loadedPlugins.size()]; loadedPlugins.keySet().toArray(temp); for(int i=0;i