1 5 package net.nutch.plugin; 6 import java.io.IOException ; 7 import java.lang.reflect.Constructor ; 8 import java.lang.reflect.InvocationTargetException ; 9 import java.net.MalformedURLException ; 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.logging.Logger ; 14 import net.nutch.util.LogFormatter; 15 import org.dom4j.DocumentException; 16 28 public class PluginRepository { 29 private static PluginRepository fInstance; 30 private ArrayList fRegisteredPlugins; 31 private HashMap fExtensionPoints; 32 private HashMap fActivatedPlugins; 33 public static final Logger LOG = LogFormatter 34 .getLogger("net.nutch.plugin.PluginRepository"); 35 38 private PluginRepository() throws MalformedURLException , IOException , 39 DocumentException, PluginRuntimeException { 40 fActivatedPlugins = new HashMap (); 41 fExtensionPoints = new HashMap (); 42 fRegisteredPlugins = getDependencyCheckedPlugins(PluginManifestParser 43 .parsePluginFolder()); 44 installExtensions(fRegisteredPlugins); 45 } 46 47 50 private void installExtensions(ArrayList 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 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 70 private ArrayList getDependencyCheckedPlugins(ArrayList pLoadedPlugins) { 71 ArrayList availablePlugins = new ArrayList (); 72 for (int i = 0; i < pLoadedPlugins.size(); i++) { 73 PluginDescriptor descriptor = (PluginDescriptor) pLoadedPlugins.get(i); 74 String [] dependencyIDs = descriptor.getDependencies(); 75 boolean available = true; 76 for (int j = 0; j < dependencyIDs.length; j++) { 77 String id = dependencyIDs[j]; 78 if (!dependencyIsAvailabel(id, pLoadedPlugins)) { 79 available = false; 80 break; 82 } 83 } 84 if (available) { 85 availablePlugins.add(descriptor); 87 ExtensionPoint[] points = descriptor.getExtenstionPoints(); 88 for (int j = 0; j < points.length; j++) { 89 ExtensionPoint point = points[j]; 90 String xpId = point.getId(); 91 fExtensionPoints.put(xpId, point); 93 } 94 } 95 } 96 return availablePlugins; 97 } 98 103 private boolean dependencyIsAvailabel(String id, ArrayList 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 118 public static synchronized PluginRepository getInstance() { 119 if (fInstance != null) 120 return fInstance; 121 try { 122 fInstance = new PluginRepository(); 123 } catch (Exception e) { 124 LOG.severe(e.toString()); 125 throw new RuntimeException (e); 126 } 127 return fInstance; 128 } 129 134 public PluginDescriptor[] getPluginDescriptors() { 135 return (PluginDescriptor[]) fRegisteredPlugins 136 .toArray(new PluginDescriptor[fRegisteredPlugins.size()]); 137 } 138 144 public PluginDescriptor getPluginDescriptor(String 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 158 public ExtensionPoint getExtensionPoint(String pXpId) { 159 return (ExtensionPoint) fExtensionPoints.get(pXpId); 160 } 161 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 synchronized (pDescriptor) { 185 PluginClassLoader loader = pDescriptor.getClassLoader(); 186 Class pluginClass = loader.loadClass(pDescriptor.getPluginClass()); 187 Constructor constructor = pluginClass 188 .getConstructor(new Class []{PluginDescriptor.class}); 189 Plugin plugin = (Plugin) constructor 190 .newInstance(new Object []{pDescriptor}); 191 plugin.startUp(); 192 fActivatedPlugins.put(pDescriptor.getPluginId(), plugin); 193 return plugin; 194 } 195 } catch (ClassNotFoundException e) { 196 throw new PluginRuntimeException(e); 197 } catch (InstantiationException e) { 198 throw new PluginRuntimeException(e); 199 } catch (IllegalAccessException e) { 200 throw new PluginRuntimeException(e); 201 } catch (NoSuchMethodException e) { 202 throw new PluginRuntimeException(e); 203 } catch (InvocationTargetException e) { 204 throw new PluginRuntimeException(e); 205 } 206 } 207 212 public void finalize() throws Throwable { 213 shotDownActivatedPlugins(); 214 } 215 218 private void shotDownActivatedPlugins() throws PluginRuntimeException { 219 Iterator iterator = fActivatedPlugins.keySet().iterator(); 220 while (iterator.hasNext()) { 221 String pluginId = (String ) iterator.next(); 222 Plugin object = (Plugin) fActivatedPlugins.get(pluginId); 223 object.shutDown(); 224 } 225 } 226 } 227 | Popular Tags |