1 5 package net.nutch.plugin; 6 import java.io.File ; 7 import java.io.UnsupportedEncodingException ; 8 import java.net.MalformedURLException ; 9 import java.net.URL ; 10 import java.net.URLDecoder ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 import java.util.logging.Logger ; 14 import net.nutch.util.NutchConf; 15 import org.dom4j.Attribute; 16 import org.dom4j.Document; 17 import org.dom4j.DocumentException; 18 import org.dom4j.Element; 19 import org.dom4j.io.SAXReader; 20 import java.util.regex.Pattern ; 21 22 28 public class PluginManifestParser { 29 public static final Logger LOG = PluginRepository.LOG; 30 31 private static final boolean WINDOWS 32 = System.getProperty("os.name").startsWith("Windows"); 33 34 42 public static ArrayList parsePluginFolder() { 43 ArrayList list = new ArrayList (); 44 String [] pluginFolders = NutchConf.getStrings("plugin.folders"); 45 Pattern excludes = Pattern.compile(NutchConf.get("plugin.excludes", "")); 46 Pattern includes = Pattern.compile(NutchConf.get("plugin.includes", "")); 47 if (pluginFolders == null) 48 throw new IllegalArgumentException ("plugin.folders is not defined"); 49 for (int i = 0; i < pluginFolders.length; i++) { 50 String name = pluginFolders[i]; 51 File directory = getPluginFolder(name); 52 if (directory == null) 53 continue; 54 LOG.info("Plugins: looking in: "+directory); 55 File [] files = directory.listFiles(); 56 if (files == null) 57 continue; 58 for (int j = 0; j < files.length; j++) { 59 File oneSubFolder = files[j]; 60 if (oneSubFolder.isDirectory()) { 61 62 if (!includes.matcher(oneSubFolder.getName()).matches()) { 63 LOG.info("not including: "+oneSubFolder); 64 continue; 65 } 66 67 if (excludes.matcher(oneSubFolder.getName()).matches()) { 68 LOG.info("excluding: "+oneSubFolder); 69 continue; 70 } 71 72 String manifestPath = oneSubFolder.getAbsolutePath() 73 + File.separator + "plugin.xml"; 74 try { 75 LOG.info("parsing: "+manifestPath); 76 list.add(parseManifestFile(manifestPath)); 77 } catch (MalformedURLException e) { 78 LOG.info(e.toString()); 79 } catch (DocumentException e) { 80 LOG.info(e.toString()); 81 } 82 } 83 } 84 } 85 return list; 86 } 87 88 90 static File getPluginFolder(String name) { 91 File directory = new File (name); 92 if (!directory.isAbsolute()) { 93 URL url =PluginManifestParser.class.getClassLoader().getResource(name); 94 if (url == null) { 95 LOG.info("Plugins: directory not found: "+name); 96 return null; 97 } else if (!"file".equals(url.getProtocol())) { 98 LOG.info("Plugins: not a file: url. Can't load plugins from: "+url); 99 return null; 100 } 101 String path = url.getPath(); 102 if (WINDOWS && path.startsWith("/")) path = path.substring(1); 104 try { 105 path = URLDecoder.decode(path, "UTF-8"); } catch (UnsupportedEncodingException e) {} 107 directory = new File (path); 108 } 109 return directory; 110 } 111 112 115 private static PluginDescriptor parseManifestFile(String pManifestPath) 116 throws MalformedURLException , DocumentException { 117 Document document = parseXML(new File (pManifestPath).toURL()); 118 String pPath = new File (pManifestPath).getParent(); 119 return parsePlugin(document, pPath); 120 } 121 126 private static Document parseXML(URL url) throws DocumentException { 127 SAXReader reader = new SAXReader(); 128 Document document = reader.read(url); 129 return document; 130 } 131 134 private static PluginDescriptor parsePlugin(Document pDocument, String pPath) 135 throws MalformedURLException { 136 Element rootElement = pDocument.getRootElement(); 137 String id = rootElement.attributeValue("id"); 138 String name = rootElement.attributeValue("name"); 139 String version = rootElement.attributeValue("version"); 140 String providerName = rootElement.attributeValue("provider-name"); 141 String pluginClazz = rootElement.attributeValue("class"); 142 PluginDescriptor pluginDescriptor = new PluginDescriptor(id, version, 143 name, providerName, pluginClazz, pPath); 144 parseExtension(rootElement, pluginDescriptor); 147 parseExtensionPoints(rootElement, pluginDescriptor); 148 parseLibraries(rootElement, pluginDescriptor); 149 return pluginDescriptor; 150 } 151 155 private static void parseLibraries(Element pRootElement, 156 PluginDescriptor pDescriptor) throws MalformedURLException { 157 Element runtime = pRootElement.element("runtime"); 158 if (runtime == null) 159 return; 160 List libraries = runtime.elements("library"); 161 for (int i = 0; i < libraries.size(); i++) { 162 Element library = (Element) libraries.get(i); 163 String libName = library.attributeValue("name"); 164 Element exportElement = library.element("extport"); 165 if (exportElement != null) 166 pDescriptor.addExportedLibRelative(libName); 167 else 168 pDescriptor.addNotExportedLibRelative(libName); 169 } 170 } 171 175 private static void parseExtensionPoints(Element pRootElement, 176 PluginDescriptor pPluginDescriptor) { 177 List list = pRootElement.elements("extension-point"); 178 if (list != null) { 179 for (int i = 0; i < list.size(); i++) { 180 Element oneExtensionPoint = (Element) list.get(i); 181 String id = oneExtensionPoint.attributeValue("id"); 182 String name = oneExtensionPoint.attributeValue("name"); 183 String schema = oneExtensionPoint.attributeValue("schema"); 184 ExtensionPoint extensionPoint = new ExtensionPoint(id, name, 185 schema); 186 pPluginDescriptor.addExtensionPoint(extensionPoint); 188 } 189 } 190 } 191 195 private static void parseExtension(Element pRootElement, 196 PluginDescriptor pPluginDescriptor) { 197 List extensions = pRootElement.elements("extension"); 198 if (extensions != null) { 199 for (int i = 0; i < extensions.size(); i++) { 200 Element oneExtension = (Element) extensions.get(i); 201 String pointId = oneExtension.attributeValue("point"); 202 List extensionImplementations = oneExtension.elements(); 203 if (extensionImplementations != null) { 204 for (int j = 0; j < extensionImplementations.size(); j++) { 205 Element oneImplementation = (Element) extensionImplementations 206 .get(j); 207 String id = oneImplementation.attributeValue("id"); 208 String extensionClass = oneImplementation.attributeValue("class"); 209 LOG.fine("impl: point="+pointId+" class="+extensionClass); 210 Extension extension = new Extension(pPluginDescriptor, 211 pointId, id, extensionClass); 212 List list = oneImplementation.attributes(); 213 for (int k = 0; k < list.size(); k++) { 214 Attribute attribute = (Attribute) list.get(k); 215 String name = attribute.getName(); 216 if (name.equals("id") || name.equals("class")) 217 continue; 218 String value = attribute.getValue(); 219 extension.addAttribute(name, value); 220 } 221 pPluginDescriptor.addExtension(extension); 222 } 223 } 224 } 225 } 226 } 227 } 228 | Popular Tags |