KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > plugin > PluginRepository


1 /*
2  * Copyright (c) 2003 The Nutch Organization. All rights reserved. Use subject
3  * to the conditions in http://www.nutch.org/LICENSE.txt.
4  */

5 package net.nutch.plugin;
6 import java.io.IOException JavaDoc;
7 import java.lang.reflect.Constructor JavaDoc;
8 import java.lang.reflect.InvocationTargetException JavaDoc;
9 import java.net.MalformedURLException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.logging.Logger JavaDoc;
14 import net.nutch.util.LogFormatter;
15 import org.dom4j.DocumentException;
16 /**
17  * The plugin repositority is a registry of all plugins.
18  *
19  * At system boot up a repositority is builded by parsing the mainifest files of
20  * all plugins. Plugins that require not existing other plugins are not
21  * registed. For each plugin a plugin descriptor instance will be created. The
22  * descriptor represent all meta information about a plugin. So a plugin
23  * instance will be created later when it is required, this allow lazy plugin
24  * loading.
25  *
26  * @author joa23
27  */

28 public class PluginRepository {
29   private static PluginRepository fInstance;
30   private ArrayList JavaDoc fRegisteredPlugins;
31   private HashMap JavaDoc fExtensionPoints;
32   private HashMap JavaDoc fActivatedPlugins;
33   public static final Logger JavaDoc LOG = LogFormatter
34     .getLogger("net.nutch.plugin.PluginRepository");
35   /**
36    * @see java.lang.Object#Object()
37    */

38   private PluginRepository() throws MalformedURLException JavaDoc, IOException JavaDoc,
39                                     DocumentException, PluginRuntimeException {
40     fActivatedPlugins = new HashMap JavaDoc();
41     fExtensionPoints = new HashMap JavaDoc();
42     fRegisteredPlugins = getDependencyCheckedPlugins(PluginManifestParser
43                                                      .parsePluginFolder());
44     installExtensions(fRegisteredPlugins);
45   }
46
47   /**
48    * @param fRegisteredPlugins
49    */

50   private void installExtensions(ArrayList JavaDoc pRegisteredPlugins)
51     throws PluginRuntimeException {
52     for (int i = 0; i < pRegisteredPlugins.size(); i++) {
53       PluginDescriptor descriptor= (PluginDescriptor)pRegisteredPlugins.get(i);
54       Extension[] extensions = descriptor.getExtensions();
55       for (int j = 0; j < extensions.length; j++) {
56         Extension extension = extensions[j];
57         String JavaDoc xpId = extension.getTargetPoint();
58         ExtensionPoint point = getExtensionPoint(xpId);
59         if (point == null)
60           throw new PluginRuntimeException
61             ("extension point: "+xpId+" does not exist.");
62         point.addExtension(extension);
63       }
64     }
65   }
66   /**
67    * @param pLoadedPlugins
68    * @return ArrayList
69    */

70   private ArrayList JavaDoc getDependencyCheckedPlugins(ArrayList JavaDoc pLoadedPlugins) {
71     ArrayList JavaDoc availablePlugins = new ArrayList JavaDoc();
72     for (int i = 0; i < pLoadedPlugins.size(); i++) {
73       PluginDescriptor descriptor = (PluginDescriptor) pLoadedPlugins.get(i);
74       String JavaDoc[] dependencyIDs = descriptor.getDependencies();
75       boolean available = true;
76       for (int j = 0; j < dependencyIDs.length; j++) {
77         String JavaDoc id = dependencyIDs[j];
78         if (!dependencyIsAvailabel(id, pLoadedPlugins)) {
79           available = false;
80           //LOG.fine("Skipping " + descriptor.getName());
81
break;
82         }
83       }
84       if (available) {
85         //LOG.fine("Adding " + descriptor.getName());
86
availablePlugins.add(descriptor);
87         ExtensionPoint[] points = descriptor.getExtenstionPoints();
88         for (int j = 0; j < points.length; j++) {
89           ExtensionPoint point = points[j];
90           String JavaDoc xpId = point.getId();
91           //LOG.fine("Adding extension point " + xpId);
92
fExtensionPoints.put(xpId, point);
93         }
94       }
95     }
96     return availablePlugins;
97   }
98   /**
99    * @param id
100    * @param pLoadedPlugins
101    * @return boolean
102    */

103   private boolean dependencyIsAvailabel(String JavaDoc id, ArrayList JavaDoc pLoadedPlugins) {
104     if (pLoadedPlugins != null && id != null) {
105       for (int i = 0; i < pLoadedPlugins.size(); i++) {
106         PluginDescriptor descriptor = (PluginDescriptor) pLoadedPlugins
107           .get(i);
108         if (descriptor.getPluginId().equals(id)) {
109           return true;
110         }
111       }
112     }
113     return false;
114   }
115   /**
116    * Returns the singelton instance of the <code>PluginRepository</code>
117    */

118   public static synchronized PluginRepository getInstance() {
119     if (fInstance != null)
120       return fInstance;
121     try {
122       fInstance = new PluginRepository();
123     } catch (Exception JavaDoc e) {
124       LOG.severe(e.toString());
125       throw new RuntimeException JavaDoc(e);
126     }
127     return fInstance;
128   }
129   /**
130    * Returns all registed plugin descriptors.
131    *
132    * @return PluginDescriptor[]
133    */

134   public PluginDescriptor[] getPluginDescriptors() {
135     return (PluginDescriptor[]) fRegisteredPlugins
136       .toArray(new PluginDescriptor[fRegisteredPlugins.size()]);
137   }
138   /**
139    * Returns the descriptor of one plugin identified by a plugin id.
140    *
141    * @param pPluginId
142    * @return PluginDescriptor
143    */

144   public PluginDescriptor getPluginDescriptor(String JavaDoc pPluginId) {
145     for (int i = 0; i < fRegisteredPlugins.size(); i++) {
146       PluginDescriptor descriptor = (PluginDescriptor) fRegisteredPlugins
147         .get(i);
148       if (descriptor.getPluginId().equals(pPluginId))
149         return descriptor;
150     }
151     return null;
152   }
153   /**
154    * Returns a extension point indentified by a extension point id.
155    *
156    * @param xXpId
157    */

158   public ExtensionPoint getExtensionPoint(String JavaDoc pXpId) {
159     return (ExtensionPoint) fExtensionPoints.get(pXpId);
160   }
161   /**
162    * Returns a instance of a plugin. Plugin instances are cached. So a plugin
163    * exist only as one instance. This allow a central management of plugin own
164    * resources.
165    *
166    * After creating the plugin instance the startUp() method is invoked. The
167    * plugin use a own classloader that is used as well by all instance of
168    * extensions of the same plugin. This class loader use all exported
169    * libraries from the dependend plugins and all plugin libraries.
170    *
171    * @param pDescriptor
172    * @return Plugin
173    * @throws PluginRuntimeException
174    */

175   public Plugin getPluginInstance(PluginDescriptor pDescriptor)
176     throws PluginRuntimeException {
177     if (fActivatedPlugins.containsKey(pDescriptor.getPluginId()))
178       return (Plugin) fActivatedPlugins.get(pDescriptor.getPluginId());
179     try {
180       // Must synchronize here to make sure creation and initialization
181
// of a plugin instance are done by one and only one thread.
182
// The same is in Extension.getExtensionInstance().
183
// Suggested by Stefan Groschupf <sg@media-style.com>
184
synchronized (pDescriptor) {
185         PluginClassLoader loader = pDescriptor.getClassLoader();
186         Class JavaDoc pluginClass = loader.loadClass(pDescriptor.getPluginClass());
187         Constructor JavaDoc constructor = pluginClass
188           .getConstructor(new Class JavaDoc[]{PluginDescriptor.class});
189         Plugin plugin = (Plugin) constructor
190           .newInstance(new Object JavaDoc[]{pDescriptor});
191         plugin.startUp();
192         fActivatedPlugins.put(pDescriptor.getPluginId(), plugin);
193         return plugin;
194       }
195     } catch (ClassNotFoundException JavaDoc e) {
196       throw new PluginRuntimeException(e);
197     } catch (InstantiationException JavaDoc e) {
198       throw new PluginRuntimeException(e);
199     } catch (IllegalAccessException JavaDoc e) {
200       throw new PluginRuntimeException(e);
201     } catch (NoSuchMethodException JavaDoc e) {
202       throw new PluginRuntimeException(e);
203     } catch (InvocationTargetException JavaDoc e) {
204       throw new PluginRuntimeException(e);
205     }
206   }
207   /*
208    * (non-Javadoc)
209    *
210    * @see java.lang.Object#finalize()
211    */

212   public void finalize() throws Throwable JavaDoc {
213     shotDownActivatedPlugins();
214   }
215   /**
216    * @throws PluginRuntimeException
217    */

218   private void shotDownActivatedPlugins() throws PluginRuntimeException {
219     Iterator JavaDoc iterator = fActivatedPlugins.keySet().iterator();
220     while (iterator.hasNext()) {
221       String JavaDoc pluginId = (String JavaDoc) iterator.next();
222       Plugin object = (Plugin) fActivatedPlugins.get(pluginId);
223       object.shutDown();
224     }
225   }
226 }
227
Popular Tags