Apache Nutch is an extensible and scalable web crawler
Remove dependency on dom4j from core. Patch from Stefan Groschupf.
Douglass Cutting6154a5b267bd
diff --git a/lib/dom4j-1.5.2.LICENSE.txt b/lib/dom4j-1.5.2.LICENSE.txtdeleted file mode 100644index 384ae7aec..000000000--- a/lib/dom4j-1.5.2.LICENSE.txt+++ /dev/null@@ -1,45 +0,0 @@-/*- $Id: LICENSE.txt,v 1.4 2004/06/25 08:03:26 maartenc Exp $-- Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.-- Redistribution and use of this software and associated documentation- ("Software"), with or without modification, are permitted provided- that the following conditions are met:-- 1. Redistributions of source code must retain copyright- statements and notices. Redistributions must also contain a- copy of this document.-- 2. Redistributions in binary form must reproduce the- above copyright notice, this list of conditions and the- following disclaimer in the documentation and/or other- materials provided with the distribution.-- 3. The name "DOM4J" must not be used to endorse or promote- products derived from this Software without prior written- permission of MetaStuff, Ltd. For written permission,- please contact [email protected].-- 4. Products derived from this Software may not be called "DOM4J"- nor may "DOM4J" appear in their names without prior written- permission of MetaStuff, Ltd. DOM4J is a registered- trademark of MetaStuff, Ltd.-- 5. Due credit should be given to the DOM4J Project -- http://www.dom4j.org-- THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS- ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT- NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL- METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED- OF THE POSSIBILITY OF SUCH DAMAGE.-- */diff --git a/lib/dom4j-1.5.2.jar b/lib/dom4j-1.5.2.jardeleted file mode 100644index 8e96e0641..000000000Binary files a/lib/dom4j-1.5.2.jar and /dev/null differdiff --git a/src/java/org/apache/nutch/plugin/PluginManifestParser.java b/src/java/org/apache/nutch/plugin/PluginManifestParser.javaindex d45998242..799a2908c 100644--- a/src/java/org/apache/nutch/plugin/PluginManifestParser.java+++ b/src/java/org/apache/nutch/plugin/PluginManifestParser.java@@ -1,5 +1,5 @@/*-/**+ /*** Copyright 2005 The Apache Software Foundation** Licensed under the Apache License, Version 2.0 (the "License");@@ -15,225 +15,271 @@* limitations under the License.*/package org.apache.nutch.plugin;+import java.io.File;+import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLDecoder;import java.util.ArrayList;-import java.util.List;import java.util.logging.Logger;-import org.apache.nutch.util.NutchConf;-import org.dom4j.Attribute;-import org.dom4j.Document;-import org.dom4j.DocumentException;-import org.dom4j.Element;-import org.dom4j.io.SAXReader;import java.util.regex.Pattern;+import javax.xml.parsers.DocumentBuilder;+import javax.xml.parsers.DocumentBuilderFactory;+import javax.xml.parsers.ParserConfigurationException;++import org.apache.nutch.util.NutchConf;+import org.w3c.dom.Document;+import org.w3c.dom.Element;+import org.w3c.dom.NamedNodeMap;+import org.w3c.dom.Node;+import org.w3c.dom.NodeList;+import org.xml.sax.SAXException;+/**- * The <code>PluginManifestParser</code> parser just parse the manifest file in all plugin- * directories.+ * The <code>PluginManifestParser</code> parser just parse the manifest file+ * in all plugin directories.** @author joa23*/public class PluginManifestParser {- public static final Logger LOG = PluginRepository.LOG;-- private static final boolean WINDOWS- = System.getProperty("os.name").startsWith("Windows");-- /**- * Returns a list with plugin descriptors.- *- * @return ArrayList- * @throws IOException- * @throws MalformedURLException- * @throws DocumentException- */- public static ArrayList parsePluginFolder() {- ArrayList list = new ArrayList();- String[] pluginFolders = NutchConf.get().getStrings("plugin.folders");- Pattern excludes = Pattern.compile(NutchConf.get().get("plugin.excludes", ""));- Pattern includes = Pattern.compile(NutchConf.get().get("plugin.includes", ""));- if (pluginFolders == null)- throw new IllegalArgumentException("plugin.folders is not defined");- for (int i = 0; i < pluginFolders.length; i++) {- String name = pluginFolders[i];- File directory = getPluginFolder(name);- if (directory == null)- continue;- LOG.info("Plugins: looking in: "+directory);- File[] files = directory.listFiles();- if (files == null)- continue;- for (int j = 0; j < files.length; j++) {- File oneSubFolder = files[j];- if (oneSubFolder.isDirectory()) {-- if (!includes.matcher(oneSubFolder.getName()).matches()) {- LOG.info("not including: "+oneSubFolder);- continue;- }-- if (excludes.matcher(oneSubFolder.getName()).matches()) {- LOG.info("excluding: "+oneSubFolder);- continue;- }-- String manifestPath = oneSubFolder.getAbsolutePath()- + File.separator + "plugin.xml";- try {- LOG.info("parsing: "+manifestPath);- list.add(parseManifestFile(manifestPath));- } catch (MalformedURLException e) {- LOG.info(e.toString());- } catch (DocumentException e) {- LOG.info(e.toString());- }+ public static final Logger LOG = PluginRepository.LOG;++ private static final boolean WINDOWS = System.getProperty("os.name")+ .startsWith("Windows");++ /**+ * Returns a list with plugin descriptors.+ *+ * @return ArrayList+ *+ */+ public static ArrayList parsePluginFolder() {+ ArrayList list = new ArrayList();+ String[] pluginFolders = NutchConf.get().getStrings("plugin.folders");+ Pattern excludes = Pattern.compile(NutchConf.get().get(+ "plugin.excludes", ""));+ Pattern includes = Pattern.compile(NutchConf.get().get(+ "plugin.includes", ""));+ if (pluginFolders == null)+ throw new IllegalArgumentException("plugin.folders is not defined");+ for (int i = 0; i < pluginFolders.length; i++) {+ String name = pluginFolders[i];+ File directory = getPluginFolder(name);+ if (directory == null)+ continue;+ LOG.info("Plugins: looking in: " + directory);+ File[] files = directory.listFiles();+ if (files == null)+ continue;+ for (int j = 0; j < files.length; j++) {+ File oneSubFolder = files[j];+ if (oneSubFolder.isDirectory()) {++ if (!includes.matcher(oneSubFolder.getName()).matches()) {+ LOG.info("not including: " + oneSubFolder);+ continue;+ }++ if (excludes.matcher(oneSubFolder.getName()).matches()) {+ LOG.info("excluding: " + oneSubFolder);+ continue;+ }++ String manifestPath = oneSubFolder.getAbsolutePath()+ + File.separator + "plugin.xml";+ try {+ LOG.info("parsing: " + manifestPath);+ list.add(parseManifestFile(manifestPath));+ } catch (MalformedURLException e) {+ LOG.info(e.toString());+ } catch (SAXException e) {+ LOG.info(e.toString());+ } catch (IOException e) {+ LOG.info(e.toString());+ } catch (ParserConfigurationException e) {+ LOG.info(e.toString());+ }+ }+ }}- }+ return list;}- return list;- }-- /** Return the named plugin folder. If the name is absolute then it is- * returned. Otherwise, for relative names, the classpath is scanned.*/- static File getPluginFolder(String name) {- File directory = new File(name);- if (!directory.isAbsolute()) {- URL url =PluginManifestParser.class.getClassLoader().getResource(name);- if (url == null) {- LOG.info("Plugins: directory not found: "+name);- return null;- } else if (!"file".equals(url.getProtocol())) {- LOG.info("Plugins: not a file: url. Can't load plugins from: "+url);- return null;- }- String path = url.getPath();- if (WINDOWS && path.startsWith("/")) // patch a windows bug- path = path.substring(1);- try {- path = URLDecoder.decode(path, "UTF-8"); // decode the url path- } catch (UnsupportedEncodingException e) {}- directory = new File(path);++ /**+ * Return the named plugin folder. If the name is absolute then it is+ * returned. Otherwise, for relative names, the classpath is scanned.+ */+ static File getPluginFolder(String name) {+ File directory = new File(name);+ if (!directory.isAbsolute()) {+ URL url = PluginManifestParser.class.getClassLoader().getResource(+ name);+ if (url == null) {+ LOG.info("Plugins: directory not found: " + name);+ return null;+ } else if (!"file".equals(url.getProtocol())) {+ LOG.info("Plugins: not a file: url. Can't load plugins from: "+ + url);+ return null;+ }+ String path = url.getPath();+ if (WINDOWS && path.startsWith("/")) // patch a windows bug+ path = path.substring(1);+ try {+ path = URLDecoder.decode(path, "UTF-8"); // decode the url path+ } catch (UnsupportedEncodingException e) {+ }+ directory = new File(path);+ }+ return directory;}- return directory;- }-- /**- * @param manifestPath- */- private static PluginDescriptor parseManifestFile(String pManifestPath)- throws MalformedURLException, DocumentException {- Document document = parseXML(new File(pManifestPath).toURL());- String pPath = new File(pManifestPath).getParent();- return parsePlugin(document, pPath);- }- /**- * @param url- * @return Document- * @throws DocumentException- */- private static Document parseXML(URL url) throws DocumentException {- SAXReader reader = new SAXReader();- Document document = reader.read(url);- return document;- }- /**- * @param document- */- private static PluginDescriptor parsePlugin(Document pDocument, String pPath)- throws MalformedURLException {- Element rootElement = pDocument.getRootElement();- String id = rootElement.attributeValue("id");- String name = rootElement.attributeValue("name");- String version = rootElement.attributeValue("version");- String providerName = rootElement.attributeValue("provider-name");- String pluginClazz = rootElement.attributeValue("class");- PluginDescriptor pluginDescriptor = new PluginDescriptor(id, version,- name, providerName, pluginClazz, pPath);-// LOG.fine("plugin: id="+id+" name="+name+" version="+version-// +" provider="+providerName+"class="+pluginClazz);- parseExtension(rootElement, pluginDescriptor);- parseExtensionPoints(rootElement, pluginDescriptor);- parseLibraries(rootElement, pluginDescriptor);- return pluginDescriptor;- }- /**- * @param rootElement- * @param pluginDescriptor- */- private static void parseLibraries(Element pRootElement,- PluginDescriptor pDescriptor) throws MalformedURLException {- Element runtime = pRootElement.element("runtime");- if (runtime == null)- return;- List libraries = runtime.elements("library");- for (int i = 0; i < libraries.size(); i++) {- Element library = (Element) libraries.get(i);- String libName = library.attributeValue("name");- Element exportElement = library.element("export");- if (exportElement != null)- pDescriptor.addExportedLibRelative(libName);- else- pDescriptor.addNotExportedLibRelative(libName);++ /**+ * @param manifestPath+ * @throws ParserConfigurationException+ * @throws IOException+ * @throws SAXException+ * @throws MalformedURLException+ */+ private static PluginDescriptor parseManifestFile(String pManifestPath)+ throws MalformedURLException, SAXException, IOException,+ ParserConfigurationException {+ Document document = parseXML(new File(pManifestPath).toURL());+ String pPath = new File(pManifestPath).getParent();+ return parsePlugin(document, pPath);}- }- /**- * @param rootElement- * @param pluginDescriptor- */- private static void parseExtensionPoints(Element pRootElement,- PluginDescriptor pPluginDescriptor) {- List list = pRootElement.elements("extension-point");- if (list != null) {- for (int i = 0; i < list.size(); i++) {- Element oneExtensionPoint = (Element) list.get(i);- String id = oneExtensionPoint.attributeValue("id");- String name = oneExtensionPoint.attributeValue("name");- String schema = oneExtensionPoint.attributeValue("schema");- ExtensionPoint extensionPoint = new ExtensionPoint(id, name,- schema);- //LOG.fine("plugin: point="+id);- pPluginDescriptor.addExtensionPoint(extensionPoint);- }++ /**+ * @param url+ * @return Document+ * @throws IOException+ * @throws SAXException+ * @throws ParserConfigurationException+ * @throws DocumentException+ */+ private static Document parseXML(URL url) throws SAXException, IOException,+ ParserConfigurationException {+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();+ DocumentBuilder builder = factory.newDocumentBuilder();+ return builder.parse(url.openStream());}- }- /**- * @param rootElement- * @param pluginDescriptor- */- private static void parseExtension(Element pRootElement,- PluginDescriptor pPluginDescriptor) {- List extensions = pRootElement.elements("extension");- if (extensions != null) {- for (int i = 0; i < extensions.size(); i++) {- Element oneExtension = (Element) extensions.get(i);- String pointId = oneExtension.attributeValue("point");- List extensionImplementations = oneExtension.elements();- if (extensionImplementations != null) {- for (int j = 0; j < extensionImplementations.size(); j++) {- Element oneImplementation = (Element) extensionImplementations- .get(j);- String id = oneImplementation.attributeValue("id");- String extensionClass = oneImplementation.attributeValue("class");- LOG.fine("impl: point="+pointId+" class="+extensionClass);- Extension extension = new Extension(pPluginDescriptor,- pointId, id, extensionClass);- List list = oneImplementation.attributes();- for (int k = 0; k < list.size(); k++) {- Attribute attribute = (Attribute) list.get(k);- String name = attribute.getName();- if (name.equals("id") || name.equals("class"))- continue;- String value = attribute.getValue();- extension.addAttribute(name, value);++ /**+ * @param document+ * @throws MalformedURLException+ */+ private static PluginDescriptor parsePlugin(Document pDocument, String pPath)+ throws MalformedURLException {+ Element rootElement = pDocument.getDocumentElement();+ String id = rootElement.getAttribute("id");+ String name = rootElement.getAttribute("name");+ String version = rootElement.getAttribute("version");+ String providerName = rootElement.getAttribute("provider-name");+ String pluginClazz = null;+ if (rootElement.getAttribute("class").trim().length() > 0) {+ pluginClazz = rootElement.getAttribute("class");+ }+ PluginDescriptor pluginDescriptor = new PluginDescriptor(id, version,+ name, providerName, pluginClazz, pPath);+ LOG.fine("plugin: id="+id+" name="+name+" version="+version+ +" provider="+providerName+"class="+pluginClazz);+ parseExtension(rootElement, pluginDescriptor);+ parseExtensionPoints(rootElement, pluginDescriptor);+ parseLibraries(rootElement, pluginDescriptor);+ return pluginDescriptor;+ }++ /**+ * @param rootElement+ * @param pluginDescriptor+ * @throws MalformedURLException+ */+ private static void parseLibraries(Element pRootElement,+ PluginDescriptor pDescriptor) throws MalformedURLException {+ NodeList nodelist = pRootElement.getElementsByTagName("runtime");+ if (nodelist.getLength() > 0) {++ Element runtime = (Element) nodelist.item(0);++ NodeList libraries = runtime.getElementsByTagName("library");+ for (int i = 0; i < libraries.getLength(); i++) {+ Element library = (Element) libraries.item(i);+ String libName = library.getAttribute("name");+ NodeList list = library.getElementsByTagName("export");+ Element exportElement = (Element) list.item(0);+ if (exportElement != null)+ pDescriptor.addExportedLibRelative(libName);+ else+ pDescriptor.addNotExportedLibRelative(libName);+ }+ }+ }++ /**+ * @param rootElement+ * @param pluginDescriptor+ */+ private static void parseExtensionPoints(Element pRootElement,+ PluginDescriptor pPluginDescriptor) {+ NodeList list = pRootElement.getElementsByTagName("extension-point");+ if (list != null) {+ for (int i = 0; i < list.getLength(); i++) {+ Element oneExtensionPoint = (Element) list.item(i);+ String id = oneExtensionPoint.getAttribute("id");+ String name = oneExtensionPoint.getAttribute("name");+ String schema = oneExtensionPoint.getAttribute("schema");+ ExtensionPoint extensionPoint = new ExtensionPoint(id, name,+ schema);+ //LOG.fine("plugin: point="+id);+ pPluginDescriptor.addExtensionPoint(extensionPoint);+ }+ }+ }++ /**+ * @param rootElement+ * @param pluginDescriptor+ */+ private static void parseExtension(Element pRootElement,+ PluginDescriptor pPluginDescriptor) {+ NodeList extensions = pRootElement.getElementsByTagName("extension");+ if (extensions != null) {+ for (int i = 0; i < extensions.getLength(); i++) {+ Element oneExtension = (Element) extensions.item(i);+ String pointId = oneExtension.getAttribute("point");++ NodeList extensionImplementations = oneExtension+ .getChildNodes();+ if (extensionImplementations != null) {+ for (int j = 0; j < extensionImplementations.getLength(); j++) {+ Node node = extensionImplementations.item(j);+ if (!node.getNodeName().equals("implementation")) {+ continue;+ }+ Element oneImplementation = (Element) node;+ String id = oneImplementation.getAttribute("id");+ String extensionClass = oneImplementation+ .getAttribute("class");+ LOG.info("impl: point=" + pointId + " class="+ + extensionClass);+ Extension extension = new Extension(pPluginDescriptor,+ pointId, id, extensionClass);+ NamedNodeMap list = oneImplementation.getAttributes();+ for (int k = 0; k < list.getLength(); k++) {+ Node attribute = list.item(k);+ String name = attribute.getNodeName();+ if (name.equals("id") || name.equals("class"))+ continue;+ String value = attribute.getNodeValue();+ extension.addAttribute(name, value);+ }+ pPluginDescriptor.addExtension(extension);+ }+ }}- pPluginDescriptor.addExtension(extension);- }}- }}- }-}+}\ No newline at end of filediff --git a/src/java/org/apache/nutch/plugin/PluginRepository.java b/src/java/org/apache/nutch/plugin/PluginRepository.javaindex 4a3c3e49d..e504b0cb9 100644--- a/src/java/org/apache/nutch/plugin/PluginRepository.java+++ b/src/java/org/apache/nutch/plugin/PluginRepository.java@@ -1,5 +1,5 @@/*-/**+ /*** Copyright 2005 The Apache Software Foundation** Licensed under the Apache License, Version 2.0 (the "License");@@ -15,16 +15,16 @@* limitations under the License.*/package org.apache.nutch.plugin;-import java.io.IOException;+import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;-import java.net.MalformedURLException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.logging.Logger;+import org.apache.nutch.util.LogFormatter;-import org.dom4j.DocumentException;+/*** The plugin repositority is a registry of all plugins.*@@ -38,201 +38,218 @@ import org.dom4j.DocumentException;* @author joa23*/public class PluginRepository {- private static PluginRepository fInstance;- private ArrayList fRegisteredPlugins;- private HashMap fExtensionPoints;- private HashMap fActivatedPlugins;- public static final Logger LOG = LogFormatter- .getLogger("org.apache.nutch.plugin.PluginRepository");- /**- * @see java.lang.Object#Object()- */- private PluginRepository() throws MalformedURLException, IOException,- DocumentException, PluginRuntimeException {- fActivatedPlugins = new HashMap();- fExtensionPoints = new HashMap();- fRegisteredPlugins = getDependencyCheckedPlugins(PluginManifestParser- .parsePluginFolder());- installExtensions(fRegisteredPlugins);- }-- /**- * @param fRegisteredPlugins- */- private void installExtensions(ArrayList pRegisteredPlugins)- throws PluginRuntimeException {- for (int i = 0; i < pRegisteredPlugins.size(); i++) {- PluginDescriptor descriptor= (PluginDescriptor)pRegisteredPlugins.get(i);- Extension[] extensions = descriptor.getExtensions();- for (int j = 0; j < extensions.length; j++) {- Extension extension = extensions[j];- String xpId = extension.getTargetPoint();- ExtensionPoint point = getExtensionPoint(xpId);- if (point == null)- throw new PluginRuntimeException- ("extension point: "+xpId+" does not exist.");- point.addExtension(extension);- }+ private static PluginRepository fInstance;++ private ArrayList fRegisteredPlugins;++ private HashMap fExtensionPoints;++ private HashMap fActivatedPlugins;++ public static final Logger LOG = LogFormatter+ .getLogger("org.apache.nutch.plugin.PluginRepository");++ /**+ * @throws PluginRuntimeException+ * @see java.lang.Object#Object()+ */+ private PluginRepository() throws PluginRuntimeException{+ fActivatedPlugins = new HashMap();+ fExtensionPoints = new HashMap();+ fRegisteredPlugins = getDependencyCheckedPlugins(PluginManifestParser+ .parsePluginFolder());+ installExtensions(fRegisteredPlugins);}- }- /**- * @param pLoadedPlugins- * @return ArrayList- */- private ArrayList getDependencyCheckedPlugins(ArrayList pLoadedPlugins) {- ArrayList availablePlugins = new ArrayList();- for (int i = 0; i < pLoadedPlugins.size(); i++) {- PluginDescriptor descriptor = (PluginDescriptor) pLoadedPlugins.get(i);- String[] dependencyIDs = descriptor.getDependencies();- boolean available = true;- for (int j = 0; j < dependencyIDs.length; j++) {- String id = dependencyIDs[j];- if (!dependencyIsAvailabel(id, pLoadedPlugins)) {- available = false;- //LOG.fine("Skipping " + descriptor.getName());- break;++ /**+ * @param fRegisteredPlugins+ */+ private void installExtensions(ArrayList pRegisteredPlugins)+ throws PluginRuntimeException {+ for (int i = 0; i < pRegisteredPlugins.size(); i++) {+ PluginDescriptor descriptor = (PluginDescriptor) pRegisteredPlugins+ .get(i);+ Extension[] extensions = descriptor.getExtensions();+ for (int j = 0; j < extensions.length; j++) {+ Extension extension = extensions[j];+ String xpId = extension.getTargetPoint();+ ExtensionPoint point = getExtensionPoint(xpId);+ if (point == null)+ throw new PluginRuntimeException("extension point: " + xpId+ + " does not exist.");+ point.addExtension(extension);+ }}- }- if (available) {- //LOG.fine("Adding " + descriptor.getName());- availablePlugins.add(descriptor);- ExtensionPoint[] points = descriptor.getExtenstionPoints();- for (int j = 0; j < points.length; j++) {- ExtensionPoint point = points[j];- String xpId = point.getId();- //LOG.fine("Adding extension point " + xpId);- fExtensionPoints.put(xpId, point);+ }++ /**+ * @param pLoadedPlugins+ * @return ArrayList+ */+ private ArrayList getDependencyCheckedPlugins(ArrayList pLoadedPlugins) {+ ArrayList availablePlugins = new ArrayList();+ for (int i = 0; i < pLoadedPlugins.size(); i++) {+ PluginDescriptor descriptor = (PluginDescriptor) pLoadedPlugins+ .get(i);+ String[] dependencyIDs = descriptor.getDependencies();+ boolean available = true;+ for (int j = 0; j < dependencyIDs.length; j++) {+ String id = dependencyIDs[j];+ if (!dependencyIsAvailabel(id, pLoadedPlugins)) {+ available = false;+ //LOG.fine("Skipping " + descriptor.getName());+ break;+ }+ }+ if (available) {+ //LOG.fine("Adding " + descriptor.getName());+ availablePlugins.add(descriptor);+ ExtensionPoint[] points = descriptor.getExtenstionPoints();+ for (int j = 0; j < points.length; j++) {+ ExtensionPoint point = points[j];+ String xpId = point.getId();+ //LOG.fine("Adding extension point " + xpId);+ fExtensionPoints.put(xpId, point);+ }+ }}- }+ return availablePlugins;}- return availablePlugins;- }- /**- * @param id- * @param pLoadedPlugins- * @return boolean- */- private boolean dependencyIsAvailabel(String id, ArrayList pLoadedPlugins) {- if (pLoadedPlugins != null && id != null) {- for (int i = 0; i < pLoadedPlugins.size(); i++) {- PluginDescriptor descriptor = (PluginDescriptor) pLoadedPlugins- .get(i);- if (descriptor.getPluginId().equals(id)) {- return true;++ /**+ * @param id+ * @param pLoadedPlugins+ * @return boolean+ */+ private boolean dependencyIsAvailabel(String id, ArrayList pLoadedPlugins) {+ if (pLoadedPlugins != null && id != null) {+ for (int i = 0; i < pLoadedPlugins.size(); i++) {+ PluginDescriptor descriptor = (PluginDescriptor) pLoadedPlugins+ .get(i);+ if (descriptor.getPluginId().equals(id)) {+ return true;+ }+ }}- }+ return false;}- return false;- }- /**- * Returns the singelton instance of the <code>PluginRepository</code>- */- public static synchronized PluginRepository getInstance() {- if (fInstance != null)- return fInstance;- try {- fInstance = new PluginRepository();- } catch (Exception e) {- LOG.severe(e.toString());- throw new RuntimeException(e);++ /**+ * Returns the singelton instance of the <code>PluginRepository</code>+ */+ public static synchronized PluginRepository getInstance() {+ if (fInstance != null)+ return fInstance;+ try {+ fInstance = new PluginRepository();+ } catch (Exception e) {+ LOG.severe(e.toString());+ throw new RuntimeException(e);+ }+ return fInstance;}- return fInstance;- }- /**- * Returns all registed plugin descriptors.- *- * @return PluginDescriptor[]- */- public PluginDescriptor[] getPluginDescriptors() {- return (PluginDescriptor[]) fRegisteredPlugins- .toArray(new PluginDescriptor[fRegisteredPlugins.size()]);- }- /**- * Returns the descriptor of one plugin identified by a plugin id.- *- * @param pPluginId- * @return PluginDescriptor- */- public PluginDescriptor getPluginDescriptor(String pPluginId) {- for (int i = 0; i < fRegisteredPlugins.size(); i++) {- PluginDescriptor descriptor = (PluginDescriptor) fRegisteredPlugins- .get(i);- if (descriptor.getPluginId().equals(pPluginId))- return descriptor;++ /**+ * Returns all registed plugin descriptors.+ *+ * @return PluginDescriptor[]+ */+ public PluginDescriptor[] getPluginDescriptors() {+ return (PluginDescriptor[]) fRegisteredPlugins+ .toArray(new PluginDescriptor[fRegisteredPlugins.size()]);}- return null;- }- /**- * Returns a extension point indentified by a extension point id.- *- * @param xXpId- */- public ExtensionPoint getExtensionPoint(String pXpId) {- return (ExtensionPoint) fExtensionPoints.get(pXpId);- }- /**- * Returns a instance of a plugin. Plugin instances are cached. So a plugin- * exist only as one instance. This allow a central management of plugin own- * resources.- *- * After creating the plugin instance the startUp() method is invoked. The- * plugin use a own classloader that is used as well by all instance of- * extensions of the same plugin. This class loader use all exported- * libraries from the dependend plugins and all plugin libraries.- *- * @param pDescriptor- * @return Plugin- * @throws PluginRuntimeException- */- public Plugin getPluginInstance(PluginDescriptor pDescriptor)- throws PluginRuntimeException {- if (fActivatedPlugins.containsKey(pDescriptor.getPluginId()))- return (Plugin) fActivatedPlugins.get(pDescriptor.getPluginId());- try {- // Must synchronize here to make sure creation and initialization- // of a plugin instance are done by one and only one thread.- // The same is in Extension.getExtensionInstance().- // Suggested by Stefan Groschupf <[email protected]>- synchronized (pDescriptor) {- PluginClassLoader loader = pDescriptor.getClassLoader();- Class pluginClass = loader.loadClass(pDescriptor.getPluginClass());- Constructor constructor = pluginClass- .getConstructor(new Class[]{PluginDescriptor.class});- Plugin plugin = (Plugin) constructor- .newInstance(new Object[]{pDescriptor});- plugin.startUp();- fActivatedPlugins.put(pDescriptor.getPluginId(), plugin);- return plugin;- }- } catch (ClassNotFoundException e) {- throw new PluginRuntimeException(e);- } catch (InstantiationException e) {- throw new PluginRuntimeException(e);- } catch (IllegalAccessException e) {- throw new PluginRuntimeException(e);- } catch (NoSuchMethodException e) {- throw new PluginRuntimeException(e);- } catch (InvocationTargetException e) {- throw new PluginRuntimeException(e);++ /**+ * Returns the descriptor of one plugin identified by a plugin id.+ *+ * @param pPluginId+ * @return PluginDescriptor+ */+ public PluginDescriptor getPluginDescriptor(String pPluginId) {+ for (int i = 0; i < fRegisteredPlugins.size(); i++) {+ PluginDescriptor descriptor = (PluginDescriptor) fRegisteredPlugins+ .get(i);+ if (descriptor.getPluginId().equals(pPluginId))+ return descriptor;+ }+ return null;}- }- /*- * (non-Javadoc)- *- * @see java.lang.Object#finalize()- */- public void finalize() throws Throwable {- shotDownActivatedPlugins();- }- /**- * @throws PluginRuntimeException- */- private void shotDownActivatedPlugins() throws PluginRuntimeException {- Iterator iterator = fActivatedPlugins.keySet().iterator();- while (iterator.hasNext()) {- String pluginId = (String) iterator.next();- Plugin object = (Plugin) fActivatedPlugins.get(pluginId);- object.shutDown();++ /**+ * Returns a extension point indentified by a extension point id.+ *+ * @param pXpId+ */+ public ExtensionPoint getExtensionPoint(String pXpId) {+ return (ExtensionPoint) fExtensionPoints.get(pXpId);+ }++ /**+ * Returns a instance of a plugin. Plugin instances are cached. So a plugin+ * exist only as one instance. This allow a central management of plugin own+ * resources.+ *+ * After creating the plugin instance the startUp() method is invoked. The+ * plugin use a own classloader that is used as well by all instance of+ * extensions of the same plugin. This class loader use all exported+ * libraries from the dependend plugins and all plugin libraries.+ *+ * @param pDescriptor+ * @return Plugin+ * @throws PluginRuntimeException+ */+ public Plugin getPluginInstance(PluginDescriptor pDescriptor)+ throws PluginRuntimeException {+ if (fActivatedPlugins.containsKey(pDescriptor.getPluginId()))+ return (Plugin) fActivatedPlugins.get(pDescriptor.getPluginId());+ try {+ // Must synchronize here to make sure creation and initialization+ // of a plugin instance are done by one and only one thread.+ // The same is in Extension.getExtensionInstance().+ // Suggested by Stefan Groschupf <[email protected]>+ synchronized (pDescriptor) {+ PluginClassLoader loader = pDescriptor.getClassLoader();+ Class pluginClass = loader.loadClass(pDescriptor+ .getPluginClass());+ Constructor constructor = pluginClass+ .getConstructor(new Class[] { PluginDescriptor.class });+ Plugin plugin = (Plugin) constructor+ .newInstance(new Object[] { pDescriptor });+ plugin.startUp();+ fActivatedPlugins.put(pDescriptor.getPluginId(), plugin);+ return plugin;+ }+ } catch (ClassNotFoundException e) {+ throw new PluginRuntimeException(e);+ } catch (InstantiationException e) {+ throw new PluginRuntimeException(e);+ } catch (IllegalAccessException e) {+ throw new PluginRuntimeException(e);+ } catch (NoSuchMethodException e) {+ throw new PluginRuntimeException(e);+ } catch (InvocationTargetException e) {+ throw new PluginRuntimeException(e);+ }+ }++ /*+ * (non-Javadoc)+ *+ * @see java.lang.Object#finalize()+ */+ public void finalize() throws Throwable {+ shotDownActivatedPlugins();+ }++ /**+ * @throws PluginRuntimeException+ */+ private void shotDownActivatedPlugins() throws PluginRuntimeException {+ Iterator iterator = fActivatedPlugins.keySet().iterator();+ while (iterator.hasNext()) {+ String pluginId = (String) iterator.next();+ Plugin object = (Plugin) fActivatedPlugins.get(pluginId);+ object.shutDown();+ }}- }-}+}\ No newline at end of filediff --git a/src/test/org/apache/nutch/plugin/TestPluginSystem.java b/src/test/org/apache/nutch/plugin/TestPluginSystem.javaindex ca5324fc7..7ad0d56be 100644--- a/src/test/org/apache/nutch/plugin/TestPluginSystem.java+++ b/src/test/org/apache/nutch/plugin/TestPluginSystem.java@@ -1,5 +1,5 @@/*-/**+ /*** Copyright 2005 The Apache Software Foundation** Licensed under the Apache License, Version 2.0 (the "License");@@ -15,6 +15,7 @@* limitations under the License.*/package org.apache.nutch.plugin;+import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;@@ -23,243 +24,237 @@ import java.io.IOException;import java.util.LinkedList;import java.util.Locale;import java.util.Properties;-import java.net.URL;+import junit.framework.TestCase;+import org.apache.nutch.util.NutchConf;-import org.dom4j.Document;-import org.dom4j.DocumentFactory;-import org.dom4j.Element;+/*** Unit tests for the plugin system** @author joa23*/public class TestPluginSystem extends TestCase {- private int fPluginCount;- private LinkedList fFolders = new LinkedList();-- protected void setUp() throws Exception {- fPluginCount = 5;- createDummyPlugins(fPluginCount);- }- /*- * (non-Javadoc)- *- * @see junit.framework.TestCase#tearDown()- */- protected void tearDown() throws Exception {- for (int i = 0; i < fFolders.size(); i++) {- File folder = (File) fFolders.get(i);- delete(folder);- folder.delete();- }-- }- /**- * @throws IOException- */- public void testPluginConfiguration() throws IOException {- String string = getPluginFolder();- File file = new File(string);- if (!file.exists()) {- file.mkdir();+ private int fPluginCount;++ private LinkedList fFolders = new LinkedList();++ protected void setUp() throws Exception {+ fPluginCount = 5;+ createDummyPlugins(fPluginCount);}- assertTrue(file.exists());- }- /**- * @throws IOException- */- public void testLoadPlugins() throws IOException {- PluginDescriptor[] descriptors = PluginRepository.getInstance()- .getPluginDescriptors();- int k = descriptors.length;- assertTrue(fPluginCount <= k);- for (int i = 0; i < descriptors.length; i++) {- PluginDescriptor descriptor = descriptors[i];- if (!descriptor.getPluginId().startsWith("getPluginFolder()")) {- continue;- }- assertEquals(1, descriptor.getExportedLibUrls().length);- assertEquals(1, descriptor.getNotExportedLibUrls().length);++ /*+ * (non-Javadoc)+ *+ * @see junit.framework.TestCase#tearDown()+ */+ protected void tearDown() throws Exception {+ for (int i = 0; i < fFolders.size(); i++) {+ File folder = (File) fFolders.get(i);+ delete(folder);+ folder.delete();+ }+}- }- /**- *- */- public void testGetExtensionAndAttributes() {- String xpId = " sdsdsd";- ExtensionPoint extensionPoint = PluginRepository.getInstance()- .getExtensionPoint(xpId);- assertEquals(extensionPoint, null);- Extension[] extension1 = PluginRepository.getInstance()- .getExtensionPoint(getGetExtensionId()).getExtentens();- assertEquals(extension1.length, fPluginCount);- for (int i = 0; i < extension1.length; i++) {- Extension extension2 = extension1[i];- String string = extension2.getAttribute(getGetConfigElementName());- assertEquals(string, getAttributeValue());++ /**+ */+ public void testPluginConfiguration() {+ String string = getPluginFolder();+ File file = new File(string);+ if (!file.exists()) {+ file.mkdir();+ }+ assertTrue(file.exists());+ }++ /**+ */+ public void testLoadPlugins() {+ PluginDescriptor[] descriptors = PluginRepository.getInstance()+ .getPluginDescriptors();+ int k = descriptors.length;+ assertTrue(fPluginCount <= k);+ for (int i = 0; i < descriptors.length; i++) {+ PluginDescriptor descriptor = descriptors[i];+ if (!descriptor.getPluginId().startsWith("getPluginFolder()")) {+ continue;+ }+ assertEquals(1, descriptor.getExportedLibUrls().length);+ assertEquals(1, descriptor.getNotExportedLibUrls().length);+ }}- }- /**- * @throws PluginRuntimeException- */- public void testGetExtensionInstances() throws PluginRuntimeException {- Extension[] extensions = PluginRepository.getInstance()- .getExtensionPoint(getGetExtensionId()).getExtentens();- assertEquals(extensions.length, fPluginCount);- for (int i = 0; i < extensions.length; i++) {- Extension extension = extensions[i];- Object object = extension.getExtensionInstance();- if (!(object instanceof HelloWorldExtension))- fail(" object is not a instance of HelloWorldExtension");- ((ITestExtension) object).testGetExtension("Bla ");- String string = ((ITestExtension) object).testGetExtension("Hello");- assertEquals("Hello World", string);++ /**+ *+ */+ public void testGetExtensionAndAttributes() {+ String xpId = " sdsdsd";+ ExtensionPoint extensionPoint = PluginRepository.getInstance()+ .getExtensionPoint(xpId);+ assertEquals(extensionPoint, null);+ Extension[] extension1 = PluginRepository.getInstance()+ .getExtensionPoint(getGetExtensionId()).getExtentens();+ assertEquals(extension1.length, fPluginCount);+ for (int i = 0; i < extension1.length; i++) {+ Extension extension2 = extension1[i];+ String string = extension2.getAttribute(getGetConfigElementName());+ assertEquals(string, getAttributeValue());+ }}- }- /**- *- *- */- public void testGetClassLoader() {- PluginDescriptor[] descriptors = PluginRepository.getInstance()- .getPluginDescriptors();- for (int i = 0; i < descriptors.length; i++) {- PluginDescriptor descriptor = descriptors[i];- assertNotNull(descriptor.getClassLoader());++ /**+ * @throws PluginRuntimeException+ */+ public void testGetExtensionInstances() throws PluginRuntimeException {+ Extension[] extensions = PluginRepository.getInstance()+ .getExtensionPoint(getGetExtensionId()).getExtentens();+ assertEquals(extensions.length, fPluginCount);+ for (int i = 0; i < extensions.length; i++) {+ Extension extension = extensions[i];+ Object object = extension.getExtensionInstance();+ if (!(object instanceof HelloWorldExtension))+ fail(" object is not a instance of HelloWorldExtension");+ ((ITestExtension) object).testGetExtension("Bla ");+ String string = ((ITestExtension) object).testGetExtension("Hello");+ assertEquals("Hello World", string);+ }}- }- /**- * @throws PluginRuntimeException- * @throws IOException- */- public void testGetResources() throws PluginRuntimeException, IOException {- PluginDescriptor[] descriptors = PluginRepository.getInstance()- .getPluginDescriptors();- for (int i = 0; i < descriptors.length; i++) {- PluginDescriptor descriptor = descriptors[i];- if (!descriptor.getPluginId().startsWith("getPluginFolder()")) {- continue;- }- String value = descriptor.getResourceString("key", Locale.UK);- assertEquals("value", value);- value = descriptor.getResourceString("key",- Locale.TRADITIONAL_CHINESE);- assertEquals("value", value);-++ /**+ *+ *+ */+ public void testGetClassLoader() {+ PluginDescriptor[] descriptors = PluginRepository.getInstance()+ .getPluginDescriptors();+ for (int i = 0; i < descriptors.length; i++) {+ PluginDescriptor descriptor = descriptors[i];+ assertNotNull(descriptor.getClassLoader());+ }}- }- /**- * @return a PluginFolderPath- */- private String getPluginFolder() {- String[] strings = NutchConf.get().getStrings("plugin.folders");- if (strings == null || strings.length == 0)- fail("no plugin directory setuped..");- String name = strings[0];- return PluginManifestParser.getPluginFolder(name).toString();- }+ /**+ * @throws IOException+ */+ public void testGetResources() throws IOException {+ PluginDescriptor[] descriptors = PluginRepository.getInstance()+ .getPluginDescriptors();+ for (int i = 0; i < descriptors.length; i++) {+ PluginDescriptor descriptor = descriptors[i];+ if (!descriptor.getPluginId().startsWith("getPluginFolder()")) {+ continue;+ }+ String value = descriptor.getResourceString("key", Locale.UK);+ assertEquals("value", value);+ value = descriptor.getResourceString("key",+ Locale.TRADITIONAL_CHINESE);+ assertEquals("value", value);- /**- * Creates some Dummy Plugins- *- * @param pCount- */- private void createDummyPlugins(int pCount) {- String string = getPluginFolder();- try {- File folder = new File(string);- folder.mkdir();- for (int i = 0; i < pCount; i++) {- String pluginFolder = string + File.separator + "DummyPlugin"- + i;- File file = new File(pluginFolder);- file.mkdir();- fFolders.add(file);- createPluginManifest(i, file.getAbsolutePath());- createResourceFile(i, file.getAbsolutePath());- }- } catch (IOException e) {- e.printStackTrace();+ }}- }- /**- * Creates an ResourceFile- *- * @param i- * @param pFolderPath- * @throws FileNotFoundException- * @throws IOException- */- private void createResourceFile(int i, String pFolderPath)- throws FileNotFoundException, IOException {- Properties properties = new Properties();- properties.setProperty("key", "value");- properties.store(new FileOutputStream(pFolderPath + File.separator- + "messages" + ".properties"), "");- }- /**- * Deletes files in path- *- * @param path- * @throws IOException- */- private void delete(File path) throws IOException {- File[] files = path.listFiles();- for (int i = 0; i < files.length; ++i) {- if (files[i].isDirectory())- delete(files[i]);- files[i].delete();++ /**+ * @return a PluginFolderPath+ */+ private String getPluginFolder() {+ String[] strings = NutchConf.get().getStrings("plugin.folders");+ if (strings == null || strings.length == 0)+ fail("no plugin directory setuped..");++ String name = strings[0];+ return PluginManifestParser.getPluginFolder(name).toString();+ }++ /**+ * Creates some Dummy Plugins+ *+ * @param pCount+ */+ private void createDummyPlugins(int pCount) {+ String string = getPluginFolder();+ try {+ File folder = new File(string);+ folder.mkdir();+ for (int i = 0; i < pCount; i++) {+ String pluginFolder = string + File.separator + "DummyPlugin"+ + i;+ File file = new File(pluginFolder);+ file.mkdir();+ fFolders.add(file);+ createPluginManifest(i, file.getAbsolutePath());+ createResourceFile(file.getAbsolutePath());+ }+ } catch (IOException e) {+ e.printStackTrace();+ }+ }++ /**+ * Creates an ResourceFile+ *+ * @param pFolderPath+ * @throws FileNotFoundException+ * @throws IOException+ */+ private void createResourceFile(String pFolderPath)+ throws FileNotFoundException, IOException {+ Properties properties = new Properties();+ properties.setProperty("key", "value");+ properties.store(new FileOutputStream(pFolderPath + File.separator+ + "messages" + ".properties"), "");+ }++ /**+ * Deletes files in path+ *+ * @param path+ * @throws IOException+ */+ private void delete(File path) throws IOException {+ File[] files = path.listFiles();+ for (int i = 0; i < files.length; ++i) {+ if (files[i].isDirectory())+ delete(files[i]);+ files[i].delete();+ }+ }++ /**+ * Creates an Plugin Manifest File+ *+ * @param i+ * @param pFolderPath+ * @throws IOException+ */+ private void createPluginManifest(int i, String pFolderPath)+ throws IOException {+ FileWriter out = new FileWriter(pFolderPath + File.separator+ + "plugin.xml");+ String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!--this is just a simple plugin for testing issues.--><nutch-plugin id=\"org.apache.nutch.plugin."+ + i+ + "\" name=\""+ + i+ + "\" version=\"1.0\" provider-name=\"joa23\" class=\"org.apache.nutch.plugin.SimpleTestPlugin\"><extension-point id=\"aExtensioID\" name=\"simple Parser Extension\" schema=\"schema/testExtensionPoint.exsd\"/><runtime><library name=\"libs/exported.jar\"><extport/></library><library name=\"libs/not_exported.jar\"/></runtime><extension point=\"aExtensioID\"><implementation name=\"simple Parser Extension\" id=\"aExtensionId.\" class=\"org.apache.nutch.plugin.HelloWorldExtension\"/></extension></nutch-plugin>";+ out.write(xml);+ out.flush();+ out.close();+ }++ private String getAttributeValue() {+ return "simple Parser Extension";+ }++ private static String getGetExtensionId() {+ return "aExtensioID";+ }++ private static String getGetConfigElementName() {+ return "name";+ }++ public static void main(String[] args) throws IOException {+ new TestPluginSystem().createPluginManifest(1, "/");}- }- /**- * Creates an Plugin Manifest File- *- * @param i- * @param pFolderPath- * @throws IOException- */- private void createPluginManifest(int i, String pFolderPath)- throws IOException {- FileWriter out = new FileWriter(pFolderPath + File.separator- + "plugin.xml");- Document document = DocumentFactory.getInstance().createDocument();- document.addComment("this is just a simple plugin for testing issues.");- Element pluginElement = document.addElement("nutch-plugin")- .addAttribute("id", "org.apache.nutch.plugin." + i).addAttribute(- getGetConfigElementName(), "" + i).addAttribute(- "version", "1.0")- .addAttribute("provider-name", "joa23").addAttribute("class",- "org.apache.nutch.plugin.SimpleTestPlugin");- pluginElement.addElement("extension-point").addAttribute("id",- getGetExtensionId()).addAttribute(getGetConfigElementName(),- getAttributeValue()).addAttribute("schema",- "schema/testExtensionPoint.exsd");- Element runtime = pluginElement.addElement("runtime");- Element element = runtime.addElement("library").addAttribute("name",- "libs/exported.jar");- element.addElement("extport");- Element element1 = runtime.addElement("library").addAttribute("name",- "libs/not_exported.jar");- Element extension = pluginElement.addElement("extension").addAttribute(- "point", getGetExtensionId());- extension.addElement("extemsion-item").addAttribute("objectClass",- "IInterfaceForExtensionPoint").addAttribute(- getGetConfigElementName(), getAttributeValue()).addAttribute(- "id", "aExtensionId.").addAttribute("class",- "org.apache.nutch.plugin.HelloWorldExtension");- document.write(out);- out.flush();- out.close();- }- private String getAttributeValue() {- return "simple Parser Extension";- }- private static String getGetExtensionId() {- return "aExtensioID";- }- private static String getGetConfigElementName() {- return "name";- }-}+}\ No newline at end of file