Source code: org/altara/mars/plugin/PluginRegistry.java
1 /* MARS Extension Framework
2 Copyright (C) 2002 Leapfrog Research & Development, LLC
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (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, it is available at
16 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21 package org.altara.mars.plugin;
22
23 import org.altara.util.*;
24 import org.altara.mars.*;
25 import org.altara.mars.engine.*;
26 import java.util.*;
27 import java.io.*;
28 import org.jdom.*;
29
30 /** Manages the extension framework. PluginRegistry handles loading
31 and configuration of plugins dynamically.
32 */
33
34 public class PluginRegistry {
35
36 public static final String PLUGIN_JAR_PREFIX =
37 "plugin_";
38 public static final String PLUGIN_MANIFEST_KEY =
39 "MarsPluginClass";
40
41 private LinkedList plugins;
42
43 public PluginRegistry() {
44 plugins = new LinkedList();
45 }
46
47 public void registerPlugin(Plugin plugin) {
48 Controller controller = Main.getMain().getController();
49 // connect the plugin to the controller
50 if (plugin instanceof NotificationListener) {
51 controller.addNotificationListener(
52 (NotificationListener)plugin);
53 }
54 if (plugin instanceof StatusChangeListener) {
55 controller.addStatusChangeListener(
56 (StatusChangeListener)plugin);
57 }
58 if (plugin instanceof ProbeListener) {
59 controller.addProbeListener((ProbeListener)plugin);
60 }
61 // add it to the registry
62 plugins.add(plugin);
63 }
64
65 public void configAll(Element rootElem) throws Exception {
66 Iterator plugins = this.plugins.iterator();
67 while (plugins.hasNext()) {
68 /* retrieve the configuration element */
69 Plugin thisPlugin = (Plugin)plugins.next();
70 Element configElem = rootElem.getChild(
71 thisPlugin.getElementName(),MarsModel.NAMESPACE);
72 /* config the plugin if it's there */
73 if (configElem != null) thisPlugin.setConfig(configElem);
74 }
75 }
76
77 public void mergeConfig(Element rootElem) {
78 Iterator plugins = this.plugins.iterator();
79 while (plugins.hasNext()) {
80 Plugin thisPlugin = (Plugin)plugins.next();
81 rootElem.addContent(thisPlugin.getConfig());
82 }
83 }
84
85 public Plugin[] getRegisteredPlugins() {
86 return (Plugin[])plugins.toArray(new Plugin[plugins.size()]);
87 }
88
89 public void loadDynamic(File homeDir) {
90 PluginLoadExceptionHandler pleh = new PluginLoadExceptionHandler();
91 PluginFilenameFilter pff = new PluginFilenameFilter();
92 Main.getMain().showStatus("Scanning for plugins in "+homeDir.getAbsolutePath()+"...");
93 Iterator plugins = ExtensionLoader.scanExtensions(homeDir,
94 pff, PLUGIN_MANIFEST_KEY, pleh).iterator();
95 while (plugins.hasNext()) {
96 try {
97 Class nextPluginClass = (Class)plugins.next();
98 Plugin nextPlugin = (Plugin)nextPluginClass.newInstance();
99 registerPlugin(nextPlugin);
100 Main.getMain().showStatus("Loaded plugin "+nextPluginClass);
101 } catch (Exception ex) {
102 pleh.handleLoadException(null,ex);
103 }
104 }
105 }
106
107 private static class PluginLoadExceptionHandler
108 implements LoadExceptionHandler {
109 public void handleLoadException(File file, Exception ex) {
110 if (file == null) {
111 Main.getMain().showStatus("Error loading plugin: "+ex.getMessage());
112 } else {
113 Main.getMain().showStatus("Error loading plugin from"+
114 file.getAbsolutePath()+": "+ex.getMessage());
115 }
116 ex.printStackTrace();
117 }
118 }
119
120 private static class PluginFilenameFilter
121 implements FilenameFilter {
122 public boolean accept(File dir, String name) {
123 return (name.startsWith(PLUGIN_JAR_PREFIX) && name.endsWith(".jar"));
124 }
125 }
126 }