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.io.IOException; |
---|
28 | import java.lang.ref.WeakReference; |
---|
29 | import java.lang.reflect.Method; |
---|
30 | import java.net.MalformedURLException; |
---|
31 | import java.net.URL; |
---|
32 | import java.text.MessageFormat; |
---|
33 | import java.util.*; |
---|
34 | |
---|
35 | import javax.swing.UIManager; |
---|
36 | import javax.xml.parsers.SAXParser; |
---|
37 | |
---|
38 | import nu.fw.jeti.backend.Start; |
---|
39 | import nu.fw.jeti.backend.URLClassloader; |
---|
40 | import nu.fw.jeti.jabber.Backend; |
---|
41 | import nu.fw.jeti.util.I18N; |
---|
42 | import nu.fw.jeti.util.Preferences; |
---|
43 | |
---|
44 | /** |
---|
45 | * @author E.S. de Boer |
---|
46 | * |
---|
47 | */ |
---|
48 | public class PluginsInfo |
---|
49 | { |
---|
50 | private static URLClassloader classLoader;//change back to private |
---|
51 | private static Map loadedPlugins = new HashMap(); |
---|
52 | public static Map loadedPreferencePanels = new HashMap(); |
---|
53 | private static Backend backend; |
---|
54 | private static Map pluginInstances = new HashMap(); |
---|
55 | |
---|
56 | public PluginsInfo(Backend backend,SAXParser parser,Start start) |
---|
57 | { |
---|
58 | PluginsInfo.backend = backend; |
---|
59 | |
---|
60 | if(!Start.applet) |
---|
61 | { |
---|
62 | classLoader = new URLClassloader(new URL[]{Start.programURL}, getClass().getClassLoader()); |
---|
63 | UIManager.put("ClassLoader", classLoader); |
---|
64 | } |
---|
65 | new PluginData(parser); |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | // nu.fw.jeti.plugins.xmppuri.Plugin.init(backend); |
---|
70 | |
---|
71 | // loadedPlugins.put("xmpp",nu.fw.jeti.plugins.xmpp.Plugin.class); |
---|
72 | // nu.fw.jeti.plugins.xmpp.Plugin.init(backend); |
---|
73 | // loadedPlugins.put("compression",nu.fw.jeti.plugins.compression.Plugin.class); |
---|
74 | // nu.fw.jeti.plugins.compression.Plugin.init(backend); |
---|
75 | |
---|
76 | // loadedPreferencePanels.put("groupchatalerter",nu.fw.jeti.plugins.groupchatalerter.PrefPanel.class); |
---|
77 | // loadedPlugins.put("groupchatalerter",nu.fw.jeti.plugins.groupchatalerter.Plugin.class); |
---|
78 | // nu.fw.jeti.plugins.groupchatalerter.Plugin.init(backend); |
---|
79 | // nu.fw.jeti.plugins.groupchat.Plugin.init(backend); |
---|
80 | |
---|
81 | //new test.QueryServers(backend); |
---|
82 | // |
---|
83 | // nu.fw.jeti.plugins.pubsub.Plugin.init(backend); |
---|
84 | // nu.fw.jeti.plugins.useractivity.Plugin.init(backend); |
---|
85 | // |
---|
86 | // nu.fw.jeti.plugins.jingle.Plugin.init(backend); |
---|
87 | |
---|
88 | |
---|
89 | // try |
---|
90 | // { |
---|
91 | // nu.fw.jeti.plugins.tts.Plugin.init(backend); |
---|
92 | // }catch (Exception e){ |
---|
93 | // e.printStackTrace(); |
---|
94 | // } |
---|
95 | |
---|
96 | // nu.fw.jeti.plugins.vcard.Plugin.init(backend); |
---|
97 | |
---|
98 | // nu.fw.jeti.plugins.ichatavatars.Plugin.init(backend); |
---|
99 | // loadedPlugins.put("ichatavatars", nu.fw.jeti.plugins.ichatavatars.Plugin.class); |
---|
100 | |
---|
101 | // nu.fw.jeti.plugins.spell.Plugin.init(backend); |
---|
102 | // loadedPlugins.put("spell", nu.fw.jeti.plugins.spell.Plugin.class); |
---|
103 | // loadedPreferencePanels.put("spell",nu.fw.jeti.plugins.spell.PrefPanel.class); |
---|
104 | |
---|
105 | // nu.fw.jeti.plugins.metaltheme.Plugin.init(backend); |
---|
106 | // loadedPreferencePanels.put("metaltheme",nu.fw.jeti.plugins.metaltheme.PrefPanel.class); |
---|
107 | |
---|
108 | // nu.fw.jeti.plugins.filetransfer.Plugin.init(backend); |
---|
109 | // loadedPreferencePanels.put("filetransfer",nu.fw.jeti.plugins.filetransfer.PrefPanel.class); |
---|
110 | // |
---|
111 | // nu.fw.jeti.plugins.sound.Plugin.init(backend); |
---|
112 | // loadedPreferencePanels.put("sound",nu.fw.jeti.plugins.sound.PrefPanel.class); |
---|
113 | // |
---|
114 | // nu.fw.jeti.plugins.xhtml.Plugin.init(backend); |
---|
115 | // loadedPlugins.put("xhtml", nu.fw.jeti.plugins.xhtml.Plugin.class); |
---|
116 | |
---|
117 | // } |
---|
118 | // catch (Exception e) |
---|
119 | // { |
---|
120 | // // TOD Auto-generated catch block |
---|
121 | // e.printStackTrace(); |
---|
122 | // } |
---|
123 | |
---|
124 | // nu.fw.jeti.plugins.search.Plugin.init(backend); |
---|
125 | |
---|
126 | // nu.fw.jeti.plugins.buddyspacemaps.Plugin.init(backend); |
---|
127 | |
---|
128 | |
---|
129 | // |
---|
130 | // nu.fw.jeti.plugins.openpgp.Plugin.init(backend); |
---|
131 | // loadedPlugins.put("openpgp", nu.fw.jeti.plugins.openpgp.Plugin.class); |
---|
132 | |
---|
133 | //nu.fw.jeti.plugins.translators.breezah.Plugin.init(backend); |
---|
134 | |
---|
135 | //new nu.fw.jeti.plugins.logtoserver.Plugin(backend); |
---|
136 | //new nu.fw.jeti.plugins.servertolog.Plugin(backend); |
---|
137 | // |
---|
138 | // try |
---|
139 | // { |
---|
140 | // nu.fw.jeti.plugins.emoticons.Plugin.init(backend); |
---|
141 | // } catch (IOException e) |
---|
142 | // { |
---|
143 | // e.printStackTrace(); |
---|
144 | // } |
---|
145 | // loadedPlugins.put("emoticons", nu.fw.jeti.plugins.emoticons.Plugin.class); |
---|
146 | // loadedPreferencePanels.put("emoticons",nu.fw.jeti.plugins.emoticons.PrefPanel.class); |
---|
147 | |
---|
148 | // nu.fw.jeti.plugins.alertwindow.Plugin.init(backend); |
---|
149 | // loadedPreferencePanels.put("alertwindow",nu.fw.jeti.plugins.alertwindow.PrefPanel.class); |
---|
150 | |
---|
151 | //start.LoadPlugins(urlString); |
---|
152 | for (Iterator i = Preferences.getPlugins().iterator(); i.hasNext();) |
---|
153 | { |
---|
154 | Object[] temp = (Object[]) i.next(); |
---|
155 | if (temp[2] == null)continue; //missing plugin |
---|
156 | if (((Boolean) temp[1]).booleanValue()) |
---|
157 | { |
---|
158 | start.setSplashText((String) temp[0]); |
---|
159 | loadPlugin((String) temp[0]); |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | //translator hackje vervang door translator interfaces as in io |
---|
165 | private static Translator translator; |
---|
166 | |
---|
167 | public static void setTranslator(Translator translator) |
---|
168 | { |
---|
169 | PluginsInfo.translator = translator; |
---|
170 | } |
---|
171 | |
---|
172 | public static Translator getTranslator() |
---|
173 | { |
---|
174 | return translator; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | public static boolean isPluginLoaded(String name) |
---|
179 | { |
---|
180 | return loadedPlugins.containsKey(name); |
---|
181 | } |
---|
182 | |
---|
183 | public static void loadPlugin(String name) |
---|
184 | { |
---|
185 | Class loadedClass =null; |
---|
186 | if (!Start.applet) |
---|
187 | {//non applet version |
---|
188 | URL url =null;// new URL[1]; |
---|
189 | try |
---|
190 | { |
---|
191 | //if (Start.programURL != null) |
---|
192 | url = new URL(Start.programURL, "plugins/" + name + ".jar"); |
---|
193 | //else url = new URL(Start.localURL, "plugins/" + name + ".jar"); |
---|
194 | } |
---|
195 | catch (MalformedURLException e) |
---|
196 | {} |
---|
197 | //System.out.println(url); |
---|
198 | //URLClassLoader loader = new URLClassLoader(url, parentClassLoader); |
---|
199 | classLoader.addURL(url); |
---|
200 | try |
---|
201 | { |
---|
202 | loadedClass = classLoader.loadClass("nu.fw.jeti.plugins." + name + ".Plugin"); |
---|
203 | } |
---|
204 | catch (ClassNotFoundException e) |
---|
205 | { |
---|
206 | System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_plugin_not_found"), new Object[]{name})); |
---|
207 | return; |
---|
208 | } |
---|
209 | } |
---|
210 | else loadedClass = nu.fw.jeti.applet.Jeti.getPlugin(name); |
---|
211 | |
---|
212 | |
---|
213 | try |
---|
214 | {//init plugin |
---|
215 | Method m = loadedClass.getMethod("init",new Class[]{Backend.class}); |
---|
216 | m.invoke(null,new Object[]{backend}); |
---|
217 | }catch(Exception e) |
---|
218 | { |
---|
219 | e.printStackTrace(); |
---|
220 | //System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_plugin_failed_to_load"), new Object[]{name})); |
---|
221 | return; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | // if (!loadOnFirstUse) |
---|
226 | // { |
---|
227 | // |
---|
228 | // try |
---|
229 | // { |
---|
230 | // Constructor c = loadedClass.getConstructor(new Class[] { Class.forName("nu.fw.jeti.jabber.Backend")}); |
---|
231 | // addInstance(name, new WeakReference(c.newInstance(new Object[] { backend }))); |
---|
232 | // } |
---|
233 | // catch (InvocationTargetException ie) |
---|
234 | // { |
---|
235 | // ie.printStackTrace(); |
---|
236 | // //System.out.println("Error while initializing " + name + " : " + ie.getCause().getMessage()); |
---|
237 | // return; |
---|
238 | // } |
---|
239 | // catch (Exception e2) |
---|
240 | // { |
---|
241 | // e2.printStackTrace(); |
---|
242 | // return; |
---|
243 | // } |
---|
244 | // } |
---|
245 | |
---|
246 | loadedPlugins.put(name, loadedClass); |
---|
247 | if(!Start.applet) |
---|
248 | { |
---|
249 | try |
---|
250 | { |
---|
251 | loadedPreferencePanels.put(name, classLoader.loadClass("nu.fw.jeti.plugins." + name + ".PrefPanel")); |
---|
252 | } |
---|
253 | catch (ClassNotFoundException e) |
---|
254 | { |
---|
255 | // Do nothing |
---|
256 | } |
---|
257 | } |
---|
258 | else nu.fw.jeti.applet.Jeti.getPrefPanel(name,loadedPreferencePanels); |
---|
259 | //System.out.println(name + "loaded"); |
---|
260 | } |
---|
261 | |
---|
262 | public static void loadLibrary(String name) |
---|
263 | { |
---|
264 | URL url =null; |
---|
265 | try |
---|
266 | { |
---|
267 | url = new URL(Start.programURL, "lib/" + name + ".jar"); |
---|
268 | } |
---|
269 | catch (MalformedURLException e) |
---|
270 | {} |
---|
271 | classLoader.addURL(url); |
---|
272 | } |
---|
273 | |
---|
274 | public static void unloadPlugin(String name) |
---|
275 | { |
---|
276 | Class loadedClass = (Class)loadedPlugins.remove(name); |
---|
277 | if (loadedClass == null) { |
---|
278 | return; |
---|
279 | } |
---|
280 | System.out.println("removing "+ name); |
---|
281 | loadedPreferencePanels.remove(name); |
---|
282 | List list = (List) pluginInstances.remove(name); |
---|
283 | try |
---|
284 | {//unload cleaning |
---|
285 | Method m = loadedClass.getMethod("unload",new Class[]{Backend.class}); |
---|
286 | m.invoke(null,new Object[]{backend}); |
---|
287 | }catch(NoSuchMethodException e){System.out.println(name + " has no remove");} |
---|
288 | catch(Exception e) |
---|
289 | { |
---|
290 | e.printStackTrace(); |
---|
291 | } |
---|
292 | |
---|
293 | if (list == null) return; |
---|
294 | for (Iterator i = list.iterator(); i.hasNext();) |
---|
295 | {//unload loaded plugins |
---|
296 | Plugins plugin = (Plugins)((WeakReference)i.next()).get(); |
---|
297 | if (plugin !=null)plugin.unload(); |
---|
298 | } |
---|
299 | System.out.println(name +" removed"); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | /** |
---|
304 | * Gets a single instance of a plugin, the plugin has to have the static getInstance() method |
---|
305 | * @param name name of the plugin |
---|
306 | * @return instance of the plugin |
---|
307 | */ |
---|
308 | public static Object getPluginInstance(String name) |
---|
309 | { |
---|
310 | Object o = null; |
---|
311 | try |
---|
312 | { |
---|
313 | Class loadedClass = (Class) loadedPlugins.get(name); |
---|
314 | try |
---|
315 | { |
---|
316 | Method m = loadedClass.getMethod("getInstance",null); |
---|
317 | o = m.invoke(null,null); |
---|
318 | }catch(NoSuchMethodException e){System.out.println(name + " has no instance method");} |
---|
319 | catch(Exception e) |
---|
320 | { |
---|
321 | e.printStackTrace(); |
---|
322 | } |
---|
323 | } |
---|
324 | catch (Exception ie) |
---|
325 | { |
---|
326 | ie.printStackTrace(); |
---|
327 | //System.out.println("Error while initializing " + name + " : " + ie.getCause().getMessage()); |
---|
328 | return null; |
---|
329 | } |
---|
330 | return o; |
---|
331 | } |
---|
332 | |
---|
333 | public static Plugins newPluginInstance(String name) |
---|
334 | { |
---|
335 | Object o = null; |
---|
336 | try |
---|
337 | { |
---|
338 | o = ((Class) loadedPlugins.get(name)).newInstance(); |
---|
339 | addInstance(name,o); |
---|
340 | } |
---|
341 | catch (Exception ie) |
---|
342 | { |
---|
343 | ie.printStackTrace(); |
---|
344 | //System.out.println("Error while initializing " + name + " : " + ie.getCause().getMessage()); |
---|
345 | return null; |
---|
346 | } |
---|
347 | return (Plugins) o; |
---|
348 | } |
---|
349 | |
---|
350 | private static void addInstance(String name, Object object) |
---|
351 | { |
---|
352 | List list = (List) pluginInstances.get(name); |
---|
353 | if (list == null) |
---|
354 | { |
---|
355 | list = new LinkedList(); |
---|
356 | pluginInstances.put(name, list); |
---|
357 | } |
---|
358 | list.add(new WeakReference(object)); |
---|
359 | } |
---|
360 | |
---|
361 | public static String getAbout() |
---|
362 | { |
---|
363 | StringBuffer buffer = new StringBuffer(); |
---|
364 | for(Iterator i = loadedPlugins.entrySet().iterator();i.hasNext();) |
---|
365 | { |
---|
366 | Map.Entry entry = (Map.Entry)i.next(); |
---|
367 | String text = null; |
---|
368 | try |
---|
369 | { |
---|
370 | text = (String) ((Class)entry.getValue()).getField("ABOUT").get(null); |
---|
371 | } |
---|
372 | catch (Exception e1) |
---|
373 | {} |
---|
374 | if(text!=null) buffer.append("\n - " + (String)entry.getKey() + '\n' + text); |
---|
375 | } |
---|
376 | return buffer.toString(); |
---|
377 | } |
---|
378 | |
---|
379 | //remove plugins |
---|
380 | public void exit() |
---|
381 | { |
---|
382 | String[] temp=new String[loadedPlugins.size()]; |
---|
383 | loadedPlugins.keySet().toArray(temp); |
---|
384 | for(int i=0;i<temp.length;i++) |
---|
385 | { |
---|
386 | unloadPlugin(temp[i]); |
---|
387 | } |
---|
388 | System.out.println("plugins unloaded"); |
---|
389 | } |
---|
390 | } |
---|
391 | /* |
---|
392 | * Overrides for emacs |
---|
393 | * Local variables: |
---|
394 | * tab-width: 4 |
---|
395 | * End: |
---|
396 | */ |
---|