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 | { |
---|
43 | private static Backend backend; |
---|
44 | private static Map loadedPlugins = new HashMap(); |
---|
45 | public static Map loadedPreferencePanels = new HashMap(); |
---|
46 | private static Map pluginInstances = new HashMap(); |
---|
47 | private static String pluginsJava; |
---|
48 | |
---|
49 | public PluginsInfo( Backend backend, Start start ) |
---|
50 | { |
---|
51 | PluginsInfo.backend = backend; |
---|
52 | PluginsInfo.pluginsJava = nu.fw.jeti.applet.Jeti.JAVAPLUGINS; |
---|
53 | |
---|
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 | */ |
---|
64 | |
---|
65 | String[] plugins = pluginsJava.split(","); |
---|
66 | |
---|
67 | for(int i = 0 ; i < plugins.length; i++) |
---|
68 | PluginsInfo.loadPlugin(plugins[i].toString()); |
---|
69 | } |
---|
70 | |
---|
71 | public static boolean isPluginLoaded(String name) |
---|
72 | { |
---|
73 | return loadedPlugins.containsKey(name); |
---|
74 | } |
---|
75 | |
---|
76 | private static void loadPlugin(String name) |
---|
77 | { |
---|
78 | Class loadedClass = nu.fw.jeti.applet.Jeti.getPlugin(name); |
---|
79 | |
---|
80 | try |
---|
81 | { |
---|
82 | //Init plugin |
---|
83 | Method m = loadedClass.getMethod("init",new Class[]{Backend.class}); |
---|
84 | m.invoke(null,new Object[]{backend}); |
---|
85 | } |
---|
86 | catch(Exception e) |
---|
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); |
---|
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 | |
---|
106 | List list = (List) pluginInstances.remove(name); |
---|
107 | |
---|
108 | try |
---|
109 | { |
---|
110 | //unload cleaning |
---|
111 | Method m = loadedClass.getMethod("unload",new Class[]{Backend.class}); |
---|
112 | m.invoke(null,new Object[]{backend}); |
---|
113 | } |
---|
114 | catch(NoSuchMethodException e) |
---|
115 | { |
---|
116 | System.out.println("ERRO : " + name + " " + I18N.gettext("main.pluginsInfo.has_no_remove")); |
---|
117 | } |
---|
118 | catch(Exception e) |
---|
119 | { |
---|
120 | e.printStackTrace(); |
---|
121 | } |
---|
122 | |
---|
123 | if ( list == null) |
---|
124 | return; |
---|
125 | |
---|
126 | for (Iterator i = list.iterator(); i.hasNext();) |
---|
127 | { |
---|
128 | //unload loaded plugins |
---|
129 | Plugins plugin = (Plugins)((WeakReference)i.next()).get(); |
---|
130 | |
---|
131 | if ( plugin != null ) |
---|
132 | plugin.unload(); |
---|
133 | } |
---|
134 | |
---|
135 | System.out.println( "Removed plugin : " + name ); |
---|
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); |
---|
153 | } |
---|
154 | catch(NoSuchMethodException e) |
---|
155 | { |
---|
156 | System.out.println(name + " has no instance method"); |
---|
157 | } |
---|
158 | catch(Exception e) |
---|
159 | { |
---|
160 | e.printStackTrace(); |
---|
161 | } |
---|
162 | } |
---|
163 | catch (Exception ie) |
---|
164 | { |
---|
165 | ie.printStackTrace(); |
---|
166 | return null; |
---|
167 | } |
---|
168 | |
---|
169 | return o; |
---|
170 | } |
---|
171 | |
---|
172 | public static Plugins newPluginInstance(String name) |
---|
173 | { |
---|
174 | Object o = null; |
---|
175 | |
---|
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 | } |
---|
186 | |
---|
187 | return (Plugins) o; |
---|
188 | } |
---|
189 | |
---|
190 | private static void addInstance(String name, Object object) |
---|
191 | { |
---|
192 | List list = (List) pluginInstances.get(name); |
---|
193 | |
---|
194 | if (list == null) |
---|
195 | { |
---|
196 | list = new LinkedList(); |
---|
197 | pluginInstances.put(name, list); |
---|
198 | } |
---|
199 | |
---|
200 | list.add(new WeakReference(object)); |
---|
201 | } |
---|
202 | |
---|
203 | public static String getAbout() |
---|
204 | { |
---|
205 | StringBuffer buffer = new StringBuffer(); |
---|
206 | |
---|
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 | } |
---|
215 | catch (Exception e1){} |
---|
216 | |
---|
217 | if( text != null) |
---|
218 | buffer.append("\n - " + (String)entry.getKey() + '\n' + text); |
---|
219 | } |
---|
220 | |
---|
221 | return buffer.toString(); |
---|
222 | } |
---|
223 | |
---|
224 | // Remove plugins |
---|
225 | public void exit() |
---|
226 | { |
---|
227 | String[] temp = new String[loadedPlugins.size()]; |
---|
228 | loadedPlugins.keySet().toArray(temp); |
---|
229 | |
---|
230 | for(int i=0;i<temp.length;i++) |
---|
231 | { |
---|
232 | unloadPlugin(temp[i]); |
---|
233 | } |
---|
234 | |
---|
235 | System.out.println(I18N.gettext("main.pluginsInfo.plugins_unloaded")); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | /* |
---|
240 | * Overrides for emacs |
---|
241 | * Local variables: |
---|
242 | * tab-width: 4 |
---|
243 | * End: |
---|
244 | */ |
---|